Hi,
I've got some code set up for drawing to a RenderTexture instead of directly to the window to be able to run screen based shaders over the "world". This RenderTexture could be thought of as a scene buffer and in my case I do not need it to be transparent since all I do is draw it to the window in the end. This frees up the alpha channel of the RenderTexture which is great because I can use that to store a depth buffer in the same draw call, or so I thought.
Turns out that it doesn't behave the way you would expect, and somehow when I supply the RenderTexture to a shader the alpha channel is completely white and the alpha I set when drawing to it seems to be taken into account when sampling the RGB channels.
Here's the shader I use to write sprites to the buffer:
uniform sampler2D texture; // Sprite texture
uniform sampler2D rt_scene; // Scene render texture, which is what the shader is drawing to. I pass this in here so I can mix without setting the alpha
uniform float r_depth; // Fixed depth value for every sprite
void main()
{
vec4 color = texture2D( texture, gl_TexCoord[0].xy );
vec4 sceneBuffer = texture2D( rt_scene, gl_TexCoord[0].zw ); // gl_TexCoord[0].zw are screen space texture coords
gl_FragColor.rgb = mix( sceneBuffer.rgb, color.rgb, color.a );
gl_FragColor.a = mix( sceneBuffer.a, r_depth, color.a );
}
Then when I draw the Render Texture to the window I also have a custom shader and do:
vec3 color = texture2D( texture, gl_TexCoord[0].xy ).rgb;
gl_FragColor.rgb = color;
gl_FragColor.a = 1;
And here's an image that shows the problem (the contents of the RenderTexture):
I've done some testing and it'd be my guess that the problem lies outside of the GLSL code but I'm not very familiar with things like OpenGL flags and the like, so I come here hoping someone might have an idea of what to do. Fixing this would bring about a pretty significant performance boost (and order-independent draw sorting(!)) so I would certainly appreciate it!
Thanks