Search Unity

How to prevent my projector from being clipped?

Discussion in 'Shaders' started by sarah22, Jun 12, 2019.

  1. sarah22

    sarah22

    Joined:
    Aug 6, 2017
    Posts:
    15
    I'm using projector with this shader. (source: https://hyunkell.com/blog/rts-style-unit-selection-in-unity-5/)

    Code (CSharp):
    1. Shader "Projector/AdditiveTint" {
    2. Properties {
    3.     _Color ("Tint Color", Color) = (1,1,1,1)
    4.     _Attenuation ("Falloff", Range(0.0, 1.0)) = 1.0
    5.     _ShadowTex ("Cookie", 2D) = "gray" {}
    6. }
    7. Subshader {
    8.     Tags {"Queue"="Transparent"}
    9.     Pass {
    10.         ZWrite Off
    11.         ColorMask RGB
    12.         Blend SrcAlpha One // Additive blending
    13.         Offset -1, -1
    14.  
    15.         CGPROGRAM
    16.         #pragma vertex vert
    17.         #pragma fragment frag
    18.         #include "UnityCG.cginc"
    19.  
    20.         struct v2f {
    21.             float4 uvShadow : TEXCOORD0;
    22.             float4 pos : SV_POSITION;
    23.         };
    24.  
    25.         float4x4 unity_Projector;
    26.         float4x4 unity_ProjectorClip;
    27.  
    28.         v2f vert (float4 vertex : POSITION)
    29.         {
    30.             v2f o;
    31.             o.pos = UnityObjectToClipPos (vertex);
    32.             o.uvShadow = mul (unity_Projector, vertex);
    33.             return o;
    34.         }
    35.  
    36.         sampler2D _ShadowTex;
    37.         fixed4 _Color;
    38.         float _Attenuation;
    39.  
    40.         fixed4 frag (v2f i) : SV_Target
    41.         {
    42.             // Apply tint & alpha mask
    43.             fixed4 texCookie = tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
    44.             fixed4 outColor = _Color * texCookie.a;
    45.             // Distance attenuation
    46.             float depth = i.uvShadow.z; // [-1(near), 1(far)]
    47.             return outColor * clamp(1.0 - abs(depth) + _Attenuation, 0.0, 1.0);
    48.         }
    49.         ENDCG
    50.     }
    51. }
    It works perfectly unless I try to move far away from it.



    I tried to just return _Color * texCookie.a in the frag function but it still clips the projector. Could anyone shed some light on this? I'm still really new with shaders. If you could also recommend a good book about shaders in unity I would really appreciate it. Thank you.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I suspect you're not doing anything wrong, this is likely the projector not taking into account terrain LOD. Unfortunately there's not much you can do to fix that from the projector or shader side. You could play with your terrain settings to push the LOD distance further away.
     
    sarah22 likes this.
  3. sarah22

    sarah22

    Joined:
    Aug 6, 2017
    Posts:
    15
    Thank you for the reply. I actually solved it by unchecking generate mip maps on the the texture settings. I found the solution here in case someone wants it. https://en.wikibooks.org/wiki/Cg_Programming/Unity/Projectors