Search Unity

Question When do you have too many shader/variants?

Discussion in 'Shaders' started by lejean, Feb 23, 2021.

  1. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    392
    When I run through my scenes, prefabs and what not I get about 60 shaders/467 variants, as seen in the picture, but I wonder if this is too much?

    How impactful is it too have X amount of shaders/variants?

    One of the problems though is that I'm using several asset store packages and alot of them have created their own custom shaders which have to be included.

    I've been reading about optimizing shaders but I find it too complicated and might aswell break alot of functionality.

    So should this be worrying or no?

    shaders.png
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    That's fine.

    I've seen games with thousands of variants, and that's fine too. Unity's Standard shader is especially variant happy. It just means shaders take up a bit more of the build (could be a few hundred MB of just Standard shader variants, which is rough for mobile downloads).

    Some stats from Falcon Age on PC, Oculus Quest, and Nintendo Switch.
    PC: 95 shaders, 421 total variants
    Switch: 128 shaders, 650 total variants
    Quest: 90 shaders, 169 total variants

    But here's the thing, that number doesn't really mean what you think it means. That's the number of shaders and variants the editor has seen since it started tracking for your project. It's not how many are in the build. Any shader you may have at one time used, or played with the settings of that triggered a new variant is going to be be listed. The PC and Switch projects are essentially identical in terms of actual shaders & variants that end up in the built project, with both having 94 shaders and 418 variants.

    The Quest version is using 100% custom shaders with an extremely stripped down feature set which greatly reduces the number of variants.

    The main thing you want to do is ... ignore that for now. When you make a build of the game, look in the build log to find out how many shaders you're actually using. Or hit the "clear" button in the Graphics settings and do a full play through of the game or two to get a more realistic number of the shaders & variants being used. The build will grab all that it can find being referenced, plus a bunch extra for things like fog or lighting that it can't track easily. You can disable some of these from the Stripping options just above there. If any are material settings, specifically keywords, being toggled from c# this will increase the variants being used in ways the build system can't track, and is actually why Unity tracks shaders and why the "preloaded shaders" above exists. Saving the current tracked shaders & variants to an asset will make sure those are also included in the build.