0

I'm writing a shader in Unity3D that isn't based on "#pragma surface surf Lambert" or any other built in lighting model. 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 :D

user3551745
  • 33
  • 2
  • 9
  • What you need is a screenspace shader, then you can use the texture coordinates. – Imapler Oct 03 '14 at 12:09
  • How do I make a screenspace shader? – user3551745 Oct 03 '14 at 15:46
  • There are plenty to find on that subject on google. This is a template for unity screen space shader, or image effect as unity calls it. http://wiki.unity3d.com/index.php/Image_Effect_Template – Imapler Oct 03 '14 at 16:42
  • But I need to apply the shader to 3D geometry, not use it as post-process effect. – user3551745 Oct 04 '14 at 01:30
  • That is not an easy problem to solve and not how you do things normally. Maybe if you explain what you need it for, it would be easier to help. – Imapler Oct 04 '14 at 08:33
  • I need it for a dot kind of pattern. Like if `screenx%2==0&&screenY%2==0`, then set the pixel to red, otherwise set the pixel to green. – user3551745 Oct 04 '14 at 10:30
  • If you use the pixel coordinates for that the dot patter will be still when the geometry moves but maybe that what you want. This requires two steps, first do a image effect that renders the dot pattern to a rendertexture, then supply that texture to your geometry shader. This is sometimes called deferred rendering. – Imapler Oct 05 '14 at 09:27

1 Answers1

0

The OP asked how to get screen position of a pixel in the fragment shader. Making changes to the OP's code for getting fragment shader to work.

Shader "selectedFace"
{

    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert 
            #pragma fragment frag
            //for maximum portability using the UNITY_VPOS_TYPE 
            **#pragma target 3.0**

            float4 vert(float4 vertexPosition:POSITION):POSITION
            {
                return mul(UNITY_MATRIX_MVP,vertexPosition);
            }

            //Mapping SV_Target(D3D10) to COLOR(Direct3D 9)
            **float4 frag(UNITY_VPOS_TYPE screenPos : VPOS):SV_Target**
            {
                 //screenPos.x - x position on Screen
                 //screenPos.y - y position on Screen

                 return float4(0.75,1.0,0.0,1.0);
            }
            ENDCG
        }
    }
}
AAE
  • 1
  • 1