I have a voxel-based object that moves around my world. Right now I have it so that the object is made up of many separate cube objects parented to the main object. Each of these separate cubes has a script attached that makes it so they unparent when hit by a projectile and thus the object is completely destructible.
The problem is, the main object is then made up of about 900 separate cubes, and all of them, even the inner ones are always loaded.
I’ve tried InstantOC, an occlusion culling system, but even at a high raycast sample rate (which gets to be similarly expensive on it’s own), the cubes don’t always render properly, especially at semi-far distances.
I really need my object to be completely destructible as it is with just as many individual objects, but this is a major roadblock as I need to have many of these objects on screen at once eventually, so this needs to be solved before I move on.
If you could offer any help or suggestions, I would greatly appreciate it, thanks!
You have fallen into the biggest and most fallen into trap in the world of voxel-making. Voxels aren’t actually cubes. If they were, then even a very small world, say 10x10x10 blocks would be 1000 cubes, which means 12000 triangles and 24000 vertices. Voxel engines actually use ‘hypothetical voxels’, which contain all of the information you might need to construct a voxel (uv coords, light, light emission, solidity, transparency…). Everything except a transform(because this is dependent on their location within the chunk. Then you need to make chunks(these can be objects), with a script that both stores a 3d array of hypotheticals, and then uses them to proceduraly generate a mesh for the chunk that only shows the faces which would be visible.
Essentially: Take this voxel. If it is solid, then proceed, otherwise skip it and move on. If there is a transparent voxel on this side, build a side of the cube, otherwise skip that side and go on.(do this for all of the sides) Then move on to the next voxel…
REMEMBER! YOU NEED AIR VOXELS! (non-solid, transparent voxels).
VOXEL GENERATION CAN BE VERY EASY, BUT REMEMBER: VOXELS ARE NOT OBJECTS
I hope this helped. I am working on a voxel engine as well, and once I learned about this, My month long brain-ache finally stopped.
I would try a different approach: child the cubes to the healthy object and keep them deactivated - this way the cubes aren’t rendered and don’t waste time with physics calculations. When needed, detach and activate the children and destroy the healthy object.
The hierarchy could be something like this:
HealthyObject // the healthy version, is active from the beginning
DestroyedVersion // damaged version - empty object deactivated from the beginning
Cube // object pieces, all of them childed to the empty DestroyedObject
Cube // the pieces are set active by default, but will stay deactivated
Cube // because their parent isn't active
...
When the object is hit, set DestroyedVersion.transform.parent to null in order to detach it and destroy or deactivate HealthyObject: all pieces below DestroyedVersion in the hierarchy will wake up, since they already were active (although kept inactive due to their parent being deactivated).
maybe make particle system with those cubes as an particles, and when object gets hit maybe change it to other model (maybe 1 block? or replace with explosion ), then some script enabling particle system shoting cubes in all directions