- Home /
WebCam Texture scaling on iPhoneX
Hi, I am trying to make a fullscreen camera feed image on a mobile phone (iPhone X).
A: If I set AspectRatio Fitter to 'Fit in Parent' then it appears at full size but scaled down.
B: If I set the image (on which I am applying the WebCam Texture) AspectRatio Fitter to 'Envelope Parent' (which is Canvas) then the image appears really cropped up.
C: I tried using SetTextureScale("_MainTex", new Vector2(1f, 1f)); but it doesn't seem to affect the image being way out of screen size. If I set the texture scaling to 1.5f it seems I get the correct image size but scales the texture, not the image... (looks cool btw :D)
Please help me understand how the image scaling works in terms of aspect ratio. How can I have a full-screen webcam image, but also at the normal scale?
This is a code I am using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class WebCam : MonoBehaviour
{
private bool camAvailable;
private WebCamTexture backCam;
private Texture defaultBackground;
public RawImage backgroud;
public AspectRatioFitter fit;
void Start()
{
defaultBackground = backgroud.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length == 0)
{
Debug.Log("No Cameras found");
camAvailable = false;
return;
}
for (int i = 0; i < devices.Length; i++)
{
if (!devices[i].isFrontFacing)
{
backCam = new WebCamTexture(devices[i].name, Screen.width, Screen.height);
}
}
if (backCam == null)
{
Debug.Log("Back Camera failed...");
return;
}
backCam = new WebCamTexture(devices[0].name, Screen.width, Screen.height);
backCam.Play();
backgroud.texture = backCam;
camAvailable = true;
}//EO START
void Update()
{
if (!camAvailable) {
return;
}
float ratio = (float)backCam.width / (float)backCam.height;
fit.aspectRatio = ratio;
float scaleY = backCam.videoVerticallyMirrored ? -1f : 1f;
backgroud.rectTransform.localScale = new Vector3(1f, scaleY,1f);
int orient = -backCam.videoRotationAngle;
backgroud.rectTransform.localEulerAngles = new Vector3(0,0,orient);
backgroud.material.SetTextureScale("_MainTex", new Vector2(1f, 1f));
}
}
Answer by b_mayank · Jun 27, 2020 at 10:01 AM
I'm also stuck at this point. @lvaliauga Did you figured it out how to do it? if yes, please can you show how did you do it. <3