HOW TO SETTING UP TO MATERIALS LOD.

MATERIALS LOD FOR INGAME PLAY.
Organization / Netease Pangu Studio Technical Art Team.
Author / JP.Lee

Go Github

Commit schedule : 2016-9-26.

To be honest, this is already four years ago.
Most recently I do not use this method.
Because many easier methods have already been discovered.

老实说,这已经是四年前了。
最近我不使用这种方法。
因为已经发现了许多更简单的方法。

Overview
What is this have purpose of requirement.
In the game, according to the change of the quality template set by the user, the calculation quality of the Shader used in the game, the type of Shader, etc. will be automatically changed. The change materials has decided to use Shader LOD and Material Quality Phase Set (based on the quality setting, it will be replaced with a predefined Material).
The advantages are relatively simple to achieve overall.
Disadvantages are fine-thinking about the management of Material.

游戏中,根据用户设置的品质模板的改变,游戏中使用的Shader的运算品质,Shader的种类等,都会自动变更。变更要素已经决定使用Shader LOD和Material Quality Phase Set(根据品质设定,会更换成预先定义的Material)。
优点整体实现较为简单。
缺点美术得对Matrial的管理费点心思。


Shader structure of QUALITY PHASE.

Depending on the distance, you can use shaders optimized for each quality. # Performance issues # Metal roughness values have a performance difference of about 25% When using variables instead of textures. You need to divide the quality level and the LOD level, set the rules and use the variable parameters.
随着距离的不同,可以使用根据各品质别设置的灯光模型的优化Shader。#性能问题#Metallic Roughness值上面使用贴图和使用变量参数,性能上有25%左右的差别。我们要划分好品质等级和LOD等级,定好规则,使用变量参数。

美术材质球品质设置脚本

This major feature is artist they only when you open the Edit Mode, you can preview the changes in the edit state without clicking gameplay.
只有打开Edit Mode的时候,即使不点击gameplay,也能在编辑状态上预览到变更结果。

using UnityEngine;
using System.Collections;
[System.Serializable]
[ExecuteInEditMode]
public enum EditableMaterialsList
{
    LOD0,
    LOD1,
    LOD2
};

[ExecuteInEditMode]
public class ArtistMaterialSetLOD : MonoBehaviour
{
    public bool editMode = true;
    public Material mat1;
    public Material mat2;
    public Material mat3;
    public EditableMaterialsList editableMaterialsList = EditableMaterialsList.LOD0;
    
    Renderer rend;
    void Start()
    {
     
    }
#if UNITY_EDITOR
    void UpdateMatLOD()
    {
        rend = GetComponent<Renderer>();
        if (UnityEditor.EditorApplication.isPlaying)
        {
            editMode = false;
        }
        else editMode = true;

        if (!editMode)
        {
            return;
        }
        else
        {
            switch (editableMaterialsList)
            {
                case EditableMaterialsList.LOD0:
                    rend.material = mat1;
                    break;
                case EditableMaterialsList.LOD1:
                    rend.material = mat2;
                    break;
                case EditableMaterialsList.LOD2:
                    rend.material = mat3;
                    break;
                default:
                    break;
            }
        }
     
    }


    
    void Update()
    {
        UpdateMatLOD();
    }
#endif

}

测试用Material LOD script.

The distance is divided into 3 levels in total, using 3 LOD materials. The shader for the sample code has to be set manually.
距离总共分为3档,使用3个LOD material。样本代码的材质球得手动设置。

using UnityEngine;
using System.Collections;

[System.Serializable]
[ExecuteInEditMode]
public class SwitchMaterialSwapLOD : MonoBehaviour
{


    public bool shadowLOD = true;
    public float[] matLodDistance = { 60f, 20f, 0f };
    public Material[] lodMat;
    Renderer rend;
    public float updataTime = 0.25f;


    void Start()
    {
        rend = GetComponent<Renderer>();
        StartCoroutine("UpdateMatLOD");
    }



    IEnumerator UpdateMatLOD()
    {
        var period = updataTime;
        var cam = Camera.main.transform;
        rend.sharedMaterial = lodMat[0];
        while (true)
        {
            var distance = (transform.position - cam.position).sqrMagnitude;
            Debug.Log(distance);

            if (distance > matLodDistance[0])
            {
                rend.sharedMaterial = lodMat[2];
            }
            else if (distance > matLodDistance[1])
            {
                rend.sharedMaterial = lodMat[1];
            }
            else if (distance > matLodDistance[2])
            {
                rend.sharedMaterial = lodMat[0];
            }
            else rend.sharedMaterial = lodMat[0];


            if (shadowLOD)
            {
                if (distance > matLodDistance[1])
                {
                    this.GetComponent<SkinnedMeshRenderer>().receiveShadows = false;
                }
                else this.GetComponent<SkinnedMeshRenderer>().receiveShadows = true;
            }

            // make LOD checking non deterministic. avoids stampeding LOD calls?
            yield return new WaitForSeconds(period);
        }
    }
}


细部构成概要
3 shaders-shader name _LOD_0.
-Shader name _LOD_1.
-Shader name_LOD_2.
Shader of each file.

3档材质球-材质球名_LOD_0
-材质球名_LOD_1
-材质球名_LOD_2
各个档的Shader.
-shaderName LOD 0 ( LOD 300 , LOD 200 , LOD 100 )
:Full shader.
-shaderName LOD 1 ( LOD 300 , LOD 200 , LOD 100 )
: Down data precision ( float to half ).- roughness texture fetch to constant value.
-shaderName LOD 2 ( LOD 300 , LOD 200 , LOD 100 )
:Down data precision ( half to fixed )- roughness with metallic texture fetch to constant value.- Optional normal map enabler.

Only Test Phase of Images below

LOD 0

LOD 1

LOD 2

Mesh LOD with Material LOD.

#Mesh LOD的话,可以以Mech renderer单位来登录LOD。

Categories: tools

Tagged as:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s