Search Unity

How to edit shader to make back face color adjustable?

Discussion in 'Shaders' started by mhogle, Sep 22, 2017.

  1. mhogle

    mhogle

    Joined:
    Jan 3, 2016
    Posts:
    20
    Hello everyone,

    I've been using Unity for a while, but I don't know much about writing Shaders. Recently, I've encountered an issue where I'm adjusting the front face color to go from opaque to transparent and the back face is not being adjusted at the same time.

    How can I make it work for that if I change the Alpha value via a slider then both front face and back face color would change?

    Attached image is the issue and here is my shader codes:
    Code (CSharp):
    1. Shader "ShaderEnamelBackface" {
    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.     }
    8.  
    9.         SubShader{
    10.         Tags { "RenderType"="Opaque" }
    11.  
    12.         Pass{
    13.             ColorMask 0
    14.             Offset 1,1
    15.         }
    16.  
    17.         Pass{
    18.             Offset 1,1
    19.             ZWrite Off
    20.             Cull Front
    21.             Material {
    22.                 Diffuse [_Color]
    23.                 Ambient [_Color]
    24.             }  
    25.             Color[_Color]
    26.         }
    27.  
    28.         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    29.         ZWrite Off
    30.         Lighting On
    31.         LOD 200
    32.  
    33.         CGPROGRAM
    34. #pragma surface surf Lambert alpha:auto
    35.  
    36.         half4 LightingWrapLambert(SurfaceOutput s, half3 lightDir, half atten) {
    37.         half NdotL = dot(s.Normal, lightDir);
    38.         half diff = NdotL * 0.5 + .2;
    39.         half4 c;
    40.         c.rgb = s.Albedo * _LightColor0.rgb * (diff * atten);
    41.         c.a = s.Alpha;
    42.         return c;
    43.     }
    44.  
    45.     struct Input
    46.     {
    47.         float2 uv_MainTex;
    48.         float3 worldPos;
    49.         float3 viewDir;
    50.         float3 worldNormal;
    51.     };
    52.  
    53.     sampler2D _MainTex;
    54.     float4 _section;
    55.     fixed4 _Color;
    56.  
    57.     void surf(Input IN, inout SurfaceOutput o) {
    58.         float toClip = _section.x * 0.1 * IN.worldPos.x +
    59.             _section.y * 0.1 * IN.worldPos.y +
    60.             _section.z * 0.1 * IN.worldPos.z +
    61.             _section.w;
    62.  
    63.         clip(toClip);
    64.  
    65.         float fd = dot(IN.viewDir, IN.worldNormal);
    66.  
    67.         if (fd.x > 0)
    68.         {
    69.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    70.             o.Albedo = c.rgb;
    71.             o.Alpha = c.a;
    72.         }
    73.  
    74.     }
    75.  
    76.     ENDCG
    77.  
    78.  
    79.     }
    80.  
    81.         Fallback "Diffuse"
    82. }
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Don't use the dot product. It's a popular technique for this, but wrong.

    Search for VFACE.
     
  3. mhogle

    mhogle

    Joined:
    Jan 3, 2016
    Posts:
    20
    Can you please tell me more details about this and how it can be done? Sorry, but I know very little about these shader things. Thanks
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
  5. mhogle

    mhogle

    Joined:
    Jan 3, 2016
    Posts:
    20
    Hello there, thanks for your help, so I think I achieve the effect that I want now. When I adjust Transparency, both backface and frontface are adjusted. Here are my codes:
    Code (CSharp):
    1. Shader "ShaderEnamel"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Albedo Texture", 2D) = "white" {}
    6.         _TintColor("Tint Color", Color) = (1,1,1,1)
    7.         _Transparency("Transparency", Range(0.0,1)) = 0.25
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags {"Queue"="Transparent" "RenderType"="Transparent" }
    13.         LOD 100
    14.  
    15.         ZWrite Off
    16.         Blend SrcAlpha OneMinusSrcAlpha
    17.         Cull Off
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 float4 vertex : SV_POSITION;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.                float _Transparency;
    40.                float4 _MainTex_ST;
    41.                float4 _TintColor;
    42.            
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.                 //v.vertex.x += sin(_Time.y * _Speed + v.vertex.y * _Amplitude) * _Distance * _Amount;
    47.                 o.vertex = UnityObjectToClipPos(v.vertex);
    48.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    49.                 return o;
    50.             }
    51.            
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {
    54.                 // sample the texture
    55.                 fixed4 col = tex2D(_MainTex, i.uv);
    56.                 col.a = _Transparency;
    57.                 return col;
    58.             }
    59.             ENDCG
    60.         }
    61.     }
    62. }
    However, right now the color is just white as I don't apply any texture on it. My question is, can I have a texture which has 2 different colors for front face and back face as back face is usually darker because it's inside? Thanks