I am using custom pass to render from a disabled camera. This works perfect for opaque and transparent geometry, but it doesn’t render UI elements. Any help with this would be appreciated!
using UnityEngine;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using System.Reflection;
#if UNITY_EDITOR
using UnityEditor.Rendering.HighDefinition;
[CustomPassDrawer(typeof(RenderFromCamera))]
class FPSForegroundEditor : CustomPassDrawer
{
protected override PassUIFlag commonPassUIFlags => PassUIFlag.Name;
}
#endif
class RenderFromCamera : CustomPass
{
public LayerMask foregroundMask;
public Camera UICamera;
public RenderTexture targetRenderTexture;
public Material overrideMaterial;
public ClearFlag clearFlag = ClearFlag.None;
public RenderQueueType renderQue = RenderQueueType.All;
public CompareFunction depthState = CompareFunction.NotEqual;
protected override bool executeInSceneView => false;
FieldInfo cullingResultField, hdCamField;
protected override void AggregateCullingParameters(ref ScriptableCullingParameters cullingParameters, HDCamera hdCamera)
=> cullingParameters.cullingMask |= (uint)foregroundMask.value;
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
cullingResultField = typeof(CustomPassContext).GetField(nameof(CustomPassContext.cullingResults));
}
protected override void Execute(CustomPassContext ctx)
{
if (ctx.hdCamera.camera.cameraType == CameraType.SceneView)
return;
UICamera.enabled = false;
var depthTestOverride = new RenderStateBlock(RenderStateMask.Depth)
{
depthState = new DepthState(true, depthState),
};
UICamera.TryGetCullingParameters(out var cullingParams);
cullingResultField.SetValueDirect(__makeref(ctx), ctx.renderContext.Cull(ref cullingParams));
CustomPassUtils.RenderFromCamera(ctx, UICamera, /*targetRenderTexture*/ctx.cameraColorBuffer, clearFlag, foregroundMask, renderQue, overrideRenderState: depthTestOverride);
}
protected override void Cleanup()
{
}
}