Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Transparent shader color

Discussion in 'Shaders' started by Volkerku, May 22, 2020.

  1. Volkerku

    Volkerku

    Joined:
    Nov 23, 2016
    Posts:
    113
    Hi
    I got this shader (forgot where from) and I try to change the rim color to black. If I do thsi with the shader controls, the figure becomes 100% transparent. I want to keep the rim visible, as in teh img attached, but in black. Any ideas?

    shader.JPG

    https://pasteboard.co/J9sHYsD.jpg

    Code (CSharp):
    1.  
    2. Shader "Custom/Model_Transparent" {
    3.      Properties
    4.      {
    5.        _InnerColor ("Inner Color", Color) = (0.0, 0.0, 0.0, 1.0)
    6.        _RimColor ("Rim Color", Color) = (1.0,1.0,1.0,1.0)
    7.        _RimPower ("Rim Power", Range(0.5,8.0)) = 2.47
    8.      }
    9.      SubShader
    10.      {
    11.        Tags { "Queue" = "Transparent" }
    12.  
    13.        Cull Back
    14.        Blend One One
    15.  
    16.        CGPROGRAM
    17.        #pragma surface surf Lambert
    18.  
    19.        struct Input
    20.        {
    21.            float3 viewDir;
    22.        };
    23.  
    24.        float4 _InnerColor;
    25.        float4 _RimColor;
    26.        float _RimPower;
    27.  
    28.        void surf (Input IN, inout SurfaceOutput o)
    29.        {
    30.            o.Albedo = _InnerColor.rgb;
    31.            half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
    32.            o.Emission = _RimColor.rgb * pow (rim, _RimPower);
    33.        }
    34.        ENDCG
    35.      }
    36.      Fallback "Diffuse"
    37.    }
    38.  
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Right now the shader is blending using color value, even though Blend mode is One One, for some reason it behaves this way in transparent queue with lambert setting.

    Here is the shader updated to use proper blend mode and output an alpha value to blend with instead of being based on color, so black will work. And also allow the inner and outer colors to play nicely.


    Code (CSharp):
    1. Shader "Custom/Model_Transparent" {
    2.      Properties
    3.      {
    4.         _InnerColor ("Inner Color", Color) = (0.0, 0.0, 0.0, 1.0)
    5.         _RimColor ("Rim Color", Color) = (1.0,1.0,1.0,1.0)
    6.         _RimPower ("Rim Power", Range(0.5,8.0)) = 2.47
    7.      }
    8.      SubShader
    9.      {
    10.        Tags { "Queue" = "Transparent" }
    11.        Cull Back
    12.        //Blend based on output Alpha, not color value
    13.        Blend One OneMinusSrcAlpha
    14.        CGPROGRAM
    15.        #pragma surface surf Lambert alpha:fade
    16.        struct Input
    17.        {
    18.            float3 viewDir;
    19.        };
    20.        float4 _InnerColor;
    21.        float4 _RimColor;
    22.        float _RimPower;
    23.        void surf (Input IN, inout SurfaceOutput o)
    24.        {
    25.            half rim = 1.0 - saturate( dot(normalize(IN.viewDir), o.Normal));
    26.            rim = pow(rim, _RimPower);
    27.            //Shift inner color to rim color by rim value adjusted by _InnerColor transparency to reduce its effect
    28.            //on semi-transparent part of rim color the more transparent inner color becomes.
    29.            o.Emission = lerp(_InnerColor.rgb, _RimColor.rgb, saturate(rim + (1 - _InnerColor.a) - (1 - _RimColor.a)));
    30.            o.Alpha = lerp(_InnerColor.a, _RimColor.a, rim);
    31.        }
    32.        ENDCG
    33.      }
    34.      Fallback "Diffuse"
    35.    }
     
  3. Volkerku

    Volkerku

    Joined:
    Nov 23, 2016
    Posts:
    113
    Thanks so much. I’ll try that first thing in the morning.
    Currently the shader is pretty light, meaning it plays nicely on mobile. Do you think this will still be the case?
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Yes it's still light, though you could make it lighter by making it an unlit vert/frag shader instead of a surface shader if you don't need lighting to affect the surface shading at all.