Search Unity

Vertex Fragment shader - Projector

Discussion in 'Shaders' started by MaT227, Aug 14, 2014.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Hello eveybody,

    I am trying to create a simple Vertex Fragment shader for projector but I am facing some issues. In Orthographic mode the projection is perfect and resize according to the projector property.
    Unfortunately, in perspective mode, the texture is docked in a corner of the projector and doesn't resize. Could you tell me what I am doing wrong ?

    Thank you very much.

    Code (CSharp):
    1.            
    2.             struct vertOut
    3.             {
    4.                 float4 pos : POSITION;
    5.                 float2 uv : TEXCOORD0;
    6.             };
    7.            
    8.             struct fragOut
    9.             {
    10.                 float4 col : COLOR;
    11.             };
    12.            
    13.             vertOut vert(appdata_base IN)
    14.             {
    15.                 vertOut o;
    16.                 o.pos = mul(UNITY_MATRIX_MVP, IN.vertex);
    17.                 o.uv = TRANSFORM_TEX(mul(_Projector, IN.vertex).xy, _Mask);
    18.                 return o;
    19.             }
    20.            
    21.             fragOut frag(vertOut IN)
    22.             {
    23.                 fragOut o;
    24.                 float4 mask = tex2D(_Mask, saturate(IN.uv));
    25.                 o.col = mask;
    26.                 return o;
    27.             }
    Here is an other solution which fixe the position and size in both mode but I can't avoid the repetition. The texture is not clamped.

    Code (CSharp):
    1.  
    2.             struct vertOut
    3.             {
    4.                 float4 pos : POSITION;
    5.                 float4 uv : TEXCOORD0;
    6.             };
    7.          
    8.             struct fragOut
    9.             {
    10.                 float4 col : COLOR;
    11.             };
    12.          
    13.             vertOut vert(appdata_base IN)
    14.             {
    15.                 vertOut o;
    16.                 o.pos = mul(UNITY_MATRIX_MVP, IN.vertex);
    17.                 o.uv = mul(_Projector, IN.vertex);
    18.                 return o;
    19.             }
    20.          
    21.             fragOut frag(vertOut IN)
    22.             {
    23.                 fragOut o;
    24.                 float4 mask = tex2Dproj(_Mask, IN.uv);
    25.                 o.col = mask;
    26.                 return o;
    27.             }
     
    Last edited: Aug 14, 2014
  2. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    I just solved the issue by replacing :

    Code (CSharp):
    1. float4 mask = tex2Dproj(_Mask, IN.uv);
    Code (CSharp):
    1. float4 mask = tex2D(_Mask, saturate(float2(IN.uv.xy) / IN.uv.w));