XRRGBCamera.cs 4.5 KB

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