遮挡器节点

除了通过 房间和入口 实现遮挡外,Godot 还提供了使用简单几何 Occluder(遮挡器)节点进行简单遮挡的能力。这些几何体形状会在编辑器中以控制器的形式显示,在运行时不可见。

完全被该形状遮挡(位于背部或内部)的对象在运行时会被剔除。它们是以简单易用、不带来运行时负担为目的而设计的,但也因此可能没有 房间和入口 高效。然而,在某些情况下仍然会带来显著的性能提升。

备注

It is important to understand that geometric occluders work by testing the axis aligned bounding box (AABB) of the occludee against the occluder. The AABB must be fully occluded to be culled. The consequence of this is that smaller objects are more likely to be effectively culled than larger objects, and larger occluders tend to be much more effective than smaller ones.

Occluder 节点的主要优势在于它们是完全动态的。例如你为宇宙飞船添加了遮挡器子节点,那么这个遮挡器就会跟随父对象移动。

Occluder 节点之所以对性能影响那么小,是因为引擎在运行时会根据当前 Camera 的视角动态选择最相关的遮挡器。也就是说, 你可以在一个场景里拥有上百个遮挡器,在同一时间,只有那个最相关的会被激活。

Occluder 节点自身就是 OccluderShape 资源的持有者,功能顾名思义。首先,在你的场景树中添加一个 Occluder 节点。

小技巧

你会看到一个黄色的警告三角形,表示在 Occluder 可以生效之前,你必须在检查器中为其设置一个 OccluderShape。

OccluderShapeSphere

球体是最简单快速的遮挡器之一,设置和摆放都很方便。缺点是球体往往只在特定的游戏关卡设计中有意义,更适用于地形和有机背景几何体。

添加 OccluderNode 并在检查器中新建 OccluderShapeSphere 后,点击检查器中的 OccluderShapeSphere 调出它的参数。

../../_images/occluder_shape_sphere_inspector.png

与很多 Node 节点不同,OccluderShapeSphere 可以在一个对象中存储多个球体。在引擎中更高效,也能让你的 SceneTree 场景树更清爽。你不必把所有的球体都保存在同一个 Occluder 里,管理起来会很麻烦,加 10 个左右的球体是比较合理的。这些球体非常轻量,而且通常加得越多越能与几何体相匹配。

为了保存多个球体,它们会被存入 Array 数组。你可以在检查器中点击 Array,然后把 Array 的大小增大,来添加一个球体。

../../_images/occluder_shape_sphere_terrain.png

编辑器窗口中,这个球体会显示为一个小小的粉色圆形物体。每个球体有两个句柄。中间较大的的句柄可以用来在 Occluder 的局部空间中移动球体,较小的句柄可以用来调整半径。

虽然你也可以通过在检查器中修改 Occluder 节点的变换属性来改变这个球体的位置,但是这样就会移动数组中的所有球体。如果你想在一个遮挡器中使用多个球体,请使用那些句柄来调整位置。因为要支持 3D 空间中的定位,控制器会根据编辑器的视点,只允许移动 3D 位置中的两个主轴。百闻不如一试。

小技巧

使用多个球体时,编辑器还提供了一个便捷的功能。如果你单击工具栏上的居中节点按钮,就会重新计算这些球体相对于整个节点的局部位置,然后修改 Occluder 节点的变换。这个功能可以让排布小球变得更方便。

在运行时,你可以通过修改 Occluder 节点的可见性来开关这些球体,这个节点也能进行移动、缩放、旋转等操作。

球体遮挡器的常见使用场景是为山体/丘陵地形提供遮挡。把球体放在山体内部,你就可以防止渲染山背面的树木、植物、建筑、对象等内容。如果发挥想象力,还可以把它们用于大型飞行器、星球等移动对象。

OccluderShapePolygon

The polygon is a generalist occluder. It can be made to work well in almost all situations, and can quickly provide a degree of occlusion culling to most scenes.

As with all geometric occluders, the key to success is to make them large. They do not have to match rendered geometry, and in many cases they will work better if you extend them past rendered geometry to make them as big as possible (without blocking legitimate lines of sight). The reason why they need to be large is that in general, they will only cull objects whose AABB is completely hidden by the polygon. For large objects to be culled, you will need large occluders.

