How to add voxel to empty volume at runtime?

photonable posted this 26 April 2018

I have an empty volume on top of a filled volumed, and I would like to add a voxel to this empty volume. The empty volume is a prefab. I am currently using a slightly modified version of the PicaIslands script. Any help would be excellent!

Order By: Standard | Newest | Votes
GarethIW posted this 27 April 2018

Adding a voxel to a volume programatically is really simple, as long as you know the world position you want to add the voxel at:

Volume.SetVoxelAtWorldPosition(Vector3 pos, Voxel v);

It's getting the world position that is the tricky part! In traditional voxel engines, voxels are aligned to a set grid (axis aligned), so it's easy to calculate the position of an individual volume where a camera/mouse ray intersects.

PicaVoxel was designed so that volumes can be positioned and rotated anywhere in your scene, so it is tricky to get a voxel position without detecting an active voxel to attach another one to.

As you mentioned the PicaIslands demo, I'm assuming you want to add a voxel using raycasts to paint into your empty volume. I've come up with a modification of the IslandClickBoom.cs script that will allow you to do that. The accuracy of voxel placement will depend on the camera angle, zoom, and the size of voxels.

Attach the script to the camera, and paint with left mouse. If you want to paint at a first-person crosshair position, replace the raycast line with the one from IslandsClickBoom.cs.

Let me know if that helps

Last edited 27 April 2018

photonable posted this 27 April 2018

This works perfectly. I had the right idea for finding the world position, I just couldn't figure how to implement it. I am a beginner programmer... Thank you for the help!!

Our 137 members have posted 527 times in 128 discussions