From 3b2fe58f4e17ab061d75c6606d30b77480b86e7d Mon Sep 17 00:00:00 2001 From: Niek Date: Tue, 17 Feb 2026 12:12:46 +0100 Subject: [PATCH] fix: Use RenderTexture instead of Texture2D --- Runtime/Scripts/CameraVideoSource.cs | 23 +++++------------------ Runtime/Scripts/RtcVideoSource.cs | 5 ++--- Runtime/Scripts/ScreenVideoSource.cs | 21 ++++----------------- Runtime/Scripts/TextureVideoSource.cs | 14 +++++++------- Runtime/Scripts/WebCameraSource.cs | 3 +-- 5 files changed, 19 insertions(+), 47 deletions(-) diff --git a/Runtime/Scripts/CameraVideoSource.cs b/Runtime/Scripts/CameraVideoSource.cs index 05dad0a8..07b7f282 100644 --- a/Runtime/Scripts/CameraVideoSource.cs +++ b/Runtime/Scripts/CameraVideoSource.cs @@ -33,7 +33,7 @@ protected override VideoRotation GetVideoRotation() return VideoRotation._0; } - public CameraVideoSource(Camera camera, VideoBufferType bufferType = VideoBufferType.Rgba) : base(VideoStreamSource.Screen, bufferType) + public CameraVideoSource(Camera camera, VideoBufferType bufferType = VideoBufferType.Rgba) : base(VideoStreamSource.Screen, bufferType) { Camera = camera; base.Init(); @@ -78,26 +78,14 @@ protected override bool ReadBuffer() _renderTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); Camera.targetTexture = _renderTexture as RenderTexture; _captureBuffer = new NativeArray(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent); - _previewTexture = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false); + _previewTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); textureChanged = true; } - ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture); -#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX - // Flip the texture for OSX - Graphics.CopyTexture(_renderTexture, _previewTexture); - var pixels = _previewTexture.GetPixels(); - var flippedPixels = new Color[pixels.Length]; - for (int i = 0; i < _previewTexture.height; i++) - { - Array.Copy(pixels, i * _previewTexture.width, flippedPixels, (_previewTexture.height - i - 1) * _previewTexture.width, _previewTexture.width); - } - _previewTexture.SetPixels(flippedPixels); -#else - Graphics.CopyTexture(_renderTexture, _previewTexture); -#endif + ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture); - AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _renderTexture, 0, _textureFormat, OnReadback); + Graphics.Blit(_renderTexture, _previewTexture, new Vector2(1f, -1f), new Vector2(0f, 1f)); + AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _previewTexture, 0, _textureFormat, OnReadback); } catch (Exception e) { @@ -107,4 +95,3 @@ protected override bool ReadBuffer() } } } - diff --git a/Runtime/Scripts/RtcVideoSource.cs b/Runtime/Scripts/RtcVideoSource.cs index fc9b7677..23274da6 100644 --- a/Runtime/Scripts/RtcVideoSource.cs +++ b/Runtime/Scripts/RtcVideoSource.cs @@ -26,11 +26,11 @@ public enum VideoStreamSource protected abstract VideoRotation GetVideoRotation(); - public delegate void TextureReceiveDelegate(Texture2D tex2d); + public delegate void TextureReceiveDelegate(RenderTexture tex2d); /// Called when we receive a new texture (first texture or the resolution changed) public event TextureReceiveDelegate TextureReceived; - protected Texture2D _previewTexture; + protected RenderTexture _previewTexture; protected NativeArray _captureBuffer; protected VideoStreamSource _sourceType; protected VideoBufferType _bufferType; @@ -220,4 +220,3 @@ protected virtual void Dispose(bool disposing) } } } - diff --git a/Runtime/Scripts/ScreenVideoSource.cs b/Runtime/Scripts/ScreenVideoSource.cs index 2e3321e0..0064d324 100644 --- a/Runtime/Scripts/ScreenVideoSource.cs +++ b/Runtime/Scripts/ScreenVideoSource.cs @@ -71,26 +71,14 @@ protected override bool ReadBuffer() _bufferType = GetVideoBufferType(_textureFormat); _renderTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); _captureBuffer = new NativeArray(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent); - _previewTexture = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false); + _previewTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); textureChanged = true; } - ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture); -#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX - // Flip the texture for OSX - Graphics.CopyTexture(_renderTexture, _previewTexture); - var pixels = _previewTexture.GetPixels(); - var flippedPixels = new Color[pixels.Length]; - for (int i = 0; i < _previewTexture.height; i++) - { - Array.Copy(pixels, i * _previewTexture.width, flippedPixels, (_previewTexture.height - i - 1) * _previewTexture.width, _previewTexture.width); - } - _previewTexture.SetPixels(flippedPixels); -#else - Graphics.CopyTexture(_renderTexture, _previewTexture); -#endif + ScreenCapture.CaptureScreenshotIntoRenderTexture(_renderTexture); - AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _renderTexture, 0, _textureFormat, OnReadback); + Graphics.Blit(_renderTexture, _previewTexture, new Vector2(1f, -1f), new Vector2(0f, 1f)); + AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _previewTexture, 0, _textureFormat, OnReadback); } catch (Exception e) { @@ -110,4 +98,3 @@ protected override bool SendFrame() } } } - diff --git a/Runtime/Scripts/TextureVideoSource.cs b/Runtime/Scripts/TextureVideoSource.cs index 5b140c86..5f1ec411 100644 --- a/Runtime/Scripts/TextureVideoSource.cs +++ b/Runtime/Scripts/TextureVideoSource.cs @@ -1,9 +1,8 @@ -using UnityEngine; using LiveKit.Proto; -using UnityEngine.Rendering; using Unity.Collections; +using UnityEngine; using UnityEngine.Experimental.Rendering; -using System; +using UnityEngine.Rendering; namespace LiveKit { @@ -47,18 +46,19 @@ protected override bool ReadBuffer() _reading = true; var textureChanged = false; - if (_previewTexture == null || _previewTexture.width != GetWidth() || _previewTexture.height != GetHeight()) { + if (_previewTexture == null || _previewTexture.width != GetWidth() || _previewTexture.height != GetHeight()) + { var compatibleFormat = SystemInfo.GetCompatibleFormat(Texture.graphicsFormat, FormatUsage.ReadPixels); _textureFormat = GraphicsFormatUtility.GetTextureFormat(compatibleFormat); _bufferType = GetVideoBufferType(_textureFormat); _captureBuffer = new NativeArray(GetWidth() * GetHeight() * GetStrideForBuffer(_bufferType), Allocator.Persistent); - _previewTexture = new Texture2D(GetWidth(), GetHeight(), _textureFormat, false); + _previewTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); textureChanged = true; } - Graphics.CopyTexture(Texture, _previewTexture); + + Graphics.Blit(Texture, _previewTexture, new Vector2(1f, -1f), new Vector2(0f, 1f)); AsyncGPUReadback.RequestIntoNativeArray(ref _captureBuffer, _previewTexture, 0, _textureFormat, OnReadback); return textureChanged; } } } - diff --git a/Runtime/Scripts/WebCameraSource.cs b/Runtime/Scripts/WebCameraSource.cs index efe2098a..89f4940c 100644 --- a/Runtime/Scripts/WebCameraSource.cs +++ b/Runtime/Scripts/WebCameraSource.cs @@ -71,7 +71,7 @@ protected override bool ReadBuffer() _bufferType = GetVideoBufferType(_textureFormat); _readBuffer = new Color32[width * height]; - _previewTexture = new Texture2D(width, height, TextureFormat.BGRA32, false); + _previewTexture = new RenderTexture(GetWidth(), GetHeight(), 0, compatibleFormat); _captureBuffer = new NativeArray(width * height * GetStrideForBuffer(_bufferType), Allocator.Persistent); if (CamTexture.graphicsFormat != _previewTexture.graphicsFormat) @@ -105,4 +105,3 @@ protected override bool ReadBuffer() } } } -