Taking advantage of shader branching and shader variants
When you first started your journey in the world of shaders and you took your first steps writing vertex and fragment functions, life was easy: you wrote a single piece of code that always executed the same (see the examples shown in the Appendix: Some Quick Refreshers on Shaders in Unity, or in Chapter 1).
But now that you are getting into more advanced rendering schemes, you might want to introduce some conditional behavior into your shader code so that it executes differently under different circumstances – for example, because of one of the following reasons:
- You want to distinguish between two target platforms and their respective graphics backends
- You don’t want to execute expensive code such as vertex inputs or large loops
- You want your shader to sample a different texture depending on some instance-specific data
For all those cases, you might need to add some branching logic to...