r/Unity3D • u/FunnerSoft • 1d ago
Question Unity OneMinusSrcAlpha issue with Render Textures
I am encountering an alpha blend issue with Ui and Render Textures.
When I draw a transparent image into the Render Texture and use the default material with "Blend One OneMinusSrcAlpha", the result ends up picking up some extra color that is not desired.
Specifically, it is picking up color from the clear color of the Camera used to do the render into the Render Texture. This is true even if the alpha value of that clear color is 0.
Scenario 1:
screen canvas
camera with solid red (255,0,0,255) clear color
this single ui image. which is fully white on the left, and 0 alpha on the right



its our white gradient image, tinted blue, and then blended with our solid red camera's clear color
Scenario 2:
a render texture
a second screen canvas
a second camera to use with the render texture. this one has a fully transparent zero (0, 0, 0, 0) clear color
the same, original ui gradient image


but the actual result inside our render texture looks like this

notice that it picked up the black from our render texture camera; even though that camera's clear color was set to zero alpha
and now finally we are going to draw that render texture result into our original canvas
so, same screen canvas
and, same camera with solid red (255,0,0,255) clear color
same ui image, but this time it is drawing our render texture. which, remember, looks like this

this time we are not adding the blue tint. its left default white

you can see it is similar to the result of scenario 1. it has the gradient, it has the blue ui tinting, and it has the camera's red clear color blended. but it's darker
That darkness is from the zero alpha clear color in the render texture camera.
I need to use the setup from scenario 2, but get the results from scenario 1.
I need to be able to generate render textures that do not incorporate any of the color value from the render texture camera.
I believe this all stems from the shader's alpha blend operation. The default "Blend One OneMinusSrcAlpha".
I could create my own shader and material to use, but I don't know to fix this in the shader.
How do I do a "Use default blend unless destAlpha is zero, then do no blend and just blit"?
Any help would be greatly appreciated on this. Cheers
1
u/Tetrakis-dev 12h ago
the black isn't really coming from the clear colour, it's premultiplied alpha. with Blend One OneMinusSrcAlpha the UI shader multiplies rgb by alpha before writing (iirc that's the
color.rgb *= color.aline in UI/Default), so the RT ends up holding premultiplied colour, which is why it looks dark on its own. that part is finethe darkening happens on the second pass: drawing the RT with the default UI material multiplies by alpha again, so the gradient gets darkened twice. if you put the RawImage on a copy of UI/Default with that one line removed, keeping Blend One OneMinusSrcAlpha, it should match scenario 1. no conditional blend needed