Chroma keying through Pixel shader programme

I want to blend two textures :

Texture 1 : 8 bit Luminance
Texture 2 : 32 bit R5G5B5A1

While merging these two, I want to treat RGB(0,0,0) in texture 2 to be transparent.

Can anybody guide me how to do it through shader programme ?

Yeah, in the fragment shader you sample the textures, check the returned color of the second
texture, if it’s not 0,0,0 you can blend it with the first texture as follows (this is GLSL pseudocode):

vec4 tex1Sample = texture2D();
vec4 tex2Sample = texture2D();

if(tex2Sample.rgb = vec3(0,0,0))
gl_FragColor = tex1Sample;
gl_FragColor = tex1Sample*tex2Sample;

this will simply use the first texture value if the second texture rgb is black (0,0,0) or modulate them otherwise.

hope that’s along the lines of what you’re asking.

I realize it’s 15 years later, but this example helped me with a shader for threejs and I wanted to note for future readers that the conditional operator in the if statement should be “==” not “=”. :slight_smile: