Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma kernel CSMain
- struct Triangle
- {
- float3 vertex0;
- float3 vertex1;
- float3 vertex2;
- };
- StructuredBuffer<float> voxelGrid;
- StructuredBuffer<int> caseToNumTris;
- StructuredBuffer<int> caseToTriEdges;
- StructuredBuffer<int3> edgeToCorners;
- RWStructuredBuffer<float3> vertices;
- RWStructuredBuffer<uint> indices;
- RWStructuredBuffer<uint> vertexCounter;
- int3 gridSize;
- int stepSize;
- int VoxelIndex(int x, int y, int z)
- {
- return (z * gridSize.x * gridSize.y) + (y * gridSize.x) + x;
- }
- int CalculateCase(int x, int y, int z)
- {
- // The 8 corner values of the voxel at X, Y, Z
- float c0 = voxelGrid[VoxelIndex(x, y, z)];
- float c1 = voxelGrid[VoxelIndex(x, y + 1, z)];
- float c2 = voxelGrid[VoxelIndex(x + 1, y + 1, z)];
- float c3 = voxelGrid[VoxelIndex(x + 1, y, z)];
- float c4 = voxelGrid[VoxelIndex(x, y, z + 1)];
- float c5 = voxelGrid[VoxelIndex(x, y + 1, z + 1)];
- float c6 = voxelGrid[VoxelIndex(x + 1, y + 1, z + 1)];
- float c7 = voxelGrid[VoxelIndex(x + 1, y, z + 1)];
- int cubeIndex = (c0 > 0 ? 1 : 0)
- | (c1 > 0 ? 2 : 0)
- | (c2 > 0 ? 4 : 0)
- | (c3 > 0 ? 8 : 0)
- | (c4 > 0 ? 16 : 0)
- | (c5 > 0 ? 32 : 0)
- | (c6 > 0 ? 64 : 0)
- | (c7 > 0 ? 128 : 0);
- return cubeIndex;
- }
- float3 InterpolateVertex(int edge, int x, int y, int z)
- {
- int3 edgeCorner0 = edgeToCorners[edge * 2];
- int3 edgeCorner1 = edgeToCorners[edge * 2 + 1];
- int3 p0 = edgeCorner0 * stepSize;
- int3 p1 = edgeCorner1 * stepSize;
- float v0 = voxelGrid[VoxelIndex(x + edgeCorner0.x, y + edgeCorner0.y, z + edgeCorner0.z)];
- float v1 = voxelGrid[VoxelIndex(x + edgeCorner1.x, y + edgeCorner1.y, z + edgeCorner1.z)];
- float isolevel = 0;
- float t = (isolevel - v0) / (v1 - v0);
- float3 voxelPosition = float3(x, y, z) * stepSize;
- float3 vertexPosition = float3(voxelPosition.x + p0.x, voxelPosition.y + p0.y, voxelPosition.z + p0.z) + t * float3(p1.x - p0.x, p1.y - p0.y, p1.z - p0.z);
- return vertexPosition;
- }
- void MarchCube(int x, int y, int z)
- {
- int voxelCase = CalculateCase(x, y, z);
- int numTris = caseToNumTris[voxelCase];
- int edgeStartIndex = voxelCase * 15;
- for (int i = 0; i < numTris; ++i)
- {
- int triEdge0 = caseToTriEdges[edgeStartIndex + i * 3];
- int triEdge1 = caseToTriEdges[edgeStartIndex + i * 3 + 1];
- int triEdge2 = caseToTriEdges[edgeStartIndex + i * 3 + 2];
- float3 vertex0 = InterpolateVertex(triEdge0, x, y, z);
- float3 vertex1 = InterpolateVertex(triEdge1, x, y, z);
- float3 vertex2 = InterpolateVertex(triEdge2, x, y, z);
- uint triIndex = vertexCounter.IncrementCounter();
- uint vertexIndex = triIndex * 3;
- vertices[vertexIndex] = vertex0;
- vertices[vertexIndex + 1] = vertex1;
- vertices[vertexIndex + 2] = vertex2;
- indices[vertexIndex] = vertexIndex;
- indices[vertexIndex + 1] = vertexIndex + 1;
- indices[vertexIndex + 2] = vertexIndex + 2;
- }
- }
- [numthreads(8,8,8)]
- void CSMain (uint3 id : SV_DispatchThreadID)
- {
- if(id.x < gridSize.x - 1 && id.y < gridSize.y - 1 && id.z < gridSize.z - 1)
- {
- MarchCube((int)id.x, (int)id.y, (int)id.z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement