Search Unity

Double Sided Standard Specular Shader

Discussion in 'Shaders' started by Harrison_Hough, Feb 16, 2017.

  1. Harrison_Hough

    Harrison_Hough

    Joined:
    Dec 3, 2015
    Posts:
    43
    Hey Guys,

    I have grabbed the Unity shaders of the website and am trying to edit the Standard Specular shader to be double sided with correct lighting calculations for the back side. Has anybody had any luck or done a similar thing ?

    I am aware that setting Cull Off enables back face shading but it doesn't have the correct lighting calculations.

    In theory i think I just have to apply the reverse normals (on the back face) and apply the lighting to the back faces but unsure how/where to do it as the Standard specular shader is so big with multiple passes.

    Anyways any help would be great!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,327
    There's an asset on the store that does this, to implement properly requires creating very minorly modified versions of several of the standard shader files. Alternatively you can make a standard surface shader that does this much more easily, though it won't have all of the features of the default standard shader. I've posted some examples elsewhere on the forum, search for VFACE.
     
  3. Harrison_Hough

    Harrison_Hough

    Joined:
    Dec 3, 2015
    Posts:
    43
    Okay so I have come up with this and it seems to work correctly. Just wondering if this is a bad way to do it?

    Code (CSharp):
    1. Shader "Custom/DoubleSided" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _BumpMap("NormalMap", 2D) = "Bump" {}
    8.         _BumpScale("Normal Power", Float) = 1.0
    9.         _NormalDefault("Normal Default", Color) = (128, 128, 255)
    10.     }
    11.     SubShader {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 200
    14.  
    15.             // Render back faces first
    16.             Cull Front
    17.             CGPROGRAM
    18.             // Physically based Standard lighting model, and enable shadows on all light types
    19.         #pragma surface surf Standard fullforwardshadows
    20.  
    21.             // Use shader model 3.0 target, to get nicer looking lighting
    22.         #pragma target 3.0
    23.  
    24.             sampler2D _MainTex;
    25.         sampler2D _BumpMap;
    26.  
    27.         struct Input {
    28.             float2 uv_MainTex;
    29.         };
    30.  
    31.         half _Glossiness;
    32.         half _BumpScale;
    33.         half _Metallic;
    34.         fixed4 _Color;
    35.         half3 _NormalDefault;
    36.  
    37.         void surf(Input IN, inout SurfaceOutputStandard o) {
    38.             // Albedo comes from a texture tinted by color
    39.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    40.             o.Albedo = c.rgb;
    41.             // Metallic and smoothness come from slider variables
    42.             o.Metallic = _Metallic;
    43.             o.Smoothness = _Glossiness;
    44.             o.Normal = -UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    45.             //o.Normal = -_NormalDefault;
    46.             o.Alpha = c.a;
    47.         }
    48.         ENDCG
    49.        
    50.  
    51.             // Now render front faces
    52.             Cull Back
    53.         CGPROGRAM
    54.         // Physically based Standard lighting model, and enable shadows on all light types
    55.         #pragma surface surf Standard fullforwardshadows
    56.  
    57.         // Use shader model 3.0 target, to get nicer looking lighting
    58.         #pragma target 3.0
    59.  
    60.         sampler2D _MainTex;
    61.         sampler2D _BumpMap;
    62.  
    63.         struct Input {
    64.             float2 uv_MainTex;
    65.         };
    66.  
    67.         half _Glossiness;
    68.         half _BumpScale;
    69.         half _Metallic;
    70.         fixed4 _Color;
    71.         half3 _NormalDefault;
    72.  
    73.         void surf (Input IN, inout SurfaceOutputStandard o) {
    74.             // Albedo comes from a texture tinted by color
    75.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    76.             o.Albedo = c.rgb;
    77.             // Metallic and smoothness come from slider variables
    78.             o.Metallic = _Metallic;
    79.             o.Smoothness = _Glossiness;
    80.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
    81.             //o.Normal = _NormalDefault;
    82.             o.Alpha = c.a;
    83.         }
    84.         ENDCG
    85.     }
    86.     FallBack "Diffuse"
    87. }
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,327
    Last edited: Feb 17, 2017
  5. Harrison_Hough

    Harrison_Hough

    Joined:
    Dec 3, 2015
    Posts:
    43
    Oh thanks I think the shader you gave (Standard Two Sided Soft Blend) from that was exactly what I am after, it's also for a hair shader.

    VFACE for the win!


    Thanks again!