Rotation and Scaling/Zooming of Camera Feed ✅ with WebCamTexture

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:

        Rect uvRectForVideoVerticallyMirrored = new(1f, 0f, -1f, 1f);
        Rect uvRectForVideoNotVerticallyMirrored = new(0f, 0f, 1f, 1f);
        Vector3 currentLocalEulerAngles = Vector3.zero;
----
----
----
        if (webCamTexture && webCamTexture.width >= 100f)
        {
            currentCWNeeded = targetDevice.isFrontFacing
                ? webCamTexture.videoRotationAngle
                : -webCamTexture.videoRotationAngle;

            if (webCamTexture.videoVerticallyMirrored)
            {
                currentCWNeeded += 180f;
            }

            currentLocalEulerAngles.z = currentCWNeeded;
            rawImage.rectTransform.localEulerAngles = currentLocalEulerAngles;

            if ((webCamTexture.videoVerticallyMirrored
                && !targetDevice.isFrontFacing)
                ||
                (!webCamTexture.videoVerticallyMirrored
                && targetDevice.isFrontFacing))
            {
                rawImage.uvRect = uvRectForVideoVerticallyMirrored;
            }
            else
            {
                rawImage.uvRect = uvRectForVideoNotVerticallyMirrored;
            }
        }

P.S. I released Camera Feed on Background (docs) — Unity Asset which operates WebCamTexture class and does all the work for you.

9050041--1250419--camera-feed-cover-made-with-unity.jpg

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).

9054844--1251535--3.png