Example6: Using addElement() to build block type model
Btw. I am not so clear about these lines:
collision = STEP,
ao = false
The code:
viewer.mode = FULLSCREEN
function setup()
scene = craft.scene()
scene.sky.material.sky = color(80, 203, 233)
scene.sky.material.horizon = color(80, 108, 233)
player = scene.camera:add(OrbitViewer, vec3( 40, 25, 40), 8, 1, 400)
-- set voxel terrain parameters: size, coord
scene.voxels:resize(vec3(5,1,5))
scene.voxels.coordinates = vec3(0,0,0)
-- Using default block type: SOLID as the filling unit
scene.voxels:fill("solid")
-- Create a box using "Solid" block type, every unit in the box is a "solid" block
scene.voxels:box(0,0,0, 16*8,10,16*8)
-- Create a new block type "myDirt"
newBlockTypeDirt()
-- Create a new block type "myDirtModel1", using craft model
newBlockTypeDirtWithCraftModel()
-- Create a new block type "myDirtModel2", using addElement() to build model
newBlockTypeDirtWithAddElement()
scene.voxels:fill("myDirt")
-- A sphere larger than half can not blink, so interesting
scene.voxels:sphere(40,20,77,8)
-- Half sphere can blink
scene.voxels:sphere(10,20,0,8)
scene.voxels:line(10,20,0,10,50,30)
-- Building model
scene.voxels:fill("myDirtModel2")
scene.voxels:block(vec3(30,20,35))
scene.voxels:line(40,20,30,10,50,30)
scene.voxels:sphere(70,20,77,2)
-- craft.scene.main = scene
end
function newBlockTypeDirtWithAddElement()
local dirt = scene.voxels.blocks:new("myDirtModel2")
dirt.setTexture(ALL, "Blocks:Dirt")
-- dirt.setColor(ALL, color(227, 89, 14))
dirt.scripted = true
-- custom model from state
function dirt:buildModel(model)
model:clear()
model:addElement
{
lower = vec3(0,0,0),
upper = vec3(255,80,255),
textures = "Blocks:Wood",
collision = STEP,
ao = false
}
model:addElement
{
lower = vec3(0,60,0),
upper = vec3(156,156,190),
textures = "Blocks:Stone",
collision = STEP,
ao = false
}
model:addElement
{
lower = vec3(0,0,0),
upper = vec3(106,255,100),
textures = "Blocks:Dirt",
collision = STEP,
ao = false
}
end
return dirt
end
function newBlockTypeDirtWithCraftModel()
local dirt = scene.voxels.blocks:new("myDirtModel1")
dirt.dynamic = true
dirt.scripted = true
dirt.geometry = TRANSPARENT
-- The internal script
function dirt:created()
e = self.entity
self.base = scene:entity()
self.base.parent = e
self.base.position = vec3(0.5, 0.3, 0.5)
local r = self.base:add(craft.renderer, craft.model.cube(vec3(2.8,0.6,2.8)))
r.material = craft.material(asset.builtin.Materials.Specular)
r.material.diffuse = color(133, 79, 30, 255)
r.material.map = readImage(asset.builtin.Blocks.Brick_Grey)
self.top = scene:entity()
self.top.parent = e
self.top.position = vec3(0.1, 0.6, 0.1)
local r2 = self.top:add(craft.renderer, craft.model.cube(vec3(2.8,0.2,2.8), vec3(1.4,0.1,1.4)))
-- local r2 = self.top:add(craft.renderer, craft.model(asset.builtin.Primitives.Monkey))
r2.material = craft.material(asset.builtin.Materials.Specular)
r2.material.diffuse = color(66, 47, 30, 255)
r2.material.map = readImage(asset.n0)
r2.material.normalMap = readImage(asset.n1)
self.angle = 0
end
-- Because of using the craft standard model(craft.model.cube), here’s update()
function dirt:update()
self.top.rotation = quat.eulerAngles(0, self.angle+ElapsedTime*100, 0)
end
return dirt
end
function newBlockTypeDirt()
-- Create user-defined block type
scene.voxels.blocks:addAssetPack("Blocks")
local dirt = scene.voxels.blocks:new("myDirt")
dirt.setTexture(ALL, "Blocks:Dirt")
dirt.setColor(ALL, color(239, 222, 5))
dirt.tinted = true
dirt.scripted = true
dirt.state:addFlag("on", false)
-- The internal script
function dirt:created()
self:schedule(60)
self:set(COLOR, color(108, 233, 80))
end
function dirt:blockUpdate()
-- Randomise colour based on location
-- local x,y,z = self:xyz()
-- math.randomseed(x * y * z)
local c =color(math.random(128,255), math.random(128,255), math.random(128,255))
-- self.voxels:set(x,y,z,"color", c)
self:set(COLOR, c)
self:schedule(10)
end
return dirt
end
function update(dt)
scene:update(dt)
end
function draw()
update(DeltaTime)
scene:draw()
end