Render object per camera in URP
I need objects to only render to specific cameras. There is a potentially infinite number of renderers and cameras, so using layer masks isn't suitable.
I'd solved this problem in the past using OnPreCull and OnPostRender: http://answers.unity.com/answers/782248/view.html
In URP we have beginCameraRendering and endCameraRendering events, but it seems the begin event fires for every camera, then the end event fires for every camera, meaning there is no way to do anything on a per camera basis.
Any help would be great.
Answer by thehen2 · Jun 01, 2020 at 11:05 AM
I made a mistake, the events do fire consecutively per camera.
void OnEnable()
{
RenderPipelineManager.beginCameraRendering += RenderPipelineManager_beginCameraRendering;
RenderPipelineManager.endCameraRendering += RenderPipelineManager_endCameraRendering;
}
void OnDisable()
{
RenderPipelineManager.beginCameraRendering -= RenderPipelineManager_beginCameraRendering;
RenderPipelineManager.endCameraRendering -= RenderPipelineManager_endCameraRendering;
}
private void RenderPipelineManager_beginCameraRendering(ScriptableRenderContext context, Camera camera)
{
if (camera == paperCamera) {
foreach (var thisSceneOnlyGameObject in thisSceneOnlyGameObjects)
{
thisSceneOnlyGameObject.layer = LayerMask.NameToLayer("Default");
}
}
}
private void RenderPipelineManager_endCameraRendering(ScriptableRenderContext context, Camera camera)
{
if (camera == paperCamera)
{
foreach (var thisSceneOnlyGameObject in thisSceneOnlyGameObjects)
{
thisSceneOnlyGameObject.layer = LayerMask.NameToLayer("Invisible");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612235754im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera doesn't output right RenderTexture in URP 0 Answers
URP Don clear flag 0 Answers
URP Camera stack with multiple renderers : nothing showing 0 Answers
(URP) Problem with post-processing with overlay camera. 0 Answers
How can I change the viewport rect of two active cameras at runtime? - Splitscreen Multiplayer 1 Answer