Skip to content

Commit

Permalink
Limit render_cull_margin to affect Y axis only
Browse files Browse the repository at this point in the history
  • Loading branch information
TokisanGames committed Aug 19, 2024
1 parent ed3d86d commit 88bb58e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion doc/api/class_terrain3d.rst
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ Tells the renderer how to cast shadows from the terrain onto other objects. This
- |void| **set_cull_margin**\ (\ value\: :ref:`float<class_float>`\ )
- :ref:`float<class_float>` **get_cull_margin**\ (\ )

This margin is added to the terrain bounding box (AABB). The terrain already sets its AABB, so this setting only needs to be used if the shader has expanded the terrain beyond the AABB and the terrain meshes are being culled, as might happen from using :ref:`Terrain3DMaterial.world_background<class_Terrain3DMaterial_property_world_background>` with NOISE and a large height value. This sets ``GeometryInstance3D.extra_cull_margin`` in the engine.
This margin is added to the vertical component of the terrain bounding box (AABB). The terrain already sets its AABB from :ref:`Terrain3DStorage.height_range<class_Terrain3DStorage_property_height_range>`, which is calculated while sculpting. This setting only needs to be used if the shader has expanded the terrain beyond the AABB and the terrain meshes are being culled at certain viewing angles. This might happen from using :ref:`Terrain3DMaterial.world_background<class_Terrain3DMaterial_property_world_background>` with NOISE and a height value larger than the terrain heights. This setting is similar to `GeometryInstance3D.extra_cull_margin`, but it only affects the Y axis.

.. rst-class:: classref-item-separator

Expand Down
2 changes: 1 addition & 1 deletion doc/classes/Terrain3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
Tells the renderer how to cast shadows from the terrain onto other objects. This sets [code skip-lint]GeometryInstance3D.ShadowCastingSetting[/code] in the engine.
</member>
<member name="render_cull_margin" type="float" setter="set_cull_margin" getter="get_cull_margin" default="0.0">
This margin is added to the terrain bounding box (AABB). The terrain already sets its AABB, so this setting only needs to be used if the shader has expanded the terrain beyond the AABB and the terrain meshes are being culled, as might happen from using [member Terrain3DMaterial.world_background] with NOISE and a large height value. This sets [code skip-lint]GeometryInstance3D.extra_cull_margin[/code] in the engine.
This margin is added to the vertical component of the terrain bounding box (AABB). The terrain already sets its AABB from [member Terrain3DStorage.height_range], which is calculated while sculpting. This setting only needs to be used if the shader has expanded the terrain beyond the AABB and the terrain meshes are being culled at certain viewing angles. This might happen from using [member Terrain3DMaterial.world_background] with NOISE and a height value larger than the terrain heights. This setting is similar to `GeometryInstance3D.extra_cull_margin`, but it only affects the Y axis.
</member>
<member name="render_layers" type="int" setter="set_render_layers" getter="get_render_layers" default="2147483649">
The render layers the terrain is drawn on. This sets [code skip-lint]VisualInstance3D.layers[/code] in the engine. The defaults is layer 1 and 32 (for the mouse cursor). When you set this, make sure the layer for [member render_mouse_layer] is included, or set that variable again after this so that the mouse cursor works.
Expand Down
27 changes: 11 additions & 16 deletions src/terrain_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,41 +951,36 @@ void Terrain3D::update_aabbs() {
height_range.y += abs(height_range.x); // Add below zero to total size

AABB aabb = RS->mesh_get_custom_aabb(_meshes[GeoClipMap::CROSS]);
aabb.position.y = height_range.x;
aabb.size.y = height_range.y;
aabb.position.y = height_range.x - _cull_margin;
aabb.size.y = height_range.y + _cull_margin * 2.f;
RS->instance_set_custom_aabb(_data.cross, aabb);
RS->instance_set_extra_visibility_margin(_data.cross, _cull_margin);

aabb = RS->mesh_get_custom_aabb(_meshes[GeoClipMap::TILE]);
aabb.position.y = height_range.x;
aabb.size.y = height_range.y;
aabb.position.y = height_range.x - _cull_margin;
aabb.size.y = height_range.y + _cull_margin * 2.f;
for (int i = 0; i < _data.tiles.size(); i++) {
RS->instance_set_custom_aabb(_data.tiles[i], aabb);
RS->instance_set_extra_visibility_margin(_data.tiles[i], _cull_margin);
}

aabb = RS->mesh_get_custom_aabb(_meshes[GeoClipMap::FILLER]);
aabb.position.y = height_range.x;
aabb.size.y = height_range.y;
aabb.position.y = height_range.x - _cull_margin;
aabb.size.y = height_range.y + _cull_margin * 2.f;
for (int i = 0; i < _data.fillers.size(); i++) {
RS->instance_set_custom_aabb(_data.fillers[i], aabb);
RS->instance_set_extra_visibility_margin(_data.fillers[i], _cull_margin);
}

aabb = RS->mesh_get_custom_aabb(_meshes[GeoClipMap::TRIM]);
aabb.position.y = height_range.x;
aabb.size.y = height_range.y;
aabb.position.y = height_range.x - _cull_margin;
aabb.size.y = height_range.y + _cull_margin * 2.f;
for (int i = 0; i < _data.trims.size(); i++) {
RS->instance_set_custom_aabb(_data.trims[i], aabb);
RS->instance_set_extra_visibility_margin(_data.trims[i], _cull_margin);
}

aabb = RS->mesh_get_custom_aabb(_meshes[GeoClipMap::SEAM]);
aabb.position.y = height_range.x;
aabb.size.y = height_range.y;
aabb.position.y = height_range.x - _cull_margin;
aabb.size.y = height_range.y + _cull_margin * 2.f;
for (int i = 0; i < _data.seams.size(); i++) {
RS->instance_set_custom_aabb(_data.seams[i], aabb);
RS->instance_set_extra_visibility_margin(_data.seams[i], _cull_margin);
}
}

Expand Down Expand Up @@ -1329,7 +1324,7 @@ void Terrain3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "render_layers", PROPERTY_HINT_LAYERS_3D_RENDER), "set_render_layers", "get_render_layers");
ADD_PROPERTY(PropertyInfo(Variant::INT, "render_mouse_layer", PROPERTY_HINT_RANGE, "21, 32"), "set_mouse_layer", "get_mouse_layer");
ADD_PROPERTY(PropertyInfo(Variant::INT, "render_cast_shadows", PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"), "set_cast_shadows", "get_cast_shadows");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "render_cull_margin", PROPERTY_HINT_RANGE, "0, 10000, 1, or_greater"), "set_cull_margin", "get_cull_margin");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "render_cull_margin", PROPERTY_HINT_RANGE, "0.0,10000.0,.5,or_greater"), "set_cull_margin", "get_cull_margin");

ADD_GROUP("Collision", "collision_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collision_enabled"), "set_collision_enabled", "get_collision_enabled");
Expand Down

0 comments on commit 88bb58e

Please sign in to comment.