(Unity 2021 LTS)
Hello,
In my game, I have some objects that need to render outlines under specific conditions, and need to be able to animate those outlines within their shaders for special effects. It does work, but when rotating the camera, it is obvious the outline is lagging and is not in sync with object rendering :
I’m using a Command Buffer during the “beginCameraRendering” step, rendering the needed objects in two Render Textures, one with the Normals in the rgb channels, and one for the Depth. The result is then Blit with an Outline shader similar to a post-process. The final texture is used within the objects shaders :
void InitializeOnce()
{
m_BaseCommands = new CommandBuffer { name = "AzurineBaseCommand" };
DepthRT = new RenderTexture(TextureSize.x, TextureSize.y, 16, RenderTextureFormat.Depth)
{
hideFlags = HideFlags.DontSave,
filterMode = FilterMode.Point,
name = "DepthRT"
};
NormalRT = new RenderTexture(TextureSize.x, TextureSize.y, 0, GraphicsFormat.R16G16B16A16_SFloat)
{
hideFlags = HideFlags.DontSave,
filterMode = FilterMode.Point,
name = "NormalRT"
};
OutlineRTH = RTHandles.Alloc(TextureSize.x, TextureSize.y, colorFormat: GraphicsFormat.R16_SFloat, dimension: TextureDimension.Tex2D, name: "OutlineRTH");
Shader.SetGlobalTexture("_outlineRT", OutlineRTH.rt, RenderTextureSubElement.Color);
OutlineMaterial.SetTexture("_DepthTex", DepthRT);
RenderPipelineManager.beginCameraRendering += BeginCameraRendering;
m_initialized = true;
}
void BeginCameraRendering(ScriptableRenderContext context, Camera camera)
{
m_BaseCommands.SetRenderTarget(NormalRT, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare, DepthRT, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.DontCare);
m_BaseCommands.ClearRenderTarget(true, true, Color.black);
foreach (AzurineRegisterOutline azs in Setters)
{
if (!azs.Azurine) // Include renderer only if powered
continue;
m_BaseCommands.DrawRenderer(azs.Rend, NormalMaterial);
}
m_BaseCommands.Blit(NormalRT, OutlineRTH, OutlineMaterial);
context.ExecuteCommandBuffer(m_BaseCommands);
context.Submit();
m_BaseCommands.Clear();
}
The final texture is handled in the shaders like this:
The frame debugger shows that the command buffer happens before the camera rendering, so it should be OK:
Does someone knows why the effect is lagging? Is there a better way to achieve the effect?
Thanks!




