Chroma Key Shader

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
JarJarBinkks
Prole
Posts: 1
Joined: Sun Apr 28, 2024 10:24 am

Chroma Key Shader

Post by JarJarBinkks »

Hi, i have an ogv video with a black background because ogv dont support alpha channel. How can i create a shader that remove the black color of my video.
Hi, i juste started using love2D
RNavega
Party member
Posts: 264
Joined: Sun Aug 16, 2020 1:28 pm

Re: Chroma Key Shader

Post by RNavega »

Did you create the video, and if so, can you make it into like a PNG image sequence instead? If the video is short (like your studio logo), an image sequence would work fine.

The reason I'm asking is that while you can try to derive the alpha channel from the RGB channels, it's a lossy process.
If your background color is black and you want to have soft degrees of transparency, this means that the darker a pixel is, the more transparent it'll be, and the only 100% opaque pixel is a fully white one -- anything less than white will have some level of transparency, especially dark colors, even if in the original scene they'd be opaque. So a dark but fully opaque object will end up looking transparent in your chroma-keyed video.

If you didn't create the video or if you can't have it as a PNG image sequence (that is, if you really need to derive alpha from RGB in a shader), then you can simply plug the Y component of the video into the alpha channel.
Löve internally uses 3 textures for a video, each for the Y, Cb and Cr components of the video, and the Y texture has the luma (the brightness) of each pixel in the video. That's your alpha channel.
You'd create a pixel shader with this code:

Code: Select all

void effect() {
        vec4 videoColor = VideoTexel(VaryingTexCoord.xy) * VaryingColor;
        float alpha = Texel(love_VideoYChannel, VaryingTexCoord.xy).r;
	love_PixelColor = vec4(videoColor.xyz, alpha);
}
References:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests