与静态合批动态合批一样,GPU实例化的目的是对于多个网格同一个材质不同属性,尽可能减少Draw Call的次数,减少合批数量进而达到提高性能的目的
简单的GPU实例化的案例实现
首先创建一个基本的c#脚本
主要的代码是在Start中,设定游戏一开始计算一个for循环,当 i 小于我们设定的固定数量后,结束循环,在循环体中加入下面的计算
固定写法Instantiate
第一个参数是源数据,即我们需要实例化的模型prefabTree
第二个参数是position,我们用一个三维向量来填充x,y,z,其中,x,z设置一个随机值,所以在上方声明一个圆形范围的随机值Random.insideUnitCircle,乘以我们设定的Range以控制范围大小
第三个参数是旋转,使用默认参数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Practic_Instance : MonoBehaviour
{
// Start is called before the first frame update
[Header("生成对象")]
public GameObject prefabTree;
[Header("生成数量")]
public int Count;
[Header("生成范围")]
public float Range = 10;
void Start()
{
for (int i = 0; i < Count; i++)
{
Vector2 pos = Random.insideUnitCircle * Range;
GameObject tree = Instantiate(prefabTree,new Vector3(pos.x,6,pos.y),Quaternion.identity);
}
}
// Update is called once per frame
}
设置好暴露参数后我们尝试运行

效果是有了,但是合批数量依然是每个网格进行一次Draw

最低0.47元/天 解锁文章
254

被折叠的 条评论
为什么被折叠?



