I’m trying to use the custom pass system to support a sonar simulation I’m working on. To capture the data I need, I implemented a custom vert/frag shader that should run on all objects, but only for a specific camera. I created this script, using a custom pass with a DrawRenderersCustomPass shader override. However, when the clear flags are not set to “Everything”, I get z-fighting and unwanted skybox rendering, as the objects seem to render twice. Once with their default shaders, and once with the override shader. Is this intended? I can solve the problem by setting the clear flag, but I imagine this decreases performance, since it’s gonna be rendering the same objects twice. I would appreciate some clarification! The desired behavior is for the objects to only be rendered once from the sonar camera, using only the sonar replacement shader.
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
public class SonarReplacement : MonoBehaviour
{
private CustomPassVolume passVolume;
private DrawRenderersCustomPass pass;
void Start()
{
passVolume = gameObject.AddComponent<CustomPassVolume>();
passVolume.isGlobal = false;
passVolume.targetCamera = GetComponent<Camera>();
passVolume.injectionPoint = CustomPassInjectionPoint.BeforeTransparent;
pass = new DrawRenderersCustomPass();
pass.overrideMode = DrawRenderersCustomPass.OverrideMaterialMode.Shader;
pass.overrideShader = Shader.Find("Unlit/SonarReplace");
pass.clearFlags = ClearFlag.All;
passVolume.customPasses.Add(pass);
}
void Update()
{
}
}
Without clear flags:
With clear flags (intended result):