I am trying to creat a green screen filter. I would like to have the option of passing it a background image, but if no image was passed, then to just make the background transparent. How would I go about checking if a background image was passed to the input?
2 Answers
By default an image texture with no associated file will output a completely black value. You can use this to drive the alpha value of your background, using a "Math" node set to "Greater Than 0" in order to have full transparency when there is no image texture and zero transparency when there is an associated image texture.
-
$\begingroup$ if the background image has black in it though, then it will end up transparent though wouldn't it? $\endgroup$– ToMakPoCommented Jan 15, 2020 at 1:18
-
$\begingroup$ Yes indeed ! You could however tweak your background image to be not completely black, having a color like (R(001),G(000),B(000)) It will still appear as black but your node network will consider the value is in fact greater than 0 $\endgroup$– GorgiousCommented Jan 15, 2020 at 8:54
An image is interpreted as having colors made out of 4 RGBA components in range 0..1. For example, if an image has 32 bit colors, each component is an 8 bit integer in range 0..255, so it's interpreted as x/255 to be converted to the 0..1 range.
(aditionally, if Color Space is not set as non-color, Blender also applies a curve to reshape the distribution of colors so it resembles physical value of the strength of light, called linear color space, but this is irrelevant to this problem as the color components still stay in 0..1 range, hence the parentheses)
That color is then processed as a vector of 3 components RGB (which in the node tree is displayed as a yellow Color type, but it's the same thing as a blue Vector type), and separately Alpha value. Each of those 4 values is stored in float32. Such type is not limited to 0..1 range! It seems so, because sliders are soft-locked to that range - however, you can manually input a value outside of that range...
(in shaders, color components often go outside 0..1 range, either by accident when you do some operations or them, or intentionally, for example in the Filmic color transform, the actual value of the full white color is around 16.19
; again, parentheses)
So it seems the best sentinel value to pick is not black as Gorgious suggested, but one outside the 0..1 range, I decided to pick 3.4028233e+38
, which is 2nd biggest value, not counting infinity.
- In N Numbers Panel go to Group tab.
- Click on the Default color to set it. Then click on the
R
(Red) component field and paste3.4028233e+38
- Make sure Hide Value is checked.
- Now use the setup like below, with the value
3.4028233e+38
pasted into the Math > Compare node. Make sure the Epsilon of that node is exactly0
:
- Tab Exit to higher context, remove your custom group and add it again, to reset the value to the default_value