- Home /
Alpha values not blending in RenderTexture
The problem I'm having is that when I draw a new texture into the RenderTexture, instead of blending with the texture that's already there (the render texture is set to clear "depth only" - though same thing happens with "don't clear") ... the alpha value of the new texture REPLACES instead of blending with the texture that's already there...
the texture that's already there has not been drawn this frame - it's only still there because the RenderTexture is set to don't clear, which is as I need it.
The images i've added are -
A) what i see on screen
B) the renderTexture Alpha layer
C) the renderTexrture setup
D) the shader i'm currently using to draw the input texture...
Not sure if it's relevant but i'm also using a depthmask so that only pixels within the shape can be renderer to... (but i've turned it off and same behaviour... so...)
grateful for any leads here :) ta... ![alt text][2]
I've got the same problem. I'm trying to draw two flat sprites with alpha channels and that render O$$anonymous$$ on the screen, but when rendered to RenderTextrure, the above sprite's alpha channel values just being overwritten, not blended.
Don't know how to solve that.
Don't post this as an answer. This isn't an answer. At best, post it as a comment (I'm converting it to one).
Answer by Hoeloe · Oct 07, 2013 at 09:40 AM
It's not replacing something that's already there, but it's rendering it back to front, and the ZTest argument is causing it to ignore everything behind the semi-transparent pixels, meaning it's not drawn at all. Try removing the ZTest call in the shader.
Thanks - I tried removing the ZTest call but it still does exactly the same...
Hmmm... I'm afraid I'm not sure then. I'm not hugely familiar with that form of surface shader, I prefer to use the more explicit ones.
Got one you think i could try? i'm not attached to that one by any means ;)
Well, I'm not entirely sure what it is you're trying to render, and it doesn't help that I have no experience with RenderTextures, as I don't have Unity Pro.
Answer by Owen-Reynolds · Oct 07, 2013 at 03:00 PM
Say you draw a solid cube to the render texture, alpha=1 everywhere. Then you draw an alpha=0.5ish smoke texture over it. That smoke should blend with the cube, but the final alpha should be 1 (the smoke didn't make the cube become transparent.) If any of the smoke went off the edge of the cube, that would have a 0.5 alpha.
Then, using "don't clear" to keep the previous frame (really, all previous frames,) the render texture blends in. The solid aplha=1 cube overwrites, extra 0.5 smoke blends in.
The problem is, a normal "alpha blend with background" (I think) will set the background/destination alpha the same as it sets colors -- to the weighted average. Your solid cube is being turned into a 0.75 alpha by the smoke. No one usually cares, since you never look at background alpha anyway -- you always use src, oneMinusSrc. Or, for render-textures, you just slam it on the screen as the final step. As Hoeloe notes, surface shaders are easier to test (like displaying alpha as grey scale, so you can check it.)
I think, on the initial blend, alpha should be set to Max(src,dest) (if that's even a setting.)
But, always blending with the previous frame seems wrong to me. I'm thinking that whatever effect you're trying to get is going to come some other way. Like a second camera ... .
Thanks for your answer - the effect i want is pretty simple really - it'd just be like painting into texture - using a brush the way photoshop does... so if there is already something down that has an alpha of 0.8 and i paint over that pixel with an alpha of 0.1 it's going to be 0.8 or more ... not getting replaced by the 0.1 value...
I'd think a "real" painting program would involve having your own Texture2D, which you directly manipulate using setPixel commands. A small hassle (need a nested loops with the correct offsets to apply a brush,) but you have full control over how everything is applied.
Seems "don't clear" might be a fast&dirty way at first, but you're going to eventually have grid marks and other GUI elements, with no good way to separate them from the real image.
Hi Owen... well the camera that is rendering the RenderTeture will only have the painted images on it... so no GUI or anything there... and then that rendertexture is just one layer of the overall scene... i think setPixel is too slow for this... that's my understanding anyway - thanks for your replies though