前言
? ? ? 我們之前研究過為什么Unity的UI可以合批,是因為使用了相同的材質(zhì)進行渲染,UI上不同圖片渲染是通過把圖片打成一張圖集后,使用Image組件對頂點填充了不同的UV值實現(xiàn)的。
? ? ? 那么有沒有什么辦法可以讓3D的物體也像UI一樣,使用相同材質(zhì),但是可以表現(xiàn)出不一樣的樣子呢(比如顏色/位置等)?
? ? ? 我們知道unity有兩種傳統(tǒng)的批處理的方式:靜態(tài)批處理,動態(tài)批處理。其中動態(tài)批處理可以實現(xiàn)讓物體使用相同的材質(zhì),擁有不同的位置信息。但是動態(tài)批處理的局限性很高(頂點數(shù)限制,PASS數(shù)限制等)。
? ? ? Unity在5.4及以后的版本支持了一種新的批處理方式:GPU Instancing。通過這種方式,我們可以給同一材質(zhì)傳遞一些不同的信息,進而渲染出不同的效果。
? ? ? GPU Instancing官方文檔
1.使用Unity的Standard材質(zhì)
? ? ? 首先,為了避免動態(tài)批處理影響我們觀察GPU Instance的結(jié)果,我們要先把動態(tài)批處理關(guān)掉(在Build Setting中的Player Setting中):

? ? ? 然后我們在unity中新建一個材質(zhì)球,把面板上的GPU Instancing選項勾上,新建幾個cube得到的結(jié)果是這樣的:

? ? ?WTF?16個cube居然有67個Batches?這哪里優(yōu)化了,分明是負優(yōu)化好吧。。。
? ? ?這里我們讀一下文檔,文檔中介紹說我們需要修改一下我們的shader以支持GPUInstancing
#pragma multi_compile_instancing
struct appdata
{
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
? ? ?因為我使用的是舊版本的unity,看了一下stanard材質(zhì)中并沒有相關(guān)的宏定義,以下是我使用的standard材質(zhì)的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們進行實驗。

? ? ?在使用了文檔中提供的例子后,cube們真的可以通過一個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進行存儲不同的顏色值,給相同的材質(zhì)賦予同一個Block,才能進行批處理,其中除了可以存color類型,還可以存float,texture,Matrix等類型,以實現(xiàn)不同的需求。代碼:
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