Search Unity

RenderTexture with transparent background is possible?

Discussion in 'General Graphics' started by WildStyle69, Nov 8, 2016.

  1. WildStyle69

    WildStyle69

    Joined:
    Jul 20, 2016
    Posts:
    318
    Hi folks.. I'm trying to achieve a layered material effect in my project, so I can modify the top material texture without affecting the GameObject's objects main material / look / feel.
    • To do this I have 2 materials on an GameObject.
    • Material index 0 is the base model material, and index 1 is the target material to modify, via a Blit on RenderTexture.
    • This works to some extent, however where things fail is transparency, the material at index 1 must have a transparent background, so that you can still see the base model material at index 0 behind it.
    • I've tried to modify how the RenderTexture is created to be transparent, by changing the camera culling and clear flags with a transparent background. Also passing in a transparent texture for the starting texture that I Blit to setup... I think I did this at the code level successfully, however visually this still did not work correctly, as I can no longer see the material underneath at index 0.
    • Also worth noting that I'm using a modified 'Standard' shader to move the z-index of the material at index 1 forward a little to ensure it's visible.
    Hope the above makes sense, if anyone can let me know if what I'm trying to achieve is actually possible? Can you layer materials with transparency in a dynamically generated RenderTexture?

    Any help / points greatly appreciated, thanks!

    // WildStyle
     
    Last edited: Nov 8, 2016
  2. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    642
    You can write anything into a rendertexture that you could have in a regular texture. You do need to make sure your rendertexture actually has an alpha channel, though (use the ARGB32 format)

    Most likely, when you do your blit into the render texture, it's setting the alpha to 1.0 (opaque) for every pixel that it writes. Even when you render opaque geometry, the destination alpha channel can be affected.

    You probably want a shader that writes something else into that alpha channel, whether it's a constant level of opacity, or a mask from another texture?
     
    zhuhaiyia1 likes this.
  3. WildStyle69

    WildStyle69

    Joined:
    Jul 20, 2016
    Posts:
    318
    Thanks for reply @bluescrn - and for confirming that what I'm trying to do should be possible. You could be onto something reference the alpha channel in the shader, I'll look into this some more tonight!
     
  4. WildStyle69

    WildStyle69

    Joined:
    Jul 20, 2016
    Posts:
    318
    I was unable to get this working with the material at index 1 being Opaque... it still shows a background regardless... event though I'm sure the Blit shader is returning transparency. However when changing the material at index 1 mode to Transparent or Cutout, then you can see the layered materiel behind, however that creates other issues. Guessing this could perhaps be possible with custom rendering shader.
     
  5. restush96

    restush96

    Joined:
    May 28, 2019
    Posts:
    137
    You need to disable HDR in the camera to show a transparent background.