XRRGBCamera.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Collections;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using UnityEngine.XR.ARFoundation;
  8. using UnityEngine.XR.ARSubsystems;
  9. using SC.XR.Unity;
  10. public class XRRGBCamera : SingletonMono<XRRGBCamera>
  11. {
  12. public bool isARCamrea;
  13. public static XRRGBCamera Instance;
  14. public Texture CaptureImage;
  15. public WebCamTexture RGBCamTexture;
  16. public bool isAuto = true;
  17. private void Awake()
  18. {
  19. Instance = this;
  20. }
  21. private void Start()
  22. {
  23. if (isARCamrea)
  24. {
  25. // 获取 ARCameraManager 组件
  26. cameraManager = FindObjectOfType<ARCameraManager>();
  27. cameraManager.frameReceived -= OnCameraFrameReceived;
  28. // 订阅更新事件
  29. cameraManager.frameReceived += OnCameraFrameReceived;
  30. }
  31. // 获取可用的摄像头设备
  32. if(isAuto)
  33. playCamera(1280,720);
  34. }
  35. public ARCameraManager cameraManager;
  36. private Material cameraBackgroundMaterial;
  37. public void playCamera(int w, int h)
  38. {
  39. if (isARCamrea)
  40. {
  41. // Vuforia.VuforiaBehaviour.Instance.enabled = true;
  42. }
  43. else
  44. {
  45. // 将WebCamTexture绑定到RawImage组件以显示摄像头捕捉到的图像
  46. WebCamDevice[] devices = WebCamTexture.devices;
  47. if (devices.Length == 0)
  48. {
  49. Debug.Log("No webcam devices found.");
  50. return;
  51. }
  52. // 使用第一个可用的摄像头设备(你可以根据需要选择其他设备)
  53. RGBCamTexture = new WebCamTexture(devices[0].name, w, h, 30);
  54. Debug.Log("开启摄像头" + devices[0].name);
  55. // 开始摄像头捕捉
  56. RGBCamTexture.Play();
  57. CaptureImage = RGBCamTexture;
  58. if (this.GetComponent<RawImage>() != null)
  59. {
  60. this.GetComponent<RawImage>().texture = CaptureImage;
  61. this.GetComponent<RawImage>().transform.localEulerAngles = new Vector3(0, 0, 0);
  62. }
  63. if (this.GetComponent<MeshRenderer>() != null)
  64. {
  65. this.GetComponent<MeshRenderer>().material.mainTexture = CaptureImage;
  66. this.GetComponent<MeshRenderer>().transform.localEulerAngles = new Vector3(0, 0, 0);
  67. }
  68. Debug.Log("开启摄像头");
  69. }
  70. }
  71. Texture2D cameraTexture;
  72. private void OnCameraFrameReceived(ARCameraFrameEventArgs eventArgs)
  73. {
  74. // Debug.Log("获取OnCameraFrameReceivedOnCameraFrameReceived");
  75. // 获取最新的摄像头图像
  76. if (!cameraManager.TryAcquireLatestCpuImage(out XRCpuImage image))
  77. {
  78. return;
  79. }
  80. var format = TextureFormat.RGBA32;
  81. if (cameraTexture == null || cameraTexture.width != image.width || cameraTexture.height != image.height)
  82. {
  83. cameraTexture = new Texture2D(image.width, image.height, format, false);
  84. }
  85. XRCpuImage.Transformation m_Transformation = XRCpuImage.Transformation.MirrorY;
  86. // Convert the image to format, flipping the image across the Y axis.
  87. // We can also get a sub rectangle, but we'll get the full image here.
  88. var conversionParams = new XRCpuImage.ConversionParams(image, format, m_Transformation);
  89. // Texture2D allows us write directly to the raw texture data
  90. // This allows us to do the conversion in-place without making any copies.
  91. var rawTextureData = cameraTexture.GetRawTextureData<byte>();
  92. try
  93. {
  94. image.Convert(conversionParams, rawTextureData);
  95. }
  96. finally
  97. {
  98. // We must dispose of the XRCpuImage after we're finished
  99. // with it to avoid leaking native resources.
  100. image.Dispose();
  101. }
  102. // Apply the updated texture data to our texture
  103. cameraTexture.Apply();
  104. CaptureImage = cameraTexture;
  105. if (this.GetComponent<RawImage>() != null)
  106. {
  107. this.GetComponent<RawImage>().texture = CaptureImage;
  108. this.GetComponent<RawImage>().transform.localEulerAngles = new Vector3(0, 0, 180);
  109. }
  110. if (this.GetComponent<MeshRenderer>() != null)
  111. {
  112. this.GetComponent<MeshRenderer>().material.mainTexture = CaptureImage;
  113. this.GetComponent<MeshRenderer>().transform.localEulerAngles = new Vector3(0, 0, 180);
  114. }
  115. }
  116. public void Update()
  117. {
  118. }
  119. public void stopCamera()
  120. {
  121. if (RGBCamTexture != null && RGBCamTexture.isPlaying)
  122. {
  123. RGBCamTexture.Stop();
  124. }
  125. }
  126. }