Search Unity

Can't get Image Effect Transparency to work

Discussion in 'Shaders' started by joey-squla, Aug 30, 2016.

  1. joey-squla

    joey-squla

    Joined:
    May 30, 2015
    Posts:
    16
    I'm trying to write a image effect to transition one camera into the other but I'm stuck on this last problem for a while now and tried googling for solutions and reading up about Blending and Colormasks but so far came up dry. I hope that someone here can help me on my way.

    The problem is that instead of Camera1 transitioning to transparent to show Camera2 underneath it, it transitions to black. My guess is that I'm doing something wrong with either the stencil buffer not setting pixels to transparent but to black or overlooking something in the shader setting alpha channel. If I applied the shader to a UI/Image the transparency did work though.

    The setup is as followed:

    Camera1: Depth Only, Ortho, Depth: 1

    Camera 2: Depth Only, Ortho, Depth: 0

    This is the code on Camera1 for Blitting:

    Code (CSharp):
    1. void OnRenderImage (RenderTexture src, RenderTexture dst)
    2.          {
    3.              if (TransitionMaterial != null) {
    4.                  RenderTexture rnd = RenderTexture.GetTemporary (src.width, src.height, 0, RenderTextureFormat.ARGB32);
    5.                  Graphics.Blit (rnd, dst, TransitionMaterial);
    6.                  //RenderTexture.ReleaseTemporary (rnd);
    7.              }
    8.            
    9.          }
    And I'm using this shader to my 1st Camera:

    Code (CSharp):
    1. Shader "Custom/Masking"
    2. {
    3.      Properties
    4.      {
    5.          _MainTex ("Main Texture", 2D) = "white" {}
    6.          _MaskTex ("Mask Texture", 2D) = "white" {}
    7.          _AmountSlider ("Mask Amount", Range (0,1)) = 0.4
    8.          _Color ("Tint", Color) = (1,1,1,1)
    9.          _Invert ("Invert", Float) = 0
    10.  
    11.          _ColorMask ("Color Mask", Float) = 15
    12.      }
    13.  
    14.      SubShader
    15.      {
    16.          Tags
    17.          {
    18.              "Queue"="Transparent"
    19.              "IgnoreProjector"="True"
    20.              "RenderType"="Transparent"
    21.              "PreviewType"="Plane"
    22.              "CanUseSpriteAtlas"="True"
    23.          }
    24.      
    25.          Stencil
    26.          {
    27.              Ref [_Stencil]
    28.              Comp [_StencilComp]
    29.              Pass [_StencilOp]
    30.              ReadMask [_StencilReadMask]
    31.              WriteMask [_StencilWriteMask]
    32.          }
    33.          ZWrite Off
    34.          Cull Off
    35.          Lighting Off
    36.          ZTest Less
    37.          Blend SrcAlpha OneMinusSrcAlpha
    38.          ColorMask [_ColorMask]
    39.          
    40.          Pass
    41.          {
    42.          CGPROGRAM
    43.              #pragma vertex vert
    44.              #pragma fragment frag
    45.  
    46.              #include "UnityCG.cginc"
    47.              #include "UnityUI.cginc"
    48.          
    49.              struct appdata_t
    50.              {
    51.                  float4 vertex   : POSITION;
    52.                  float4 color    : COLOR;
    53.                  float2 texcoord : TEXCOORD0;
    54.              };
    55.  
    56.              struct v2f
    57.              {
    58.                  float4 vertex   : SV_POSITION;
    59.                  fixed4 color    : COLOR;
    60.                  half2 texcoord  : TEXCOORD0;
    61.                  float4 worldPosition : TEXCOORD1;
    62.                  half2 screenpos : TEXCOORD2;
    63.              };
    64.          
    65.              fixed4 _Color;
    66.              fixed4 _TextureSampleAdd;
    67.  
    68.              bool _UseClipRect;
    69.  
    70.              v2f vert(appdata_t IN)
    71.              {
    72.                  v2f OUT;
    73.                  OUT.worldPosition = IN.vertex;
    74.                  OUT.vertex = mul(UNITY_MATRIX_MVP, OUT.worldPosition);
    75.  
    76.                  OUT.texcoord = IN.texcoord;
    77.                   OUT.screenpos = ComputeScreenPos(OUT.vertex);
    78.                  #ifdef UNITY_HALF_TEXEL_OFFSET
    79.                  OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
    80.                  #endif
    81.              
    82.                  OUT.color = IN.color * _Color;
    83.                  return OUT;
    84.              }
    85.  
    86.              sampler2D _MainTex;
    87.              sampler2D _MaskTex;
    88.                float _AmountSlider;
    89.                float _Invert;
    90.              fixed4 frag(v2f IN) : SV_Target
    91.              {
    92.                  float4 color = tex2D (_MainTex, IN.texcoord );
    93.                  float alpha =  (abs(_Invert-(tex2D(_MaskTex, IN.screenpos).r)) - _AmountSlider) + 1;
    94.                  float4 alpha_color = float4(color.r, color.g, color.b, smoothstep(alpha, 1, 0) * color.a);
    95.                  return alpha_color;
    96.              }
    97.          ENDCG
    98.          }
    99.      }
    100. }
     
  2. joey-squla

    joey-squla

    Joined:
    May 30, 2015
    Posts:
    16
    Should I instead use a stencil buffer or render textures to do this?
     
    Last edited: Aug 31, 2016
  3. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    498
    I think it will be easier to use a render texture
     
    joey-squla likes this.
  4. joey-squla

    joey-squla

    Joined:
    May 30, 2015
    Posts:
    16
    Not exactly like I hoped it would work, but! it does the trick.
    Thanks!