Search Unity

Does alpha blending work on image effects? (is _MainTex alpha presevered?)

Discussion in 'Image Effects' started by SoftwareGeezers, Jul 15, 2017.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    I've two cameras rendering my background and foreground separately, and want to composite them with an image effect. This is working except that the alpha value for the foreground provided as 'src' in the OnRenderImage() is always 1, despite setting the camera's clear flags to (0,0,0,0).

    Code (CSharp):
    1.  
    2. static const fixed4 _yellow = fixed4(1, 1, 0, 1);
    3.  
    4. half4 frag_composite(v2f_img i) : SV_Target {
    5.      float4 s;
    6.      s = tex2D(_MainTex, i.uv);
    7.      s = lerp(s, _yellow, s.a);
    8.      return s;
    9. }
    10. ENDCG
    11. Subshader {
    12.      Pass {    // 0
    13.          ZTest Always Cull Off ZWrite Off
    14.          Blend SrcAlpha OneMinusSrcAlpha
    15.          CGPROGRAM
    16.          #pragma vertex vert_img
    17.          #pragma fragment frag_composite
    18.          ENDCG
    19.      }
    20. }
    21.  
    22.  
    23. private void OnRenderImage(RenderTexture src, RenderTexture dest){
    24.      Graphics.Blit(src, dest,shader_compositor);
    25. }
    This just draws _MainTex without drawing yellow in the transparent background. The contents of _MainTex are my spaceship and star field and empty space behind.
     
  2. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Turns out alpha works fine. I was using someone else's code as a starting point and it's outdated. Still compiles and runs, annoyingly!