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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

How to achieve Render texture with correct alpha channel

Discussion in 'Shaders' started by DenisM, Aug 5, 2014.

  1. DenisM

    DenisM

    Joined:
    Dec 6, 2013
    Posts:
    62
    What im trying to do:

    1) Created second camera
    2) Created render texture
    3) Set render texture to second camera
    4) Created plane
    5) Create material with render texture from second camera
    6) Set material on plane
    7) Set the layer mask on camera to render only some effects

    So i want to achieve transparent texture only with some effects on it.
    But i cant. Alpha channel of the render rexture always 0.

    How to achieve correct alpha values in render texture?
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    You need to make sure of three things:
    1. Your second camera needs to use a clear colour with the alpha value you want in empty spaces.
    2. The objects rendered by your second camera need to use shaders that write appropriate alpha values
    3. The material for your plane needs to support blending so that the alpha values from the render texture are used appropriately as transparency.
     
    altair2020 and MaximilianPs like this.
  3. DenisM

    DenisM

    Joined:
    Dec 6, 2013
    Posts:
    62
    1. done
    2. I cant make that shader. Seems im mising smth. Can you give me simple example?
    3. done

    What i need to modify in that shader so it will write to alpha?

    Code (CSharp):
    1. Shader "Mobile/Particles/Alpha Blended1" {
    2. Properties {
    3.     _MainTex ("Particle Texture", 2D) = "white" {}
    4. }
    5.  
    6. Category {
    7.     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    8.     Blend SrcAlpha OneMinusSrcAlpha
    9.     Cull Off Lighting Off ZWrite Off Fog { Color (0,0,0,0) }
    10.    
    11.    
    12.     BindChannels {
    13.         Bind "Color", color
    14.         Bind "Vertex", vertex
    15.         Bind "TexCoord", texcoord
    16.     }
    17.    
    18.     SubShader {
    19.         Pass {
    20.             SetTexture [_MainTex] {
    21.                 combine texture * primary
    22.             }
    23.         }
    24.     }
    25. }
    26. }
     
  4. PrefabEvolution

    PrefabEvolution

    Joined:
    Mar 27, 2014
    Posts:
    225
    You should add "Color RGBA" to allow write alpha to frame buffer
     
    ElasticSea likes this.
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    That shader should write alpha to the target. Are you using textures with alpha? Do they appear correctly when you view them in your regular camera?
    It would be ColorMask, and RGBA is the default, so there's not need to add ColorMask RGBA.
     
  6. DenisM

    DenisM

    Joined:
    Dec 6, 2013
    Posts:
    62
    Made all from scratch and it finally work. With shader above. Thanks to all.
     
  7. RavenWits

    RavenWits

    Joined:
    Jan 18, 2019
    Posts:
    2
    How did it excatly work? Can you share please?
     
  8. JonnyHilly

    JonnyHilly

    Joined:
    Sep 4, 2009
    Posts:
    742
    What part of that shader specifically makes it write alpha into the output texture ?
    I'm trying to do the same thing but render varying alpha into the render texture...
    if I use pragma surface surf Lambert alpha:fade then the whole render texture has 0 alpha.
    But if I render the same object in the scene, it has graduated alpha over the whole object specified by the alpha on the 3d object's texture

    if I use pragma surface surf Lambert then the object renders solid diffuse and renders ok as a RawImage it has 1 alpha where my object is and 0 around it. but no graduated alpha across my object.

    How can I make the alpha on the texture get rendered out into the render texture please ?
     
    Last edited: Mar 12, 2019
  9. altair2020

    altair2020

    Joined:
    Mar 6, 2011
    Posts:
    45
    Life saver.
     
  10. restush96

    restush96

    Joined:
    May 28, 2019
    Posts:
    60
    You need to disable HDR in the camera to show a transparent background.
     
    AliBuck likes this.
  11. AliBuck

    AliBuck

    Joined:
    Aug 22, 2020
    Posts:
    25
    Thanks! This was it for me. Also, on URP unchecking the Post Processing on camera helps.