Search Unity

Unity 5 Standard Cutaway

Discussion in 'Shaders' started by Redtail87, Apr 7, 2015.

  1. Redtail87

    Redtail87

    Joined:
    Jul 17, 2013
    Posts:
    125
    So I have a cutaway shader that is based on the legacy shaders and would like to port it over to the standard shader to get that delicious pbr, but looking at the source for the standard it is way different from what I am used to, seems they are doing a lot of fancy stuff that is not editable directly in the .shader file? What would be the best way to accomplish this?

    Here is the source I am trying to port:

    Code (CSharp):
    1. Shader "Cutaway/Diffuse"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _CutColor ("Cut Color", color) = (1,0,0,1)
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags { "RenderType" = "Opaque" }
    12.         Cull Off
    13.  
    14.         CGPROGRAM
    15.             #pragma surface surf Lambert
    16.  
    17.             struct Input
    18.             {
    19.                 float2 uv_MainTex;
    20.                 float3 worldPos;
    21.                 float3 worldNormal;
    22.                 float3 viewDir;
    23.             };
    24.  
    25.             sampler2D _MainTex;
    26.             float _d;
    27.             float enableClip = 1;
    28.             float4 clippingVector, _CutColor;
    29.                    
    30.             void surf (Input IN, inout SurfaceOutput o)
    31.             {
    32.                 if(enableClip == 1)
    33.                 {
    34.                     float dist = clippingVector.x * IN.worldPos.x +
    35.                                  clippingVector.y * IN.worldPos.y +
    36.                                  clippingVector.z * IN.worldPos.z + clippingVector.w;
    37.                
    38.                     clip(-dist);
    39.                     float fd = dot( IN.viewDir, IN.worldNormal);
    40.                     if (fd.x > 0)
    41.                     {
    42.                         fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    43.                         o.Albedo = c.rgb;
    44.                         o.Alpha = c.a;
    45.                         return;
    46.                     }
    47.                
    48.                     o.Emission = _CutColor;
    49.                 }else{
    50.                     fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    51.                     o.Albedo = c.rgb;
    52.                     o.Alpha = c.a;
    53.                 }
    54.             }
    55.            
    56.         ENDCG
    57.     }
    58.     Fallback "Diffuse"
    59. }
    Thanks
     
  2. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    322
    Hey Mike, here's a version with the standard lighting. In testing I wasn't super sure if I'd broken anything, because I have no idea of the use case or standard parameters are for this shader.

    Code (CSharp):
    1. Shader "Cutaway/Diffuse"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _CutColor ("Cut Color", color) = (1,0,0,1)
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType" = "Opaque" }
    13.         Cull Off
    14.         CGPROGRAM
    15.             // Physically based Standard lighting model, and enable shadows on all light types
    16.             #pragma surface surf Standard fullforwardshadows
    17.             struct Input
    18.             {
    19.                 float2 uv_MainTex;
    20.                 float3 worldPos;
    21.                 float3 worldNormal;
    22.                 float3 viewDir;
    23.             };
    24.             sampler2D _MainTex;
    25.             float _d;
    26.             float enableClip = 1;
    27.             float4 clippingVector, _CutColor;
    28.             half _Glossiness;
    29.             half _Metallic;
    30.                  
    31.             void surf (Input IN, inout SurfaceOutputStandard o)
    32.             {
    33.                 if(enableClip == 1)
    34.                 {
    35.                     float dist = clippingVector.x * IN.worldPos.x +
    36.                                  clippingVector.y * IN.worldPos.y +
    37.                                  clippingVector.z * IN.worldPos.z + clippingVector.w;
    38.              
    39.                     clip(-dist);
    40.                     float fd = dot( IN.viewDir, IN.worldNormal);
    41.                     if (fd.x > 0)
    42.                     {
    43.                         fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    44.                         o.Albedo = c.rgb * _CutColor;
    45.                         o.Alpha = c.a;
    46.                         return;
    47.                     }
    48.              
    49.                 }else{
    50.                     fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    51.                     o.Albedo = c.rgb;
    52.                     o.Alpha = c.a;
    53.                 }
    54.  
    55.                 o.Metallic = _Metallic;
    56.                 o.Smoothness = _Glossiness;
    57.             }
    58.          
    59.         ENDCG
    60.     }
    61.     Fallback "Diffuse"
    62. }
    As a side note, it would be fairly trivial to remove the if(){}else{} statements in this. Have you considered doing that? It would probably boost performance.
     
  3. Redtail87

    Redtail87

    Joined:
    Jul 17, 2013
    Posts:
    125
    Awesome thanks!

    This seems to almost work, only thing is that the _CutColor seems to need to be applied as a solid color after the return, with how it is currently it colors the visible part of the model and not the inside cut, how I did this before is using the Emission, but I can not seem to get the emission to have any effect, any ideas? It would be great to speed up the performance if possible, how would you remove the conditionals while keeping the functionality?

    Here is what I changed it to :

    Code (CSharp):
    1. Shader "Cutaway/Diffuse"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _CutColor ("Cut Color", color) = (1,0,0,1)
    7.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    8.         _Metallic ("Metallic", Range(0,1)) = 0.0
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType" = "Opaque" }
    13.         Cull Off
    14.         CGPROGRAM
    15.             // Physically based Standard lighting model, and enable shadows on all light types
    16.             #pragma surface surf Standard fullforwardshadows
    17.  
    18.             struct Input
    19.             {
    20.                 float2 uv_MainTex;
    21.                 float3 worldPos;
    22.                 float3 worldNormal;
    23.                 float3 viewDir;
    24.             };
    25.             sampler2D _MainTex;
    26.             float _d;
    27.             float enableClip = 1;
    28.             float4 clippingVector, _CutColor;
    29.             half _Glossiness;
    30.             half _Metallic;
    31.                
    32.             void surf (Input IN, inout SurfaceOutputStandard o)
    33.             {
    34.                 if(enableClip == 1)
    35.                 {
    36.                     float dist = clippingVector.x * IN.worldPos.x +
    37.                                  clippingVector.y * IN.worldPos.y +
    38.                                  clippingVector.z * IN.worldPos.z + clippingVector.w;
    39.            
    40.                     clip(-dist);
    41.                     float fd = dot( IN.viewDir, IN.worldNormal);
    42.                     if (fd.x > 0)
    43.                     {
    44.                         fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    45.                         o.Albedo = c.rgb;
    46.                         o.Alpha = c.a;
    47.                         return;
    48.                     }
    49.  
    50.                     o.Albedo = _CutColor;
    51.                     o.Emission = _CutColor;
    52.  
    53.                 }else{
    54.                     fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    55.                     o.Albedo = c.rgb;
    56.                     o.Alpha = c.a;
    57.                 }
    58.                 o.Metallic = _Metallic;
    59.                 o.Smoothness = _Glossiness;
    60.             }
    61.        
    62.         ENDCG
    63.     }
    64.     Fallback "Diffuse"
    65. }
    Thanks again!