Hm, it has to work:) That is: the engine internally also uses TCastleTerrainImage.Height to make terrain with data from TCastleTerrainImage work. I just opened in editor examples/terrain/data/additional_terrain_tests.castle-user-interface that confirms data from image works.
Some possible hints:
Are you sure you set TCastleTerrainImage.Image before?
What are your TCastleTerrainImage.MinLevel, TCastleTerrainImage.MaxLevel?
The function TCastleTerrainImage.Height(const Coord, TexCoord: TVector2): Single; call uses only the 2nd argument, TexCoord. It is assumed to be in 0…1 range if you want to query the whole image. Your line of code possibly uses too big coordinates, try as 2nd argument to pass something like
I can see now that the height value (th) is not 100% correct.
Maybe the height value represents the pixel color and the height for the avatar or the terrain is interpolated between the different pixels in the heightmap.
that’s right, I tried TCastleStickToSurface, but I couldn’t get this to work.
For the grass I use this:
for i := 1 to 50 do
GrassTransforms[I] := TCastleTransform.Create(FreeAtStop);
GrassTransforms[I].Translation := CalcTransformPos(40);
With the function CalcTransformPos I’m now able to set the height of terrain.
Do you think it works with TCastleStickToSurface?
I do hope to extend our terrain tools in the future to have such things (terrain modification, grass and trees planting on terrain etc.). Good that in the meantime, it is doable already
Indeed. Heights returned by TCastleTerrainImage.Height (or in general by any TCastleTerrainData.Height) are not final. They are used for the points on the mesh generated by TCastleTerrain, and between them we place triangles and so effectively introduce linear interpolation between them.
I can see now that I should fix this. TCastleTerrainImage.Height should actually already interpolate the results (that is, perform bilinear filtering), otherwise if you use mesh with a big density – you will start to see pixels. Maybe it deserves a property at TCastleTerrainImage, like BilinearFiltering: Boolean. I added this to my TODO.