opacityMapChannel does not work for transparent video textures

Hello! I have a working hevc and webm dual source setup that is letting me play a transparent video in HTML. I’m trying to create a texture using that video and use that to display the transparent video in 3D space on a material. Following various tutorials, I landed on:

    var videoTexture = new pc.Texture(app.graphicsDevice, {
        format: pc.PIXELFORMAT_R5_G6_B5,
        autoMipmap: false
    });
    videoTexture.minFilter = pc.FILTER_LINEAR;
    videoTexture.magFilter = pc.FILTER_LINEAR;
    videoTexture.addressU = pc.ADDRESS_CLAMP_TO_EDGE;
    videoTexture.addressV = pc.ADDRESS_CLAMP_TO_EDGE;

var video = document.getElementById("main-video");
videoTexture.setSource(video);

for (var i = 0; i < this.materials.length; i++) {
        var material = this.materials[i].resource;
        material.emissiveMap = videoTexture;
        material.opacityMap = videoTexture;
        material.blendType = pc.BLEND_NORMAL;
        material.update();
    }

which produced this result:

(You can see the working transparent video in the upper corner)

Seeing this before with other alpha materials I was fairly sure that the opacity map channel was set to “r” by default, so I decided to set that in the code:

for (var i = 0; i < this.materials.length; i++) {
        var material = this.materials[i].resource;
        material.emissiveMap = videoTexture;
        material.opacityMap = videoTexture;
        material.blendType = pc.BLEND_NORMAL;
        material.opacityMapChannel = "a";//NEW
        material.update();
    }

which yields this unexpected result:

It seems like it can’t find the alpha channel in the texture.

I thought it was just a problem with transparent video since that’s a fairly niche use case, but when I copied the video onto a transparent canvas and then used the canvas in place of the video for the texture source, I was getting the exact same issue.

Code changed to get a canvas as the source:

    var canvas = document.getElementById("main-canvas");
    videoTexture.setSource(canvas);

In the screenshots, you can see the canvas in the top right (video is in the top left)

I feel like there is something wrong with the texture settings, or the material settings. Is their another way I should be setting the alpha channel?

This is a related post, but it seems like they used a workaround rather than fixing the core issue: Videotexture with alpha channel

Without being able to check on your project, you are trying to use the alpha channel of a texture that only has RGB (see the pixel format line).

Also, I would check if the video is actually transparent. I’m not sure if MP4 containers or web browsers support videos that have alpha.

See: How to use transparent videos on the web in 2022 - Rotato

If you can share an example project, that would help too.

Thanks for the quick response! Changing the texture format fixed the transparency issue!

The above screenshot is using pc.PIXELFORMAT_RGBA32F. Do you have a suggestion for what texture format I should be using to get the most high definition image? The colors also look a bit washed out at the moment.

What is the diffuse colour used on the material? Try making it black.

If you are still having issues, please post a small public project of the issue for the community to look

We’re completely up and running!

The issue with the blown out colors was due to the lighting in the scene making the emissive extra bright.

Here is a working project: PlayCanvas 3D HTML5 Game Engine

On the material, you can disable the lighting and use gamma and tonemapping on the material settings.

This allows you to still have the lighting you want in scene but not affect certain materials

Awesome! Thanks for that!

Updated the documentation in the project :grinning:

1 Like