Jump to content
Lunarovich

Terrain - how to access it from Lua code? + make it cast shadows?

Recommended Posts

I have two questions regarding terrain:

 

1) How can I access a terrain made in the editor from Lua script? Secondly, is there any way to create it programmatically / procedurally, ie. via scripting and not in the editor?

 

2) I see that a terrain is shaded, which is fine. However, I notice that it does not cast shadows, which is very unrealistic. Is there a way to make it cast shadows.

 

Thank you!

Share this post


Link to post

1. Probably not supported, since CountEntities and GetEntity are not dicumented, but here is a way

 

 --get world and terrain
local world=World:GetCurrent()
for i=0,world:CountEntities()-1 do
 if world:GetEntity(i):GetClass()==Object.TerrainClass then
		 self.terrain=world:GetEntity(i)
		 tolua.cast(self.terrain,"Terrain")
		 break
 end
end

 

2. Not that I know.

Share this post


Link to post

Also, the terrain commands (not supported/documented) are exposed to lua.

 

example Main.lua:

window = Window:Create("terrain example",0,0,800,600)
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:SetPosition(0,6,0)
camera:SetRotation(0,45,0)
camera:SetMultisampleMode(8)
skybox = Texture:Load("Materials/Sky/skybox_texture.tex")
camera:SetSkybox(skybox)
light = DirectionalLight:Create()
light:SetRotation(45,45,0)

terrain = Terrain:Create(128,true)
terrain:SetLayerTexture(0, Texture:Load("Materials/Nature/terrain_savannah_dirt.tex"), 0)
terrain:SetLayerTexture(0, Texture:Load("Materials/Nature/terrain_savannah_dirtdot3.tex"), 1)
terrain:SetScale(1,5,1)
for x = 0, 64 do
	for y = 64, 128 do
		height = Math:Random(0,1)+0.5
		height = Math:Min(height,1.0)
		terrain:SetHeight(x,y,height)
	end
end
terrain:UpdateNormals()

camrot = camera:GetRotation()
gx=Math:Round(context:GetWidth()/2)
gy=Math:Round(context:GetHeight()/2)
move = 0
strafe = 0

while window:KeyDown(Key.Escape)==false do
	if window:Closed() then break end
	local mouseposition = window:GetMousePosition()
	local dx = mouseposition.x - gx
	local dy = mouseposition.y - gy
	camrot.x = camrot.x + dy / 10.0
	camrot.y = camrot.y + dx / 10.0
	camera:SetRotation(camrot)
	window:SetMousePosition(gx,gy)
	move = Math:Curve(((window:KeyDown(Key.W)and 1 or 0) - (window:KeyDown(Key.S) and 1 or 0)),move,10)
	strafe = Math:Curve(((window:KeyDown(Key.D)and 1 or 0) - (window:KeyDown(Key.A) and 1 or 0)),strafe,10)
	camera:Move(strafe,0,move)
 
	Time:Update()
	world:Update()
	world:Render()
	
	
	context:Sync()
end

 

As for terrain shadows, I haven't seen those since LE2. I suspect it will be re-introduced once LE3 gets vegetation.

Share this post


Link to post

Thank you for these excellent answers! I'm wandering, though, why those stay undocumented...

 

Btw, how do you know these things? From Visual Studio auto-complete, trial & error, and similar?

 

P.S. If they are there, why do you say that they are unsupported? Because they might change in the future, might disappear, or similar?

Share this post


Link to post

I look in the cpp header files yes

If they are not documented, they might change/be taken out/or not. Only Josh knows for sure.

Share this post


Link to post

I perform a global variable dump in lua since i do not have the c++ version of LE. I can see each command/member available for each class. This is the latest version:

globals.txt

 

Edit- and like shadmar suggests, if it is not documented, then Josh has repeatedly warned that it may be removed or stop working at any time.

Share this post


Link to post
I perform a global variable dump in lua since i do not have the c++ version of LE.

 

Maybe now it's the right time to get it if you need it:

 

n7PJAr2.png

 

P.S. How do you do a global variable dump?

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   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...