1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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<ARCameraManager>();
- _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<byte>();
- var rawTexturePtr = new IntPtr(rawTextureData.GetUnsafePtr());
-
- try
- {
- image.Convert(conversionParams, new IntPtr(rawTextureData.GetUnsafePtr()), rawTextureData.Length);
- }
- finally
- {
- image.Dispose();
- }
- _cameraTexture.Apply();
- this.GetComponent<RawImage>().texture = _cameraTexture;
- }
- // Update is called once per frame
- void Update()
- {
-
- }
- }
|