How to add voxel to empty volume at runtime?

0
photonable posted this 5 weeks ago

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!

2 Comments
Order By: Standard | Newest | Votes
0
GarethIW posted this 5 weeks ago

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.

https://gist.github.com/GarethIW/bb8634cef9e061d17049cdbc43213e1f

Let me know if that helps

Last edited 5 weeks ago

0
photonable posted this 4 weeks ago

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 123 members have posted 452 times in 108 discussions