Search Unity

[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:
    63
    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:
    63
    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. }