Jump to content
  • entries
  • comments
  • views

Creating hills + terrain + paint from script







I could strangely find nothing about terrain generation from script except for LE5... 

So here is an example that will run with LE4.6 on how you can generate 2 little  hills on a terrain and paint it from script. Can be usefull for games with random generated landscape.

The script I modified is from here: https://www.leadwerks.com/community/topic/15715-minimap-generator/?do=findComment&comment=104697

So first install the minimap shader to run this:




window = Window:Create("example",0,0,1600,900,Window.Titlebar+Window.Center)
context = Context:Create(window)
world = World:Create()
skybox = Texture:Load("Materials/Sky/skybox_texture.tex")
light = DirectionalLight:Create()

player = Prefab:Load("Prefabs/player/fpsplayer.pfb")
camera = tolua.cast(player.script.camera, "Camera")

mm_size = 256.0 -- size of map 64
mm_zoom = 0.1
mm_toggle = 0
mm_shader = Shader:Load("Shaders/Drawing/drawing_minimap.shader")
mm_mask = Texture:Load("Materials/developer/trigger.tex")
mm_shader:SetVec2("terrain_size", Vec2(mm_size,mm_size)) --send terrain size to shader uniform
cur_buffer = Buffer:GetCurrent()
mm_buffer = Buffer:Create(1024,1024,1,0) -- can be any size but higher = better resolution

terraintex0 = Texture:Load("Materials/Grass/grass01.tex")
terraintex1 = Texture:Load("Materials/Rock//rockwall01.tex")
terraintex1_nrm = Texture:Load("Materials/Rock/rockwall01_dot3.tex")
terraintex1_disp = Texture:Load("Materials/Rock/rockwall01_disp.tex")

terrain = Terrain:Create(256)

local varx
local vary
local Posx={}
local Posy={}
 Posx[1]=150    -- hill 1 pos

 Posx[2]=-15    -- hill 2 distance from hill 1

local des
local des1
local hauteur=5        -- hill height 1
local hauteur2=10    -- hill height 2
local longueur=3
local largeur=1

    for varx=-50,50 do
        for vary=-50,50 do

                des=Math:Random(150,200)    -- intensity noise (0-200)
                des1=math.random()*0.75    -- height noise (0-2)

---------------------------------        <--used to build a hill---------->
            local HauteurTerrainButte1 = ((varx*varx*longueur)+(vary*vary*largeur))/-200

------------ = if over water level:
            if hauteur + HauteurTerrainButte1 > 0 then
------------------------------------------------------           <---- used to build noise-------------------->   
                terrain:SetElevation(Posx[1]+varx, Posy[1]+vary, ((math.sin(varx+des))*math.sin(vary+des)*des1)+ hauteur + HauteurTerrainButte1 , true)
--        estimation of terrain elevation at second hill position:
            HauteurTerrainButte1 = (((varx+Posx[2])*(varx+Posx[2])*longueur)+((vary+Posx[2])*(vary+Posx[2])*largeur))/-200
            local HauteurTerrainButte2 = ((varx*varx*longueur)+(vary*vary*largeur*4))/-20

------  if over the elevation:
            if hauteur +hauteur2+ HauteurTerrainButte2 > hauteur + HauteurTerrainButte1 then 
                terrain:SetElevation(Posx[1]+varx+Posx[2], Posy[1]+vary+Posx[2], ((math.sin(varx+des))*math.sin(vary+des)*des1)+ hauteur+hauteur2 + HauteurTerrainButte2, true)



-- paint the ground: // only run well with 4 layers 0,1,2,3.
terrain:SetLayerTexture(0, terraintex0, 0)
terrain:SetLayerTexture(1, terraintex1, 0)        -- diffuse
terrain:SetLayerTexture(1, terraintex1_nrm, 1) -- normal
terrain:SetLayerTexture(1, terraintex1_disp, 2) -- disp
terrain:SetLayerSlopeConstraints(1, 25, 90, 5)

while not window:KeyHit(Key.Escape) do
    if window:Closed() then return false end


--local CamPos=camera:GetPosition(true)

    if mm_toggle==0 then
        mm_toggle = 1
        camera_prev_mat = camera:GetMatrix()
        --calculate proper height based on camera's FOV
        mm_pos = math.tan((90-camera:GetFOV()/2)*math.pi/180) * (mm_size/2)
        minimap = mm_buffer:GetColorTexture(0)

    if window:KeyHit(Key.Right) then mm_zoom = mm_zoom + 0.1 end
    if window:KeyHit(Key.Left) then mm_zoom = mm_zoom - 0.1 end
    mm_zoom = math.max(mm_zoom, 0.1)
    mm_zoom = math.min(mm_zoom, 1.0)
    cur_shader = context:GetShader()
    pos = player:GetPosition(true)
    mm_shader:SetVec2("player_pos", Vec2(pos.x,pos.z)) --send player's position to shader uniform
    mm_shader:SetVec2("zoom", Vec2(mm_zoom)) --send current zoom to shader uniform
    context:DrawText(string.format("FPS: %.2f", Time:UPS()),2,2)
    context:DrawText("Player Position: "..player:GetPosition():ToString(),2,22)
    context:DrawText("Press LEFT/RIGHT Arrow Keys To Change Zoom",2,42)
    context:DrawText("Zoom: "..mm_zoom,2,62)

You can only handle 4 layers from the script: 0,1,2,3. More will have no effect.




> Add this to create vegetation:


-- veget:


            HerbeRouge:SetSlopeConstraints(0, 20)    
            HerbeRouge:SetHeightConstraints(0, 300)    


    for varx=-50,50 do
        for vary=-50,50 do

            HerbeRouge:SetVisibility(varx, vary, false)


  • Like 4

1 Comment

Recommended Comments

You can add multiple gradient noise generators to make different type of height maps. You can also subtract height gradually more  at the edges and make an island generator. 
In the library I used I used only one perlin noise, with different octaves and frequency of details in it. if you have less frequency hills will get larger on xz and if you have higher octaves you can get more hills and holes.

You can also generate Biomes / textures with https://en.wikipedia.org/wiki/Voronoi_diagram Voronoi.
basically use one texture ant interpolate between them.

  • Like 1
Link to comment
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...