Why is my marching cubes code not working?
Hi i'm trying to create marching cubes in unity but it's not working.
I'm pretty new to voxel stuff so what I wrote could actually be pretty stupid. ;-;
But please try to explain my very small brain what i did wrong and give me some tipps. XD
This is what I used to create the not working mess:http://paulbourke.net/geometry/polygonise/
And this is is my current code:
using System.Collections.Generic;
using UnityEngine;
public static class MarchingCubes
{
public static Mesh MarchCube(Cube cube, float isolevel)
{
Vector3[] vertlist = new Vector3[12];
List<int> triangles = new List<int>();
int cubeindex = 0;
if (cube.voxels[0].value < isolevel) cubeindex |= 1;
if (cube.voxels[0].value < isolevel) cubeindex |= 2;
if (cube.voxels[0].value < isolevel) cubeindex |= 4;
if (cube.voxels[0].value < isolevel) cubeindex |= 8;
if (cube.voxels[0].value < isolevel) cubeindex |= 16;
if (cube.voxels[0].value < isolevel) cubeindex |= 32;
if (cube.voxels[0].value < isolevel) cubeindex |= 64;
if (cube.voxels[0].value < isolevel) cubeindex |= 128;
/* Cube is entirely in/out of the surface */
if (Tables.edgeTable[cubeindex] == 0) return null;
/* Find the vertices where the surface intersects the cube */
if ((Tables.edgeTable[cubeindex] & 1) > 0) vertlist[0] = VertexInterp(isolevel, cube.voxels[0], cube.voxels[1]);
if ((Tables.edgeTable[cubeindex] & 2) > 0) vertlist[1] = VertexInterp(isolevel, cube.voxels[1], cube.voxels[2]);
if ((Tables.edgeTable[cubeindex] & 4) > 0) vertlist[2] = VertexInterp(isolevel, cube.voxels[2], cube.voxels[3]);
if ((Tables.edgeTable[cubeindex] & 8) > 0) vertlist[3] = VertexInterp(isolevel, cube.voxels[3], cube.voxels[0]);
if ((Tables.edgeTable[cubeindex] & 16) > 0) vertlist[4] = VertexInterp(isolevel, cube.voxels[4], cube.voxels[5]);
if ((Tables.edgeTable[cubeindex] & 32) > 0) vertlist[5] = VertexInterp(isolevel, cube.voxels[5], cube.voxels[6]);
if ((Tables.edgeTable[cubeindex] & 64) > 0) vertlist[6] = VertexInterp(isolevel, cube.voxels[6], cube.voxels[7]);
if ((Tables.edgeTable[cubeindex] & 128) > 0) vertlist[7] = VertexInterp(isolevel, cube.voxels[7], cube.voxels[4]);
if ((Tables.edgeTable[cubeindex] & 256) > 0) vertlist[8] = VertexInterp(isolevel, cube.voxels[0], cube.voxels[4]);
if ((Tables.edgeTable[cubeindex] & 512) > 0) vertlist[9] = VertexInterp(isolevel, cube.voxels[1], cube.voxels[5]);
if ((Tables.edgeTable[cubeindex] & 1024) > 0) vertlist[10] = VertexInterp(isolevel, cube.voxels[2], cube.voxels[6]);
if ((Tables.edgeTable[cubeindex] & 2048) > 0) vertlist[11] = VertexInterp(isolevel, cube.voxels[3], cube.voxels[7]);
/* Create the triangle */
int ntriang = 0;
for (int i = 0; Tables.triTable[cubeindex][i] != -1; i += 3)
{
triangles[ntriang] = Tables.triTable[cubeindex][i];
triangles[ntriang + 1] = Tables.triTable[cubeindex][i + 1];
triangles[ntriang + 2] = Tables.triTable[cubeindex][i + 2];
}
Mesh mesh = new Mesh();
mesh.vertices = vertlist;
mesh.triangles = triangles.ToArray();
return mesh;
}
public static Vector3 VertexInterp(float isovalue ,Voxel V1, Voxel V2)
{
return Vector3.Lerp(V1.scenePosition, V2.scenePosition, (isovalue - V1.value) / (V2.value - V1.value));
}
}
Thanks to everyone might try to help :D