Voxels and Volumes

Here’s an example I have for drawing voxels and volumes.

PS. Made changes to the coords for the hole in the sphere. Made it just large enough so it goes thru, but still leaves the single block a each end.

viewer.mode=FULLSCREEN

function setup()
    assert(OrbitViewer, "Please include Cameras as a dependency")   
    
    scene=craft.scene()
    craft.scene.main=scene  
    
    v=scene.camera:add(OrbitViewer,vec3(20,0,0),100,0,200)
    v.rx=10 
    
    scene.voxels.blocks:addAssetPack("Blocks")
    missing=scene.voxels.blocks:new("Missing")
    missing.setTexture(ALL,"Blocks:Missing")
    error=scene.voxels.blocks:new("Error")
    error.setTexture(ALL,"Blocks:Error")
    oven=scene.voxels.blocks:new("Oven")
    oven.setTexture(ALL,"Blocks:Oven")
    
    scene.voxels:resize(vec3(16,1,16))          
    scene.voxels.coordinates=vec3(20,0,20) 
    
    -- draw a single block
    scene.voxels:set(20,3,1,BLOCK_NAME,"missing") 
    
    -- draw a single block
    scene.voxels:fill(BLOCK_NAME,"Solid",COLOR,color(239,255,0))
    scene.voxels:block(18,4,8)
    
    -- draw a box (floor) 40x40 1 block high  
    scene.voxels:fill(BLOCK_NAME,"Solid",COLOR,color(255,0,0))
    scene.voxels:box(0,0,0,40,0,40)
    
    -- draw a large box 
    scene.voxels:fill(BLOCK_NAME,"error")
    scene.voxels:box(2,20,15,5,25,20)
       
    -- draw a sphere 10 block radius
    scene.voxels:fill(BLOCK_NAME,"Solid",COLOR,color(255))
    scene.voxels:sphere(20,15,20,10)
    
    -- draw a hole in the sphere
    scene.voxels:fill(BLOCK_NAME,"empty")
    scene.voxels:box(19,14,11,21,16,29)
    
    -- draw a line
    scene.voxels:fill(BLOCK_NAME,"oven")
    scene.voxels:line(35,2,2,35,25,30)
    
    -- draw a large volume block   
    volume = scene:entity():add(craft.volume,5,15,5) 
    for x=1,5 do
        for y=1,15 do
            for z=1,5 do
                volume:set(x,y,z,COLOR,color(0,222,255),BLOCK_NAME,"Solid") 
            end
        end
    end   
end

function draw()
    scene:draw()
end

It seems like a fairly complete demonstration of the basic abilities.

I’ve also been curious why those neat functions like sphere and line aren’t available in volumes, only in basic voxels.

@UberGoober There’s still a lot more that could have been added. I’m trying to find more info but there’s not too much so far.

A nice start at a useful learning demo. I find that there’s some context that i just don’t have, will try to read the docs more fully. Something about relation between volume, voxels, blocks, …

Noticed the hole z coords go beyond the sphere, confused me a bit. Some of the setup boiler plate could use some what/why commentary if/when you know, notably resize and coordinates calls.

I might fiddle with it a bit, see if i can gain some clues and express them, but I’m on a different path today …

Thanks!

@RonJeffries As for the hole coords, I just picked some values that were beyond the sphere size. I guess I could go back and update it with the minimum values for the hole.

I changed the coords in the above code so the hole thru the sphere goes thru but still leaves the single cube at each end.

I did that too, thinking it’s cute.

An nice example! Very well! It’s good to see you’re back on craft’s voxel

@binaryblues I like to give examples as simple as I can. I think that makes it easier to see what’s going on instead of some complex example. Complex examples are great for showing off what can be done, but when you’re just learning, simple is the best in my opinion. I just wish there were more docs that explain things instead of having to go thru code looking for what’s what.

Yes. That’s why I’m interested in what each of those setup statements does.

Yes, your simple examples are very useful and make it especially clear to the learner how to imitate them. I’ve collected most of the examples you’ve published. It’s true that learning through complex code is difficult, and I’ve gone over the built-in sample projects many times to learn craft.