Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Transparent shaders > rendertexture with alpha

Discussion in 'General Graphics' started by jeroenvdv, May 16, 2017.

  1. jeroenvdv

    jeroenvdv

    Joined:
    Oct 25, 2010
    Posts:
    52
    Hi experts!

    I would like to compose a scene with several planes, having a PNG texture with transparency (not just cutout, true alpha).

    This scene has a camera rendering to a Rendertexture containing alpha.

    In another scene, I would like to be able to put a plane with that rendertexture, in front other planes (eventually also containing alpha, etcetera). This should render to a rendertexture with correct alpha again.

    Graphical explanation:





    What shaders would I need to be able to do this? My current transparent shader doesn't work with every setting I tried on the camera for clearing the background.

    My shader:

    Code (JavaScript):
    1. Shader "Mode/Alpha with Mask" {
    2.     Properties {
    3.       _MainTex ("Texture", 2D) = "white" {}
    4.       _BumpMap ("Texture", 2D) = "white" {}
    5.       _Color ("Main Color", Color) = (1.0, 1.0, 1.0, 1.0)
    6.       _AddColor ("Add Color", Color) = (0.0, 0.0, 0.0, 1.0)
    7.       _Mode("Mode", float) = 0.0
    8.     }
    9.     SubShader {
    10.       Tags { "RenderType" = "transparent" "Queue"="transparent"}
    11.       ZWrite Off
    12.       //Lighting Off
    13.       CGPROGRAM
    14.       #pragma target 3.0
    15.       #pragma surface surf Unlit noambient alpha
    16.       struct Input {
    17.         float2 uv_MainTex;
    18.         float2 uv_BumpMap;
    19.         fixed4 _Color;
    20.         fixed4 _AddColor;
    21.         float _Mode;
    22.       };
    23.       sampler2D _MainTex;
    24.       sampler2D _BumpMap;
    25.       fixed4 _Color;
    26.       fixed4 _AddColor;
    27.       float _Mode;
    28.         inline half4 LightingUnlit (SurfaceOutput s, half3 lightDir, half atten) {
    29.             float4 c;
    30.             c.rgb = s.Albedo;
    31.             c.a = s.Alpha;
    32.             return c;
    33.         }
    34.  
    35.         inline half4 LightingUnlit_PrePass (SurfaceOutput s, half4 light) {
    36.             float4 c;
    37.             c.rgb = s.Albedo;
    38.             c.a = s.Alpha;
    39.             return c;
    40.         }
    41.      
    42.      void surf (Input IN, inout SurfaceOutput o) {
    43.           fixed4 tex = tex2D (_MainTex, IN.uv_MainTex);
    44.           float4 c = tex2D(_BumpMap, IN.uv_BumpMap);
    45.         float3 output = tex.rgb * _Color.rgb;
    46.         float grey = (c.r + c.g + c.b) / 3.0;
    47.         float alpha = tex.a * _Color.a * grey * c.a;
    48.         o.Alpha = alpha;
    49.         o.Emission = output.rgb + _AddColor.rgb;
    50.       }
    51.      
    52.      
    53.       ENDCG
    54.     }
    55.     Fallback "Unlit/Texture"
    56.   }
     
  2. npatch

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    227
    Not entirely sure since I don't have much experience with surf shaders(they end up as vert/frag anyway) but I don't see you indicating Blend mode.
     
  3. jeroenvdv

    jeroenvdv

    Joined:
    Oct 25, 2010
    Posts:
    52
    Thanks for your reply, I'll dive into the world of 'blend modes' to see if that's something I've missed! :)