How to Do Unity Shader Optimization?

    01.06.2021
    643
    How to Do Unity Shader Optimization?

    If you have previously added a large shader package to your Unity project for Unity Shader optimization settings, either from the Asset Store or elsewhere, you may have witnessed that both the build time and the size of the game increased significantly.

    What is Unity 3D Shader? How is Shader optimization done?
    how to do unity shader optimization

    I came across this situation when I added the Toony Color Pro 2 asset to my project: the shader that came with it supported all of Unity’s scriptable render pipelines: LWRP/URP, HDRP, and built-in by default. Even though I did not use LWRP/URP or HDRP, the shader was being built for these render pipelines, and the build size was increased by 50 MB (iOS), and the build time was 1-hour longer (as the built shaders are cached, this time in subsequent builds) much shorter).

    While I was thinking about what to do about this situation, I came across this blog post of Unity and wrote an editor script with the technique I learned from there: (GitHub Link)

    This editor script turns off unused features in my shaders and prevents shaders from being unnecessarily built for LWRP/URP and HDRP (this is called shader stripping). In this way, my build time was reduced to a few minutes and the build size decreased to levels that the shader could not notice.

    You can read the rest of the article for more information…

    Unity Outline Shader

    I’ve optimized this editor script for Unity’s built-in render pipeline, which comes by default, and Forward Rendering Unity Outline Shader, which is used by default (this is used by most mobile projects). If you are using LWRP/URP, HDRP, or Deferred Rendering, you may not get as much efficiency from the script as I do.

    What you need to do is to create a new C# script named ShaderStripper in the Editor folder of your project (create it if this folder does not exist) and replace the content of the script with the code in the link.

    This editor script uses Unity’s IPreprocessShaders interface; that is, Unity automatically calls the OnProcessShader function of this script during the build. I also prevent the build of shader properties that I do not want in this function.

    How do Shaders work?

    If I were to explain how the shader works:

    • First of all, I never touch the shaders in the Hidden, Unlit, Legacy Shaders, and Particles categories. These shaders don’t have a significant impact on build time or size anyway, and it’s best not to touch them as soon as possible, especially since Unity uses Hidden shaders in different places.
    • I prevent shaders using Deferred Rendering from getting builds (PassType.Deferred, PassType.LightPrePassBase, PassType.LightPrePassFinal). If you are using deferred rendering, delete these conditions from the if
    • I prevent shaders from getting builds for LWRP/URP and HDRP (PassType.ScriptableRenderPipeline, PassType.ScriptableRenderPipelineDefaultUnlit). If you are using one of these scriptable render pipelines, delete these conditions from if.
    • I turn off the light cookies features of the shaders (I’ve very rarely seen a cookie given to lights, and on those rare occasions they are usually given to flashlights in horror games)

    Unity Shader Optimization Tutorial 2

    How to make Unity 3D Shader tutorial?
    unity shader tutorial

    There are a few other settings you can tinker with to get even more out of shader optimization:

    • If you do not plan to use Fog (fog), set the value of Edit-Project Settings-Graphics-Fog Modes to Custom and turn off all 3 fog options. The fog feature of shaders will now be turned off.
    • If your project doesn’t need to utilize GPU instancing (unless you’re rendering thousands of clones of the same object in the scene at once), set the value of Edit-Project Settings-Graphics-Instancing Variants to Strip All. Thus, the GPU instancing feature of shaders will be turned off.
    • If you are using Forward Rendering, turn off Deferred, Deferred Reflections and Legacy Deferred features in Edit-Project Settings-Graphics-Built-in Shader Settings.
    • Turn off the Motion Vectors, Light Halo and Lens Flare features in Edit-Project Settings-Graphics-Built-in Shader Settings (Motion Vectors feature is used in motion blur, others are used in Halo and Flare components)

    If you remove the comment of the #define SHADER_COMPILATION_LOGGING line at the beginning of the script, you can find the list of all the shaders that were built in the Unity-project-location/Library/Shader Compilation Results.txt file after build.

    If you remove the comment of the #define SKIP_SHADER_COMPILATION line at the beginning of the script, none of the shaders will be built while the game is being built, but these shaders are still listed in the Shader Compilation Results.txt file. If you want to know which shaders have been built but don’t want to wait for the shaders to build, you can remove the comments for this line and the line I mentioned earlier.

    Category: Unity 3D

    AUTHOR INFO
    Welcome to Gameplay Developer, your number one source for all things related to 30 Rose Lane, Henderson, NC... We're dedicated to giving you the very best of 30 Rose Lane, Henderson, NC. with a focus on quality and real-world problem solutions.
    COMMENTS

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    No comments yet, be the first by filling the form.