Unity Marching Cubes Compute Lookup Tables

Hello!

Today I’m releasing a Unity compute shader include which has part of the Transvoxel lookup table implemented in hlsl. I built this while working on my Unity terrain generator’s compute shader support (see image above). I am releasing this part of it simply because it was annoying to convert to hlsl, so I’m releasing it to save anyone else the bother. I will be adding the transition cell stuff once I implement them in my system.

I hope this saves you time when working on your next marching cubes project. Also, this probably will also work with other hlsl compute shaders, not just unity.

Usage example

Here’s an example as to how you use this (hint: it’s exactly the same as the original).

// Generate case code
int caseCode = 0;
for (int i = 0; i < 8; i++)
    if (densities[i] < isoLevel) caseCode |= 1 << i;

// Get the cell class
int cellClass = regularCellClass[caseCode];

// Get cell data
RegularCell cellData = regularCellData[cellClass];

// Now you have access to the number of triangles, number of vertices, vertex data and the indices.
int triangleCount = cellData.tCount;
int vertexCount = cellData.vCount;
int indices[] = cellData.indices;

// Get the vertex data
int vertexData[] = regularVertexData[caseCode];

And here’s an example on using the vertex data (note, this example does not show vertex reuse).

// Vertex data usage example
int edgeCode = vertexData[vertexIndex];
int v0 = ((edgeCode & 0xFF) >> 0) & 0x0F;
int v1 = ((edgeCode & 0xFF) >> 4) & 0x0F;