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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Rotation and Scaling/Zooming of Camera Feed ✅ with WebCamTexture

Discussion in 'Community Learning & Teaching' started by makaka-org, Jun 2, 2023.

  1. makaka-org


    Dec 1, 2013
    When you are Dealing with Camera Stream on a Mobile Device, then Setting correct Rotation and Displaying for Different Cameras and Platforms on the Device Screen is not an easy task.

    I have next workaround using RawImage for displaying Camera Stream correctly with WebCamTexture:
    Code (CSharp):
    2.         Rect uvRectForVideoVerticallyMirrored = new(1f, 0f, -1f, 1f);
    3.         Rect uvRectForVideoNotVerticallyMirrored = new(0f, 0f, 1f, 1f);
    4.         Vector3 currentLocalEulerAngles =;
    5. ----
    6. ----
    7. ----
    8.         if (webCamTexture && webCamTexture.width >= 100f)
    9.         {
    10.             currentCWNeeded = targetDevice.isFrontFacing
    11.                 ? webCamTexture.videoRotationAngle
    12.                 : -webCamTexture.videoRotationAngle;
    14.             if (webCamTexture.videoVerticallyMirrored)
    15.             {
    16.                 currentCWNeeded += 180f;
    17.             }
    19.             currentLocalEulerAngles.z = currentCWNeeded;
    20.             rawImage.rectTransform.localEulerAngles = currentLocalEulerAngles;
    22.             if ((webCamTexture.videoVerticallyMirrored
    23.                 && !targetDevice.isFrontFacing)
    24.                 ||
    25.                 (!webCamTexture.videoVerticallyMirrored
    26.                 && targetDevice.isFrontFacing))
    27.             {
    28.                 rawImage.uvRect = uvRectForVideoVerticallyMirrored;
    29.             }
    30.             else
    31.             {
    32.                 rawImage.uvRect = uvRectForVideoNotVerticallyMirrored;
    33.             }
    34.         }
    P.S. I released Camera Feed on Background (docs) — Unity Asset which operates WebCamTexture class and does all the work for you.

  2. makaka-org


    Dec 1, 2013
    There is a Zooming/Scaling issue when you are trying to get and show Camera Feed on the Device Screen in all the tutorials on the Web related to Unity's WebCamTexture, RawImage and Aspect Ratio Fitter.

    The image is getting enlarged in some cases, hiding thousands of pixels, which is critical, for example, for AR.

    Often manuals are designed to solve one specific task and are not universal: only for Landscape Mode, Only for Rear/Back Camera of Mobile Device, and so on.

    On the other side, when you are using Assets from the Unity Asset Store, you are most likely buying a universal solution that has been tested for different situations and has taken weeks or months of work.

    In short, I solved the Zooming issue for all device orientations and cameras with the code on screenshot attached (my answer on forum).

    This code is a part of my Unity Asset Camera Feed on Background (docs).