优化 3D 性能

剔除

Godot会自动执行视图视锥剔除, 以防止渲染视口外的物体. 这对于发生在小范围内的游戏来说效果很好, 然而在较大的关卡中, 事情很快就会变得很麻烦.

遮挡剔除

比如走在一个小镇上, 你可能只能看到你所在的街道上的几栋建筑, 以及天空和几只飞过头顶的鸟. 然而就一个天真的渲染器而言, 你仍然可以看到整个小镇. 它不会只渲染你前面的建筑, 它会渲染那后面的街道, 与那条街上的人, 那后面的建筑. 你很快就会遇到这样的情况: 你试图渲染比可见的东西多10倍或100倍的东西.

Things aren’t quite as bad as they seem, because the Z-buffer usually allows the GPU to only fully shade the objects that are at the front. This is called depth prepass and is enabled by default in Godot when using the Forward+ or Compatibility rendering methods. However, unneeded objects are still reducing performance.

我们可以减少算绘量的一种方法是 take advantage of occlusion。Godot 4.0及更高版本提供了一种使用遮挡器节点进行遮蔽剔除的新方法。有关在场景中设定遮蔽剔除的说明,请参阅 遮挡剔除

备注

In some cases, you may have to adapt your level design to add more occlusion opportunities. For example, you may have to add more walls to prevent the player from seeing too far away, which would decrease performance due to the lost opportunities for occlusion culling.

透明物体

Godot通过 MaterialShader 对对象进行排序以提高性能. 然而, 这对透明物体来说是不可能的. 透明物体从后往前渲染, 以便与后面的物体混合. 因此, 尽量少使用透明对象 . 如果一个物体有一小部分是透明的, 尽量让这部分成为一个独立的表面, 有自己的材质.

更多信息请参阅 GPU 优化 文档。

细节程度(LOD)

在某些情况下, 特别是在远处, 用简单的版本**代替复杂的几何图形可能是个好主意. 最终用户可能看不出什么区别. 考虑看看远处的大量树木. 有几种策略可以替换不同距离的模型. 你可以使用较低的多边形模型, 或者使用透明度来模拟更复杂的几何体.

Godot 4 提供了多种控制细节层次的方法:

  • 使用 网格的细节级别(LOD) 进行网格导入的自动方法。

  • 在3D节点中使用 :ref:`doc_visibility_ranges`配置的手动方法。

  • ref:Decals <doc_using_decals> 和 lights 也可以使用它们各自的 Distance Fade 属性从细节级别中获益。

虽然它们可以单独使用,但一起使用时这些方法最有效。例如,你可以设定可见范围来隐藏距离玩家太远而无法注意到的粒子效果。同时,你可以依靠网格LOD来使粒子效果的网格在远处算绘时细节较少。

可见范围也是为远处几何体设定「冒充者」的好方法(见下文)。

Billboard 和 imposter

The simplest version of using transparency to deal with LOD is billboards. For example, you can use a single transparent quad to represent a tree at distance. This can be very cheap to render, unless of course, there are many trees in front of each other. In this case, transparency may start eating into fill rate (for more information on fill rate, see GPU 优化).

另一种方法是不只渲染一棵树, 而是将一些树作为一组来渲染. 如果你能看到一个区域, 但在游戏中不能实际接近它, 这可能是特别有效的.

你可以通过预先渲染对象的不同角度的视图来制作冒牌货. 或者你甚至可以更进一步, 周期性地将一个物体的视图重新渲染到一个纹理上, 作为一个冒牌货使用. 在远处, 你需要将观察者移动相当长的距离, 视角才会发生显著变化. 这可能是复杂的工作, 但可能是值得的, 这取决于你正在制作的项目类型.

使用实例化(MultiMesh)

如果必须在同一地点或附近绘制多个相同的对象, 请尝试使用 MultiMesh 来代替.MultiMesh允许以很小的性能代价来绘制成千上万的对象, 这使得它非常适合用于绘制羊群, 草地, 粒子以及其他任何有成千上万相同对象的地方.

See also the Using MultiMesh documentation.

烘焙照明

Lighting objects is one of the most costly rendering operations. Realtime lighting, shadows (especially multiple lights), and global illumination are especially expensive. They may simply be too much for lower power mobile devices to handle.

Consider using baked lighting, especially for mobile. This can look fantastic, but has the downside that it will not be dynamic. Sometimes, this is a tradeoff worth making.

有关使用烘焙光照贴图的说明,请参阅doc_using_lightmap_gi。为了获得最佳性能,你应该将灯光的烘焙模式设定为 Static ,而不是预设的 Dynamic,因为这将跳过具有烘焙光照的网格体上的即时光照。

使用 Static 烘焙模式的灯光的缺点是,它们无法将阴影投射到具有烘焙照明的网格上。这可以使具有室外环境和动态对象的场景看起来平坦。性能和质量之间的良好平衡是 DirectionalLight3D 节点保持 Dynamic,并对大多数(如果不是全部)泛光灯和聚光灯使用 Static

动画和皮肤

Animation and vertex animation such as skinning and morphing can be very expensive on some platforms. You may need to lower the polycount considerably for animated models, or limit the number of them on screen at any given time. You can also reduce the animation rate for distant or occluded meshes, or pause the animation entirely if the player is unlikely to notice the animation being stopped.

VisibleOnScreenEnabler3DVisibleOnScreenNotifier3D 节点可用于此目的。

庞大的世界

如果你要制作大型游戏, 则与小型游戏可能会有所不同.

大型的世界可能需要用碎片建立, 可以在你在世界中移动时按需加载, 这可以防止内存使用失控, 也可以将所需的处理限制在局部区域.

There may also be rendering and physics glitches due to floating point error in large worlds. This can be resolved using 大世界坐标. If using large world coordinates is not an option, you may be able to use techniques such as orienting the world around the player (rather than the other way around), or shifting the origin periodically to keep things centred around Vector3(0, 0, 0).