Chunk Class for Unity Voxel Engine (Example)

Here’s an example of a Chunk class i use in some variants of the engine. This one is a bit experimental but working OK so far.

chunk

 

using UnityEngine;
using System.Collections.Generic;

public enum ChunkState:int {
    Invalid = 0,
    Base = 1,
    Terra = 10,
    ReGen = 11,
    Light = 15,
    Post_Light = 16,
    Mesh = 20,
    Render = 30   

}
public class LightSource {

    public Lux light;
    public int x;
    public int y;
    public int z;
}

public class Chunk {

    //render mesh data
    public List<int> triangles;
    public List<int>[] subtriangles;
    public List<Vector2> uvs;
    public List<Vector3> vertices;
    public List<Color> colours;
    public List<Vector3> normals;
    public List<Vector4> tangents;
    public List<Lux> lightsources;
    public List<IntVect> sunlitBlocks;

    //collision mesh data
    public List<int> col_triangles;
    public List<Vector3> col_vertices;



    //chunk world position
    public int x;
    public int y;
    public int z;

    public GameObject chunkObject;
    public ChunkState state;

 

    public bool AboveReady(ChunkState s) {
      
        if (y == World.yChunkRadius - 1) { return true; }

        int ts = (int)s;
        int cs = (int)World.GetChunkState(x, y + 1, z);

        if (cs == 0) { return true; }
        if (cs >= ts) { return true; }
        return false;
    }

    public bool SurroundingState(ChunkState cstate)
    {
        ChunkState north = World.GetChunkState(x, y, z + 1);
        if ((int)north < (int)cstate && north != ChunkState.Invalid) { return false; }

        ChunkState south = World.GetChunkState(x, y, z - 1);
        if ((int)south < (int)cstate && south != ChunkState.Invalid) { return false; }

        ChunkState east = World.GetChunkState(x + 1, y, z);
        if ((int)east < (int)cstate && east != ChunkState.Invalid) { return false; }

        ChunkState west = World.GetChunkState(x - 1, y, z);
        if ((int)west < (int)cstate && west != ChunkState.Invalid) { return false; }
        if (!AboveReady(cstate)) { return false; }

        /*
        ChunkState stack;
        for (int i = 0; i < World.yChunkRadius; i++)
        {
            stack = World.GetChunkState(x, i, z);
            if ((int)stack < (int)cstate && stack != ChunkState.Invalid) { return false; }
        }
        */


        return true;
    }

    public void Init(int _x, int _y, int _z)
    {
        x = _x; y = _y; z = _z;
        state = ChunkState.Base;

    }
    public void ReBuild() {
        sunlitBlocks = new List<IntVect>();
        col_vertices = new List<Vector3>();
        col_triangles = new List<int>();
        vertices = new List<Vector3>();
        triangles = new List<int>();
        uvs = new List<Vector2>();
        colours = new List<Color>();
        normals = new List<Vector3>();
        tangents = new List<Vector4>();

        subtriangles = new List<int>[Mats.matCount];
        for (int n = 0; n < Mats.matCount; n++) { subtriangles[n] = new List<int>(); }

    }





}

nVox

A voxel engine in c# and Unity 3D. Written from the ground up it’s been a fun learning experience so far. I have a better understanding now of 3D engines that use chunks to store data. Unity’s single main thread makes it a bit difficult to update collision data as fast as i would like.  Terrain generation and lighting can all be multi-threaded but the unity game object is a real bottle neck so far. I’ve had to limit the amount of work i let it do to keep the frame rate reasonable.

screen