Commit b2446d05 authored by Ylly's avatar Ylly
Browse files

Add ArCameraToImageUtil

parent d0549b37
No related merge requests found
Showing with 76 additions and 0 deletions
+76 -0
using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
namespace AvvyLand.Scripts.Utils.Debug
{
public class ArCameraToImageUtil : MonoBehaviour
{
[SerializeField]
private RawImage RawImage;
private ARCameraManager CameraManager;
private Texture2D RGB_Texture;
private void Update()
{
FindCameraManager();
}
private void FindCameraManager()
{
if (CameraManager != null) return;
CameraManager = GameObject.FindObjectOfType<ARCameraManager>();
CameraManager.frameReceived += (x) => CameraFrameReceived();
}
private unsafe void CameraFrameReceived()
{
if (!CameraManager.TryAcquireLatestCpuImage(out XRCpuImage image)) return;
var conversionParams = new XRCpuImage.ConversionParams
{
inputRect = new RectInt(0, 0, image.width, image.height),
outputDimensions = new Vector2Int(244, 244),
outputFormat = TextureFormat.RGBA32,
transformation = XRCpuImage.Transformation.MirrorY
};
int size = image.GetConvertedDataSize(conversionParams);
var buffer = new NativeArray<byte>(size, Allocator.Temp);
image.Convert(conversionParams, new IntPtr(buffer.GetUnsafePtr()), buffer.Length);
var x = conversionParams.outputDimensions.x;
var y = conversionParams.outputDimensions.y;
// if (RGB_Texture == null)
{
RGB_Texture = new Texture2D(x, y, conversionParams.outputFormat, false);
RGB_Texture.LoadRawTextureData(buffer);
RGB_Texture.Apply();
}
image.Dispose();
buffer.Dispose();
RawImage.texture = RGB_Texture;
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 68bb9db3ad2138d4fbf74f3ee6248bf1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment