Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Unity Complie all Standard shader variants

Tags:

When add Standard Shader to Graphics——Always Include Shaders, Unity always stucks in sharedassets0.asset. I know it's unity complies shader variants in the background. it's about 60000 variants which make the .apk size is triple bigger than normal size. And it takes a long time(a couple of hours) to build, even if an empty project.If I remove the shader from the list, when I load a model from asset bundle, it will be a pink.Only add Standard to the list, then everything is ok!Is there any way to solve this problem? Please help me!I googled all I can google!

like image 971
Marsir Avatar asked May 03 '18 08:05

Marsir


People also ask

Are Unity Shaders HLSL?

In Unity, shader programs are written in a variant of HLSL language (also called Cg but for most practical uses the two are the same).

What is shader stripping Unity?

Stripping your Shaders gives you smaller build sizes and shorter build times. This is useful if your project is never going to use certain features or keywords. For example, you might have a project where you never use shadows for directional lights.

What is asynchronous shader?

Asynchronous Shader compilation prevents the Unity Editor from stalling by rendering dummy Shaders while compiling new Shader Variants. Asynchronous Shader compilation is enabled in the Editor settings (menu: Edit > Project Settings… > Editor > Shader Compilation) by default.

Is shader a compilation?

Shader Compilation is the term used to describe the process by which OpenGL Shading Language scripts are loaded into OpenGL to be used as shaders. OpenGL has three ways to compile shader text into usable OpenGL objects. All of these forms of compilation produce a Program Object.


1 Answers

Edit: Unity 2018.2 added IPreprocessShaders which is described at: https://blogs.unity3d.com/2018/05/14/stripping-scriptable-shader-variants/ You can review each individual shader keyword combination in IList<ShaderCompilerData> and remove ones you are certain you don't need. The this API is not complicated, but standard shader keywords are not well documented. Some of the keyword correspond to enum BuiltinShaderDefine while others are not. I ended up using this preprocessor to remove any ShaderCompilerData with BuiltinShaderDefine.UNITY_HARDWARE_TIER1, BuiltinShaderDefine.UNITY_HARDWARE_TIER2 and INSTANCING_ON(This is not in the BuiltinShaderDefine enum) turned on since I am certain we didn't use quality settings toggle and instancing.

The Unity Shader Variant Collections works kind of wonky. I ended up using a plugin called "Shader Control" to clean up the mess. But the process is still far from enjoyable since when you remove a keyword there is a chance you break something.

The best solution I could think of is remove all the "Built-in Shader" (Maybe not "Deferred" is you are using Deferred Rendering) and "Always Include Shaders" at the beginning of the project and if someone needs a built-in shader, download "Builit in Shaders" from the Unity Download Archive and import only the one needed back to the project. So that you can:

  • Have 100% control over built-in shader
  • Only compile shaders you used
  • Avoid shader duplication in AssetBundles (If you pack something referencing default imported shaders, the shader will be duplicated for each AssetBundle)

But this "best solution" is certainly more of a hindsight and does not help your current situation though. Give Shader Control a try.

like image 61
Chun-Fu Chao Avatar answered Sep 28 '22 18:09

Chun-Fu Chao