2026-06-01 00:23:11 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using RenderStreaming;
|
|
|
|
|
|
using Stary.Evo;
|
2026-06-03 22:05:03 +08:00
|
|
|
|
using Unity.RenderStreaming;
|
2026-06-01 00:23:11 +08:00
|
|
|
|
using Unity.XR.XREAL;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Script
|
|
|
|
|
|
{
|
|
|
|
|
|
public class YUVToRenderTexture : MonoBehaviour, IController
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 本地视频显示
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public RenderTexture localRenderTexture;
|
|
|
|
|
|
|
|
|
|
|
|
private Material localVideoMaterial;
|
|
|
|
|
|
private XREALRGBCameraTexture m_RGBCameraTexture;
|
|
|
|
|
|
//private RawImage _localVideoImage;
|
|
|
|
|
|
|
|
|
|
|
|
//private RawImage _textureImage;
|
2026-06-03 22:05:03 +08:00
|
|
|
|
private VideoStreamSender videoStreamSender;
|
2026-06-01 00:23:11 +08:00
|
|
|
|
private void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
//_localVideoImage= GameObject.Find("CanvasMain/RawImage").GetComponent<RawImage>();
|
|
|
|
|
|
//_textureImage= GameObject.Find("CanvasMain/RawImage1").GetComponent<RawImage>();
|
|
|
|
|
|
localVideoMaterial = Resources.Load<Material>("LocalRenderMaterial");
|
|
|
|
|
|
m_RGBCameraTexture = XREALRGBCameraTexture.CreateSingleton();
|
2026-06-03 22:05:03 +08:00
|
|
|
|
videoStreamSender=this.GetComponent<VideoStreamSender>();
|
2026-06-01 00:23:11 +08:00
|
|
|
|
Play();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Play()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!m_RGBCameraTexture.IsCapturing)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.Log($"[RGBCamera] Play");
|
|
|
|
|
|
m_RGBCameraTexture.StartCapture();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
|
{
|
|
|
|
|
|
var yuvTextures = m_RGBCameraTexture.GetYUVFormatTextures();
|
|
|
|
|
|
if (yuvTextures[0] != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ConvertYUVToRenderTexture(yuvTextures);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ConvertYUVToRenderTexture(Texture2D[] yuvTextures)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (yuvTextures == null || yuvTextures.Length < 3)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// 获取Y纹理的尺寸作为目标RenderTexture的尺寸
|
2026-06-03 22:05:03 +08:00
|
|
|
|
var width = videoStreamSender.width;
|
|
|
|
|
|
var height = videoStreamSender.height;
|
2026-06-01 00:23:11 +08:00
|
|
|
|
// if (_localVideoImage==null || _localVideoImage.rectTransform.sizeDelta.x != width ||
|
|
|
|
|
|
// _localVideoImage.rectTransform.sizeDelta.y != height)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// _localVideoImage.rectTransform.sizeDelta = new Vector2(width, height);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// 如果RenderTexture不存在或尺寸不匹配,创建新的
|
|
|
|
|
|
if (localRenderTexture == null || localRenderTexture.width != width ||
|
|
|
|
|
|
localRenderTexture.height != height)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 释放旧的RenderTexture
|
|
|
|
|
|
if (localRenderTexture != null)
|
|
|
|
|
|
localRenderTexture.Release();
|
|
|
|
|
|
|
|
|
|
|
|
// 创建新的RenderTexture
|
2026-06-03 22:05:03 +08:00
|
|
|
|
localRenderTexture = new RenderTexture((int)width, (int)height, 0, RenderTextureFormat.ARGB32);
|
2026-06-01 00:23:11 +08:00
|
|
|
|
localRenderTexture.enableRandomWrite = true;
|
|
|
|
|
|
localRenderTexture.Create();
|
|
|
|
|
|
//_textureImage.texture = localRenderTexture;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置YUV纹理到material
|
|
|
|
|
|
//_localVideoImage.texture = yuvTextures[0];
|
|
|
|
|
|
localVideoMaterial.SetTexture("_MainTex", yuvTextures[0]);
|
|
|
|
|
|
localVideoMaterial.SetTexture("_UTex", yuvTextures[1]);
|
|
|
|
|
|
localVideoMaterial.SetTexture("_VTex", yuvTextures[2]);
|
|
|
|
|
|
|
|
|
|
|
|
Graphics.Blit(null, localRenderTexture, localVideoMaterial);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (m_RGBCameraTexture.IsCapturing)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (localRenderTexture != null)
|
|
|
|
|
|
localRenderTexture.Release();
|
|
|
|
|
|
Debug.Log($"[RGBCamera] Stop");
|
|
|
|
|
|
m_RGBCameraTexture.StopCapture();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IArchitecture GetArchitecture()
|
|
|
|
|
|
{
|
|
|
|
|
|
return MainArchitecture.Interface;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|