Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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

Discussion in 'Audio & Video' started by makaka-org, Nov 5, 2019.

  1. makaka-org


    Dec 1, 2013
    1. I use VideoPlayer with Render Mode = MaterialOverride.
    2. I need to use exactly this mode.
    3. Unity 2019.3.0.b9

    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:

    1. 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.
    2. VideoPlayer.aspectRatio is not using for this Render Mode.
    dimib likes this.
  2. CrispyCritter


    May 11, 2018
    i have this same problems, its a shame that the unity forums is the worst place to come for any kind of help :(
    loaikk9 and dimib like this.
  3. DominiqueLrx


    Unity Technologies

    Dec 14, 2016

    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Video;
    3. public class VideoPlayerTextureController : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         GetComponent<VideoPlayer>().prepareCompleted += ConfigureTexture;
    8.     }
    10.     void ConfigureTexture(VideoPlayer vp)
    11.     {
    12.         vp.texture.wrapMode = TextureWrapMode.Mirror;
    13.     }
    14. }
    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
  4. sgtkoolaid


    May 11, 2010
    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?