Search Unity

Voxelbased Game Mesh Calculation

Discussion in 'Scripting' started by Rabbito, Apr 5, 2015.

  1. Rabbito

    Rabbito

    Joined:
    Apr 5, 2015
    Posts:
    13
    Hello

    I had a simple VoxelWorld. I build it like in this tutorial http://alexstv.com/index.php/category/voxels .
    In short: I create a Mesh for one Chunk, based on the Blockdata in the Chunk. The Chunk is 32x1x32. I need the possibility to hide every Blocklayer in the y Axsis (thats why the Chunk has only y = 1). On every Frame i call Graphics.drawMesh() from MinY to the Blocklayer i want to see. Because of this i dont need GameObjects for the Rendering Meshes. VoxelWorld.png

    For the Physics i need to create one Gameobject for every Chunk with a Mesh-Collider.
    The biggest Problem ocours when i want to set a new Block. If i am setting a new Block i have to recalculate the hole Chunk.(and the Chunk over and the Chunk topsy the Manipulated Block). If I set a one Block in the World this is not a problem. But if i am setting 20 Blocks (in different Chunks) i need to calculate 20*3 = 60 Chunks (Maybe more if the Block is at the X or Z edge of the Chunk)

    - How i can manipulate the Mesh of the Chunk with the new Block without creating it from the scratch?
    My idea was to Manipulate the Mesh.vertices directly. Or to do the Calculation of the Mesh in a thread. But the Code to assign the Manipulated Mesh have to run in Main Thread.
    mesh.vertices = ManipulatedVertices; (and this will copy the hole array)
    For The Physics i have to do the same but assigning a new Mesh to a MeshCollider is a very slow operation.
    Maybe i could creating a simple Physics Script that dont allow other Objects to move to near to a Voxel.

    Maybe I could use another system for the hole thing?
     
  2. Rabbito

    Rabbito

    Joined:
    Apr 5, 2015
    Posts:
    13
    If Someone is interested: I had done it the Way i wrote.

    For the Physics: I dont create the gameObjects with a Meshcollider and i had implement an Physic script that the characters cant go throught the Renders-Meshes.

    For the recalculation of the Chunks:
    I stored the MeshData in my own array and i am manipulating the MeshData directly and only remove the Triangels and Vertices from the array which were assigned to the block.I need twice as much memory because i had to store the hole mesh in the memory. With this system i am able to update 120 Chunks in one Frame. ( I could get the MeshData directly from the Mesh with myVertices = mesh.vertices . This would be more CPU intesive but would free up Memory)