Jump to content

Recommended Posts

Posted

Useful Lua example here:

-- Get the display list
local displays = GetDisplays()

-- Create a window
local window = CreateWindow("Leadwerks", 0, 0, 1280 * displays[1].scale, 720 * displays[1].scale, displays[1], WINDOW_CENTER + WINDOW_TITLEBAR)

-- Create a world
local world = CreateWorld()

-- Create a framebuffer
local framebuffer = CreateFramebuffer(window)

-- Create a camera
local camera = CreateCamera(world)
camera:SetFov(70)
camera:SetPosition(0, 100, -100)
camera:SetRotation(45, 0, 0)
camera:SetClearColor(0.125)

-- Sunlight
local light = CreateDirectionalLight(world)
light:SetRotation(45, 35, 0)

-- Create terrain
local terrain = CreateTerrain(world, 512, 512)

local mtl1 = CreateMaterial()
mtl1:SetColor(1,0,0)

local mtl2 = CreateMaterial()
mtl2:SetColor(0,1,0)

local mtl3 = CreateMaterial()
mtl3:SetColor(0,0,1)

local mtl4 = CreateMaterial()
mtl4:SetColor(1,0,1)

local layer1 = terrain:AddLayer(mtl1)
local layer2 = terrain:AddLayer(mtl2)
local layer3 = terrain:AddLayer(mtl3)
local layer4 = terrain:AddLayer(mtl4)

local x
local y
for x = 0, 256 do 
	for y = 0, 256 do 
		terrain:SetLayerWeight(layer1, x, y, 1.0)
	end
end
for x = 256, 512 do 
	for y = 0, 256 do 
		terrain:SetLayerWeight(layer2, x, y, 1.0)
	end
end
for x = 0, 256 do 
	for y = 256, 512 do 
		terrain:SetLayerWeight(layer3, x, y, 1.0)
	end
end
for x = 256, 512 do 
	for y = 256, 512 do 
		terrain:SetLayerWeight(layer4, x, y, 1.0)
	end
end

-- Camera controls
camera:AddComponent(CameraControls)

local rect = CreateTile(camera, 100, 100)

function GetTerrainMaterialAtPoint(terrain, point)
	point = TransformPoint(point, nil, terrain)
	x = Round(point.x + terrain.resolution.x / 2)
	y = Round(point.z + terrain.resolution.y / 2)
	x = Clamp(x, 0, terrain.resolution.x - 1)
	y = Clamp(y, 0, terrain.resolution.y - 1)
	--Print(tostring(x)..", "..tostring(y))
	local layers = terrain:GetLayers(x, y)
	if #layers > 0 then
		return terrain:GetLayerMaterial(layers[1])
	end
	return terrain:GetLayerMaterial(1)
end

-- Main loop
while not window:Closed() and not window:KeyDown(KEY_ESCAPE) do
	
	window:SetMousePosition(framebuffer.size.x / 2, framebuffer.size.y / 2)
	
	if window:MouseDown(MOUSE_LEFT) then
		local pick = camera:Pick(framebuffer, framebuffer.size.x / 2, framebuffer.size.y / 2, 0, true)
		if pick.entity == terrain then
			local mtl = GetTerrainMaterialAtPoint(terrain, pick.position)
			if mtl ~= nil then
				local color = mtl:GetColor()
				rect:SetColor(color)
			else
				rect:SetColor(1,1,1)
			end
		end		
	end
	
    world:Update()
    world:Render(framebuffer)
end

 

  • Like 2
  • Thanks 1

Let's build cool stuff and have fun. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

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