To create an outline effect in Unity's Universal Render Pipeline (URP) that works through occluding objects (similar to Unity Editor's outline), the best approach is to use a post-processing-based solution. This method leverages URP's post-processing stack and stencil buffers to achieve the desired effect.
Here’s a step-by-step guide:
1. Use a Post-Processing-Based Outline Effect
The most efficient way to achieve this is by using a custom render feature or leveraging a Stencil Buffer to isolate the object and apply an outline post-process effect.
Step-by-Step Solution
Step 1: Set Up URP
Ensure your project is using URP:
- Go to
Edit > Project Settings > Graphics.
- Set the Scriptable Render Pipeline Asset to your URP asset.
- If you don’t have a URP asset, create one (
Assets > Create > Rendering > Universal Render Pipeline > Pipeline Asset).
Step 2: Add a Custom Stencil Shader
Create a shader that writes to the stencil buffer for the object you want to outline.
Shader Code (StencilShader.shader):
Shader "Custom/StencilOutline" {
SubShader {
Tags { "RenderType" = "Opaque" "Queue" = "Geometry+1" }
LOD 200
Pass {
Name "StencilWrite"
Tags { "LightMode" = "UniversalForward" }
ColorMask 0 // Don't write to color buffer
ZWrite Off // Don't write to depth buffer
Stencil {
Ref 1
Comp Always
Pass Replace
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = TransformObjectToHClip(v.vertex.xyz);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(0, 0, 0, 0); // No color output
}
ENDHLSL
}
}
}
Step 3: Create a Material with the Stencil Shader
- Create a new material (
Assets > Create > Material).
- Assign the
StencilShader to the material.
- Apply this material to the GameObject(s) you want to outline.
Step 4: Add a Post-Processing Outline Effect
Use a Post-Processing Volume to apply the outline effect.
-
Install Post-Processing Stack:
- Ensure you have the Post-Processing package installed via the Package Manager (
Window > Package Manager).
-
Create a Post-Processing Volume:
- Right-click in the Hierarchy →
Volume > Global Volume.
- Add a new profile to the volume.
-
Add an Outline Effect:
- In the Volume Profile, click
Add Override and search for Edge Detection or Outline.
- Configure the edge detection settings (e.g., thickness, color, sensitivity).
Step 5: Modify the Renderer Feature
If the built-in edge detection doesn’t work as expected, you can create a custom Renderer Feature to handle the outline rendering.
Steps to Create a Custom Renderer Feature:
-
Create a new script (OutlineRendererFeature.cs):
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class OutlineRendererFeature : ScriptableRendererFeature {
[System.Serializable]
public class OutlineSettings {
public Material outlineMaterial;
public int outlineThickness = 1;
}
public OutlineSettings settings = new OutlineSettings();
private OutlinePass outlinePass;
public override void Create() {
outlinePass = new OutlinePass(settings);
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) {
if (settings.outlineMaterial != null) {
renderer.EnqueuePass(outlinePass);
}
}
}
public class OutlinePass : ScriptableRenderPass {
private OutlineRendererFeature.OutlineSettings settings;
public OutlinePass(OutlineRendererFeature.OutlineSettings settings) {
this.settings = settings;
renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
CommandBuffer cmd = CommandBufferPool.Get("OutlinePass");
// Draw outlines using the stencil buffer
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
}
-
Add the OutlineRendererFeature to your URP asset:
- Open your URP asset (
Assets > YourPipelineAsset).
- Under Renderer Features, add a new feature and select
OutlineRendererFeature.
-
Assign the stencil material to the outlineMaterial field in the inspector.
Step 6: Test the Outline
- Select the GameObject you want to outline.
- Apply the stencil material.
- Run the scene and verify that the outline appears through occluding objects.
Why This Works
- Stencil Buffer: The stencil shader isolates the object by writing a unique value to the stencil buffer.
- Post-Processing: The post-processing effect reads the stencil buffer and applies the outline effect only to the isolated object.
- URP Compatibility: Using URP’s post-processing stack ensures the effect integrates seamlessly with the pipeline.
Advantages of This Approach
- Performance: Post-processing effects are optimized for performance.
- Flexibility: You can customize the outline thickness, color, and style.
- Scalability: Works with multiple objects and complex scenes.
Limitations
- Stencil Buffer Usage: Requires careful management of stencil values if you’re using them elsewhere in your scene.
- Post-Processing Dependency: Requires the Post-Processing package to be installed and configured.
This method provides a robust and scalable way to achieve Unity Editor-like outlines in URP while maintaining compatibility and performance.