Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

[SHADER] I want to have a shader that makes objects behind it semi-transparent.

Discussion in 'Scripting' started by dnoparker, Aug 10, 2018.

  1. dnoparker

    dnoparker

    Joined:
    Aug 28, 2013
    Posts:
    52
    I have been using this shader for a while now. It stops objects from underneath from rendering. (So I can use it as a mask. It's handy for AR development.)

    Code (CSharp):
    1. Shader "Custom/InvisibleMask"
    2. {
    3.     SubShader
    4.     {
    5.         // draw after all opaque objects (queue = 2001):
    6.         Tags{ "Queue" = "Geometry+1" }
    7.         Pass{
    8.         Blend zero one  // keep the image behind it
    9.     }
    10.     }
    11. }
    I want to modify this shader so that the objects behind become semi-transparent rather than invisible.

    Is that at all possible? I know nothing about shader code, so I can't do it myself.
     
  2. dnoparker

    dnoparker

    Joined:
    Aug 28, 2013
    Posts:
    52
    I have figured it out!


    Code (CSharp):
    1. Shader "FX/VertexLit ShowThrough" {
    2.     Properties{
    3.         _Color("Main Color", Color) = (1,1,1,1)
    4.         _MainTex("Base (RGB)", 2D) = "white" {}
    5.     _OccludeColor("Occlusion Color", Color) = (0,0,1,1)
    6.     }
    7.         SubShader{
    8.         Tags{ "Queue" = "Geometry+5" "RenderType" = "Transparent" }
    9.         // occluded pass
    10.         Pass{
    11.         ZWrite Off
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.         ZTest Greater
    14.         Color[_OccludeColor]
    15.     }
    16.         // Vertex lights
    17.         Pass{
    18.         Tags{ "LightMode" = "Vertex" }
    19.         ZWrite On
    20.         Lighting On
    21.         SeparateSpecular On
    22.         Material{
    23.         Diffuse[_Color]
    24.         Ambient[_Color]
    25.         // Emission [_PPLAmbient]
    26.     }
    27.         SetTexture[_MainTex]{
    28.         ConstantColor[_Color]
    29.         Combine texture * primary DOUBLE, texture * constant
    30.     }
    31.     }
    32.     }
    33.         FallBack "Diffuse", 1
    34. }