【U3D】后期渲染性能优化之---减少Draw Calls的调用

3 篇文章 1 订阅
1 篇文章 0 订阅

本系列文章由CSDN@萌萌的一天 出品,未经博主允许不得转载。



这是我在pencilsquaregames上看到的一篇文章,其中在讲如何减少DrawCalls调用的的问题上有很多新奇的思路,特扒出来来供大家进行学习交流。内容如下:(渣翻见谅 (=ω=)


Reducing Draw Calls (also named SetPass calls) on Unity5.

So I have been working on the standard shader support for Unity 5 and doing some tests regarding how feasible is to reduce draw calls with the new standard shader and actually the results where pretty positive!.

我在着手研究Unity5中的Standard Shader(标准着色器),并且在如何减少Unity中Draw Calls调用的问题上作出了一些有趣的实验,接下来分享给大家一些有用的方法:


(“Draw Calls”在我上篇博文【U3d】渲染统计窗口介绍(Rendering Statistics Window)中有详细的解释)

Lets talk about the standard shader a little bit. The standard shader is a physically based shader that is consistent across diverse lighting conditions, its multi-platform and texture slots enable different shader features.

首先我们来谈谈Standard Shader(标准着色器),Standard Shader是一种基于物理渲染的着色器,它可以支持各种光照条件下的渲染效果,并支持跨平台调用,它的Texture slots(Shader里的纹理槽点)可以调用各种不同的Shader特性。


This standard shader “mutates” and becomes more faster/slower depending on the textures you are using on the texture slots. being said that these two shaders are different (even if they are using the standard shader):

Standard Shader(标准着色器)运行速度的快慢取决于在Shader的Texture slots(纹理槽)里使用了的图片。因此可以说下边的这两个Shaders是完全不同的,虽然它们使用了同样的Standard Shader。



So these two shaders (even if they are “Standard”); they are totally different shaders when rendering a scene, hence they have to be handled totally different if we want to reduce some draw calls 

那么这两个Shaders渲染场景的方式是完全不同的(虽然它们都是Standard Shader(标准着色器)),所以为了能够减少Draw Calls,我们需要想一些办法来处理这两个Shader。


Before Talking about how to reduce draw calls in Unity 5 I will talk about how draw calls are reduced regardless the shader is being used and then I will talk about how to reduce draw calls with the Standard shader in Unity5.

我会先介绍减少Draw Calls通用的做法(不依赖于Shader),然后再讨论Unity5中如何在减少Standard shader (标准着色器)上的Draw Calls。

Reducing Draw Calls in Unity The Manual Way.

So, if we want to reduce draw calls in Unity or in any other engine the way to do it is using as less materials  as possible.
Basically it all gets reduced to these single steps:

1、Sort all the materials and gather them by the type of shader they are using.
2、With the materials that share the same shader gather their textures in a bigger texture (atlas texture).
3、Create one material that will contain the shader and the atlas texture(s) created on step 2.
4、Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.
5、Assign to your remapped meshes the material we created on step 3.


首先讲讲手动减少Draw Calls的方式:

如果我们想要在Unity或者其它引擎上减少Draw Calls的调用,需要尽可能少的调用Materials(材质)。

需要如下几个步骤:

1、整理所有使用的Materials,按照它们的Shader类型将所有Materials进行分类。

2、把所有使用同一个Shader的Material上的纹理放入atlas图集中。(木有NGUI肿么办。。)

3、新创建一个Material,它包含着Shader以及步骤二中所创建的atlas图集。

4、重新绘制网格上的UV纹理坐标,让它可以匹配到atlas图集。

5、把步骤三中创建的Material放到重新绘制的网格上。



1. Sort all the materials and gather them by the type of shader.
I’m going to use for easiness of explanation purposes one simple scene that will have all the meshes use the same type of materials but different textures.
In our case we are going to use this scene (which contains 4 materials each of them with a Bumped Diffuse shader):

1、整理所有的Materials,按照它们的Shader类型将Materials进行分类。

我会用相对简洁的方式来进行说明,这是一个很简单的场景,里边的物体使用同一种类型的Materials(材质),但是贴图不一样。

在这个例子里,一共有4种Materials,每个Material上边都有一个Bumped Diffuse Shader(漫反射着色器)。


Notice the SetPass calls: 4

So we have  for this scene these materials to process:

注意图中一共有个SetPass calls

之后我们要对场景里的里的Material做出如下处理:


(“SetPass Calls”在我上篇博文【U3d】渲染统计窗口介绍(Rendering Statistics Window)中有详细的解释)



2. With the materials that share the same shader gather their textures in a bigger texture (Atlas texture).
In our case we have 2 textures per shader as the Bumped Diffuse shader uses a normal map and a base texture, so what we need to do is to have 2 textures like this:

2、把所有使用同一个Shader的Material上的纹理放入atlas图集中。

在这个例子中,一个Shader上有两种纹理,一个Bumped Diffuse Shader(漫反射着色器)需要用到的法线纹理和一个基本纹理,所需的纹理如下所示:





Save these textures in the project view, we are going to need them later on:

可以把纹理放到Project面板下边,方便今后的使用



3. Create one material that will contain the shader and the atlas texture(s).
Create a material and assign (on this case) the two textures to the material with the shader used (in our case “Bumped Diffuse”):

3、新创建一个Material,它包含着Shader以及步骤二中所创建的atlas图集。

新创建一个材质,材质上包含两个纹理图片,材质的Shader使用“Bumped Diffuse


4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.
This modification can be done on any 3D editor (Maya/ 3D studio / Max / Blender / etc) of your choice.
This is the most boring/demanding step as it involves you modifying your UV coords of your meshes and make them match the  texture’s atlas pixels (the texture we created on step 2).
So what we do is that to each of our UV coords located all around [0,0 – 1,1] we Re-position them on a smaller sub-set of [0,0-1,1] that matches the pixels on the atlas texture we created on step 2.

4、重新绘制网格上的UV纹理坐标,让它可以匹配到atlas图集。

这一步操作可以在任意的3D编辑器中完成。(例如Maya/3D studio/Max/Blender等)。

这也是要求最严格的步骤,你需要仔细修改网格上的UV纹理坐标,让它们与第二步中创建的atlas图集里的纹理对其。

因此我们需要把每一个UV纹理坐标设置在比[0,0,-1,1]更小的子集坐标系内。



And...we do this for each of our meshes that share the same shader.

我们要对每一个含有“ Bumped Diffuse “Shader的网格都做这样的操作,

 5. Assign to the remapped meshes the material created on step 3.
We are almost there! still we have a couple of more steps to do:

1、Select all your UV-remapped-meshes and place them where the old meshes where located and deactivate the old meshes.
2、Assign the created material on step 3 to all your UV-remapped-meshes.

5、把步骤三中创建的Material放到重新绘制的网格上。

我们终于快要搞定啦~~~\(≧▽≦)/~,接下来还有2个步骤:

1、选择在步骤三中重新绘制的所有网格,替换掉之前的网格(就是重做时的网格),并且把旧的网格禁用掉~~~~

2、把重新绘制的网格拖到第三步创建的材质上边。




And by here, we are finished!, now lets hit play:

到这里就完成所有的工作啦~~~现在点击一下Play按钮



Before Optimizing if you go back to Step 1, we had 4 SetPass calls and after all the optimization has been done we end up with 1 SetPass calls!. which is amazing! its 75% draw call reduce on this case.
可以看到,在第一步我们优化之前时,SetPass calls数目为4,优化后数字变成了1,是不是很神奇呢~~~~在这个案例中我们把Draw calls减少了75%

Reducing Draw Calls in Unity 5 with the Standard shader.

By here you should know more or less how the optimization process is done with the standard shader. as I wrote on the beginning,  the standard shader internally “mutates” and becomes more faster/slower depending on the textures you are using on the shader’s texture slots (and also renders differently depending on the textures that are used). being said that we have to gather our object’s materials if they are using the Standard shader by the textures used. i.e: Gather all the meshes that only have colors; Gather all the meshes that only have an Albedo texture.. and so on.

Here’s a small scene I created for testing purposes:

在Unity5引擎中使用Standard shader(标准着色器)减少Draw Calls

到这里,我们已经了解到了一些关于Standard shader的优化方法。就像我刚开始说的那样,tandard Shader运行速度的快慢取决于在Shader的Texture slots(纹理槽)里使用的图片。如果材质都是用统一的Standard shader,我们可以将它们进行合并。比如可以合并所有只有颜色的网格,合并所有含一个Albedo texture(Albedo纹理)的网格等等。

下边是一个测试用的场景:



If you compare the original draw calls (91) and the optimized scene’s draw calls (22) you can see that there’s ~75% draw call decrease!. Which is amazing!
Also, if you can try to combine meshes as much as possible, this also helps a lot!.

 可以观察到,优化前场景中有91个Draw calls,优化后只有22个,Draw Calls减少了75%。

所以如果可以尽可能合并物体上的网格,在性能优化上会有很大的帮助。


Reducing Draw Calls on Unity Automatically

Knowing that this is a really cumbersome process I decided to create a tool that automatically gathers your objects and sort them by the type of shaders they use and automagically “Bakes” all your unoptimized scenes!, you can find the package here, and if your scene only uses Diffuse and Bumped Diffuse shaders please be my guest and use it for free!
 在Unity引擎中自主进行Draw Calls渲染优化

这是一个比较复杂的过程,我打算创建一个帮助工具,它可以自动收集场景中的Objects,并且按照身上的Shader类型进行自动分类,所有未优化的Scene会自动进行烘焙。你可以在这儿找到相关的插件包,如果你的Scene上只是用了Diffuse和Bumped Diffuse Shaders,那么欢迎您下载并且使用它~~~忘了说了,这是免费的 

 

如果喜欢图像渲染优化方面的资料,欢迎订阅我的博客,我会经常发布相关的国内外资料~~~>▽< 


 


  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值