From 837f7a22d4917aebd7a7fa4923c5d30f12d7f9c4 Mon Sep 17 00:00:00 2001 From: AlvaroHG Date: Fri, 22 Nov 2024 14:03:05 -0800 Subject: [PATCH] Reverts rendering manager and capture pass to tested commit ef9e76e --- unity/Assets/Scripts/CapturePass.cs | 136 ++++++++++++++++++++++- unity/Assets/Scripts/RenderingManager.cs | 18 ++- 2 files changed, 149 insertions(+), 5 deletions(-) diff --git a/unity/Assets/Scripts/CapturePass.cs b/unity/Assets/Scripts/CapturePass.cs index 7553602b71..97cc483a28 100644 --- a/unity/Assets/Scripts/CapturePass.cs +++ b/unity/Assets/Scripts/CapturePass.cs @@ -103,7 +103,7 @@ public static void SetupCameraWithPostShader( public string shaderName; - private Texture2D tex; + protected Texture2D tex; private RenderTexture renderTexture; public Material material; @@ -559,6 +559,140 @@ public void AddUpdateCapturePass(RenderToTexture pass) { // } + // Does not attatch to camera with command buffers to render every frame, only on demand + public class OnDemandCapture : RenderToTexture { + public OnDemandCapture(CaptureConfig config) : base(config, null) { + } + + public override void OnInitialize(Camera mainCamera) { + base.OnInitialize(mainCamera); + + // this.camera.enabled = false; + + } + + public override void OnCameraChange(Camera mainCamera) { + if (this.camera == null) { + this.camera = ReplacementShaderCapture.CreateHiddenCamera(mainCamera.transform, this.name); + + } + base.OnCameraChange(mainCamera); + this.camera.renderingPath = RenderingPath.Forward; + // camera renders nothing, unity is silly and dosn't allow to not render anything any other way?? + camera.cullingMask = 0; + camera.clearFlags = CameraClearFlags.SolidColor; + camera.targetTexture = this.GetRenderTexture(); + // do not render at update + this.camera.enabled = false; + // this.camera.SetReplacementShader(this.shader, ""); + } + + // public override void OnCameraChange(Camera mainCamera) { + // // this.camera.RemoveAllCommandBuffers(); + // // if (tex != null) { + // // UnityEngine.Object.Destroy(tex); + // // tex = null; + // // } + // // this.renderTexture = CreateRenderTexture(Screen.width, Screen.height); + // // this.camera.targetDisplay = this.toDisplayId.GetValueOrDefault(); + + // // // If set to render to display don't set render texture because display buffer is only written to if targetTexture is null + // // if (!this.toDisplayId.HasValue) { + // // this.camera.targetTexture = this.renderTexture; + // // } + + // // if (cb != null) { + // // cb.Clear(); + // // } + // // else { + // // cb = new CommandBuffer(); + // // } + + // // this.AddToCommandBuffer(cb); + + // // // this.camera.enabled = false; + + // base.OnCameraChange(mainCamera); + + // } + + public override void AddToCommandBuffer(CommandBuffer commandBuffer) { + + + // commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, this.GetRenderTexture(), this.material); + + commandBuffer.Blit(null, BuiltinRenderTextureType.CameraTarget, this.material); + + // If data is on a display buffer copy to render texture, just for debugging should not matter + if (!this.toDisplayId.HasValue || cloudRendering) { + commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, this.GetRenderTexture(), this.material); + } + + this.camera.AddCommandBuffer(CameraEvent.BeforeImageEffects, commandBuffer); + this.camera.depthTextureMode = DepthTextureMode.Depth; + + } + + public override byte[] GetBytes(bool jpeg = false) { + var renderTexture = this.GetRenderTexture(); + var prevActiveRT = RenderTexture.active; + RenderTexture.active = renderTexture; + + // this.camera.RenderWithShader(this.shader, ""); + this.camera.Render(); + // var bytes = base.GetBytes(jpeg); + + // float startTime = Time.realtimeSinceStartup; + + tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); + + + // startTime = Time.realtimeSinceStartup; + + // encode texture into PNG/JPG + byte[] bytes; + if (jpeg) { + bytes = tex.EncodeToJPG(); + } else { + bytes = tex.GetRawTextureData(); + } + + + RenderTexture.active = prevActiveRT; + return bytes; + + } + + public Color32[] GetColors() { + var renderTexture = this.GetRenderTexture(); + var prevActiveRT = RenderTexture.active; + RenderTexture.active = renderTexture; + + // this.camera.RenderWithShader(this.shader, ""); + this.camera.Render(); + // var bytes = base.GetBytes(jpeg); + + // float startTime = Time.realtimeSinceStartup; + + tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); + + + // startTime = Time.realtimeSinceStartup; + + // encode texture into PNG/JPG + Color32[] bytes; + + bytes = tex.GetPixels32(); + + + + RenderTexture.active = prevActiveRT; + return bytes; + + + } + } + } diff --git a/unity/Assets/Scripts/RenderingManager.cs b/unity/Assets/Scripts/RenderingManager.cs index 368b356178..8c4be8f381 100644 --- a/unity/Assets/Scripts/RenderingManager.cs +++ b/unity/Assets/Scripts/RenderingManager.cs @@ -32,7 +32,12 @@ public class RenderingManager : MonoBehaviour { private Texture2D readTex; - private static bool isMainCameraPassCreated = false; + private static bool isMainCameraPassCreated = false; + + public RenderToTexture distortionMap { + get; + private set; + } void Initialize(Camera camera) { @@ -210,13 +215,12 @@ void Awake() { // this.enabled = true; } - public RenderToTexture GetCapturePass(string passName) { + public T GetCapturePass(string passName) where T : ICapturePass { ICapturePass pass; if (!this.activePasses.TryGetValue(passName, out pass)) { Debug.LogError($"No active pass at GetPassRenderTexture {passName}"); - return null; } - return pass as RenderToTexture; + return (T)pass; } public RenderTexture GetPassRenderTexture(string passName) { @@ -273,6 +277,12 @@ string key ); } + public byte[] getDistortionMapBytes() { + // return this.GetCapturePass("_distortion_map").GetBytes(); + return this.GetCapturePass("_distortion_map").GetBytes(); + } + + // Update is called once per frame void Update() { } } \ No newline at end of file