• Unity3D学习笔记8——GPU实例化(3)


    1. 概述

    在前两篇文章《Unity3D学习笔记6——GPU实例化(1)》《Unity3D学习笔记6——GPU实例化(2)》分别介绍了通过简单的顶点着色器+片元着色器,以及通过表面着色器实现GPU实例化的过程。而在Unity的官方文档Creating shaders that support GPU instancing里,也提供了一个GPU实例化的案例,这里就详细论述一下。

    2. 详论

    2.1. 自动实例化

    一个有意思的地方在于,Unity提供的标准材质支持自动实例化,而不用像《Unity3D学习笔记6——GPU实例化(1)》《Unity3D学习笔记6——GPU实例化(2)》那样额外编写脚本和Shader。并且,会自动将transform,也就是模型矩阵作为每个实例的属性。

    照例,还是编写一个脚本挂到一个空的GameObject对象上:

    using UnityEngine;
    
    public class Note8Main : MonoBehaviour
    {
        public Mesh mesh;
        public Material material;
        public int instanceCount = 5000;
    
        // Start is called before the first frame update
        void Start()
        {
            MaterialPropertyBlock props = new MaterialPropertyBlock();
          
            for (int i = 0; i < instanceCount; i++)
            {
                GameObject go = new GameObject();
                go.name = i.ToString();
    
                MeshFilter mf = go.AddComponent<MeshFilter>();
                mf.mesh = mesh;
    
                MeshRenderer mr = go.AddComponent<MeshRenderer>();
                mr.material = material;
                
                go.transform.position = Random.insideUnitSphere * 5;
                go.transform.eulerAngles = new Vector3(Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f));
                float s = Random.value;
                go.transform.localScale = new Vector3(s, s, s);
           
                go.transform.parent = gameObject.transform;
            }
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }
    
    折叠

    这个脚本的意思是,给挂接的GameObject下新建很多GameObject,它们使用我们传入的Mesh和Material,但是位置、姿态和大小是随机的。传入的Mesh使用Unity自带的胶囊体,Material使用Unity的标准材质。运行结果如下:

    imglink1

    这个时候Unity还没有自动实例化,打开Frame Debug就可以看到:
    imglink2

    这个时候我们可以在使用的材质上勾选打开实例化的选项:
    imglink3

    再次运行,就会在Frame Debug看到Unity实现了自动实例化,绘制的批次明显减少,并且性能会有所提升:
    imglink4

    可以看到确实是自动进行实例化绘制了,但是这种方式却似乎存在实例化个数的上限,所有的实例化数据还是分成了好几个批次进行绘制。与《Unity3D学习笔记6——GPU实例化(1)》《Unity3D学习笔记6——GPU实例化(2)》提到的通过底层接口Graphic进行实例化绘制相比,效率还是要低一些。

    2.2. MaterialPropertyBlock

    自动实例化只能将transform,也就是模型矩阵作为每个实例的属性。如果需要增加自己的实例属性,就需要使用MaterialPropertyBlock,也就是材质属性块。

    修改上面的脚本:

    using UnityEngine;
    
    public class Note8Main : MonoBehaviour
    {
        public Mesh mesh;
        public Material material;
        public int instanceCount = 5000;
    
        // Start is called before the first frame update
        void Start()
        {
            MaterialPropertyBlock props = new MaterialPropertyBlock();
          
            for (int i = 0; i < instanceCount; i++)
            {
                GameObject go = new GameObject();
                go.name = i.ToString();
    
                MeshFilter mf = go.AddComponent<MeshFilter>();
                mf.mesh = mesh;
    
                MeshRenderer mr = go.AddComponent<MeshRenderer>();
                mr.material = material;
    
                float r = Random.Range(0.0f, 1.0f);
                float g = Random.Range(0.0f, 1.0f);
                float b = Random.Range(0.0f, 1.0f);
                props.SetColor("_Color", new Color(r, g, b));
                mr.SetPropertyBlock(props);
    
                go.transform.position = Random.insideUnitSphere * 5;
                go.transform.eulerAngles = new Vector3(Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f));
                float s = Random.value;
                go.transform.localScale = new Vector3(s, s, s);
           
                go.transform.parent = gameObject.transform;
            }
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }
    
    折叠

    脚本使用的材质,其使用的Shader如下,可以直接在Standard Surface Shader的基础上改:

    Shader "Custom/HiddenSurfaceIntanceShader"
    {
        Properties
        {
            _Color ("Color", Color) = (1,1,1,1)
            _MainTex ("Albedo (RGB)", 2D) = "white" {}
            _Glossiness ("Smoothness", Range(0,1)) = 0.5
            _Metallic ("Metallic", Range(0,1)) = 0.0
        }
        SubShader
        {
            Tags { "RenderType"="Opaque" }
            LOD 200
    
            CGPROGRAM
            // Physically based Standard lighting model, and enable shadows on all light types
            #pragma surface surf Standard fullforwardshadows
    
            // Use shader model 3.0 target, to get nicer looking lighting
            #pragma target 3.0
    
            sampler2D _MainTex;
    
            struct Input
            {
                float2 uv_MainTex;
            };
    
            half _Glossiness;
            half _Metallic;
            //fixed4 _Color;
    
            // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
            // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
            // #pragma instancing_options assumeuniformscaling
            UNITY_INSTANCING_BUFFER_START(Props)
                // put more per-instance properties here
    			UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
            UNITY_INSTANCING_BUFFER_END(Props)
    
            void surf (Input IN, inout SurfaceOutputStandard o)
            {
                // Albedo comes from a texture tinted by color
                //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
                o.Albedo = c.rgb;
                // Metallic and smoothness come from slider variables
                o.Metallic = _Metallic;
                o.Smoothness = _Glossiness;
                o.Alpha = c.a;
            }
            ENDCG
        }
        FallBack "Diffuse"
    }
    
    折叠

    关键的代码在于Unity内置宏UNITY_INSTANCING_BUFFER_START和UNITY_INSTANCING_BUFFER_END、UNITY_DEFINE_INSTANCED_PROP定义了实例化属性,在着色器中,通过内置宏UNITY_ACCESS_INSTANCED_PROP来获取这个属性值。这个实例化属性也就是脚本代码中MaterialPropertyBlock传入的颜色值。

    查看Unity Shader源代码,这四个用于实例化的宏封装的是一个cbuffer数组,cbuffer就是hlsl的常量缓冲区:

    #define UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(name)  cbuffer name {
    #define UNITY_INSTANCING_CBUFFER_SCOPE_END          }
    
    #define UNITY_INSTANCING_BUFFER_START(buf)      UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityInstancing_##buf) struct {
    #define UNITY_INSTANCING_BUFFER_END(arr)        } arr##Array[UNITY_INSTANCED_ARRAY_SIZE]; UNITY_INSTANCING_CBUFFER_SCOPE_END
    #define UNITY_DEFINE_INSTANCED_PROP(type, var)  type var;
    #define UNITY_ACCESS_INSTANCED_PROP(arr, var)   arr##Array[unity_InstanceID].var
    

    运行的结果如下:
    imglink5

    可以看到除了纹理,每一个胶囊体还获取了随机赋予给材质的颜色,也就是我们设置的颜色成为了实例化属性数据。MaterialPropertyBlock主要由Graphics.DrawMesh和Renderer.SetPropertyBlock使用,在希望绘制具有相同材质,但属性略有不同的多个对象时可使用它。

    个人认为使用MaterialPropertyBlock自动实例化性能比不上使用Graphics.DrawMeshInstancedIndirect(),但是它有个优点是实例化的要求没那么高,Graphics.DrawMeshInstancedIndirect()要求使用同一mesh,同一贴图;但是MaterialPropertyBlock没这个要求,只要是同一材质,任何属性不一样都可以用,在减少绘制批次的同时还能减少材质的个数。

    3. 参考

    1. 《Unity3D学习笔记6——GPU实例化(1)》
    2. 《Unity3D学习笔记6——GPU实例化(2)》
    3. Creating shaders that support GPU instancing
    4. MaterialPropertyBlock

    具体实现代码

  • 相关阅读:
    flink重温笔记(九):Flink 高级 API 开发——flink 四大基石之WaterMark(Time为核心)
    宇视雷视工勘指导(卡口电警篇)
    uni-app进行表单效验
    【动态规划】求最大子段和系列问题
    信息系统项目管理师必背核心考点(二十四)WBS分解的原则
    什么是 Nacos
    ceph 线程池分析
    Maika 与越南童模们受邀请参加中国上海时装周 hanakimi 品牌开幕
    低代码开发平台的功能有哪些?低代码“功能清单”一览
    vue 实现自定义分页打印 window.print
  • 原文地址:https://www.cnblogs.com/charlee44/p/16463812.html