Optimising Shaders

Unity users a concept of keywords available within shaders. These keywords effectively act as defines that get enabled and disabled depending on the scene presets, light sources available, and so on.

Each combination of keywords produces a so-called shader variant.

While being very flexible and powerful, such a mechanism is prone to introducing unneccessarily big amount of shader variants. This happens because Unity cannot predict whether certain variants will be used or not: for instance, having a light on the scene forces Unity to generate variants where the light does or does not cast shadows, where the light is directional, spot or point, has or doesn't have a cookie and so on - as all these things can be changed from user's scripts at any time.

By default, Luna utilizes Unity's variants trimming, exporting only the variants that Unity considers required for the set of scenes being exported.

Luckly enough, there is a built-in mechanism available to fine-control the shader variants ending up in production builds, called Shader Variant Collection.

The tutorial avalable in Unity's documentation explains how to quickly and easily record variants used by the build.

To make Luna utilize a shader variant collection for trimming the variants, simply save a shader variant into an asset named Luna.shadervariants inside project's Assets folder.