You can try this, My test code?
-- MultiLights
function setup()
-- Create a new craft scene
scene = craft.scene()
-- ?? OrbitViewer ??????????????????????
viewer = scene.camera:add(OrbitViewer, vec3(0,2,0),5, 0, 1000)
modelInit()
-- ????????????P[1].Color?P[2].Color
P = {}
-- ????????
lights = {}
for i=1,6 do
P[i] = { Color = color(230, 249, 18), Type=2, Intensity=1, Penumbra=30,
Decay=0, Height=5, X=2, Z=2, Angle=30}
local l = scene:entity():add(craft.light, POINT)
lights[i] = Light(i, l, 1, 1, 1, true)
end
-- ??????
showParameter()
end
function update(dt)
-- Update the scene (physics, transforms etc)
scene:update(dt)
lights[ID]:update(dt)
end
-- Called automatically by codea
function draw()
update(DeltaTime)
-- Draw the scene
scene:draw()
end
function modelInit()
models = {}
-- ??????
local ground = scene:entity()
ground.position = vec3(0,0,0)
ground.model = craft.model.cube(vec3(50,0.1,50))
ground.material = craft.material.preset("Surfaces:Basic Bricks")
-- ground.material = craft.material(asset.builtin.Materials.Standard)
-- ground.material.map = readImage(asset.builtin.Surfaces.Desert_Cliff_Color)
-- ground.material.normalMap = readImage(asset.builtin.Surfaces.Basic_Bricks_Normal)
-- ground.material.diffuse = color(146, 82, 69)
ground.material.offsetRepeat = vec4(1,20,20,20)
-- ground.active = false
-- ??????:?????????
cube = scene:entity()
cube.position=vec3(-1,1.5,3)
cube.model = craft.model.cube(vec3(0.2,0.2,0.2),vec3(1,0.5,1))
cube.material = craft.material(asset.builtin.Materials.Specular)
-- cube.material.diffuse = color(238, 237, 237, 255)
cube.active = true
end
-- ???
Light = class()
-- light ????scene:entity():add(craft.light)?scene.sun:get(craft.light)
function Light:init(id,light,x,y,z)
self.id = id
self.light = light
self.light.color = P[self.id].Color or color(18, 66, 249)
self.light.type = Type or SPOT
self.light.intensity = Intensity or 1
self.light.penumbra = Penumbra or 30
self.light.decay = Decay or 0
self.light.entity.y = Height or 4
self.light.entity.x = X or 2
self.light.entity.z = Z or 2
self.light.angle = Angle or 30
end
function Light:update(dt)
self.light.color = P[self.id].Color
self.light.intensity = P[self.id].Intensity
self.light.type = P[self.id].Type
self.light.angle = P[self.id].Angle
self.light.decay = P[self.id].Decay
self.light.penumbra = P[self.id].Penumbra
self.light.entity.y = P[self.id].Height
self.light.entity.x = P[self.id].X
self.light.entity.z = P[self.id].Z
end
function showParameter()
types = {"DIRECTIONAL", "POINT", "SPOT"}
parameter.watch("scene.camera.position")
parameter.integer("ID",1,6,1)
parameter.color("Color",color(249, 93, 18),function() P[ID].Color = Color end)
parameter.integer("Type", DIRECTIONAL, SPOT, SPOT, function() P[ID].Type = Type end)
parameter.watch("types[Type+1]")
parameter.number("Intensity", 0.0, 10.0, 1.0,function() P[ID].Intensity = Intensity end)
parameter.number("Penumbra", 0.0, 90, 30.0, function() P[ID].Penumbra = Penumbra end)
parameter.number("Decay", 0.0, 10.0, 0.1, function() P[ID].Decay = Decay end)
parameter.number("Height", -1, 50, 5, function() P[ID].Height = Height end)
parameter.number("X", -25, 25, 2, function() P[ID].X = X end)
parameter.number("Z", -25, 25, 2, function() P[ID].Z = Z end)
parameter.number("Angle", 0.0, 90, 30, function() P[ID].Angle = Angle end)
end