Skip to content

Commit

Permalink
Reverts rendering manager and capture pass to tested commit ef9e76e
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroHG committed Nov 22, 2024
1 parent fa04b3a commit fd703c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 262 deletions.
201 changes: 13 additions & 188 deletions unity/Assets/Scripts/CapturePass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using System.Security.Cryptography;
using System.Data.SqlTypes;

// // @TODO:
// // . support custom color wheels in optical flow via lookup textures
Expand Down Expand Up @@ -59,9 +58,7 @@ public class CaptureConfig {

public bool cloudRendering;

public RenderTextureFormat renderTextureFormat = RenderTextureFormat.ARGB32;

public int depthBits = 0;

}

public interface ICapturePass {
Expand Down Expand Up @@ -106,8 +103,8 @@ public static void SetupCameraWithPostShader(

public string shaderName;

protected Texture2D tex;
protected RenderTexture renderTexture;
private Texture2D tex;
private RenderTexture renderTexture;

public Material material;
protected Shader shader;
Expand All @@ -121,9 +118,7 @@ public static void SetupCameraWithPostShader(

private TextureFormat readTextureFormat;

private RenderTextureFormat renderTextureFormat;

private int depthBits;


// private Texture2D readTexture;

Expand All @@ -138,8 +133,6 @@ public RenderToTexture(CaptureConfig config, Camera camera) {
this.name = config.name;
this.cloudRendering = config.cloudRendering;
this.toDisplayId = config.toDisplay;
this.renderTextureFormat = config.renderTextureFormat;
this.depthBits = config.depthBits;

// TODO. if config.toDisplay is present then render to display buffer and copy to render texture
// for debugging purposes
Expand Down Expand Up @@ -198,7 +191,7 @@ public virtual void AddToCommandBuffer(CommandBuffer commandBuffer) {
}


protected RenderTexture CreateRenderTexture(int width, int height) {
private RenderTexture CreateRenderTexture(int width, int height) {

// for cloud rendering GraphicsFormat.R8G8B8A8_UNorm

Expand All @@ -219,26 +212,12 @@ protected RenderTexture CreateRenderTexture(int width, int height) {

RenderTexture rt = null;


if (cloudRendering) {

GraphicsFormat cloudRenderingRTFormat;
if (this.renderTextureFormat == RenderTextureFormat.RGFloat) {
readTextureFormat = TextureFormat.RGFloat;
cloudRenderingRTFormat = GraphicsFormat.R32G32_SFloat;
}
else if (this.renderTextureFormat == RenderTextureFormat.RFloat) {
readTextureFormat = TextureFormat.RFloat;
cloudRenderingRTFormat = GraphicsFormat.R32_SFloat;
}
else {
readTextureFormat = TextureFormat.RGBA32;
cloudRenderingRTFormat = GraphicsFormat.R8G8B8A8_UNorm;
}

// Why 0 for depth here ?
rt = new RenderTexture(Screen.width, Screen.height, 0, cloudRenderingRTFormat);
rt = new RenderTexture(Screen.width, Screen.height, 0, GraphicsFormat.R8G8B8A8_UNorm);
// TODO: if 0 then RGB24? if not RGB32?

readTextureFormat = TextureFormat.RGBA32;

// RenderTexture rt = new RenderTexture(
// width: width,
Expand All @@ -248,22 +227,8 @@ protected RenderTexture CreateRenderTexture(int width, int height) {
// );
}
else {
// Todo string matching from enums, only two supported
if (this.renderTextureFormat == RenderTextureFormat.RGFloat) {
readTextureFormat = TextureFormat.RGFloat;
}
else if (this.renderTextureFormat == RenderTextureFormat.RFloat) {
readTextureFormat = TextureFormat.RFloat;
}
else {
readTextureFormat = TextureFormat.RGBA32;
this.renderTextureFormat = RenderTextureFormat.ARGB32;
}

rt = new RenderTexture(Screen.width, Screen.height, 24, this.renderTextureFormat, RenderTextureReadWrite.Default);
// readTextureFormat = TextureFormat.RGBA32;

// RenderTextureFormat.RGFloat
rt = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
readTextureFormat = TextureFormat.RGBA32;
}

if (this.tex == null) {
Expand Down Expand Up @@ -360,7 +325,7 @@ public virtual void OnCameraChange(Camera mainCamera) {
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 || cloudRendering) {
if (!this.toDisplayId.HasValue) {
this.camera.targetTexture = this.renderTexture;
}

Expand Down Expand Up @@ -442,7 +407,6 @@ public virtual byte[] GetBytes(
} else {
bytes = tex.GetRawTextureData();
}

RenderTexture.active = prevActiveRT;
return bytes;
}
Expand Down Expand Up @@ -486,7 +450,6 @@ public static Camera CreateHiddenCamera(Transform parent, string name) {

var newCamera = go.GetComponent<Camera>();
newCamera.cullingMask = 1; // render everything, including PlaceableSurfaces

return newCamera;
}

Expand All @@ -500,7 +463,6 @@ public override void AddToCommandBuffer(CommandBuffer commandBuffer) {
this.camera.SetReplacementShader(shader, "");
this.camera.backgroundColor = Color.blue;
this.camera.clearFlags = CameraClearFlags.SolidColor;
// TODO make optional for editor debugging
this.camera.targetTexture = this.GetRenderTexture();
}

Expand Down Expand Up @@ -537,7 +499,7 @@ public override void AddToCommandBuffer(CommandBuffer commandBuffer) {
// Debug.Log($"----------- Blit for multipass");

// If rendering to display
if (this.toDisplayId.HasValue && !cloudRendering) {
if (this.toDisplayId.HasValue) {
// if it's not cloudrendering camera.targetTexture is null which means it's rendering to the display buffer
// so then we need to copy the display buffer into render texture

Expand All @@ -549,11 +511,7 @@ public override void AddToCommandBuffer(CommandBuffer commandBuffer) {

// commandBuffer.ReleaseTemporaryRT(screenCopyID);
foreach (var pass in this.passDict.Values) {
if (pass.GetName() == "_depth") {
int depthID = Shader.PropertyToID("_DepthCopyTexture");
}
commandBuffer.Blit(this.GetRenderTexture(), pass.GetRenderTexture(), pass.material);

commandBuffer.Blit(BuiltinRenderTextureType.CameraTarget, pass.GetRenderTexture(), pass.material);
}


Expand Down Expand Up @@ -593,138 +551,6 @@ 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;

}

// public class DistortionCapture : RenderToTexture {
// public DistortionCapture(CaptureConfig config, Camera camera) : base(config, camera) {
Expand All @@ -733,7 +559,6 @@ public Color32[] GetColors() {

// }

}
}


Expand Down
Loading

0 comments on commit fd703c8

Please sign in to comment.