How to choose Shader based on performance for Unity3D mobile development

Foreword

In Unity3D mobile development, it is very important to choose the appropriate Shader, which directly affects the performance and picture effects of the game. This article will introduce how to choose a Shader based on performance, and give corresponding technical details and code implementation.

Yes, there is a game development exchangegroup here. I hope everyone can click in to exchange development experiences together!

1. Understand the basic concepts of Shader

In Unity3D, Shader is a program used to control graphics rendering. It can define the color, lighting, material and other properties of the object. Shader consists of a series of Shader Passes, each Pass defines a rendering method. In mobile development, we usually use Surface Shader, which is an advanced Shader that can easily define the appearance of objects.

2. Principles for selecting Shader for performance

  1. Try to use simple Shaders

On mobile devices, performance is a very important consideration. Therefore, we should try to use simple Shaders and avoid using overly complex calculations and textures. A simple Shader can improve rendering efficiency and reduce the number of draw calls, thereby improving game performance.

  1. Avoid using dynamic Shader features

On mobile devices, dynamic Shader features (such as transparency, reflection, etc.) will add additional computing and memory overhead. Therefore, we should try to avoid using these features or disable them in scenarios with high performance requirements.

  1. Use appropriate texture compression format

On mobile devices, the memory footprint of textures is a very important factor. Therefore, we should choose an appropriate texture compression format to reduce the memory footprint of textures. Common texture compression formats include ETC, PVRTC, ASTC, etc.

  1. Proper use of LOD (Level of Detail)

On mobile devices, LOD is a very important optimization method. It can adjust the level of detail of an object based on its distance, thereby reducing rendering overhead. Therefore, we should use LOD reasonably and minimize unnecessary detail drawing.

3. Technical details and code implementation

  1. Use a simple shader

In Unity3D, we can use Surface Shader to define the appearance of objects. Surface Shader is an advanced Shader that can easily define the color and lighting properties of objects. Here is a sample code for a simple Surface Shader:

Shader "Custom/SimpleShader"
{
    Properties
    {
        _Color("Color", Color) = (1,1,1,1)
    }
    
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        
        CGPROGRAM
        #pragma surface surf Lambert
        
        struct input
        {
            float2 uv_MainTex;
        };
        
        sampler2D _MainTex;
        fixed4 _Color;
        
        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    
    FallBack "Diffuse"
}

In this example, we use the Lambert lighting model and define a _Color attribute to control the color of the object. In the surf function, we use the tex2D function to get the color of the texture and multiply it by the _Color attribute to calculate the final color of the object.

  1. Avoid using dynamic Shader features

On mobile devices, dynamic Shader features will add additional computing and memory overhead. Therefore, we should try to avoid using these features or disable them in scenarios with high performance requirements. Here is a sample code that disables the transparency feature:

Shader "Custom/NoAlphaShader"
{
    Properties
    {
        _Color("Color", Color) = (1,1,1,1)
    }
    
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        
        CGPROGRAM
        #pragma surface surf Lambert
        
        struct input
        {
            float2 uv_MainTex;
        };
        
        sampler2D _MainTex;
        fixed4 _Color;
        
        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            o.Alpha = 1; // disable transparency feature
        }
        ENDCG
    }
    
    FallBack "Diffuse"
}

In this example, we set the object’s transparency to 1, thus disabling the transparency feature.

  1. Use appropriate texture compression format

On mobile devices, the memory footprint of textures is a very important factor. Therefore, we should choose an appropriate texture compression format to reduce the memory footprint of textures. Here is a sample code using the ETC texture compression format:

Shader "Custom/ETCShader"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
    }
    
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        
        CGPROGRAM
        #pragma surface surf Lambert
        
        struct input
        {
            float2 uv_MainTex;
        };
        
        sampler2D _MainTex;
        
        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    
    FallBack "Diffuse"
}

In this example, we use the ETC texture compression format to reduce the memory footprint of the texture.

  1. Proper use of LOD

On mobile devices, LOD is a very important optimization method. It can adjust the level of detail of an object based on its distance, thereby reducing rendering overhead. Here is a sample code using LOD:

Shader "Custom/LODShader"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
    }
    
    SubShader
    {
        LOD 100
        
        Tags { "RenderType"="Opaque" }
        
        CGPROGRAM
        #pragma surface surf Lambert
        
        struct input
        {
            float2 uv_MainTex;
        };
        
        sampler2D _MainTex;
        
        void surf (Input IN, inout SurfaceOutput o)
        {
            fixed4 c = tex2Dlod (_MainTex, float4(IN.uv_MainTex, 0, 0));
            o.Albedo = c.rgb;
            o.Alpha = c.a;
        }
        ENDCG
    }
    
    FallBack "Diffuse"
}

In this example, we use LOD 100 to indicate using a lower level of detail to render objects when they are more than 100 units from the camera.

Summarize

In Unity3D mobile development, it is very important to choose the appropriate Shader. We can choose a simple Shader based on performance requirements and avoid using dynamic Shader features. In addition, we can also choose an appropriate texture compression format and rationally use LOD to optimize the performance of the game. I hope this article will help you choose the right Shader in Unity3D mobile development.

More instructional videos

https://www.bycwedu.com/promotion_channels/1928452950

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 57208 people are learning the system