1
NGUI 3 Support / 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?
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?