Terrain Building API in Leadwerks 5 Beta
An often-requested feature for terrain building commands in Leadwerks 5 is being implemented. Here is my script to create a terrain. This creates a 256 x 256 terrain with one terrain point every meter, and a maximum height of +/- 50 meters:
--Create terrain local terrain = CreateTerrain(world,256,256) terrain:SetScale(256,100,256)
Here is what it looks like:
A single material layer is then added to the terrain.
--Add a material layer local mtl = LoadMaterial("Materials/Dirt/dirt01.mat") local layerID = terrain:AddLayer(mtl)
We don't have to do anything else to make the material appear because by default the entire terrain is set to use the first layer, if a material is available there:
Next we will raise a few terrain points.
--Modify terrain height for x=-5,5 do for y=-5,5 do h = (1 - (math.sqrt(x*x + y*y)) / 5) * 20 terrain:SetElevation(127 + x, 127 + y, h) end end
And then we will update the normals for that whole section, all at once. Notice that we specify a larger grid for the normals update, because the terrain points next to the ones we modified will have their normals affected by the change in height of the neighboring pixel.
--Update normals of modified and neighboring points terrain:UpdateNormals(127 - 6, 127 - 6, 13, 13)
Now we have a small hill.
Next let's add another layer and apply it to terrain points that are on the side of the hill we just created:
--Add another layer mtl = LoadMaterial("Materials/Rough-rockface1.json") rockLayerID = terrain:AddLayer(mtl) --Apply layer to sides of hill for x=-5,5 do for y=-5,5 do slope = terrain:GetSlope(127 + x, 127 + y) alpha = math.min(slope / 15, 1.0) terrain:SetMaterial(rockLayerID, 127 + x, 127 + y, alpha) end end
We could improve the appearance by giving it a more gradual change in the rock layer alpha, but it's okay for now.
This gives you an idea of the basic terrain building API in Leadwerks 5, and it will serve as the foundation for more advanced terrain features. This will be included in the next beta.
- 6
15 Comments
Recommended Comments