Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Enable/disable multy surface pass

Discussion in 'Shaders' started by Malzar, Mar 10, 2019.

  1. Malzar

    Malzar

    Joined:
    Dec 1, 2014
    Posts:
    1
    Hi

    I'm trying to create a multi-surface pass shader but I got a problem. I don't know how to disable the second pass. I tried to block the second pass with a shader feature but the shader didn't compile. The only way that I found is blocking part of the code with the shader feature, but I want to optimize the draw call and when I don't use the second part use only one batch.

    Any idea? Thanks in advance.

    This is the shader code
    Code (CSharp):
    1. Shader "Malzar/Shield"{
    2.     Properties{
    3.         [Header(Shield)]
    4.         [Toggle(SHIELD_ACTIVE)]
    5.         _ShieldActive("Shield enabled",Float)=0
    6.         _ShieldTex("Shield pattern", 2D) = "white" {}
    7.         _ShieldColor("Shield color", Color) = (1,1,1,1)
    8.         _ShieldDistance("Shield distance", Range(0,0.1)) = 0.05
    9.         _ShieldIntensity("Shield intensity",Range(0,1))=0
    10.         [Header(Material)]
    11.         _Color ("Color", Color) = (1,1,1,1)
    12.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    13.        
    14.         _NormalMap("NormalMap", 2D) = "white" {}
    15.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    16.         _Metallic ("Metallic", Range(0,1)) = 0.0
    17.     }
    18.     SubShader{    
    19.         Tags { "Queue"="Transparent" "RenderType"="Transparent"  }
    20.         LOD 200
    21.         //ZWrite Off
    22.         Blend SrcAlpha OneMinusSrcAlpha
    23.         CGPROGRAM
    24.         #pragma surface surf Standard fullforwardshadows addshadow
    25.         #pragma target 3.0      
    26.  
    27.         sampler2D _MainTex;
    28.         sampler2D _NormalMap;
    29.         struct Input{
    30.             float2 uv_MainTex;
    31.             float2 uv_NormalMap;
    32.         };
    33.  
    34.         half _Glossiness;
    35.         half _Metallic;
    36.         fixed4 _Color;
    37.  
    38.         void surf (Input IN, inout SurfaceOutputStandard o){
    39.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    40.             o.Albedo = c.rgb;
    41.             o.Normal = UnpackNormal(tex2D(_NormalMap,IN.uv_NormalMap));
    42.             o.Metallic = _Metallic;
    43.             o.Smoothness = _Glossiness;
    44.             o.Alpha = 1;
    45.         }
    46.         ENDCG
    47.         CGPROGRAM
    48.         #pragma surface surf Lambert fullforwardshadows vertex:vert alpha:fade
    49.         #pragma target 3.0
    50.         #pragma shader_feature SHIELD_ACTIVE      
    51.         #include "UnityCG.cginc"
    52.  
    53.         sampler2D _MainTex;
    54.         sampler2D _ShieldTex;
    55.         fixed4 _ShieldColor;
    56.         half _ShieldDistance;
    57.         half _ShieldIntensity;
    58.         fixed4 _CenterPivot;
    59.  
    60.         struct appdata{
    61.             float4 vertex : POSITION;
    62.             float2 texcoord : TEXCOORD0;
    63.             float3 normal : NORMAL;
    64.         };
    65.  
    66.         struct Input{      
    67.             float2 uv_MainTex;
    68.             float2 uv_ShieldTex;        
    69.             float3 worldNormal;
    70.             float3 viewDir;
    71.         };
    72.  
    73.  
    74.         void vert (inout appdata v, out Input o){
    75.             UNITY_INITIALIZE_OUTPUT(Input,o);
    76.         #if SHIELD_ACTIVE
    77.             v.vertex = v.vertex+float4(v.normal,1)*_ShieldDistance;
    78.             o.worldNormal = v.normal;
    79.         #endif
    80.         }
    81.  
    82.         void surf (Input IN, inout SurfaceOutput  o){
    83.         #if SHIELD_ACTIVE
    84.             fixed4 shieldT= tex2D(_ShieldTex,IN.uv_ShieldTex);          
    85.             float fressnel = dot(IN.worldNormal,IN.viewDir);
    86.             fressnel = saturate(1-fressnel);
    87.             fressnel = pow(fressnel,1-_ShieldIntensity);
    88.  
    89.             o.Albedo = _ShieldColor.rgb;
    90.             o.Emission=_ShieldColor * fressnel* shieldT.r ;
    91.             o.Alpha =fressnel * shieldT.r *_ShieldColor.a ;
    92.         #endif
    93.         }
    94.         ENDCG
    95.     }
    96.     FallBack "Diffuse"
    97. }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,073
    Use two shaders. Either separate the two passes into separate shaders and assign an extra material to your object to enable the effect, or make a version of the shader with the second pass and one without and switch the shader / material used to enabled & disable the effect.
     
    Malzar likes this.
unityunity