An updated version of this article can be found here :
https://www.cyanilux.com/tutorials/urp-shader-code/
Intro
A lot of shader code tutorials you’ll see online are written for Unity’s built-in pipeline and won’t really work in the Universal Render Pipeline – They’ll either produce magenta errors or at least won’t be compatible with the SRP Batcher.
Shader graph is an amazing tool which I use a lot. I find it much easier and quicker to write shaders using it. Having previews can really help visualise what the shader is actually doing but I also just like that I don’t have to remember the very specific code syntax and function names. However there are some things which aren’t available in shadergraph just yet – such as proper support for UI shaders, access to all 8 UV channels, interpolating custom data between the vertex and fragment stages, tessellation, etc. All of these are on the shadergraph roadmap but it may be a while before we get to see them, and of course some people might just prefer writing code over node-based editors.
I’ve tried to write this in a way that works as an introduction to Universal RP shader code for complete beginners, but be aware it may get difficult to read (especially on the PBR Lighting page), if you aren’t that familiar with HLSL code. If you have no idea what a shader even is, you’ll probably want to look over some other tutorials first.
If you are already experienced with shader code from the built-in pipeline you’ll want to skip over a lot of stuff here, but I’m putting all the main differences in cyan text so hopefully it’ll be easy to find at a glance! There’s also a section at the end summarising the differences.
Enjoy!
Sections
- Shaderlab
- HLSL
- Structs
- Vertex Shader
- Fragment Shader
- Keywords & Shader Variants
- Lighting Introduction
- PBR Lighting
- ShadowCaster & Depth Only Passes
- Summary of Built-in vs URP differences
Possible sections to add in future :
- Meta Pass (used when baking lightmaps, see LitMetaPass and UnlitMetaPass for examples)
- Depth & Opaque Textures (_CameraDepthTexture & _CameraOpaqueTexture, see Scene Color & Depth Nodes post)
- Stencil operations (works the same as built-in pipeline, see here)
If you have any suggestions for new sections or improvements to other sections, or spot any mistakes or typos, feel free to contact me. @Cyanilux on twitter. I also have a discord server.
Useful Links
- Full Unlit Shader Example (Shaderlab to Fragment Shader sections)
- Full Lambert/Diffuse-only Lit Shader Example (Lighting Introduction section)
- Full Lit Shader Example(PBR Lighting section)
- Universal RP Github – ShaderLibrary
- Universal RP Github – Shaders
- Phi Lira’s Universal Shader Examples
- Phi Lira’s Universal PBR Shader Template
- Teofilo Dutra’s Built-in to URP Blog Post