XRRGBCamera.cs 4.5 KB

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