Author Topic: Render Texture / ReadPixels Transparency Problem  (Read 3304 times)

keysosaurus

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 13
    • View Profile
Render Texture / ReadPixels Transparency Problem
« on: May 26, 2014, 06:46:15 AM »
Hello,

I've created a system to capture a sprite (or set of sprites) and then redraw that capture to the screen. NGUI is being used to manage the sprite rendering although I'm not sure it is important to the problem I am having.

The issue is that when the capture is drawn to the screen, blending seems to be incorrect in areas where a transparent image is drawn on top of an opaque one. The image attached shows the issue.

The character on the left is drawn with NGUI straight to the screen, it is how it is supposed to look. The face sprite is drawn first and then the makeup on top, all blended together.



The character on the right is one single image, created with the capture system. The blending is incorrect because wherever there is makeup on the face it is semi-transparent. As you can see from the image, the green box in the background is visible through the face.

Here is the process used to create the new image:

1. Render only the character to a render texture with a camera that has it's clear flags set to a solid color which is transparent.
2. Read the pixels of the render texture into a Texture2D so that it can be read from. This is now the source Texture2D.
3. Find the portion of the texture that the character is in and copy the pixels from the source Texture2D to the destination Texture2D. The destination Texture2D is a new atlas of images.
NOTE: Source and destination Texture2D objects have a transparent background.

It seems as if the alpha value of the topmost sprite is being picked up, either at the RenderTexture or ReadPixels stage, rather than the blended value.
Has anyone who has experienced this type of problem before got a solution to it?
« Last Edit: May 26, 2014, 07:02:43 AM by keysosaurus »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Render Texture / ReadPixels Transparency Problem
« Reply #1 on: May 26, 2014, 11:24:57 AM »
Generally, you need to have a different shader for rendertextures because it bakes the alpha into the color values for whatever reason. I've had lots of problems with that in the past, but that was all for 3d models which took the alpha of their maintextures and applied as blending.

I'm not entirely sure how to solve it in your case, but that's what's going on.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Render Texture / ReadPixels Transparency Problem
« Reply #2 on: May 26, 2014, 11:23:10 PM »
That's how shaders work. When you draw an RGBA image into a render texture, it also writes the alpha channel in there. If it didn't, it would simply not have any transparency to begin with and you would just see a textured quad (switch the shader to be Diffuse to see what I mean). In your case you don't want to use alpha-based blending. You will need to write a custom shader instead, and unfortunately I can't help much with that as shaders are not related to NGUI.