Search Unity

Question RenderTexture VideoPlayer video background

Discussion in 'Audio & Video' started by ivoras, Mar 7, 2022.

  1. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    66
    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:

    Code (CSharp):
    1.  
    2.         var rendTex = new CustomRenderTexture(rendTexWidth, rendTexHeight, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
    3.         rendTex.initializationMode = CustomRenderTextureUpdateMode.OnDemand;
    4.         rendTex.initializationSource = CustomRenderTextureInitializationSource.TextureAndColor;
    5.         rendTex.initializationTexture = VideoMaterials.VideoDefaultFrame;
    6.         rendTex.initializationColor = new Color(1, 0, 0, 0);
    7.         rendTex.autoGenerateMips = false;
    8.         rendTex.useMipMap = false;
    9.  
    10.         _videoPlayer = photoGo.AddComponent<VideoPlayer>();
    11.         _videoPlayer.renderMode = VideoRenderMode.RenderTexture;
    12.         _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?
     
  2. The_Island

    The_Island

    Unity Technologies

    Joined:
    Jun 1, 2021
    Posts:
    502
    Hi @ivoras! For your #1 problem, you set the initialization mode to OnDemand so, you need to call Initialize yourself. Like this.
    Code (CSharp):
    1. var rendTex = new CustomRenderTexture(rendTexWidth, rendTexHeight, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
    2. rendTex.initializationMode = CustomRenderTextureUpdateMode.OnDemand;
    3. rendTex.initializationSource = CustomRenderTextureInitializationSource.TextureAndColor;
    4. rendTex.initializationTexture = VideoMaterials.VideoDefaultFrame;
    5. rendTex.initializationColor = new Color(1, 0, 0, 0);
    6. rendTex.autoGenerateMips = false;
    7. rendTex.useMipMap = false;
    8.  
    9. //Initialize the texture with the previous settings
    10. 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?
     
  3. ivoras

    ivoras

    Joined:
    May 21, 2020
    Posts:
    66
    That solved it, thanks :)
    Both my issues are / were the consequence of the same thing.