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 fd703c8 commit 837f7a2
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 5 deletions.
136 changes: 135 additions & 1 deletion unity/Assets/Scripts/CapturePass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static void SetupCameraWithPostShader(

public string shaderName;

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

public Material material;
Expand Down Expand Up @@ -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;


}
}

}


Expand Down
18 changes: 14 additions & 4 deletions unity/Assets/Scripts/RenderingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -210,13 +215,12 @@ void Awake() {
// this.enabled = true;
}

public RenderToTexture GetCapturePass(string passName) {
public T GetCapturePass<T>(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) {
Expand Down Expand Up @@ -273,6 +277,12 @@ string key
);
}

public byte[] getDistortionMapBytes() {
// return this.GetCapturePass<OnDemandCapture>("_distortion_map").GetBytes();
return this.GetCapturePass<OnDemandCapture>("_distortion_map").GetBytes();
}


// Update is called once per frame
void Update() { }
}

0 comments on commit 837f7a2

Please sign in to comment.