Unity Shaders and Rendering
Overview
Reference for Unity's rendering systems, shader development, lighting configuration, and visual effects. Covers all three render pipelines, Shader Graph, hand-written shaders, and VFX Graph.
Render Pipeline Comparison
| Feature |
Built-in RP |
URP |
HDRP |
| Target |
Legacy projects |
Mobile, VR, wide range |
High-end PC/console |
| Shader language |
Surface shaders + HLSL |
HLSL (no surface shaders) |
HLSL |
| Shader Graph |
Yes |
Yes |
Yes |
| SRP Batcher |
No |
Yes |
Yes |
| Render Features |
No |
Yes (ScriptableRendererFeature) |
Custom Pass |
| Post-processing |
Post Processing Stack v2 |
Volume system (built-in) |
Volume system (built-in) |
| Ray tracing |
No |
No (probe-based) |
Yes (DXR) |
| Performance |
Moderate |
Optimized for scale |
Highest fidelity |
Recommendation: Use URP for new projects unless targeting high-end visuals exclusively (HDRP). Built-in RP is legacy -- migrate when possible.
Shader Graph
Getting Started
- Right-click in Project: Create > Shader Graph > URP > Lit Shader Graph
- Double-click to open Shader Graph editor
- Build node network connecting to Master Stack outputs
- Create a Material using the shader, assign to renderers
Master Stack Outputs (URP Lit)
| Output |
Type |
Purpose |
| Base Color |
Color (RGB) |
Albedo/diffuse color |
| Normal |
Vector3 |
Tangent-space normal map |
| Metallic |
Float (0-1) |
Metal vs. dielectric |
| Smoothness |
Float (0-1) |
Roughness inverse |
| Emission |
Color (RGB) |
Self-illumination |
| Alpha |
Float (0-1) |
Transparency |
| Alpha Clip Threshold |
Float |
Cutoff for alpha testing |
Common Node Patterns
| Effect |
Key Nodes |
| Dissolve |
Noise > Step > Alpha Clip + Edge emission |
| Scrolling UV |
Time > Multiply > Add to UV |
| Fresnel glow |
Fresnel Effect > Multiply color > Emission |
| Triplanar mapping |
Triplanar node (avoids UV stretching) |
| Color shift |
Lerp between colors using parameter or time |
| Vertex displacement |
Noise > Multiply > Add to Position |
| Outline |
Two-pass: inverted hull in custom render feature |
Shader Graph Sub Graphs
Extract reusable node groups into Sub Graphs (Create > Shader Graph > Sub Graph). Use for shared noise functions, UV transformations, or custom lighting models.
Hand-Written Shaders (ShaderLab + HLSL)
URP Shader Structure
Shader "Custom/SimpleUnlit"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST;
half4 _Color;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
return tex * _Color;
}
ENDHLSL
}
}
}
Key differences from Built-in shaders:
- Use
HLSLPROGRAM/ENDHLSL (not CGPROGRAM/ENDCG)
- Include URP shader library, not UnityCG.cginc
- Use
TEXTURE2D/SAMPLER macros (not sampler2D)
- Wrap properties in
CBUFFER_START(UnityPerMaterial) for SRP Batcher compatibility
Lighting
Light Types
| Type |
Use For |
Shadow Cost |
| Directional |
Sun, global illumination |
Low (cascaded shadow maps) |
| Point |
Torches, lamps |
Medium |
| Spot |
Flashlights, stage lights |
Medium |
| Area (baked only) |
Soft window light, panels |
High (bake only) |
Lighting Modes
| Mode |
Description |
Best For |
| Realtime |
Computed every frame |
Dynamic objects, few lights |
| Baked |
Pre-computed into lightmaps |
Static environments |
| Mixed |
Baked indirect + realtime direct |
Best balance |
Lightmap Baking Tips
- Set lightmap resolution based on scene scale (10-40 texels/unit for indoor)
- Use Light Probes for dynamic objects in baked scenes
- Use Reflection Probes for metallic/reflective surfaces
- Enable GPU Lightmapper for faster bake times
- Mark objects as Contribute GI in the Static flags
Post-Processing (Volume System)
Setup:
1. Add a Volume (Global or Local) to the scene
2. Create a Volume Profile asset
3. Add overrides: Bloom, Color Adjustments, Tonemapping, etc.
4. Camera must have Post Processing enabled (URP Camera settings)
| Effect |
Performance |
Notes |
| Bloom |
Low |
Use threshold to control intensity |
| Color Adjustments |
Very Low |
Saturation, contrast, color filter |
| Tonemapping |
Very Low |
ACES for cinematic look |
| Vignette |
Very Low |
Frame darkening |
| Ambient Occlusion (SSAO) |
Medium |
Disable on mobile |
| Depth of Field |
High |
Use Bokeh only for cinematics |
| Motion Blur |
Medium |
Can cause motion sickness in VR |
VFX Graph vs Particle System
| Feature |
Particle System (Shuriken) |
VFX Graph |
| Execution |
CPU |
GPU (compute shader) |
| Particle count |
Thousands |
Millions |
| Complexity |
Component-based, simple |
Node-based, complex |
| Platform |
All |
Compute shader capable only |
| Integration |
Physics, collision |
Limited physics |
Use Particle System for gameplay-integrated effects (physics collisions, small counts). Use VFX Graph for visual spectacles (rain, fire, magic, ambient particles).
URP Render Features
Extend URP rendering with custom ScriptableRendererFeatures:
public class OutlineFeature : ScriptableRendererFeature
{
OutlinePass _pass;
public override void Create()
{
_pass = new OutlinePass();
_pass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData data)
{
renderer.EnqueuePass(_pass);
}
}
Common uses: custom outlines, screen-space effects, render texture generation, stencil-based effects.
Additional Resources
Reference Files
references/shader-recipes.md -- Complete shader implementations: toon/cel shading, water, dissolve, hologram, force field, procedural skybox, stencil portal, vertex animation, custom lighting models
references/lighting-vfx-detail.md -- Advanced lighting setups, GI troubleshooting, VFX Graph cookbook (fire, smoke, electricity, portals), Scriptable Render Pipeline customization, custom render passes
1---
2name: unity-shaders-rendering
3description: |
4 Unity shaders, materials, and rendering pipelines (URP/HDRP/Built-in).
5 PROACTIVELY activate for: (1) writing shaders in Shader Graph, HLSL, or ShaderLab, (2) URP and HDRP shader authoring, (3) custom render pipeline work (SRP), (4) lighting setup (baked vs realtime, lightmaps, Global Illumination), (5) post-processing stacks, (6) reflection probes and light probes, (7) custom render features and full-screen passes, (8) shader stripping and variant management, (9) compute shaders, (10) ray tracing in HDRP.
6 Provides: Shader Graph templates, HLSL snippets, URP/HDRP differences, lighting setup recipes, render-feature examples, and shader-variant guidance.
7metadata:
8 source: https://skillmd.com/skills/josiahsiegel/unity-shaders-rendering
9---
10
11# Unity Shaders and Rendering
12
13## Overview
14
15Reference for Unity's rendering systems, shader development, lighting configuration, and visual effects. Covers all three render pipelines, Shader Graph, hand-written shaders, and VFX Graph.
16
17## Render Pipeline Comparison
18
19| Feature | Built-in RP | URP | HDRP |
20|---------|------------|-----|------|
21| Target | Legacy projects | Mobile, VR, wide range | High-end PC/console |
22| Shader language | Surface shaders + HLSL | HLSL (no surface shaders) | HLSL |
23| Shader Graph | Yes | Yes | Yes |
24| SRP Batcher | No | Yes | Yes |
25| Render Features | No | Yes (ScriptableRendererFeature) | Custom Pass |
26| Post-processing | Post Processing Stack v2 | Volume system (built-in) | Volume system (built-in) |
27| Ray tracing | No | No (probe-based) | Yes (DXR) |
28| Performance | Moderate | Optimized for scale | Highest fidelity |
29
30**Recommendation:** Use URP for new projects unless targeting high-end visuals exclusively (HDRP). Built-in RP is legacy -- migrate when possible.
31
32## Shader Graph
33
34### Getting Started
35
361. Right-click in Project: Create > Shader Graph > URP > Lit Shader Graph
372. Double-click to open Shader Graph editor
383. Build node network connecting to Master Stack outputs
394. Create a Material using the shader, assign to renderers
40
41### Master Stack Outputs (URP Lit)
42
43| Output | Type | Purpose |
44|--------|------|---------|
45| Base Color | Color (RGB) | Albedo/diffuse color |
46| Normal | Vector3 | Tangent-space normal map |
47| Metallic | Float (0-1) | Metal vs. dielectric |
48| Smoothness | Float (0-1) | Roughness inverse |
49| Emission | Color (RGB) | Self-illumination |
50| Alpha | Float (0-1) | Transparency |
51| Alpha Clip Threshold | Float | Cutoff for alpha testing |
52
53### Common Node Patterns
54
55| Effect | Key Nodes |
56|--------|-----------|
57| **Dissolve** | Noise > Step > Alpha Clip + Edge emission |
58| **Scrolling UV** | Time > Multiply > Add to UV |
59| **Fresnel glow** | Fresnel Effect > Multiply color > Emission |
60| **Triplanar mapping** | Triplanar node (avoids UV stretching) |
61| **Color shift** | Lerp between colors using parameter or time |
62| **Vertex displacement** | Noise > Multiply > Add to Position |
63| **Outline** | Two-pass: inverted hull in custom render feature |
64
65### Shader Graph Sub Graphs
66
67Extract reusable node groups into Sub Graphs (Create > Shader Graph > Sub Graph). Use for shared noise functions, UV transformations, or custom lighting models.
68
69## Hand-Written Shaders (ShaderLab + HLSL)
70
71### URP Shader Structure
72
73```hlsl
74Shader "Custom/SimpleUnlit"
75{
76 Properties
77 {
78 _MainTex ("Texture", 2D) = "white" {}
79 _Color ("Color", Color) = (1,1,1,1)
80 }
81
82 SubShader
83 {
84 Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
85
86 Pass
87 {
88 HLSLPROGRAM
89 #pragma vertex vert
90 #pragma fragment frag
91
92 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
93
94 struct Attributes
95 {
96 float4 positionOS : POSITION;
97 float2 uv : TEXCOORD0;
98 };
99
100 struct Varyings
101 {
102 float4 positionHCS : SV_POSITION;
103 float2 uv : TEXCOORD0;
104 };
105
106 TEXTURE2D(_MainTex);
107 SAMPLER(sampler_MainTex);
108
109 CBUFFER_START(UnityPerMaterial)
110 float4 _MainTex_ST;
111 half4 _Color;
112 CBUFFER_END
113
114 Varyings vert(Attributes IN)
115 {
116 Varyings OUT;
117 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
118 OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
119 return OUT;
120 }
121
122 half4 frag(Varyings IN) : SV_Target
123 {
124 half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
125 return tex * _Color;
126 }
127 ENDHLSL
128 }
129 }
130}
131```
132
133**Key differences from Built-in shaders:**
134- Use `HLSLPROGRAM`/`ENDHLSL` (not `CGPROGRAM`/`ENDCG`)
135- Include URP shader library, not UnityCG.cginc
136- Use `TEXTURE2D`/`SAMPLER` macros (not `sampler2D`)
137- Wrap properties in `CBUFFER_START(UnityPerMaterial)` for SRP Batcher compatibility
138
139## Lighting
140
141### Light Types
142
143| Type | Use For | Shadow Cost |
144|------|---------|-------------|
145| Directional | Sun, global illumination | Low (cascaded shadow maps) |
146| Point | Torches, lamps | Medium |
147| Spot | Flashlights, stage lights | Medium |
148| Area (baked only) | Soft window light, panels | High (bake only) |
149
150### Lighting Modes
151
152| Mode | Description | Best For |
153|------|-------------|----------|
154| Realtime | Computed every frame | Dynamic objects, few lights |
155| Baked | Pre-computed into lightmaps | Static environments |
156| Mixed | Baked indirect + realtime direct | Best balance |
157
158### Lightmap Baking Tips
159
160- Set lightmap resolution based on scene scale (10-40 texels/unit for indoor)
161- Use Light Probes for dynamic objects in baked scenes
162- Use Reflection Probes for metallic/reflective surfaces
163- Enable GPU Lightmapper for faster bake times
164- Mark objects as Contribute GI in the Static flags
165
166## Post-Processing (Volume System)
167
168```text
169Setup:
1701. Add a Volume (Global or Local) to the scene
1712. Create a Volume Profile asset
1723. Add overrides: Bloom, Color Adjustments, Tonemapping, etc.
1734. Camera must have Post Processing enabled (URP Camera settings)
174```
175
176| Effect | Performance | Notes |
177|--------|-------------|-------|
178| Bloom | Low | Use threshold to control intensity |
179| Color Adjustments | Very Low | Saturation, contrast, color filter |
180| Tonemapping | Very Low | ACES for cinematic look |
181| Vignette | Very Low | Frame darkening |
182| Ambient Occlusion (SSAO) | Medium | Disable on mobile |
183| Depth of Field | High | Use Bokeh only for cinematics |
184| Motion Blur | Medium | Can cause motion sickness in VR |
185
186## VFX Graph vs Particle System
187
188| Feature | Particle System (Shuriken) | VFX Graph |
189|---------|---------------------------|-----------|
190| Execution | CPU | GPU (compute shader) |
191| Particle count | Thousands | Millions |
192| Complexity | Component-based, simple | Node-based, complex |
193| Platform | All | Compute shader capable only |
194| Integration | Physics, collision | Limited physics |
195
196Use Particle System for gameplay-integrated effects (physics collisions, small counts). Use VFX Graph for visual spectacles (rain, fire, magic, ambient particles).
197
198## URP Render Features
199
200Extend URP rendering with custom ScriptableRendererFeatures:
201
202```csharp
203public class OutlineFeature : ScriptableRendererFeature
204{
205 OutlinePass _pass;
206
207 public override void Create()
208 {
209 _pass = new OutlinePass();
210 _pass.renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
211 }
212
213 public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData data)
214 {
215 renderer.EnqueuePass(_pass);
216 }
217}
218```
219
220Common uses: custom outlines, screen-space effects, render texture generation, stencil-based effects.
221
222## Additional Resources
223
224### Reference Files
225- **`references/shader-recipes.md`** -- Complete shader implementations: toon/cel shading, water, dissolve, hologram, force field, procedural skybox, stencil portal, vertex animation, custom lighting models
226- **`references/lighting-vfx-detail.md`** -- Advanced lighting setups, GI troubleshooting, VFX Graph cookbook (fire, smoke, electricity, portals), Scriptable Render Pipeline customization, custom render passes