Integrating Bakery shader features to custom shaders
If you have a custom shader but want to add support for e.g.
- SH lightmaps;
- Per-vertex lightmaps;
- Volume lighting
- ...etc,
...there are a few steps you need to take:
Include the functions
1. You can use existing functions from Bakery.cginc, but the easiest starting point is including BakeryDecodeLightmap.hlsl which comes with the URP graph package (starting with URP graphs (rev 5.1) (16.0.4)). The latest package is always listed and freely downloadable from the main page. Unpack the package, copy the file to your shaders' folder and include it:
Select and call a function
2. Select a function for the desired feature and call it. For example:
(In the following functions normalWorld and viewDir are always world-space).
Computes final lightmap UV by applying renderer.lightmapScaleOffset to UV2. Following functions expect lightmapUV to be transformed this way.
Computes approximate specular from a directional lightmap. color will contain new specular highlights.
Samples Bakery SH lightmaps. L0 must be the default lightmap (decoded unity_Lightmap) value. sh will contain new diffuse lighting.
Samples Bakery MonoSH lightmaps. sh will contain new diffuse lighting.
Samples Bakery SH lightmaps and computes approximate specular from them. L0 must be the default lightmap (decoded unity_Lightmap) value. diffuseSH will contain new diffuse lighting; specularSH will contain new specular highlights.
Samples Bakery MonoSH lightmaps and computes approximate specular from them. diffuseSH will contain new diffuse lighting; specularSH will contain new specular highlights.
Samples Bakery Volumes. sh will contain new diffuse lighting.
Samples Bakery Volumes and computes approximate specular from them. diffuseSH will contain new diffuse lighting; specularSH will contain new specular highlights.
Samples light probes in a non-linear way, fixing negative color artifacts. color will contain new diffuse lighting.
Use the computed data
3. Given new diffuse/specular lighting, apply it to your shading calculations. The simplest approach would be:
However, if you want to take fresnel, metalness, specular color, and other properties into account, you can use this complex function:
After that just output (newDiffuse + newSpecular).
If you use a surface shader or a shader graph that doesn't allow outputting final color, a typical workaround is:
- Set Occlusion to 0 to cancel built-in indirect lighting and reflections.
- Add reflections back to newSpecular. You can use this function:
- Output final computed color to Emission.
Examples
Minimal unlit shader using SH lightmaps Minimal surface shader using SH lightmaps