How to get the screen position in an unlit shader.

Hi, I’m writing a shader in unity that isn’t based on “#pragma surface surf Lambert”. How do I get the current pixel screen position in the fragment program? I need it to be in pixels (not between 0 and 1 or -1 and 1). Here’s my shader code:

Shader "selectedFace"
{
	SubShader
	{
		Pass
		{
			CGPROGRAM
			#pragma vertex vert 
			#pragma fragment frag
			float4 vert(float4 vertexPosition:POSITION):POSITION
			{
				return mul(UNITY_MATRIX_MVP,vertexPosition);
			}
			float4 frag(void):COLOR
			{
				//GET SCREEN POSITION HERE!!!
				return float4(0.75,1.0,0.0,1.0);
			}
			ENDCG
		}
	}
}

Thanks :smiley:

Define a new member in the structure passed from your vertex to fragment shader, say, scrPos. Assign it an unused TEXCOORD semantic.

Populate it in the vertex program using the ComputeScreenPos macro:

v2f vert(appdata_full v)
 {
     o.scrPos = ComputeScreenPos(v.pos);
     ...

And then use it as follows in the fragment shader:

 fixed4 frag(v2f i) : COLOR
 {
     float2 screenPosition = (i.scrPos.xy/i.scrPos.w);