备注

Like all occluders, polygons can overlap, and in many cases they will work better if you overlap them (they are more likely to cull objects on boundaries).

Editing and details

Occluder polygons are edited as a list of points which define a convex polygon, on a single plane. In order to confine the polygon to a single plane, the points are defined in 2D space rather than 3D. The orientation, position and scale of the polygon is taken instead from the transform of the Occluder Node.

../../_images/occluder_shape_polygon_inspector.png

If you create an Occluder and add to it a OccluderShapePolygon resource, by default it will create 4 starting points forming a rectangle. If you move the position and rotation of the Occluder Node you will see how the rectangle follows the node. When the Occluder is selected in the editor, handles will appear for each of the points. You can actually click and drag these handles, to match your polygon to the environment of your scene.

../../_images/occluder_shape_polygon.png

You are not restricted to 4 points, you can add and remove points in the Inspector, but note that:

  • The editor will automatically sanitize your points to form a convex polygon. If you drag a point into a position that would form a concave polygon, it will be ignored.

  • In general, the less edges (and thus points), the faster the polygon will work at runtime. A polygon with 6 edges will have to make twice the calculations of a polygon with 3 edges. In most cases 4 is a good number.

../../_images/occluder_shape_polygon2.png

Holes

Real world game levels don't always have large continuous areas that should be occluded. Often walls will have a door or windows, caves will have an entrance, etc. In some cases we have to make do by placing several OccluderShapePolygons around such an opening, but Occluder polygons have one more trick up their sleeve - they can have "holes".

In the inspector you will notice that as well as a set of points for the polygon, the polygon has a set of points for a single "hole". If you add 3 or 4 to your polygon, you will see they appear in the editor as smaller handles. You can drag these around just like the polygon handles, to form a convex hole.

../../_images/occluder_shape_polygon_hole.png

The hole can be totally within the polygon (such as a window), abutting the edge (like a door) or crossing the edge of the polygon completely, to make the occluder concave. The way the hole works is that the culling follows a simple rule:

  • If the object to be culled is totally hidden by the polygon, it then looks to see whether it can be seen through the hole. If the object touches the hole, it is not culled, but if hidden by the polygon and not seen through the hole it is culled.

备注

Placing holes is usually far more convenient, and works faster and better at runtime, than creating lots of smaller OccluderShapePolygons.

Hole Limits

The main limitation of holes is that there can only be one per polygon. If you have a situation which requires two or more holes, you have a choice:

  • Combine the area of the two holes into one bigger hole (if they are close together).

  • Use two or more OccluderPolygons.

小技巧

Remember that if you are using more than one polygon, they can overlap, and you should use this to your advantage.

How many Occluder polys are needed?

This very much depends on your scene, but generally you can start getting a good benefit from 3 or 4 well placed polygons. After that it is totally up to you how much time you want to spend. Placing occluders is a bit of an art form, and you will get better at it and learn new tricks the more you work with them.

Some ideas:

  • Build your levels to take advantage of occlusion.

This is one of the secrets of the pros. A good level design is not just about what the gameplay demands, it should also be built with occlusion in mind.

  • When in a building with multiple floors, try placing an occluder polygon between each floor, with a hole for where the staircase transitions between them. This can potentially cull out entire floors and greatly improve performance.

  • Don't be afraid to extend your occluder polygons far past the edges of visible geometry to cull more objects - for instance far into the ground or sky.

Using polygons dynamically

Like all geometric occluders, polygons are not confined to static (non-moving) geometry. You can place them on moving objects. You can even change the relative position of the points in realtime.

Some guidelines:

  • There is a slight cost to moving polygons, the points of the polygons must be transformed on the CPU each time their transform changes.

This is not something to worry excessively about, even CPUs are reasonably fast at transforming points, and generally polygons have very few points compared to rendered geometry. But it is something to consider, if you wanted to create e.g. 10,000 simultaneous spaceships all with occluder polys.

  • Changing the transform of the polygon Node (e.g. by moving the parent object) is cheaper than changing the points themselves. Use the former rather than the latter wherever possible.