123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using SC.XR.Unity;
- using EZXR.Glass.Device;
- using EZXR.Glass.Core;
- public class XRRGBCamera : SingletonMono<XRRGBCamera>
- {
- public static XRRGBCamera Instance;
- public Texture CaptureImage;
- public WebCamTexture RGBCamTexture;
- public bool isAuto = true;
- private void Awake()
- {
- Instance = this;
- }
- NormalRGBCameraDevice rgbCameraDevice;
- private void Start()
- {
- // rgbCameraDevice = new NormalRGBCameraDevice();
- // ��ȡ���õ�����ͷ�豸
- if (isAuto)
- playCamera(1280,720);
- }
- private bool isPrepared = false;
- private MeshRenderer quad;
- private Texture2D m_VideoTexture_Y;
- private Texture2D m_VideoTexture_UV;
- private Material _videoBackgroundMat;
- private int[] previewSize = new int[2];
- private EZVIOInputImage m_CamImageBuffer;
- private bool preparePreview()
- {
- if (isPrepared)
- return true;
- if (rgbCameraDevice == null)
- return false;
- quad = this.gameObject.GetComponent<MeshRenderer>();
- if (quad == null)
- {
- quad = this.gameObject.AddComponent<MeshRenderer>();
- }
- _videoBackgroundMat = new Material(Shader.Find("EZXR/YUV2RGB"));
- int[] imageSize = rgbCameraDevice.getCameraSize();
- m_VideoTexture_Y = new Texture2D(imageSize[0], imageSize[1], TextureFormat.R8, false);
- m_VideoTexture_UV = new Texture2D(imageSize[0] / 2, imageSize[1] / 2, TextureFormat.RG16, false);
- previewSize[0] = imageSize[0];
- previewSize[1] = imageSize[1];
- _videoBackgroundMat.SetTexture("_YTex", m_VideoTexture_Y);
- _videoBackgroundMat.SetTexture("_UVTex", m_VideoTexture_UV);
- _videoBackgroundMat.SetFloat("_TexWidth", previewSize[0]);
- _videoBackgroundMat.SetFloat("_TexHeight", previewSize[1]);
- m_CamImageBuffer = new EZVIOInputImage();
- quad.material = _videoBackgroundMat;
- isPrepared = true;
- return true;
- }
- public void playCamera(int w, int h)
- {
- // rgbCameraDevice.Open();
- //preparePreview();
- return;
- if (RGBCamTexture != null && RGBCamTexture.isPlaying)
- {
- RGBCamTexture.Stop();
- }
- // ��WebCamTexture��RawImage�������ʾ����ͷ������ͼ��
- WebCamDevice[] devices = WebCamTexture.devices;
- if (devices.Length == 0)
- {
- Debug.Log("No webcam devices found.");
- return;
- }
- #if UNITY_EDITOR
- // ʹ�õ�һ�����õ�����ͷ�豸������Ը�����Ҫѡ�������豸��
- RGBCamTexture = new WebCamTexture(devices[0].name, w, h, 30);
- Debug.Log("��������ͷ" + devices[0].name);
- #else
- // ʹ�õ�һ�����õ�����ͷ�豸������Ը�����Ҫѡ�������豸��
- RGBCamTexture = new WebCamTexture(devices[1].name, w, h, 30);
- Debug.Log("��������ͷ" + devices[2].name);
- #endif
- // ��ʼ����ͷ��
- RGBCamTexture.Play();
- CaptureImage = RGBCamTexture;
- if (this.GetComponent<RawImage>() != null)
- {
- this.GetComponent<RawImage>().texture = CaptureImage;
- this.GetComponent<RawImage>().transform.localEulerAngles = new Vector3(0, 0, 0);
- }
- if (this.GetComponent<MeshRenderer>() != null)
- {
- this.GetComponent<MeshRenderer>().material.mainTexture = CaptureImage;
- this.GetComponent<MeshRenderer>().transform.localEulerAngles = new Vector3(0, 0, 0);
- }
- Debug.Log("��������ͷ");
- }
- public void Update()
- {
- }
- public void stopCamera()
- {
- // rgbCameraDevice.Close();
- }
- }
|