Advertisement
JontePonte

marching cubes gpu regular buffer

Jul 11th, 2025
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #pragma kernel CSMain
  2.  
  3. struct Triangle
  4. {
  5. float3 vertex0;
  6. float3 vertex1;
  7. float3 vertex2;
  8. };
  9.  
  10. StructuredBuffer<float> voxelGrid;
  11. StructuredBuffer<int> caseToNumTris;
  12. StructuredBuffer<int> caseToTriEdges;
  13. StructuredBuffer<int3> edgeToCorners;
  14.  
  15. RWStructuredBuffer<float3> vertices;
  16. RWStructuredBuffer<uint> indices;
  17. RWStructuredBuffer<uint> vertexCounter;
  18.  
  19. int3 gridSize;
  20. int stepSize;
  21.  
  22. int VoxelIndex(int x, int y, int z)
  23. {
  24. return (z * gridSize.x * gridSize.y) + (y * gridSize.x) + x;
  25. }
  26.  
  27. int CalculateCase(int x, int y, int z)
  28. {
  29. // The 8 corner values of the voxel at X, Y, Z
  30. float c0 = voxelGrid[VoxelIndex(x, y, z)];
  31. float c1 = voxelGrid[VoxelIndex(x, y + 1, z)];
  32. float c2 = voxelGrid[VoxelIndex(x + 1, y + 1, z)];
  33. float c3 = voxelGrid[VoxelIndex(x + 1, y, z)];
  34. float c4 = voxelGrid[VoxelIndex(x, y, z + 1)];
  35. float c5 = voxelGrid[VoxelIndex(x, y + 1, z + 1)];
  36. float c6 = voxelGrid[VoxelIndex(x + 1, y + 1, z + 1)];
  37. float c7 = voxelGrid[VoxelIndex(x + 1, y, z + 1)];
  38.  
  39. int cubeIndex = (c0 > 0 ? 1 : 0)
  40. | (c1 > 0 ? 2 : 0)
  41. | (c2 > 0 ? 4 : 0)
  42. | (c3 > 0 ? 8 : 0)
  43. | (c4 > 0 ? 16 : 0)
  44. | (c5 > 0 ? 32 : 0)
  45. | (c6 > 0 ? 64 : 0)
  46. | (c7 > 0 ? 128 : 0);
  47.  
  48. return cubeIndex;
  49. }
  50.  
  51. float3 InterpolateVertex(int edge, int x, int y, int z)
  52. {
  53. int3 edgeCorner0 = edgeToCorners[edge * 2];
  54. int3 edgeCorner1 = edgeToCorners[edge * 2 + 1];
  55.  
  56. int3 p0 = edgeCorner0 * stepSize;
  57. int3 p1 = edgeCorner1 * stepSize;
  58.  
  59. float v0 = voxelGrid[VoxelIndex(x + edgeCorner0.x, y + edgeCorner0.y, z + edgeCorner0.z)];
  60. float v1 = voxelGrid[VoxelIndex(x + edgeCorner1.x, y + edgeCorner1.y, z + edgeCorner1.z)];
  61.  
  62. float isolevel = 0;
  63. float t = (isolevel - v0) / (v1 - v0);
  64.  
  65. float3 voxelPosition = float3(x, y, z) * stepSize;
  66. 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);
  67.  
  68. return vertexPosition;
  69. }
  70.  
  71. void MarchCube(int x, int y, int z)
  72. {
  73. int voxelCase = CalculateCase(x, y, z);
  74. int numTris = caseToNumTris[voxelCase];
  75. int edgeStartIndex = voxelCase * 15;
  76.  
  77. for (int i = 0; i < numTris; ++i)
  78. {
  79. int triEdge0 = caseToTriEdges[edgeStartIndex + i * 3];
  80. int triEdge1 = caseToTriEdges[edgeStartIndex + i * 3 + 1];
  81. int triEdge2 = caseToTriEdges[edgeStartIndex + i * 3 + 2];
  82.  
  83. float3 vertex0 = InterpolateVertex(triEdge0, x, y, z);
  84. float3 vertex1 = InterpolateVertex(triEdge1, x, y, z);
  85. float3 vertex2 = InterpolateVertex(triEdge2, x, y, z);
  86.  
  87. uint triIndex = vertexCounter.IncrementCounter();
  88. uint vertexIndex = triIndex * 3;
  89.  
  90. vertices[vertexIndex] = vertex0;
  91. vertices[vertexIndex + 1] = vertex1;
  92. vertices[vertexIndex + 2] = vertex2;
  93.  
  94. indices[vertexIndex] = vertexIndex;
  95. indices[vertexIndex + 1] = vertexIndex + 1;
  96. indices[vertexIndex + 2] = vertexIndex + 2;
  97. }
  98. }
  99.  
  100. [numthreads(8,8,8)]
  101. void CSMain (uint3 id : SV_DispatchThreadID)
  102. {
  103. if(id.x < gridSize.x - 1 && id.y < gridSize.y - 1 && id.z < gridSize.z - 1)
  104. {
  105. MarchCube((int)id.x, (int)id.y, (int)id.z);
  106. }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement