[ad_1]
I need to use a Compute Shader to calculate the barycentric coordinates of a set of factors with UVs from a mesh. For that, I loop over the meshes’ UV-Coordinates within the order of the indices. This works fantastic for calculating the coordinates, however to get the fitting triangle, I additionally want the Indices of it. So I need to copy over the three indices from a buffer each time the barycentric Coords are legitimate.
The downside is, that it all the time copies the present indices of i and never the right ones when the if assertion triggers. The outcomes all the time have the identical three indices with totally different barycentric Coords. I haven’t got any thought why this works the way in which it does. Maybe it has one thing to do with the parallel nature of the GPU?
Here is the code:
StructuredBuffer<float2> _MeshUVs;
StructuredBuffer<int> _MeshIndices;
StructuredBuffer<float2> _RootUVs;
RWStructuredBuffer<int3> _FaceIndices;
RWStructuredBuffer<float3> _FaceBarys;
int _NumIndices;
[numthreads(32,1,1)]
void CalcBarys (uint3 id : SV_DispatchThreadID)
{
float3 rootUV = float3(_RootUVs[id.x], 0);
int3 faceIndices = int3(0, 0, 0);
float3 faceBarys = float3(0, 0, 0);
int check = 0;
[loop]
for (int i = 0; i < _NumIndices; i += 3)
{
int3 indices = int3(_MeshIndices[i], _MeshIndices[i + 1], _MeshIndices[i + 2]);
float3 triUVs0 = float3(_MeshUVs[i], 0);
float3 triUVs1 = float3(_MeshUVs[i + 1], 0);
float3 triUVs2 = float3(_MeshUVs[i + 2], 0);
float3 ab = triUVs1 - triUVs0;
float3 ac = triUVs2 - triUVs0;
float area2 = max(size(cross(ac, ac)), 0.0000001f);
float3 ra = triUVs0 - rootUV;
float3 rb = triUVs1 - rootUV;
float3 rc = triUVs2 - rootUV;
float alpha = size(cross(rb, rc)) / area2;
float beta = size(cross(rc, ra)) / area2;
float gamma = 1 - alpha - beta;
bool alphaInRange = alpha >= 0.0f && alpha <= 1.0f;
bool betaInRange = beta >= 0.0f && beta <= 1.0f;
bool gammaInRange = gamma >= 0.0f && gamma <= 1.0f;
bool inRange = alphaInRange && betaInRange && gammaInRange;
if (inRange)
{
faceBarys = float3(alpha, beta, gamma);
faceIndices = indices;
}
}
_FaceBarys[id.x] = faceBarys;
_FaceIndices[id.x] = faceIndices;
}
[ad_2]