Example9: More personalized simple block type
New chages:
- You can set the texture map and the color on each side, using the code like:
dirt.setTexture(UP, "Blocks:Dirt")
dirt.setTexture(DOWN, "Blocks:Stone")
dirt.setTexture(EAST, "Blocks:Grass2")
dirt.setTexture(NORTH, "Blocks:Grass1")
dirt.setTexture(WEST, "Blocks:Leaves")
dirt.setTexture(SOUTH, "Blocks:Oven")
dirt.setColor(UP, color(255))
...
dirt.setColor(SOUTH, color(255))
- Added a box with a maximum of 255 * 255 * 255 so you can locate coord
@John I found two problems:
-
NORTH will use the same texture map as SOUTH, It’s not working on its own.
-
The tinted switch works the other way around. When I set it to true, the block will go dark.
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, 4000)
-- set voxel terrain parameters: size, coord
scene.voxels:resize(vec3(5,1,5),vec3(16,128,16))
scene.voxels.coordinates = vec3(0,0,0)
-- Create 2 new block types: "simpleGrass", "simpleDirt1"
createSimpleBlockType("simpleGrass", "Blocks:Grass Top", color(201, 233, 80))
-- Using block type: simpleGrass as the filling unit
scene.voxels:fill("simpleGrass")
-- Create a box using "simpleGrass" block type, every unit in the box is a "simpleGrass" block
scene.voxels:box(0,0,0, 16*8,10,16*8)
-- Using new block type: TreeGenerator as the filling unit
scene.voxels:fill("simpleGrass")
-- A single block
scene.voxels:block(vec3(30,25,35))
-- Create a sphere and a line using "simpleGrass" block type,
-- every unit in those two shapes is a "simpleGrass" block
scene.voxels:line(10,20,0,10,30,40)
scene.voxels:sphere(40,20,60,4)
-- craft.scene.main = scene
end
function createSimpleBlockType(blockTypeName,texture,blockColor)
-- Create user-defined block type
scene.voxels.blocks:addAssetPack("Blocks")
local dirt = scene.voxels.blocks:new(blockTypeName)
---[[
dirt.setTexture(ALL, texture or "Blocks:Dirt")
dirt.setTexture(UP, "Blocks:Dirt")
dirt.setTexture(DOWN, "Blocks:Stone")
dirt.setTexture(EAST, "Blocks:Grass2")
dirt.setTexture(NORTH, "Blocks:Grass1")
dirt.setTexture(WEST, "Blocks:Leaves")
dirt.setTexture(SOUTH, "Blocks:Oven")
--]]
---[[
dirt.setColor(ALL, blockColor or color(251))
dirt.setColor(UP, color(255))
dirt.setColor(DOWN, color(238, 235, 235))
dirt.setColor(EAST, color(5, 235, 239))
dirt.setColor(NORTH, color(249, 248, 248))
dirt.setColor(WEST, color(249))
dirt.setColor(SOUTH, color(255))
--]]
dirt.tinted = false
end
function update(dt)
scene:update(dt)
blockRange()
end
function draw()
update(DeltaTime)
scene:draw()
end
-- Draw a 256*256*256 box, from vec3(0,0,0) to vec3(255,255,255), it is the block type range.
function blockRange()
scene.debug:line(vec3(0, 0, 0),vec3(255, 0, 0),color(47, 239, 5))
scene.debug:line(vec3(0,0,0),vec3(0, 255, 0),color(239, 5, 5))
scene.debug:line(vec3(0,0,0),vec3(0, 0, 255),color(5, 41, 239))
scene.debug:line(vec3(255,255,255),vec3(0, 255, 255),color(239, 91, 5))
scene.debug:line(vec3(255,255,255),vec3(255, 0, 255),color(214, 5, 239))
scene.debug:line(vec3(255,255,255),vec3(255, 255, 0),color(5, 238, 239))
scene.debug:line(vec3(255, 255, 0),vec3(255, 0, 0),color(239, 234, 5))
scene.debug:line(vec3(255,0,255),vec3(255, 0, 0),color(239, 5, 5))
scene.debug:line(vec3(0,0,255),vec3(255, 0, 255),color(128, 5, 239))
scene.debug:line(vec3(0,255,255),vec3(0, 0, 255),color(239, 5, 172))
scene.debug:line(vec3(0,255,255),vec3(0, 255, 0),color(185, 239, 5))
scene.debug:line(vec3(255,255,0),vec3(0, 255, 0),color(5, 238, 239))
end
https://youtu.be/XrQn8YV3UpE