XRRGBCamera.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using SC.XR.Unity;
  6. using EZXR.Glass.Device;
  7. using EZXR.Glass.Core;
  8. public class XRRGBCamera : SingletonMono<XRRGBCamera>
  9. {
  10. public static XRRGBCamera Instance;
  11. public Texture CaptureImage;
  12. public WebCamTexture RGBCamTexture;
  13. public bool isAuto = true;
  14. private void Awake()
  15. {
  16. Instance = this;
  17. }
  18. NormalRGBCameraDevice rgbCameraDevice;
  19. private void Start()
  20. {
  21. // rgbCameraDevice = new NormalRGBCameraDevice();
  22. // ��ȡ���õ�����ͷ�豸
  23. if (isAuto)
  24. playCamera(1280,720);
  25. }
  26. private bool isPrepared = false;
  27. private MeshRenderer quad;
  28. private Texture2D m_VideoTexture_Y;
  29. private Texture2D m_VideoTexture_UV;
  30. private Material _videoBackgroundMat;
  31. private int[] previewSize = new int[2];
  32. private EZVIOInputImage m_CamImageBuffer;
  33. private bool preparePreview()
  34. {
  35. if (isPrepared)
  36. return true;
  37. if (rgbCameraDevice == null)
  38. return false;
  39. quad = this.gameObject.GetComponent<MeshRenderer>();
  40. if (quad == null)
  41. {
  42. quad = this.gameObject.AddComponent<MeshRenderer>();
  43. }
  44. _videoBackgroundMat = new Material(Shader.Find("EZXR/YUV2RGB"));
  45. int[] imageSize = rgbCameraDevice.getCameraSize();
  46. m_VideoTexture_Y = new Texture2D(imageSize[0], imageSize[1], TextureFormat.R8, false);
  47. m_VideoTexture_UV = new Texture2D(imageSize[0] / 2, imageSize[1] / 2, TextureFormat.RG16, false);
  48. previewSize[0] = imageSize[0];
  49. previewSize[1] = imageSize[1];
  50. _videoBackgroundMat.SetTexture("_YTex", m_VideoTexture_Y);
  51. _videoBackgroundMat.SetTexture("_UVTex", m_VideoTexture_UV);
  52. _videoBackgroundMat.SetFloat("_TexWidth", previewSize[0]);
  53. _videoBackgroundMat.SetFloat("_TexHeight", previewSize[1]);
  54. m_CamImageBuffer = new EZVIOInputImage();
  55. quad.material = _videoBackgroundMat;
  56. isPrepared = true;
  57. return true;
  58. }
  59. public void playCamera(int w, int h)
  60. {
  61. // rgbCameraDevice.Open();
  62. //preparePreview();
  63. return;
  64. if (RGBCamTexture != null && RGBCamTexture.isPlaying)
  65. {
  66. RGBCamTexture.Stop();
  67. }
  68. // ��WebCamTexture�󶨵�RawImage�������ʾ����ͷ��׽����ͼ��
  69. WebCamDevice[] devices = WebCamTexture.devices;
  70. if (devices.Length == 0)
  71. {
  72. Debug.Log("No webcam devices found.");
  73. return;
  74. }
  75. #if UNITY_EDITOR
  76. // ʹ�õ�һ�����õ�����ͷ�豸������Ը�����Ҫѡ�������豸��
  77. RGBCamTexture = new WebCamTexture(devices[0].name, w, h, 30);
  78. Debug.Log("��������ͷ" + devices[0].name);
  79. #else
  80. // ʹ�õ�һ�����õ�����ͷ�豸������Ը�����Ҫѡ�������豸��
  81. RGBCamTexture = new WebCamTexture(devices[1].name, w, h, 30);
  82. Debug.Log("��������ͷ" + devices[2].name);
  83. #endif
  84. // ��ʼ����ͷ��׽
  85. RGBCamTexture.Play();
  86. CaptureImage = RGBCamTexture;
  87. if (this.GetComponent<RawImage>() != null)
  88. {
  89. this.GetComponent<RawImage>().texture = CaptureImage;
  90. this.GetComponent<RawImage>().transform.localEulerAngles = new Vector3(0, 0, 0);
  91. }
  92. if (this.GetComponent<MeshRenderer>() != null)
  93. {
  94. this.GetComponent<MeshRenderer>().material.mainTexture = CaptureImage;
  95. this.GetComponent<MeshRenderer>().transform.localEulerAngles = new Vector3(0, 0, 0);
  96. }
  97. Debug.Log("��������ͷ");
  98. }
  99. public void Update()
  100. {
  101. }
  102. public void stopCamera()
  103. {
  104. // rgbCameraDevice.Close();
  105. }
  106. }