Skip to content

Commit eecf922

Browse files
committed
Instead of displaying Texture2Ds, present the RenderTexture directly to the window:
- Previously, we copied the texture from the GPU into a Texture2D to display it. - Now, we instantiate as many different RenderTexture as necessary and display those instead.
1 parent 184c471 commit eecf922

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

Packages/dev.hai-vr.lightbox-viewer/Scripts/Editor/LightboxViewerGenerator.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class LightboxViewerGenerator
1919
private GameObject _animatedRoot;
2020
private Camera _camera;
2121
private Material _material;
22+
private bool _needsCounterRoll;
2223

2324
public void Begin(GameObject animatedRoot, float customRoll, bool counterRotate, Camera cameraOptional, bool usePostProcessing)
2425
{
@@ -29,6 +30,7 @@ public void Begin(GameObject animatedRoot, float customRoll, bool counterRotate,
2930
_material = new Material(Shader.Find("Hai/LightboxViewerCounterRoll"));
3031
_material.SetFloat("_CounterRoll", -customRoll / 180f);
3132
}
33+
_needsCounterRoll = isCustomRollSlanted && counterRotate;
3234

3335
Profiler.BeginSample("LightboxViewer.Generator.Begin.AddCameraComponent");
3436
_camera = cameraOptional != null ? Object.Instantiate(cameraOptional) : new GameObject().AddComponent<Camera>();
@@ -65,7 +67,7 @@ public void Terminate()
6567
Object.DestroyImmediate(_camera.gameObject);
6668
}
6769

68-
public void RenderNoAnimator(Texture2D element, GameObject currentLightbox, RenderTexture renderTexture, Vector3 referentialVector, Quaternion referentialQuaternion, float verticalDisplacement)
70+
public void RenderNoAnimator(Texture element, GameObject currentLightbox, RenderTexture renderTexture, Vector3 referentialVector, Quaternion referentialQuaternion, float verticalDisplacement)
6971
{
7072
var rootTransform = _animatedRoot.transform;
7173
var camTransform = _camera.transform;
@@ -83,16 +85,34 @@ public void RenderNoAnimator(Texture2D element, GameObject currentLightbox, Rend
8385
camTransform.rotation = currentLightbox.transform.rotation * referentialQuaternion * camTransform.rotation;
8486
rootTransform.rotation = currentLightbox.transform.rotation * referentialQuaternion * rootTransform.rotation;
8587

86-
renderTexture.wrapMode = TextureWrapMode.Clamp;
87-
88-
RenderCamera(renderTexture, _camera);
89-
if (SystemInfo.supportsAsyncGPUReadback && !DoNotUseAsyncReadback)
88+
if (element is RenderTexture rt)
9089
{
91-
AsyncRenderTextureTo(renderTexture, element);
90+
RenderCamera(rt, _camera);
91+
if (_needsCounterRoll && _material != null)
92+
{
93+
var diff = RenderTexture.GetTemporary(rt.width, rt.height, 24);
94+
Graphics.Blit(rt, diff);
95+
96+
_material.SetTexture("_MainTex", diff);
97+
var ratio = rt.width / (float)rt.height;
98+
_material.SetFloat("_Ratio", ratio);
99+
Graphics.Blit(diff, rt, _material);
100+
101+
RenderTexture.ReleaseTemporary(diff);
102+
}
92103
}
93-
else
104+
else if (false)
94105
{
95-
SyncRenderTextureTo(renderTexture, element);
106+
renderTexture.wrapMode = TextureWrapMode.Clamp;
107+
RenderCamera(renderTexture, _camera);
108+
if (SystemInfo.supportsAsyncGPUReadback && !DoNotUseAsyncReadback)
109+
{
110+
AsyncRenderTextureTo(renderTexture, element as Texture2D);
111+
}
112+
else
113+
{
114+
SyncRenderTextureTo(renderTexture, element as Texture2D);
115+
}
96116
}
97117
}
98118
finally

Packages/dev.hai-vr.lightbox-viewer/Scripts/Editor/LightboxViewerRenderQueue.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public class LightboxViewerRenderQueue
1919

2020
internal const bool WhenInEditMode_DoNotDisableMainAvatar = true;
2121

22-
private readonly Dictionary<int, Texture2D> _lightboxIndexToTexture;
22+
private readonly Dictionary<int, Texture> _lightboxIndexToTexture;
2323
private readonly Queue<int> _queue;
2424
private int _queueSize;
2525
private Scene _openScene;
2626
private bool _sceneLoaded;
27-
private Texture2D[] _textures = new Texture2D[1];
27+
private Texture[] _textures = new Texture[1];
2828
private string[] _names = new string[1];
2929
private int _width = 512;
3030
private int _height = 512;
@@ -71,11 +71,11 @@ public void EnableDepthTexture(bool enableDepthTexture, GameObject depthEnabler)
7171

7272
public LightboxViewerRenderQueue()
7373
{
74-
_lightboxIndexToTexture = new Dictionary<int, Texture2D>();
74+
_lightboxIndexToTexture = new Dictionary<int, Texture>();
7575
_queue = new Queue<int>();
7676
}
7777

78-
private Texture2D RequireRender(int lightboxIndex, int width, int height)
78+
private Texture RequireRender(int lightboxIndex, int width, int height)
7979
{
8080
if (_lightboxIndexToTexture.ContainsKey(lightboxIndex)
8181
&& _lightboxIndexToTexture[lightboxIndex] != null // Can happen when the texture is destroyed (Unity invalid object)
@@ -89,7 +89,11 @@ private Texture2D RequireRender(int lightboxIndex, int width, int height)
8989
return _lightboxIndexToTexture[lightboxIndex];
9090
}
9191

92-
var texture = new Texture2D(width, height, TextureFormat.RGB24, false);
92+
if (_lightboxIndexToTexture.ContainsKey(lightboxIndex))
93+
{
94+
Object.DestroyImmediate(_lightboxIndexToTexture[lightboxIndex]);
95+
}
96+
var texture = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32);
9397
_lightboxIndexToTexture[lightboxIndex] = texture; // TODO: Dimensions
9498

9599
_queue.Enqueue(lightboxIndex);
@@ -423,7 +427,7 @@ private void TrueRender(GameObject copy)
423427
}
424428

425429
var itemCount = 0;
426-
var renderTexture = RenderTexture.GetTemporary(_lightboxIndexToTexture[0].width, _lightboxIndexToTexture[0].height, 24);
430+
RenderTexture renderTexture = null;
427431
var allApplicableLightboxes = AllApplicableLightboxes();
428432
while (_queue.Count > 0 && itemCount < _queueSize)
429433
{
@@ -438,7 +442,6 @@ private void TrueRender(GameObject copy)
438442
itemCount++;
439443
}
440444
}
441-
RenderTexture.ReleaseTemporary(renderTexture);
442445
}
443446
finally
444447
{
@@ -501,7 +504,7 @@ public void ForceRequireRenderAll()
501504
var lightboxes = AllApplicableLightboxes();
502505
if (_textures.Length != lightboxes.Length)
503506
{
504-
_textures = new Texture2D[lightboxes.Length];
507+
_textures = new Texture[lightboxes.Length];
505508
_names = new string[lightboxes.Length];
506509
}
507510
for (var i = 0; i < lightboxes.Length; i++)

0 commit comments

Comments
 (0)