I am trying to get the screen space position of each pixel in a texture on a mesh and write it into a texture. I was hoping to use a compute shader to do this:
#pragma kernel CSMain
struct VertexData
{
float4 v_point;
float2 uv;
float4x4 MVP;
};
RWTexture2D<float4> Result;
RWStructuredBuffer<VertexData> vBuf;
[numthreads(16,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
// one thread per pixel in texture
//Get pixels position in relation to vertices in UV space, using models UV layout
//multiply pixels object space position against MVP matrix to bring it into clip space and do some stuff to it
//finally, output this pixels colour back into the texture, storing screen space related data.
}
In the above code (lump of comments) I can tell that I am going to run into some problems. The VertexData struct contains mesh data per vertex, but I want to run a thread for each pixel in a texture and then figure out what my pixels position is on a triangle. In a regular shader, this would be interpolated from the vertex shader when using data in the fragment shader, but from my little knowledge of compute shaders, this would require something extra. In the back of my mind I feel like its going to end up being something to do with Barycentric coords, but I just can't put my finger on it!
A picture is worth a thousand words:
The desired end result is a texture mapped to the UV coords of a mesh, storing the screen space positions of each pixel on the mesh. If there is a better way of tackling this, please let me know!