Advertisement
443eb9

Untitled

Aug 2nd, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #pragma kernel March
  2.  
  3. #include "MarchingTable.hlsl"
  4.  
  5. struct Triangle
  6. {
  7. float3 a;
  8. float3 b;
  9. float3 c;
  10. };
  11.  
  12. int reference;
  13. int sizeX;
  14. int sizeY;
  15. int sizeZ;
  16.  
  17. StructuredBuffer<float4> vertAndDensity;
  18. AppendStructuredBuffer<Triangle> triangles;
  19.  
  20. int coordToIndex(int x, int y, int z);
  21. float3 lerpVerts(float4 a, float4 b);
  22.  
  23. [numthreads(8, 8, 8)]
  24. void March(uint3 id : SV_DispatchThreadID)
  25. {
  26. if (id.x > sizeX || id.y > sizeY > id.z > sizeZ)
  27. return;
  28.  
  29. float4 vert[8] =
  30. {
  31. vertAndDensity[coordToIndex(id.x, id.y, id.z)],
  32. vertAndDensity[coordToIndex(id.x + 1, id.y, id.z)],
  33. vertAndDensity[coordToIndex(id.x + 1, id.y, id.z + 1)],
  34. vertAndDensity[coordToIndex(id.x, id.y, id.z + 1)],
  35. vertAndDensity[coordToIndex(id.x, id.y + 1, id.z)],
  36. vertAndDensity[coordToIndex(id.x + 1, id.y + 1, id.z)],
  37. vertAndDensity[coordToIndex(id.x + 1, id.y + 1, id.z + 1)],
  38. vertAndDensity[coordToIndex(id.x, id.y + 1, id.z + 1)]
  39. };
  40.  
  41. int edgeState = 0;
  42. for (int i = 0; i < 8; ++i)
  43. if (vert[i].w < reference)
  44. edgeState |= 1 << i;
  45.  
  46. Triangle a;
  47. a.a = vert[0];
  48. a.b = id;
  49. a.c = float3(sizeX, sizeY, vert[0].w);
  50. triangles.Append(a);
  51.  
  52. int tri[16] = edgeToTri[edgeState];
  53. for (int i = 0; tri[i] != -1; i += 3)
  54. {
  55. Triangle t;
  56. // t.a = lerpVerts(vert[edgeToTwoVerts[tri[i]][0]], vert[edgeToTwoVerts[tri[i]][1]]);
  57. // t.b = lerpVerts(vert[edgeToTwoVerts[tri[i + 1]][0]], vert[edgeToTwoVerts[tri[i + 1]][1]]);
  58. // t.c = lerpVerts(vert[edgeToTwoVerts[tri[i + 2]][0]], vert[edgeToTwoVerts[tri[i + 2]][1]]);
  59. t.a = float3(1, 1, 1);
  60. t.b = float3(1, 1, 1);
  61. t.c = float3(1, 1, 1);
  62. triangles.Append(t);
  63. }
  64. }
  65.  
  66. int coordToIndex(int x, int y, int z)
  67. {
  68. return x + y * sizeX + z * sizeX * sizeY;
  69. }
  70.  
  71. float3 lerpVerts(float4 a, float4 b)
  72. {
  73. return a.xyz + (b.xyz - a.xyz) * (a.w - b.w);
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement