Reconstruct screen coord with worl pos in a surface shader.

Hello,

I’m trying to make my own screen coord instead of using the float4 screenPos; in my surface shader to by pass the texture interpolator limit.

Currently I’m doing it like this :

float2 worldToScreen(float3 pos)
{
	float4 screenPos = mul(UNITY_MATRIX_VP, float4(pos,1));
	return screenPos.xy / screenPos.w;
}

float3 worldPos = IN.worldPos;

float2 screenPos = worldToScreen(worldPos);
		
float2 screenCoord = float2(0,0);

#ifdef SHADER_API_D3D9
	screenCoord = float2(0.5, -0.5)*screenPos + float2(0.5,0.5);
#else
	screenCoord = float2(0.5, 0.5)*screenPos + float2(0.5,0.5);
#endif

I got the same result as if I do it like this :

float2 screencoord = IN.screenPos.xy/IN.screenPos.w;

#ifdef SHADER_API_D3D9
	screencoord.y =1-screencoord.y;
#endif

( By same result I mean I got the same Green Red Yellow gradient )

But if I applied the coord to my noise texture I get this with mine :

and this with the unity screenPos :

With mine the noise is blurry for some reason while with the regular screenPos it’s correct and sharp.

So I tried to change my noise to point instead of Trilinear and it works but if I move the camera I can see weird artefact.

And I got this ( I increased contrast ):

It’s hard to see but the texture is miss aligned or not well place.

Could that be the mesh behind doing this ?

Anyone has faced that problem ?

Thanks :slight_smile:

Try possibly adding 0.5,0.5 to the coordinates to center them on the pixel?

Thanks, they are half blurred from the original coord now but appearss to be not enough for my BRDF sadly.

With same roughness, I can see that my brdf goes in the wrong way.

Here it’s correct.

Maybe I’m not using the right matrix ?

Instead of using a surface shader, I made a fragment program much easier to make my stuff working, no more errors now !

Thanks for the help !