Shader Screenspace Position Issue

I am trying to apply a post processing effect to all pixels outside of a certain range of my character.

This is the shader code I have so far for converting my players position into screenspace and testing each fragment against it.

//Worldspace Player Pos
float4 _PlayerPosition;

float4 Frag(VaryingsDefault i) : SV_Target
{
    float4 playerPositionClipSpace = mul(unity_MatrixVP,_PlayerPosition);
    float3 playerPositionScreenSpace = playerPositionClipSpace.xyz / playerPositionClipSpace.w;
    playerPositionScreenSpace = playerPositionScreenSpace * 0.5 + 0.5;

    float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
    if (length(playerPositionScreenSpace.xy - i.texcoord) >= 0.1f)
    {
        color.rgb = float3(0,0,0);
    }
    return color;
}

My problem is, that the resulting circle is somehow not centered on the given character position.

Would appreciate if someone has an idea for why that may be the case :slight_smile:

Okay I figured it out on my own. The issue was that when running on DirectX Texture Coordinates need to be flipped on the y-axis.
Unity - Manual: Writing shaders for different graphics APIs has more information

This is how I did it in case anyone else runs into this issue and is confused.

 if (length(playerPositionNDC - float2(i.texcoord.x,1-i.texcoord.y)) >= 0.1f)


The result is now correctly centered on the playerPosition.