Search Unity

Rotation and Scaling/Zooming of Camera Feed ✅ with WebCamTexture

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

  1. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    1,024
    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):
    1.  
    2.         Rect uvRectForVideoVerticallyMirrored = new(1f, 0f, -1f, 1f);
    3.         Rect uvRectForVideoNotVerticallyMirrored = new(0f, 0f, 1f, 1f);
    4.         Vector3 currentLocalEulerAngles = Vector3.zero;
    5. ----
    6. ----
    7. ----
    8.         if (webCamTexture && webCamTexture.width >= 100f)
    9.         {
    10.             currentCWNeeded = targetDevice.isFrontFacing
    11.                 ? webCamTexture.videoRotationAngle
    12.                 : -webCamTexture.videoRotationAngle;
    13.  
    14.             if (webCamTexture.videoVerticallyMirrored)
    15.             {
    16.                 currentCWNeeded += 180f;
    17.             }
    18.  
    19.             currentLocalEulerAngles.z = currentCWNeeded;
    20.             rawImage.rectTransform.localEulerAngles = currentLocalEulerAngles;
    21.  
    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.         }
    35.  
    36.  
    P.S. I released Camera Feed on Background (docs) — Unity Asset which operates WebCamTexture class and does all the work for you.

    camera-feed-cover-made-with-unity.jpg
     
  2. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    1,024
    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).

    3.png