Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Inverting silhouette shader

Discussion in 'Shaders' started by AfraidOfMonsters, Sep 4, 2014.

  1. AfraidOfMonsters

    AfraidOfMonsters

    Joined:
    Sep 4, 2014
    Posts:
    3
    I have a silhouette shader that flattens anything behind it. It works kind of like a pane of glass, so anything behind the glass is rendered as just a flat colour.

    I've managed to get as far as this:


    But it's inverted! Here is my shader code:

    Code (CSharp):
    1. Shader "Character Indicator"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _Indicator ("Indicator Color", Color) = (1,1,1,1)
    8.         _Cutoff ("Alpha cutoff", Range (0,1)) = 0.0
    9.     }
    10.  
    11.     SubShader
    12.     {
    13.         Tags { "Queue" = "Geometry+1" "RenderType" = "Opaque" }
    14.         CGPROGRAM
    15.         #pragma surface surf BlinnPhong alphatest:_Cutoff
    16.      
    17.         uniform float4 _Color;
    18.         uniform float4 _Indicator;
    19.         uniform sampler2D _MainTex;
    20.        
    21.         struct Input
    22.         {
    23.             float2 uv_MainTex;
    24.             float3 viewDir;
    25.         };
    26.      
    27.         void surf (Input IN, inout SurfaceOutput o)
    28.         {
    29.             o.Albedo = tex2D ( _MainTex, IN.uv_MainTex).rgb * _Color;
    30.         }
    31.         ENDCG
    32.        
    33.         Pass
    34.         {
    35.             Tags { "LightMode" = "Always" }
    36.             AlphaTest Greater [_Cutoff]
    37.             ZWrite Off
    38.             ZTest Greater
    39.            
    40.        
    41.             SetTexture [_MainTex]
    42.             {
    43.                 constantColor [_Indicator]
    44.                 //combine constant, texture
    45.                 combine constant* texture
    46.             }
    47.         }
    48.     }
    49. }
    Is there a way I can reverse the way this shader is rendering?
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    Move a "2nd" pass above surface shader part and change ztest into less.
     
  3. AfraidOfMonsters

    AfraidOfMonsters

    Joined:
    Sep 4, 2014
    Posts:
    3
    You mean like this? This just renders the plane out flat.

    Code (CSharp):
    1. Shader "Character Indicator"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _Indicator ("Indicator Color", Color) = (1,1,1,1)
    8.         _Cutoff ("Alpha cutoff", Range (0,1)) = 0.0
    9.     }
    10.  
    11.     SubShader
    12.     {
    13.         Pass
    14.         {
    15.             Tags { "LightMode" = "Always" }
    16.             AlphaTest Greater [_Cutoff]
    17.             ZWrite Off
    18.             ZTest Less
    19.            
    20.             SetTexture [_MainTex]
    21.             {
    22.                 constantColor [_Indicator]
    23.                 //combine constant, texture
    24.                 combine constant* texture
    25.             }
    26.         }
    27.  
    28.         Tags { "Queue" = "Geometry+1" "RenderType" = "Opaque" }
    29.         CGPROGRAM
    30.         #pragma surface surf BlinnPhong alphatest:_Cutoff
    31.      
    32.         uniform float4 _Color;
    33.         uniform float4 _Indicator;
    34.         uniform sampler2D _MainTex;
    35.        
    36.         struct Input
    37.         {
    38.             float2 uv_MainTex;
    39.             float3 viewDir;
    40.         };
    41.      
    42.         void surf (Input IN, inout SurfaceOutput o)
    43.         {
    44.             o.Albedo = tex2D ( _MainTex, IN.uv_MainTex).rgb * _Color;
    45.         }
    46.         ENDCG
    47.        
    48.        
    49.     }
    50. }
     
  4. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    Sorry I mistaken. I will try find solution.
     
  5. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    Ok I got it. I split shader into two part and use "UsePass"(lol) directive. But it's strange, as far i remember, last time when I do something similar, I just write unlit part on top of subshader scope.
    Code (csharp):
    1.  
    2. Shader "CharacterIndicatorLight"
    3. {
    4.     SubShader
    5.     {
    6.         Tags { "Queue" = "AlphaTest" "RenderType" = "TransparentCutout" }
    7.                
    8.         CGPROGRAM
    9.         #pragma surface surf BlinnPhong
    10.      
    11.         uniform float4 _Color;
    12.         uniform fixed _Cutoff;
    13.         uniform float4 _Indicator;
    14.         uniform sampler2D _MainTex;
    15.        
    16.         struct Input
    17.         {
    18.             float2 uv_MainTex;
    19.             float3 viewDir;
    20.         };
    21.      
    22.         void surf (Input IN, inout SurfaceOutput o)
    23.         {
    24.             fixed4 c=tex2D ( _MainTex, IN.uv_MainTex);
    25.             o.Albedo = c.rgb * _Color;
    26.             clip(c.a-_Cutoff);
    27.         }
    28.         ENDCG
    29.        
    30.     }
    31. }
    32.  
    Code (csharp):
    1.  
    2. Shader "CharacterIndicator"
    3. {
    4.     Properties
    5.     {
    6.         _Color ("Main Color", Color) = (1,1,1,1)
    7.         _MainTex ("Base (RGB)", 2D) = "white" {}
    8.         _Indicator ("Indicator Color", Color) = (1,1,1,1)
    9.         _Cutoff ("Alpha cutoff", Range (0,1)) = 0.0
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags { "Queue" = "AlphaTest" "RenderType" = "TransparentCutout" }
    15.        
    16.         UsePass "CharacterIndicatorLight/FORWARD"
    17.            
    18.         Pass
    19.         {
    20.             AlphaTest Greater [_Cutoff]
    21.             ZWrite Off
    22.             ZTest Greater
    23.            
    24.        
    25.             SetTexture [_MainTex]
    26.             {
    27.                 constantColor [_Indicator]
    28.                 //combine constant, texture
    29.                 combine constant* texture
    30.             }
    31.         }
    32.     }
    33. }
    34.  
     
  6. AfraidOfMonsters

    AfraidOfMonsters

    Joined:
    Sep 4, 2014
    Posts:
    3
    Hmm - appreciate your help, but I can't seem to get this to work. Getting numerous errors but I think the main issue is that using multiple subshaders won't work on mobile?
     
  7. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    I builded and run it on Android- it's works fine. What exactly errors say?