Example #1
0
        /// <summary>
        /// Generates the triangles for a specific voxel
        /// </summary>
        /// <param name="node">The node that contains the voxel</param>
        /// <param name="pos">The voxel position (Not real position) [16,16,16]</param>
        /// <param name="triangleList">The list used to contain triangles made so far</param>
        /// <param name="densities">The array that contains density information</param>
        /// <param name="densityNormals">The array that contains density normals</param>
        private static void generateTriangles(Node node, Vector3I pos, List<Triangle> triangleList, List<int> submeshIDList, int[] subMeshTriCount, DensityData densities, Vector3[, ,] densityNormals)
        {
            float size = NodeManager.LODSize[node.LOD];

            float[] denses = new float[8];
            denses[0] = densities.get(pos.x, pos.y, pos.z + 1);
            denses[1] = densities.get(pos.x + 1, pos.y, pos.z + 1);
            denses[2] = densities.get(pos.x + 1, pos.y, pos.z);
            denses[3] = densities.get(pos.x, pos.y, pos.z);
            denses[4] = densities.get(pos.x, pos.y + 1, pos.z + 1);
            denses[5] = densities.get(pos.x + 1, pos.y + 1, pos.z + 1);
            denses[6] = densities.get(pos.x + 1, pos.y + 1, pos.z);
            denses[7] = densities.get(pos.x, pos.y + 1, pos.z);

            byte cubeIndex = 0;

            if (denses[0] < isolevel)
                cubeIndex |= 1;
            if (denses[1] < isolevel)
                cubeIndex |= 2;
            if (denses[2] < isolevel)
                cubeIndex |= 4;
            if (denses[3] < isolevel)
                cubeIndex |= 8;
            if (denses[4] < isolevel)
                cubeIndex |= 16;
            if (denses[5] < isolevel)
                cubeIndex |= 32;
            if (denses[6] < isolevel)
                cubeIndex |= 64;
            if (denses[7] < isolevel)
                cubeIndex |= 128;

            if (cubeIndex == 0 || cubeIndex == 255)
                return;

            Vector3 origin = new Vector3((size * (pos.x))
                , (size * (pos.y))
                , (size * (pos.z)));

            Vector3[] positions = new Vector3[8];
            positions[0] = new Vector3(origin.x, origin.y, origin.z + size);
            positions[1] = new Vector3(origin.x + size, origin.y, origin.z + size);
            positions[2] = new Vector3(origin.x + size, origin.y, origin.z);
            positions[3] = new Vector3(origin.x, origin.y, origin.z);
            positions[4] = new Vector3(origin.x, origin.y + size, origin.z + size);
            positions[5] = new Vector3(origin.x + size, origin.y + size, origin.z + size);
            positions[6] = new Vector3(origin.x + size, origin.y + size, origin.z);
            positions[7] = new Vector3(origin.x, origin.y + size, origin.z);

            Vector3[][] vertlist = new Vector3[12][];
            if (IsBitSet(edgeTable[cubeIndex], 1))
                vertlist[0] = VertexInterp(isolevel, positions[0], positions[1], denses[0], denses[1], densityNormals[pos.x, pos.y, pos.z + 1], densityNormals[pos.x + 1, pos.y, pos.z + 1]);
            if (IsBitSet(edgeTable[cubeIndex], 2))
                vertlist[1] = VertexInterp(isolevel, positions[1], positions[2], denses[1], denses[2], densityNormals[pos.x + 1, pos.y, pos.z + 1], densityNormals[pos.x + 1, pos.y, pos.z]);
            if (IsBitSet(edgeTable[cubeIndex], 4))
                vertlist[2] = VertexInterp(isolevel, positions[2], positions[3], denses[2], denses[3], densityNormals[pos.x + 1, pos.y, pos.z], densityNormals[pos.x, pos.y, pos.z]);
            if (IsBitSet(edgeTable[cubeIndex], 8))
                vertlist[3] = VertexInterp(isolevel, positions[3], positions[0], denses[3], denses[0], densityNormals[pos.x, pos.y, pos.z], densityNormals[pos.x, pos.y, pos.z + 1]);
            if (IsBitSet(edgeTable[cubeIndex], 16))
                vertlist[4] = VertexInterp(isolevel, positions[4], positions[5], denses[4], denses[5], densityNormals[pos.x, pos.y + 1, pos.z + 1], densityNormals[pos.x + 1, pos.y + 1, pos.z + 1]);
            if (IsBitSet(edgeTable[cubeIndex], 32))
                vertlist[5] = VertexInterp(isolevel, positions[5], positions[6], denses[5], denses[6], densityNormals[pos.x + 1, pos.y + 1, pos.z + 1], densityNormals[pos.x + 1, pos.y + 1, pos.z]);
            if (IsBitSet(edgeTable[cubeIndex], 64))
                vertlist[6] = VertexInterp(isolevel, positions[6], positions[7], denses[6], denses[7], densityNormals[pos.x + 1, pos.y + 1, pos.z], densityNormals[pos.x, pos.y + 1, pos.z]);
            if (IsBitSet(edgeTable[cubeIndex], 128))
                vertlist[7] = VertexInterp(isolevel, positions[7], positions[4], denses[7], denses[4], densityNormals[pos.x, pos.y + 1, pos.z], densityNormals[pos.x, pos.y + 1, pos.z + 1]);
            if (IsBitSet(edgeTable[cubeIndex], 256))
                vertlist[8] = VertexInterp(isolevel, positions[0], positions[4], denses[0], denses[4], densityNormals[pos.x, pos.y, pos.z + 1], densityNormals[pos.x, pos.y + 1, pos.z + 1]);
            if (IsBitSet(edgeTable[cubeIndex], 512))
                vertlist[9] = VertexInterp(isolevel, positions[1], positions[5], denses[1], denses[5], densityNormals[pos.x + 1, pos.y, pos.z + 1], densityNormals[pos.x + 1, pos.y + 1, pos.z + 1]);
            if (IsBitSet(edgeTable[cubeIndex], 1024))
                vertlist[10] = VertexInterp(isolevel, positions[2], positions[6], denses[2], denses[6], densityNormals[pos.x + 1, pos.y, pos.z], densityNormals[pos.x + 1, pos.y + 1, pos.z]);
            if (IsBitSet(edgeTable[cubeIndex], 2048))
                vertlist[11] = VertexInterp(isolevel, positions[3], positions[7], denses[3], denses[7], densityNormals[pos.x, pos.y, pos.z], densityNormals[pos.x, pos.y + 1, pos.z]);

            int submesh = densities.getMaterial(pos.x, pos.y, pos.z);
            for (int i = 0; triTable[cubeIndex][i] != -1; i += 3)
            {
                submeshIDList.Add(submesh);
                subMeshTriCount[submesh] = subMeshTriCount[submesh] + 1;
                triangleList.Add(new Triangle(vertlist[triTable[cubeIndex][i]][0], vertlist[triTable[cubeIndex][i + 1]][0], vertlist[triTable[cubeIndex][i + 2]][0],
                    vertlist[triTable[cubeIndex][i]][1], vertlist[triTable[cubeIndex][i + 1]][1], vertlist[triTable[cubeIndex][i + 2]][1]));
            }
        }
Example #2
0
 /// <summary>
 /// Calculates the normal.
 /// </summary>
 /// <param name="p"></param>
 private static Vector3 calculateDensityNormal(Vector3I p, DensityData densities, int lod)
 {
     Vector3 normal = new Vector3();
     normal.x = (densities.get(p.x + 1, p.y, p.z) - densities.get(p.x - 1, p.y, p.z)) / (NodeManager.LODSize[lod]);
     normal.y = (densities.get(p.x, p.y + 1, p.z) - densities.get(p.x, p.y - 1, p.z)) / (NodeManager.LODSize[lod]);
     normal.z = (densities.get(p.x, p.y, p.z + 1) - densities.get(p.x, p.y, p.z - 1)) / (NodeManager.LODSize[lod]);
     normal.Normalize();
     return normal;
 }