前言
我们之前研究过为什么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_f***ase
#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);
}
更多unity2018的功能介绍请到paws3d学习中心查找。