-
-
Save Eldemarkki/193d72f77125fadbcf56f07f84d5fe87 to your computer and use it in GitHub Desktop.
A sample code for a blog post at eetumaenpaa.fi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Eldemarkki.VoxelTerrain.Meshing.Data; | |
| using Eldemarkki.VoxelTerrain.Utilities; | |
| using Eldemarkki.VoxelTerrain.VoxelData; | |
| using Unity.Burst; | |
| using Unity.Collections; | |
| using Unity.Jobs; | |
| using Unity.Mathematics; | |
| namespace Eldemarkki.VoxelTerrain.Meshing.MarchingCubes | |
| { | |
| [BurstCompile] | |
| public struct MarchingCubesJob : IJobParallelFor | |
| { | |
| [ReadOnly] public VoxelDataVolume<byte> voxelData; | |
| public byte isolevel; | |
| public NativeCounter vertexCountCounter; | |
| [NativeDisableParallelForRestriction, WriteOnly] public NativeArray<MeshingVertexData> vertices; | |
| [NativeDisableParallelForRestriction, WriteOnly] public NativeArray<ushort> triangles; | |
| public void Execute(int index) | |
| { | |
| int3 voxelLocalPosition = IndexUtilities.IndexToXyz(index, voxelData.Width-1, voxelData.Height-1); | |
| VoxelCorners<byte> densities = voxelData.GetVoxelDataUnitCube(voxelLocalPosition); | |
| byte cubeIndex = MarchingCubesFunctions.CalculateCubeIndex(densities, isolevel); | |
| if (cubeIndex == 0 || cubeIndex == 255) | |
| { | |
| return; | |
| } | |
| int edgeIndex = MarchingCubesLookupTables.EdgeTable[cubeIndex]; | |
| VertexList vertexList = MarchingCubesFunctions.GenerateVertexList(densities, voxelLocalPosition, edgeIndex, isolevel); | |
| // Index at the beginning of the row | |
| int rowIndex = 15 * cubeIndex; | |
| for (int i = 0; MarchingCubesLookupTables.TriangleTable[rowIndex + i] != -1 && i < 15; i += 3) | |
| { | |
| float3 vertex1 = vertexList[MarchingCubesLookupTables.TriangleTable[rowIndex + i + 0]]; | |
| float3 vertex2 = vertexList[MarchingCubesLookupTables.TriangleTable[rowIndex + i + 1]]; | |
| float3 vertex3 = vertexList[MarchingCubesLookupTables.TriangleTable[rowIndex + i + 2]]; | |
| if (!vertex1.Equals(vertex2) && !vertex1.Equals(vertex3) && !vertex2.Equals(vertex3)) | |
| { | |
| float3 normal = math.normalize(math.cross(vertex2 - vertex1, vertex3 - vertex1)); | |
| int triangleIndex = vertexCountCounter.Increment() * 3; | |
| vertices[triangleIndex + 0] = new MeshingVertexData(vertex1, normal); | |
| triangles[triangleIndex + 0] = (ushort)(triangleIndex + 0); | |
| vertices[triangleIndex + 1] = new MeshingVertexData(vertex2, normal); | |
| triangles[triangleIndex + 1] = (ushort)(triangleIndex + 1); | |
| vertices[triangleIndex + 2] = new MeshingVertexData(vertex3, normal); | |
| triangles[triangleIndex + 2] = (ushort)(triangleIndex + 2); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment