Search Unity

Normal Opaque projector shader?

Discussion in 'Shaders' started by tomekkie2, Jul 23, 2014.

  1. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    How to make a normal 100% opaque projector shader? with alpha channel or mask?
     
  2. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    Something like this?
     
  3. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    Thanks. Interesting, but I am just looking for a simple and free
    nomal opaque projector shader
    but haven't found in the free decal system and can't find a sign of its existence in the paid decal system.
     
  4. Quixotic7

    Quixotic7

    Joined:
    Jul 7, 2013
    Posts:
    10
    Here's some code to do a solid projector shader. I found a nice additive shader here http://hyunkell.com/blog/rts-style-unit-selection-in-unity-5/ and changed the Blend to "Blend SrcColor OneMinusSrcAlpha" to get it to blend as a solid opaque overlay.

    Code (CSharp):
    1. Shader "Projector/Solid Projector" {
    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 SrcColor OneMinusSrcAlpha
    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 _Projector;
    26.             float4x4 _ProjectorClip;
    27.          
    28.             v2f vert (float4 vertex : POSITION)
    29.             {
    30.                 v2f o;
    31.                 o.pos = mul (UNITY_MATRIX_MVP, vertex);
    32.                 o.uvShadow = mul (_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 alpha mask
    43.                 fixed4 texCookie = tex2Dproj (_ShadowTex, UNITY_PROJ_COORD(i.uvShadow));
    44.                 fixed4 outColor = _Color * texCookie.a;
    45.                 // 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.     }
    52. }
     
  5. Zivisx

    Zivisx

    Joined:
    Nov 15, 2014
    Posts:
    3
    I would like something like this as well. The provided code by quixotic7 does not work for me. Unity is currently 5.6. Does anybody know of a way to get a shader to be a completely opaque projector shader for a decal system? I have scoured the internet I feel, but seem to find a great deal of outdated shaders that do not seem to work. I have tried to patchwork my own opaque shader many times but I am not very familiar with writing shaders. Any help on this ancient post would be appreciated.
     
  6. jobo22

    jobo22

    Joined:
    Dec 1, 2016
    Posts:
    83
    I'm using Unity 2019.2 and the above code works perfectly for me. Thank you very much for it Quixotic7!