Babylon.js官⽅性能优化⽂档中⽂翻译
  在这⾥列出Babylon.js官⽅性能优化⽂档的中英⽂对照,并在CardSimulate项⽬⾥对其中的⼀些优化⽅法进⾏实践。
How To
如何
Optimize your scene
优化你的场景
Table of contents 内容列表
1. 如何优化你的场景
1. 使⽤变换节点代替抽象⽹格或者空⽹格
2. 改变每个⽹格的剔除策略
3. 降低着⾊器开销
4. 减少世界矩阵计算
5. 冻结活动的⽹格
6. 减少绘制调⽤
7. 减少gl.clear()的调⽤
8. 使⽤深度预加载
9. 使⽤未索引的⽹格
10. 关闭适应设备⽐例(ratio是⽐例,rate是速率)
11. 停⽌不好的机制
12. 使⽤动画⽐率
13. 处理WebGL上下⽂丢失
14. 处理包含⼤量⽹格的场景
15. 仪表
1. 引擎仪表
2. 场景仪表
2. 进⼀步阅读
1. 更进⼀步
How To Optimize Your Scene
如何优化你的场景
This tutorial will help you find some links and info on how you can improve your scene regarding rendering performance.这篇教程将帮助你到⼀些关于如何提升你的场景的渲染效果的链接和信息
Use TransformNode instead of AbstractMesh or empty meshes
使⽤变换节点代替抽象⽹格或者空⽹格
If you need node containers or transform nodes, do not use meshes but TransformNode instead. Use meshes only when associated with content to render.
如果你需要节点容器或者变换节点,不要使⽤⽹格,⽽是使⽤变换节点来替代。只在有关联的内容需要绘制时使⽤⽹格。
The meshes need to go through an evaluation process where the camera checks if they are in the frustum. This is an expensive process so reducing the number of candidates by using TransformNode when possible is a good practice.
⽹格需要经过⼀个验证流程,在这个流程中相机将检查这些⽹格是否在视截椎体内部。这是⼀个性能消耗较⼤的流程,所以在可能时通过使⽤变换节点减少这种计算的数量是⼀个好的实践。
(使⽤后1366*720分辨率fps似从20提升到22~24)
Changing per mesh culling strategy
改变每个⽹格的剔除策略
Starting with Babylon.js v3.3, you can now specify a strategy used to cull a specific mesh with mesh.cullingStrategy.
从Babylon.js v3.3开始,你可以设定⼀个策略来剔除⼀个特定的⽹格,通过设置mesh.cullingStrategy
You can set it to:
你可以将它设置为:
BABYLON.AbstractMesh.CULLINGSTRATEGY_STANDARD: This is the default value and it will use a combination of bounding sphere culling, bounding box culling and then frustum culling
这是默认值,它将结合使⽤边界球剔除和边界盒剔除,然后是截锥体剔除。
BABYLON.AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY: This strategy will use a bounding sphere culling and then frustum culling. This is faster than the standard one but can imply having more meshes sent to the GPU.
Really useful if you are CPU bound.
这个策略将使⽤边界球剔除,然后是截锥体剔除。这种策略⽐基础的版本更加快速,但是可能引起更多的⽹格被发送到GPU。如果你的CPU资源有限,这种⽅法会很有⽤。(把更多⼯作分给显卡做,但是我的CPU和GPU都不富裕啊)
Reducing Shaders Overhead
降低着⾊器开销
Babylon.js uses an advanced and automatic shaders engine. This system will keep shaders up to date regarding material options. If you are using a static material (ie. an immutable material) then you can let it know to Babylon.js by using the following code:
Babylon.js使⽤⼀种先进的并且⾃动的着⾊器引擎。这个系统将根据材质的选项保持着⾊器的更新。如果你使⽤的是静态材质(⽐如⼀个不变的材质)那么你可以通过以下的代码让Babylon.js知道这⼀点:
material.freeze
();
Once frozen, the shader will remain unchanged even if you change material's properties. You will have to unfreeze it to update the inner shader:
⼀旦被冻结,这个着⾊器将保持不变,即使你改变这个材质的属性(纹理是否属于着⾊器??更换其他材质会不会影响??⽹格的isvisble 会不会影响??)。你必须解冻它才能更新内部的着⾊器:
material.unfreeze
();
(建议在完成静态材质的所有设定之后使⽤,不影响更换其他材质,提升到26~27
Reducing World Matrices Computation
减少世界矩阵计算
Every mesh has a world matrix to specify its position / rotation / scaling. This matrix is evaluated on every frame. You can improve performances by freezing this matrix. Any subsequent changes to position / rotation / scaling will then be ignore:
每个⽹格都有⼀个世界矩阵⽤来确定它的位置、姿态、缩放。这个矩阵在每⼀帧中被计算。你可以通过冻结这个矩阵来改进性能。这样任何传递(通过⽗元素继承?)的位置、姿态、缩放改变都将被忽略:
mesh.freezeWorldMatrix
();
You can unfreeze a mesh with:
你可以使⽤如下代码解冻⼀个⽹格:
mesh.unfreezeWorldMatrix
();
(适合⽤在⽗元素固定不变的情况下,或者提前预知哪⼀帧会发⽣改变,然后只在这⼀帧解冻天空盒如果设置了
infiniteDistance不应冻结世界矩阵,因为天空盒需⼀直移动,冻结地⾯之后静⽌帧数提升⾄29~30
Freezing the active meshes
冻结活动的⽹格
If you are CPU bound, you can decide to keep the list of active meshes unchanged and then free the time spent by the CPU to determine active meshes:
如果你的CPU有限,你可以考虑保持“活动⽹格列表”不变,这样可以节省CPU⽤来判断⽹格是否活动的时间:
scene.freezeActiveMeshes
();
You can unfreeze the active meshes with:
你可以使⽤如下代码解冻活动⽹格列表
scene.unfreezeActiveMeshes
();
Note that you can force a mesh to be in the active meshes before freezing the list with mesh.alwaysSelectAsActiveMesh = true.注意你可以在冻结活动⽹格列表前,强制⼀个⽹格进⼊活动⽹格列表,通过mesh.alwaysSelectAsActiveMesh = true.
(⽐如isVisible==false就是⾮活动⽹格??
Reducing draw calls
减少绘制调⽤
As soon as you can please use as they are drawn with one single draw call.
请尽量快的⽤instances代替通过单独绘制调⽤绘制
If sharing the same material is a problem, you can then think about using clones which share the same geometry with mesh.clone("newName")
如果实例对象必须共⽤同⼀个材质对象是⼀个问题,你可以考虑通过语句mesh.clone("newName")⽤克隆⽅法,克隆⽅法产⽣的mesh共⽤同⼀个多边形对象
(instances可以以⼀个⽹格为基础,复制出许多形状和材质相同的实例,它们的材质和多边形对象都是共⽤的)
Reducing calls to gl.clear()
减少gl.clear()的调⽤
By default, Babylon.js automatically clears the color, depth, and stencil buffers before rendering the scene. It also clears the depth and stencil buffers after switching to a new camera and before rendering a new RenderingGroup. On systems with poor fill rates, these can add up quickly and have a significant impact on performance.
默认情况下,Babylon.js会在渲染场景之前⾃动清空颜⾊、深度和模板缓存。它同样会在切换到新相机之后以及渲染⼀个新的渲染组之前清空深度和模板缓存。对于填充率较低的系统,这些清空操作可以被快速的汇总并且对性能有巨⼤的影响。
If your scene is set up in such a way that the viewport is always 100% filled with opaque geometry (if you're always inside a skybox, for instance), you can disable the default scene clearing behavior with:
如果你的场景按这种⽅式设定:视点⼀直百分之百的被不透明多边形填满(⽐如你⼀直处在⼀个天空盒内部),你可以⽤以下⽅法禁⽤默认的场景清理:
scene.autoClear =
false
;
//
Color
buffer
颜⾊缓存
scene.autoClearDepthAndStencil =
false
;
//
Depth
and
stencil, obviously
显然是深度和模板缓存
If you know that the geometry in a particular RenderingGroup will always be positioned in front of geometry from other groups, you can disable buffer clearing for that group with the following:
如果你确定在某个渲染组中的多边形将⼀直位于其他组的多边形之前,你可以禁⽤这个组的缓存清理,通过以下⽅法:transform中文翻译
scene.setRenderingAutoClearDepthStencil
(
renderingGroupIdx
,
autoClear
,
depth
,
stencil
);
autoClear: true to enable auto clearing. If false, overrides depth and stencil
为真则启动⾃动清理。否则,覆盖深度和模板
depth: Defaults to true to enable clearing of the depth buffer
默认为真启动深度缓存清理
stencil: Defaults to true to enable clearing of the stencil buffer
默认为真启动模板缓存清理
Go ahead and be aggressive with these settings. You'll know if it's not appropriate for your application if you see any smearing!
尽管⼤胆测试这些设置。如果你在你的应⽤中看到任何污渍你将知道这个设置是否适合。
(在场景层⾯执⾏两个⽅法禁⽤了三种缓存,暂时未发现渲染异常,静⽌fps增加到37~38,但是在场景中漫游⼀段时间后fps⼜降低了,但是如果要在场景中使⽤多重半透明材质,还需要更谨慎的测试)
Using depth pre-pass
使⽤深度预传递

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。