Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma kernel March
- #include "MarchingTable.hlsl"
- struct Triangle
- {
- float3 a;
- float3 b;
- float3 c;
- };
- int reference;
- int sizeX;
- int sizeY;
- int sizeZ;
- StructuredBuffer<float4> vertAndDensity;
- AppendStructuredBuffer<Triangle> triangles;
- int coordToIndex(int x, int y, int z);
- float3 lerpVerts(float4 a, float4 b);
- [numthreads(8, 8, 8)]
- void March(uint3 id : SV_DispatchThreadID)
- {
- if (id.x > sizeX || id.y > sizeY > id.z > sizeZ)
- return;
- float4 vert[8] =
- {
- vertAndDensity[coordToIndex(id.x, id.y, id.z)],
- vertAndDensity[coordToIndex(id.x + 1, id.y, id.z)],
- vertAndDensity[coordToIndex(id.x + 1, id.y, id.z + 1)],
- vertAndDensity[coordToIndex(id.x, id.y, id.z + 1)],
- vertAndDensity[coordToIndex(id.x, id.y + 1, id.z)],
- vertAndDensity[coordToIndex(id.x + 1, id.y + 1, id.z)],
- vertAndDensity[coordToIndex(id.x + 1, id.y + 1, id.z + 1)],
- vertAndDensity[coordToIndex(id.x, id.y + 1, id.z + 1)]
- };
- int edgeState = 0;
- for (int i = 0; i < 8; ++i)
- if (vert[i].w < reference)
- edgeState |= 1 << i;
- Triangle a;
- a.a = vert[0];
- a.b = id;
- a.c = float3(sizeX, sizeY, vert[0].w);
- triangles.Append(a);
- int tri[16] = edgeToTri[edgeState];
- for (int i = 0; tri[i] != -1; i += 3)
- {
- Triangle t;
- // t.a = lerpVerts(vert[edgeToTwoVerts[tri[i]][0]], vert[edgeToTwoVerts[tri[i]][1]]);
- // t.b = lerpVerts(vert[edgeToTwoVerts[tri[i + 1]][0]], vert[edgeToTwoVerts[tri[i + 1]][1]]);
- // t.c = lerpVerts(vert[edgeToTwoVerts[tri[i + 2]][0]], vert[edgeToTwoVerts[tri[i + 2]][1]]);
- t.a = float3(1, 1, 1);
- t.b = float3(1, 1, 1);
- t.c = float3(1, 1, 1);
- triangles.Append(t);
- }
- }
- int coordToIndex(int x, int y, int z)
- {
- return x + y * sizeX + z * sizeX * sizeY;
- }
- float3 lerpVerts(float4 a, float4 b)
- {
- return a.xyz + (b.xyz - a.xyz) * (a.w - b.w);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement