Hi there, I am trying to build some PBD cloth / softbody compute shaders, but am struggling with the part I thought would be the easiest: re-calculation of normals.
My current approch: Send info about mesh triangles to compute shader, and do normals calculation in each timestep like this:
#pragma kernel UpdateNormals
[numthreads(32, 1, 1)] // TODO: play around with numthread size
void UpdateNormals(uint3 id : SV_DispatchThreadID) {
if (id.x >= numTriangles) return;
uint3 indexes = triangles[id.x];
float3 p0 = positions[indexes.x];
float3 p1 = positions[indexes.y];
float3 p2 = positions[indexes.z];
//get normals
float3 n1 = cross(p1 - p0, p2 - p0);
normals[indexes.x] += float4(n1,0);
normals[indexes.y] += float4(n1, 0);
normals[indexes.z] += float4(n1, 0);
}
“positions” are the calculated positions of vertices of the mesh; “triangles” stores the 3 vertex indexes of each triangle.
Later in the code I do:
float4 normal = normalize(normals[id.x]);
The result goes into a RenderTexture, and get rendered by shader graph via vertex displacement. Normals look somehow correct, but are flickering a lot, especially in the darker areas. I have no clue what could cause this.
Any help would be appreciated.