RenderTexture VideoPlayer video background

Hi,

I’m loading a video from a script, into a RenderTexture of a static size, so the video is rendered centred / contained within the RenderTexture.

I have two related problems with this:

#1: Before the video starts playing, the RenderTexture is shown black, which appears like a black rectangle in the game
#2: If the video aspect ratio does not match the RenderTexture’s aspect ratio, there will be black bars on top and bottom of the rectangle.

This is the code I’m using:

        var rendTex = new CustomRenderTexture(rendTexWidth, rendTexHeight, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
        rendTex.initializationMode = CustomRenderTextureUpdateMode.OnDemand;
        rendTex.initializationSource = CustomRenderTextureInitializationSource.TextureAndColor;
        rendTex.initializationTexture = VideoMaterials.VideoDefaultFrame;
        rendTex.initializationColor = new Color(1, 0, 0, 0);
        rendTex.autoGenerateMips = false;
        rendTex.useMipMap = false;

        _videoPlayer = photoGo.AddComponent<VideoPlayer>();
        _videoPlayer.renderMode = VideoRenderMode.RenderTexture;
        _videoPlayer.targetTexture = rendTex;

The CustomRenderTexture initialisation colour stuff doesn’t work at all - the rectangle is still black, even though it should be a colorized texture.

Ideally, I’d want either a texture shown there before the video starts playing, with an alpha channel so if the middle of the RenderTexture is used up by the video, there are no black bars on the top and bottom of the rectangle, or just the entire RenderTexture to contain 100% transparent pixels.

Any pointers?

Hi @ivoras ! For your #1 problem, you set the initialization mode to OnDemand so, you need to call Initialize yourself. Like this.

var rendTex = new CustomRenderTexture(rendTexWidth, rendTexHeight, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
rendTex.initializationMode = CustomRenderTextureUpdateMode.OnDemand;
rendTex.initializationSource = CustomRenderTextureInitializationSource.TextureAndColor;
rendTex.initializationTexture = VideoMaterials.VideoDefaultFrame;
rendTex.initializationColor = new Color(1, 0, 0, 0);
rendTex.autoGenerateMips = false;
rendTex.useMipMap = false;

//Initialize the texture with the previous settings
rendTex.Initialize();

The texture will be transparent and you should only see it when the VideoPlayer starts drawing in it. And I think it should fix your #2 problem, right? If not, is there a reason you don’t set the texture to the same resolution as the video?

That solved it, thanks :slight_smile:
Both my issues are / were the consequence of the same thing.