using System; using System.Collections; using System.Collections.Generic; using Unity.Collections.LowLevel.Unsafe; using UnityEngine; using UnityEngine.UI; using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems; public class ThinkRGB : MonoBehaviour { private ARCameraManager _cameraManager; private XRCpuImage _lastCpuImage; public bool RenderUsingYUVPlanes; private Texture2D _cameraTexture; // Start is called before the first frame update void Awake() { _cameraManager = FindObjectOfType(); _cameraManager.frameReceived += OnFrameReceived; } private void OnFrameReceived(ARCameraFrameEventArgs obj) { if (!_cameraManager.TryAcquireLatestCpuImage(out _lastCpuImage)) { Debug.Log("Failed to acquire latest cpu image."); return; } UpdateCameraTexture(_lastCpuImage); } private unsafe void UpdateCameraTexture(XRCpuImage image) { var format = TextureFormat.RGBA32; if (_cameraTexture == null || _cameraTexture.width != image.width || _cameraTexture.height != image.height) { _cameraTexture = new Texture2D(image.width, image.height, format, false); } var conversionParams = new XRCpuImage.ConversionParams(image, format); var rawTextureData = _cameraTexture.GetRawTextureData(); var rawTexturePtr = new IntPtr(rawTextureData.GetUnsafePtr()); try { image.Convert(conversionParams, new IntPtr(rawTextureData.GetUnsafePtr()), rawTextureData.Length); } finally { image.Dispose(); } _cameraTexture.Apply(); this.GetComponent().texture = _cameraTexture; } // Update is called once per frame void Update() { } }