搜索
您的当前位置:首页正文

[unity]GPU Instance学习

来源:二三娱乐

前言

1.使用Unity的Standard材质

      首先,为了避免动态批处理影响我们观察GPU Instance的结果,我们要先把动态批处理关掉(在Build Setting中的Player Setting中):

关闭动态批处理

      然后我们在unity中新建一个材质球,把面板上的GPU Instancing选项勾上,新建几个cube得到的结果是这样的:

16个cube的GPUInstancing

     WTF?16个cube居然有67个Batches?这哪里优化了,分明是负优化好吧。。。
     这里我们读一下文档,文档中介绍说我们需要修改一下我们的shader以支持GPUInstancing

#pragma multi_compile_instancing
 struct appdata
            {
                float4 vertex : POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

     因为我使用的是旧版本的unity,看了一下stanard材质中并没有相关的宏定义,以下是我使用的standard材质的shader:

            #pragma shader_feature _NORMALMAP
            #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
            #pragma shader_feature _EMISSION
            #pragma shader_feature _METALLICGLOSSMAP 
            #pragma shader_feature ___ _DETAIL_MULX2
            #pragma shader_feature _PARALLAXMAP
            
            #pragma multi_compile_fwdbase
            #pragma multi_compile_fog
            
            #pragma vertex vertForwardBase
            #pragma fragment fragForwardBase

            #include "UnityStandardCore.cginc"
            ENDCG

2.使用文档提供的demo进行实验

     文档中提供了一个shader和一个脚本作为例子,我们就用我们之前的cube们进行实验。

使用了demo中的shader后

     在使用了文档中提供的例子后,cube们真的可以通过一个DC绘制出来了。

再通过脚本中对颜色的控制,实现了一个DC中绘制不同颜色的相同材质

     shader代码:

Shader "SimplestInstancedShader"
{
    Properties
    {
        _Color("Color", Color) = (1, 1, 1, 1)
    }

        SubShader
    {
        Tags{ "RenderType" = "Opaque" }
        LOD 100

        Pass
    {
        CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"

    struct appdata
    {
        float4 vertex : POSITION;
        UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct v2f
    {
        float4 vertex : SV_POSITION;
        UNITY_VERTEX_INPUT_INSTANCE_ID // necessary only if you want to access instanced properties in fragment Shader.
    };

        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
        UNITY_INSTANCING_BUFFER_END(Props)

        v2f vert(appdata v)
    {
        v2f o;

        UNITY_SETUP_INSTANCE_ID(v);
        UNITY_TRANSFER_INSTANCE_ID(v, o); // necessary only if you want to access instanced properties in the fragment Shader.

        o.vertex = UnityObjectToClipPos(v.vertex);
        return o;
    }

    fixed4 frag(v2f i) : SV_Target
    {
        UNITY_SETUP_INSTANCE_ID(i); // necessary only if any instanced properties are going to be accessed in the fragment Shader.
        return  UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
    }
        ENDCG
    }
    }
}

      C#中,使用同一个Block进行存储不同的颜色值,给相同的材质赋予同一个Block,才能进行批处理,其中除了可以存color类型,还可以存float,texture,Matrix等类型,以实现不同的需求。代码:

        MaterialPropertyBlock props = new MaterialPropertyBlock();
        MeshRenderer renderer;

        foreach (GameObject obj in objects)
        {
            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));

            renderer = obj.GetComponent<MeshRenderer>();
            renderer.SetPropertyBlock(props);
        }

参考资料:

Top