Creating hills + terrain + paint from script
Example:
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:
Quotewindow = 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")
world:SetSkybox(skybox)
light = DirectionalLight:Create()
light:SetRotation(45,45,0)
window:HideMouse()player = Prefab:Load("Prefabs/player/fpsplayer.pfb")
player:SetPosition(0,20,0)
camera = tolua.cast(player.script.camera, "Camera")
camera:SetMultisampleMode(8)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 resolutionterraintex0 = 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)
terrain:SetScale(1,20,1)local varx
local vary
local Posx={}
local Posy={}
Posx[1]=150 -- hill 1 pos
Posy[1]=100Posx[2]=-15 -- hill 2 distance from hill 1
Posy[2]=-5
local des
local des1
local hauteur=5 -- hill height 1
local hauteur2=10 -- hill height 2
local longueur=3
local largeur=1for varx=-50,50 do
for vary=-50,50 dodes=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)
end
-- 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)
endend
endterrain:UpdateNormals()
-- 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)
terrain:SetLayerAlpha(1,1)
while not window:KeyHit(Key.Escape) do
if window:Closed() then return false endTime:Update()
world:Update()
world:Render()--local CamPos=camera:GetPosition(true)
--System:Print("Code="..terrain:GetLayerAtPoint(01,01))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)
camera:SetPosition(0,mm_pos,0)
camera:SetRotation(90,0,0)
mm_buffer:Enable()
world:Render()
minimap = mm_buffer:GetColorTexture(0)
camera:SetMatrix(camera_prev_mat)
cur_buffer:Enable()
endif 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)
context:SetBlendMode(1)
context:SetColor(1,1,1,0.55)
cur_shader = context:GetShader()
context:SetShader(mm_shader)
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
minimap:Bind(1)
context:DrawImage(mm_mask,600,10,180,180)
context:SetShader(cur_shader)
context:SetColor(1,0,0,1)
context:DrawRect(686,96,8,8,0,0)
context:SetColor(1,1,1,1)
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)
context:SetBlendMode(0)
context:Sync(true)
end
You can only handle 4 layers from the script: 0,1,2,3. More will have no effect.
> Add this to create vegetation:
Quote-- veget:
terrain:AddVegetationLayer()
--System:Print("CountLayers="..terrain:CountVegetationLayers())HerbeRouge=terrain:GetVegetationLayer(0)
HerbeRougeModel=Model:Load("Models/Nature/Plants/bush01.mdl")
HerbeRouge:SetModel(HerbeRougeModel)
HerbeRouge:SetSeed(100)
HerbeRouge:SetViewDistance(200)HerbeRouge:SetSlopeConstraints(0, 20)
HerbeRouge:SetHeightConstraints(0, 300)
HerbeRouge:SetDensity(5)
HerbeRouge:SetBillboardMode(false)
HerbeRouge:SetShadowMode(1)HerbeRougeModel:Release()
for varx=-50,50 do
for vary=-50,50 doHerbeRouge:SetVisibility(varx, vary, false)
end
end
- 4
1 Comment
Recommended Comments