VideoPlayer & MaterialOverride — how to set aspect ratio, scale or change tiling/offest?

Conditions:

  • I use VideoPlayer with Render Mode = MaterialOverride.
  • I need to use exactly this mode.
  • Unity 2019.3.0.b9

Task:
I need to set aspect ratio to video stream without changing of Scale (Transform) of Game Object.
How can I achieve it?

Default Behavior of Video Player Component:

  • In this mode VideoPlayer assigns some Texture to Material in RealTime (called “TempBuffer 1920x1080 in RealTime”) and I can’t set tiling/offset of Material in Unity Editor: they are blocked.
  • VideoPlayer.aspectRatio is not using for this Render Mode.
1 Like

i have this same problems, its a shame that the unity forums is the worst place to come for any kind of help :frowning:

2 Likes

Hi!

This is a bit of a late reply, but I just wanted to acknowledge this issue, and maybe this will be helpful to other users having the same interrogation.

The way that the VideoPlayer currently injects its output texture into the target material is inadvertently hijacking the Tiling and Offset parameters present in the material, thus forcing them back to their default value. We’ll fix this.

This being said, I’m not sure this will be all the control that you need because this texture has its wrap mode set to TextureWrapMode.Clamp, so it will repeat the pixels present on the edges, as opposed to providing black padding like the VideoPlayer does when rendering into a RenderTexture. Note that there is a way to change the VideoPlayer’s texture wrap mode, but you must wait that the VideoPlayer actually creates it. You can do it this way:

using UnityEngine;
using UnityEngine.Video;
public class VideoPlayerTextureController : MonoBehaviour
{
    void Start()
    {
        GetComponent<VideoPlayer>().prepareCompleted += ConfigureTexture;
    }

    void ConfigureTexture(VideoPlayer vp)
    {
        vp.texture.wrapMode = TextureWrapMode.Mirror;
    }
}

All this being said, the most flexible way to reach your goal in this case - which also has the advantage of working right now - is to use the VideoPlayer.RenderTexture render mode instead of VideoPlayer.MaterialOverride. You then set the targeted RenderTexture on the required material texture input on the Renderer to be used. This will give you full control of the texture wrap mode, which you can set on the RenderTexture asset itself. The Tiling/Offset controls in the Material will stay usable, and you’ll also get the black padding that the VideoPlayer provides in the RenderTexture. If you are careful to create this RenderTexture exactly the same resolution as the video you’re playing (as indicated here), the VideoPlayer will use a more efficient code path and save an internal blitting, which will also avoid quality loss due to resampling.

Hope this helps,

Dominique Leroux
A/V developer at Unity

1 Like

i have this issue when using URP lit material. render to texture with no scaling doesn’t work. nor does this little script. im not sure what I am doing wrong. I have black padding all around the video. but if I use unlit/transparent material it scales it properly. but i can’t have the back render for a floating video mesh I want to make. what do I do?