1. 概述
在前兩篇文章《Unity3D學(xué)習(xí)筆記6——GPU實(shí)例化(1)》《Unity3D學(xué)習(xí)筆記6——GPU實(shí)例化(2)》分別介紹了通過簡單的頂點(diǎn)著色器+片元著色器,以及通過表面著色器實(shí)現(xiàn)GPU實(shí)例化的過程。而在Unity的官方文檔Creating shaders that support GPU instancing里,也提供了一個(gè)GPU實(shí)例化的案例,這里就詳細(xì)論述一下。
2. 詳論
2.1. 自動(dòng)實(shí)例化
一個(gè)有意思的地方在于,Unity提供的標(biāo)準(zhǔn)材質(zhì)支持自動(dòng)實(shí)例化,而不用像《Unity3D學(xué)習(xí)筆記6——GPU實(shí)例化(1)》《Unity3D學(xué)習(xí)筆記6——GPU實(shí)例化(2)》那樣額外編寫腳本和Shader。并且,會(huì)自動(dòng)將transform,也就是模型矩陣作為每個(gè)實(shí)例的屬性。
照例,還是編寫一個(gè)腳本掛到一個(gè)空的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()
{
}
}
這個(gè)腳本的意思是,給掛接的GameObject下新建很多GameObject,它們使用我們傳入的Mesh和Material,但是位置、姿態(tài)和大小是隨機(jī)的。傳入的Mesh使用Unity自帶的膠囊體,Material使用Unity的標(biāo)準(zhǔn)材質(zhì)。運(yùn)行結(jié)果如下:
這個(gè)時(shí)候Unity還沒有自動(dòng)實(shí)例化,打開Frame Debug就可以看到:
這個(gè)時(shí)候我們可以在使用的材質(zhì)上勾選打開實(shí)例化的選項(xiàng):
再次運(yùn)行,就會(huì)在Frame Debug看到Unity實(shí)現(xiàn)了自動(dòng)實(shí)例化,繪制的批次明顯減少,并且性能會(huì)有所提升:
可以看到確實(shí)是自動(dòng)進(jìn)行實(shí)例化繪制了,但是這種方式卻似乎存在實(shí)例化個(gè)數(shù)的上限,所有的實(shí)例化數(shù)據(jù)還是分成了好幾個(gè)批次進(jìn)行繪制。與《Unity3D學(xué)習(xí)筆記6——GPU實(shí)例化(1)》《Unity3D學(xué)習(xí)筆記6——GPU實(shí)例化(2)》提到的通過底層接口Graphic進(jìn)行實(shí)例化繪制相比,效率還是要低一些。
2.2. MaterialPropertyBlock
自動(dòng)實(shí)例化只能將transform,也就是模型矩陣作為每個(gè)實(shí)例的屬性。如果需要增加自己的實(shí)例屬性,就需要使用MaterialPropertyBlock,也就是材質(zhì)屬性塊。
修改上面的腳本:
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()
{
}
}
腳本使用的材質(zhì),其使用的Shader如下,可以直接在Standard Surface Shader的基礎(chǔ)上改:
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"
}
關(guān)鍵的代碼在于Unity內(nèi)置宏UNITY_INSTANCING_BUFFER_START和UNITY_INSTANCING_BUFFER_END、UNITY_DEFINE_INSTANCED_PROP定義了實(shí)例化屬性,在著色器中,通過內(nèi)置宏UNITY_ACCESS_INSTANCED_PROP來獲取這個(gè)屬性值。這個(gè)實(shí)例化屬性也就是腳本代碼中MaterialPropertyBlock傳入的顏色值。
查看Unity Shader源代碼,這四個(gè)用于實(shí)例化的宏封裝的是一個(gè)cbuffer數(shù)組,cbuffer就是hlsl的常量緩沖區(qū):
#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
運(yùn)行的結(jié)果如下:
可以看到除了紋理,每一個(gè)膠囊體還獲取了隨機(jī)賦予給材質(zhì)的顏色,也就是我們設(shè)置的顏色成為了實(shí)例化屬性數(shù)據(jù)。MaterialPropertyBlock主要由Graphics.DrawMesh和Renderer.SetPropertyBlock使用,在希望繪制具有相同材質(zhì),但屬性略有不同的多個(gè)對象時(shí)可使用它。
個(gè)人認(rèn)為使用MaterialPropertyBlock自動(dòng)實(shí)例化性能比不上使用Graphics.DrawMeshInstancedIndirect(),但是它有個(gè)優(yōu)點(diǎn)是實(shí)例化的要求沒那么高,Graphics.DrawMeshInstancedIndirect()要求使用同一mesh,同一貼圖;但是MaterialPropertyBlock沒這個(gè)要求,只要是同一材質(zhì),任何屬性不一樣都可以用,在減少繪制批次的同時(shí)還能減少材質(zhì)的個(gè)數(shù)。文章來源:http://www.zghlxwxcb.cn/news/detail-421138.html
3. 參考
- 《Unity3D學(xué)習(xí)筆記6——GPU實(shí)例化(1)》
- 《Unity3D學(xué)習(xí)筆記6——GPU實(shí)例化(2)》
- Creating shaders that support GPU instancing
- MaterialPropertyBlock
具體實(shí)現(xiàn)代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-421138.html
到了這里,關(guān)于Unity3D學(xué)習(xí)筆記8——GPU實(shí)例化(3)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!