我需要從手機到 Unity 的實時攝像頭饋送。我試過 webcamTexture 但它不能按我想要的方式作業。縮放太多了,我每次都必須構建和運行才能看到我對解析度所做的更改的結果。此外,如果有人是專家并幫助我為我的學位建立我的最后一年專案,我將不勝感激。這是我用于使用相機的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraScript : MonoBehaviour
{
private bool camAvailable;
private WebCamTexture backCam;
private Texture defaultBackground;
public RawImage background;
public AspectRatioFitter fit;
public int width = 1080;
public int height = 2400;
// Start is called before the first frame update
void Start()
{
defaultBackground = background.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if(devices.Length ==0)
{
Debug.Log("No Camera Available");
camAvailable = false;
return;
}
for (int i =0; i<devices.Length; i )
{
if(!devices[i].isFrontFacing)
{
backCam = new WebCamTexture(devices[i].name, width, height);
}
}
if(backCam == null)
{
Debug.Log("Unable to find the Back Camera");
return;
}
backCam.Play();
background.texture = backCam;
camAvailable = true;
}
// Update is called once per frame
void Update()
{
if (!camAvailable)
return;
float ratio = (float)backCam.width / (float)backCam.height;
fit.aspectRatio = ratio;
float scaleY = backCam.videoVerticallyMirrored ? -1f: 1f;
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
int orient = -backCam.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
}
uj5u.com熱心網友回復:
這只是數學問題,我可以與您分享此腳本以進行正確計算。多年來,它已經在許多專案中使用。
您只需要分配一個 Quad,Quad 將位于主攝像機的遠背景平面中。
類似的想法也可以應用于 UI Rect。理論上,你應該能夠達到這個結果(C#中的視頻通話測驗)。
void CalculateBackgroundQuad()
{
Camera cam = Camera.main;
ScreenRatio = (float)Screen.width / (float)Screen.height;
BackgroundQuad.transform.SetParent(cam.transform);
BackgroundQuad.transform.localPosition = new Vector3(0f, 0f, cam.farClipPlane / 2f);
float videoRotationAngle = webCamTexture.videoRotationAngle;
BackgroundQuad.transform.localRotation = baseRotation * Quaternion.AngleAxis(webCamTexture.videoRotationAngle, Vector3.forward);
float distance = cam.farClipPlane / 2f;
float frustumHeight = 2.0f * distance * Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
BackgroundQuad.transform.localPosition = new Vector3(0f, 0f, distance);
Vector3 QuadScale = new Vector3(1f, frustumHeight, 1f);
//adjust the scaling for portrait Mode & Landscape Mode
if (videoRotationAngle == 0 || videoRotationAngle == 180)
{
//landscape mode
TextureRatio = (float)(webCamTexture.width) / (float)(webCamTexture.height);
if (ScreenRatio > TextureRatio)
{
float SH = ScreenRatio / TextureRatio;
float TW = TextureRatio * frustumHeight * SH;
float TH = frustumHeight * (webCamTexture.videoVerticallyMirrored ? -1 : 1) * SH;
QuadScale = new Vector3(TW, TH, 1f);
}
else
{
float TW = TextureRatio * frustumHeight;
QuadScale = new Vector3(TW, frustumHeight * (webCamTexture.videoVerticallyMirrored ? -1 : 1), 1f);
}
}
else
{
//portrait mode
TextureRatio = (float)(webCamTexture.height) / (float)(webCamTexture.width);
if (ScreenRatio > TextureRatio)
{
float SH = ScreenRatio / TextureRatio;
float TW = frustumHeight * -1f * SH;
float TH = TW * (webCamTexture.videoVerticallyMirrored ? 1 : -1) * SH;
QuadScale = new Vector3(TW, TH, 1f);
}
else
{
float TW = TextureRatio * frustumHeight;
QuadScale = new Vector3(frustumHeight * -1f, TW * (webCamTexture.videoVerticallyMirrored ? 1 : -1), 1f);
}
}
BackgroundQuad.transform.localScale = QuadScale;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439971.html
下一篇:如何在顫動中創建這種票形狀?
