Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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 at 11:05 AM.

  1. dnoparker

    dnoparker

    Joined:
    Aug 28, 2013
    Posts:
    41
    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:
    41
    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. }