没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Catlike Coding
Unity C# Tutorials
Rendering 19 GPU Instancing
Render a boatload of spheres.
Add support for GPU instancing.
Use material property blocks.
Make instancing work with LOD groups.
This is part 19 of a tutorial series about rendering. The previous part covered
realtime GI, probe volumes, and LOD groups. This time we'll add support for another
way to consolidate draw calls into batches.
Thousands of spheres, rendered in a few dozen batches.
1 Batching Instances
Instructing the GPU to draw something takes time. Feeding it the data to do so,
including the mesh and material properties, takes time as well. We already know of
two ways to decrease the amount of draw calls, which are static and dynamic
batching.
Unity can merge the meshes of static objects into a larger static mesh, which reduces
draw calls. Only objects that use the same material can be combined in this way. This
comes at the cost of having to store more mesh data. When dynamic batching is
enabled, Unity does the same thing at runtime for dynamic objects that are in view.
This only works for small meshes, otherwise the overhead becomes too great.
There is yet another way to combine draw calls. It is know as GPU instancing or
geometry instancing. Like dynamic batching, this is done at runtime for visible
objects. The idea is that the GPU is told to render the same mesh multiple times in
one go. So it cannot combine di!erent meshes or materials, but it's not restricted to
small meshes. We're going to try out this approach.
1.1 Many Spheres
To test GPU instancing, we need to render the same mesh many times. Let's create a
simple sphere prefab for this, which uses our white material.
White sphere prefab.
To instantiate this sphere, create a test component which spawns a prefab many
times and positions it randomly inside a spherical area. Make the spheres children of
the instantiator so the editor's hierarchy window doesn't have to struggle with
displaying thousands of instances.
using UnityEngine;
public class GPUInstancingTest : MonoBehaviour {
public Transform prefab;
public int instances = 5000;
public float radius = 50f;
void Start () {
for (int i = 0; i < instances; i++) {
Transform t = Instantiate(prefab);
t.localPosition = Random.insideUnitSphere * radius;
t.SetParent(transform);
}
}
}
Create a new scene and put a test object in it with this component. Assign the sphere
prefab to it. I'll use it to create 5000 sphere instances inside a sphere of radius 50.
Test object.
With the test object positioned at the origin, placing the camera at (0, 0, -100)
ensures that the entire ball of spheres is in view. Now we can use the statistics panel
of the game window to determine how all the objects are drawn. Turn o! the
shadows of the main light so only the spheres are drawn, plus the background. Also
set the camera to use the forward rendering path.
A sphere of spheres.
In my case, it takes 5002 draw calls to render the view, which is mentioned as
Batches
in the statistics panel. That's 5000 spheres plus two extra for the
background and camera e!ects.
Note that the spheres are not batched, even with dynamic batching enabled. That's
because the sphere mesh is too large. Had we used cubes instead, they would've
been batched.
剩余23页未读,继续阅读
资源评论
风流不在人知
- 粉丝: 8
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功