Search Unity

Stop projector shader from blending between pixels

Discussion in 'Shaders' started by ittinop, Apr 5, 2016.

  1. ittinop

    ittinop

    Joined:
    Nov 9, 2015
    Posts:
    15
    Hello,

    I am trying to use this following projector for making an overlay over buildings.
    The input texture has pixels that represent each game tiles.
    The projector shader will shade buildings with color corresponding to the game tile.
    The problem I have is with shader blending between pixels as shown in pictures below.
    At the joint part, I don't want blue and red to blend into purple.
    Is there anyway to fix this?

    Shader code is here:

    Code (CSharp):
    1. Shader "Projector/Overlay Projector"
    2. {
    3.     Properties
    4.     {
    5.        _Color ("Main Color", Color) = (1,1,1,1)
    6.        _ShadowTex ("Projected Image", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.           Pass
    12.           {    
    13.           Blend srcalpha OneMinusSrcAlpha
    14.           ZWrite Off // don't change depths
    15.           Offset -1, -1 // avoid depth fighting
    16.  
    17.           CGPROGRAM
    18.  
    19.           #pragma vertex vert
    20.           #pragma fragment frag
    21.  
    22.           uniform sampler2D _ShadowTex;
    23.  
    24.           // Projector-specific uniforms
    25.           uniform float4x4 _Projector; // transformation matrix
    26.               // from object space to projector space
    27.           struct vertexInput {
    28.              float4 vertex : POSITION;
    29.              float3 normal : NORMAL;
    30.           };
    31.  
    32.           struct vertexOutput {
    33.              float4 pos : SV_POSITION;
    34.              float4 posProj : TEXCOORD0;
    35.                 // position in projector space
    36.           };
    37.  
    38.           vertexOutput vert(vertexInput input)
    39.           {
    40.              vertexOutput output;
    41.  
    42.              output.posProj = mul(_Projector, input.vertex);
    43.              output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    44.  
    45.              return output;
    46.           }
    47.  
    48.  
    49.           float4 frag(vertexOutput input) : COLOR
    50.           {
    51.              if (input.posProj.w > 0.0) // in front of projector?
    52.              {
    53.                 return tex2D(_ShadowTex , input.posProj);//.xy / input.posProj.w);
    54.                 // alternatively: return tex2Dproj(
    55.                 //    _ShadowTex, input.posProj);
    56.              }
    57.              else // behind projector
    58.              {
    59.                 return float4(0.0, 0.0, 0.0, 0.0);
    60.              }
    61.           }
    62.  
    63.           ENDCG
    64.        }
    65.     }
    66.     // Fallback "Projector/Light"
    67. }
    Thank you very much in advance

    upload_2016-4-5_5-18-15.png

    upload_2016-4-5_5-17-48.png
     
  2. Zicandar

    Zicandar

    Joined:
    Feb 10, 2014
    Posts:
    388
    Try changing the filter mode of the texture to point.
    That will disable bilinear filtering, (blending between pixels).
     
  3. ittinop

    ittinop

    Joined:
    Nov 9, 2015
    Posts:
    15
    Yes that works!
    Thank you very much.