美文网首页Unity
[unity]GPU Instance学习

[unity]GPU Instance学习

作者: 江枫枫Maple | 来源:发表于2019-05-05 13:30 被阅读191次

前言

      我们之前研究过为什么Unity的UI可以合批,是因为使用了相同的材质进行渲染,UI上不同图片渲染是通过把图片打成一张图集后,使用Image组件对顶点填充了不同的UV值实现的。
      那么有没有什么办法可以让3D的物体也像UI一样,使用相同材质,但是可以表现出不一样的样子呢(比如颜色/位置等)?
      我们知道unity有两种传统的批处理的方式:静态批处理,动态批处理。其中动态批处理可以实现让物体使用相同的材质,拥有不同的位置信息。但是动态批处理的局限性很高(顶点数限制,PASS数限制等)。
      Unity在5.4及以后的版本支持了一种新的批处理方式:GPU Instancing。通过这种方式,我们可以给同一材质传递一些不同的信息,进而渲染出不同的效果。
      GPU Instancing官方文档

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);
        }

参考资料:

GPU Instancing官方文档
http://www.cnblogs.com/murongxiaopifu/p/7250772.html
http://www.manew.com/thread-50914-1-1.html

相关文章

网友评论

    本文标题:[unity]GPU Instance学习

    本文链接:https://www.haomeiwen.com/subject/kbyunqtx.html