Draw the same mesh multiple times using GPU instancing.
可以免去創建和管理gameObj的開銷
並不是立即繪制,如需:Graphics.DrawMeshNow
每幀調用一次,當幀發送繪制請求
Meshes are not further culled by the view frustum or baked occluders, nor sorted for transparency or z efficiency.
You can only draw a maximum of 1023 instances at once
SE 3.0 以上支持
材質要勾上 enableInstancing
總的檢查:material.enableInstancing && SystemInfo.supportsInstancing
過程中遇到的問題:
導入mesh,的Use File Scale 勾上就不顯示??? 模型的縮放調整有問題,去掉后原本的模型直接放顯得特別大
感覺像是這個函數強行用了一次file scale,再用會變得更小(函數自身*導入參數)
去掉設置,這個函數的變正常,但直接拖到游戲就不對,應該是個bug
public class GpuInstancingTest : MonoBehaviour {
public Mesh mesh;
public Material material;
List<Matrix4x4> matrices = new List<Matrix4x4>();
void Start()
{
if (!material.enableInstancing || !SystemInfo.supportsInstancing)
{
enabled = false;
return;
}
for (int i = 0; i < 10; ++i)
{
Matrix4x4 mt = Matrix4x4.TRS(transform.position + (i * 1.1f) * Vector3.up, transform.rotation, Vector3.one);
matrices.Add(mt);
}
}
// Update is called once per frame
void Update () {
Graphics.DrawMeshInstanced(mesh, 0, material, matrices);
}
}
x
26
1
public class GpuInstancingTest : MonoBehaviour {
2
public Mesh mesh;
3
public Material material;
4
5
List<Matrix4x4> matrices = new List<Matrix4x4>();
6
7
void Start()
8
{
9
if (!material.enableInstancing || !SystemInfo.supportsInstancing)
10
{
11
enabled = false;
12
return;
13
}
14
15
for (int i = 0; i < 10; ++i)
16
{
17
Matrix4x4 mt = Matrix4x4.TRS(transform.position + (i * 1.1f) * Vector3.up, transform.rotation, Vector3.one);
18
matrices.Add(mt);
19
}
20
}
21
22
// Update is called once per frame
23
void Update () {
24
Graphics.DrawMeshInstanced(mesh, 0, material, matrices);
25
}
26
}
