TerrainSize ( terrain )
Parameters
| terrain - terrain handle |
Description
|
Returns the number of grid squares along one side of a terrain. For a terrain made with CreateTerrain this is exactly the grid_size you passed in. For one made with LoadTerrain it is the width of the height map image, which is the usual reason to ask: it lets you write code that sculpts, scans or saves a landscape without hard-coding the size of the picture it came from. The value is fixed when the terrain is created and never changes, so read it once after loading and keep it in a variable rather than calling it inside a nested loop. It is always a power of two, and it counts squares, not grid points - a terrain of size 64 has 64 squares and 65 lines across it, although the last line shares its heights with the first because the grid wraps. In practice that means your sculpting loops should run 0 to TerrainSize(terrain)-1. An unscaled terrain is exactly this many world units across, so TerrainSize also tells you how far the landscape reaches: multiply by the x and z you gave ScaleEntity to get the size of the playable area, which is handy for placing objects randomly or for keeping the player inside the map. See also: CreateTerrain, LoadTerrain, TerrainHeight, ModifyTerrain, ScaleEntity. |
Example
; TerrainSize Example ; ------------------- Graphics3D 640,480,0,2 SetBuffer BackBuffer() camera=CreateCamera() CameraRange camera,1,500 light=CreateLight() RotateEntity light,45,45,0 ; Load a 256x256 height map as a terrain terrain=LoadTerrain("media/Height_map.bmp") ; Stretch it into a landscape: 2 units per grid square, 60 units tall ScaleEntity terrain,2,60,2 ; Spend around 4000 triangles, with vertex morphing to hide LOD pop-in TerrainDetail terrain,4000,True ; Texture the landscape grass_tex=LoadTexture("media/MossyGround.BMP") EntityTexture terrain,grass_tex ; Start in the middle of the terrain PositionEntity camera,256,30,256 ; TerrainSize returns the grid size the terrain was built with size=TerrainSize(terrain) While Not KeyDown(1) ; Drift forward on its own; arrow keys steer and speed up MoveEntity camera,0,0,0.2 If KeyDown(200) Then MoveEntity camera,0,0,1 If KeyDown(208) Then MoveEntity camera,0,0,-1 If KeyDown(203) Then TurnEntity camera,0,1,0 If KeyDown(205) Then TurnEntity camera,0,-1,0 ; Glide at a fixed height above the ground x#=EntityX(camera) z#=EntityZ(camera) y#=TerrainY(terrain,x,EntityY(camera),z)+8 PositionEntity camera,x,y,z RenderWorld Text 0,0,"Arrows: fly over the terrain Esc: exit" Text 0,20,"TerrainSize(terrain) = "+size+" grid squares per side" Text 0,40,"Scaled by 2, that is "+(size*2)+" world units per side" Flip Wend End
Index