Search Unity

Looking for help improving/correcting transparency and depth in a 3 pass shader.

Discussion in 'Shaders' started by ssj3_shadow, Aug 30, 2015.

  1. ssj3_shadow

    ssj3_shadow

    Joined:
    Jan 14, 2014
    Posts:
    2
    Code (CSharp):
    1. Shader "Custom/maskedTransparentDiffuse"
    2. {
    3.     Properties
    4.     {
    5.         _MaskColour("Mask Colour", Color) = (1.0, 1.0, 1.0, 1.0)//default is white
    6.         _MaskTexture("Mask Texture", 2D) = "" {}
    7.         _DiffuseTexture("Diffuse Texture", 2D) = "" {}
    8.         _OverlayTexture("Overlay Texture", 2D) = "" {}
    9.      }
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue" = "Transparent"//render after opaque layers
    15.         }
    16.         Pass//first pass: mask layer
    17.         {
    18.             Blend SrcAlpha OneMinusSrcAlpha//alpha blending
    19.             Color[_MaskColour]//set 'primary' colour
    20.             SetTexture[_MaskTexture]
    21.             {
    22.                 Combine texture * primary//apply texture * 'primary' colour
    23.             }
    24.         }
    25.         Pass//second pass: diffuse layer
    26.         {
    27.             Blend SrcAlpha OneMinusSrcAlpha//alpha blending
    28.             SetTexture[_DiffuseTexture]
    29.             {
    30.                 Combine texture//apply texture
    31.             }
    32.         }
    33.         Pass//third pass: overlay layer
    34.         {
    35.             Blend SrcAlpha OneMinusSrcAlpha//alpha blending
    36.             SetTexture[_OverlayTexture]
    37.             {
    38.                 Combine texture//apply texture
    39.             }
    40.         }
    41.     }
    42.     Fallback "Transparent/Diffuse"//if all else fails fallback to this shader
    43. }
    The shader takes 3 alpha transparent capable images and outputs them one after the other to produce the following:



    The mask layer can then be coloured:



    There are two problems I wish to resolve.

    1. If an image is not supplied to the overlay layer the output is darkened and semi transparent. To resolve this I currently supply a fully transparent image to the overlay layer as the layer is not always used, think of it as a decal layer.



    2. When an object using this shader is behind another object using this shader the foreground object doesn't calculate the transparency as I'd like, hiding the background object essentially.



    I know the solution will be cutting the shader over to CGPROGRAM use, I just need a little help doing so.
     
    Last edited: Sep 2, 2015
  2. ssj3_shadow

    ssj3_shadow

    Joined:
    Jan 14, 2014
    Posts:
    2
    Code (CSharp):
    1. ZWrite Off
    Solves issue #2.

    Any help converting the shader to use CGPROGRAM and resolving issue #1
     
    Last edited: Sep 2, 2015