Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(lod): a LODGroup with only a cull size is no longer ignored

yhh5028513c0aec
diff --git a/docs/astro/src/content/docs/performance/level-of-detail.mdx b/docs/astro/src/content/docs/performance/level-of-detail.mdx
index 129f96716..495a5a18e 100644
--- a/docs/astro/src/content/docs/performance/level-of-detail.mdx
+++ b/docs/astro/src/content/docs/performance/level-of-detail.mdx
@@ -99,6 +99,7 @@ A field at three levels reaches the GPU as roughly three draws, not as one per p
ends the ladder, and anything after it is never reached (the engine warns).
- **The sizes must descend.** `lod2Size` below `lod1Size`, and so on.
- **`cullSize: 0`** disables culling entirely, so the group is never invisible.
+- **A group may have no stand-in at all.** With only a `cullSize` it draws the renderer's own mesh while it is big enough and nothing once it is smaller: the right group for a tuft of grass, which has no cheaper version and is not worth drawing past a distance.
- **A skinned group must share one skeleton.** Every level of a skinned mesh needs
the same joint count; a level that disagrees is drawn in its bind pose, and the
engine says so.
diff --git a/src/esengine/renderer/plugins/MeshPlugin.cpp b/src/esengine/renderer/plugins/MeshPlugin.cpp
index 1091a1c2a..a6db946a2 100644
--- a/src/esengine/renderer/plugins/MeshPlugin.cpp
+++ b/src/esengine/renderer/plugins/MeshPlugin.cpp
@@ -192,7 +192,10 @@ struct LodLevels {
/**
* @brief Reads a group into the levels it can be drawn at. False when it declares
- * no usable stand-in, which leaves the renderer's own mesh the whole answer.
+ * neither a usable stand-in nor a cull size, which leaves the renderer's own
+ * mesh the whole answer. A group with only a cull size is a real one: the
+ * mesh itself to a distance, then nothing (a tuft of grass has no cheaper
+ * version, and still is not worth drawing past sixty metres).
*/
bool gatherLodLevels(resource::ResourceManager& resources, const ecs::LODGroup& group,
const Mesh& base, LodLevels& out) {
@@ -226,7 +229,7 @@ bool gatherLodLevels(resource::ResourceManager& resources, const ecs::LODGroup&
out.localMin = glm::min(out.localMin, standIn->localMin);
out.localMax = glm::max(out.localMax, standIn->localMax);
}
- return out.set.count > 0;
+ return out.set.count > 0 || out.set.cull > 0.0f;
}
/** @brief Says once what a group declares that nothing will ever use. */
diff --git a/tests/renderer/test_lod_selection.cpp b/tests/renderer/test_lod_selection.cpp
index d7a6c0280..e0c5d4d59 100644
--- a/tests/renderer/test_lod_selection.cpp
+++ b/tests/renderer/test_lod_selection.cpp
@@ -110,6 +110,14 @@ static void testSelection() {
CHECK(lod::selectLevel(set, 0.30f, 0) == 1, "under the first threshold takes level 1");
CHECK(lod::selectLevel(set, 0.10f, 0) == 2, "under the second threshold takes level 2");
CHECK(lod::selectLevel(set, 0.01f, 0) == lod::kCulled, "under the cull size nothing is drawn");
+ {
+ // A group with no stand-in and only a cull size: the mesh itself above it, nothing under it.
+ lod::LevelSet cullOnly;
+ cullOnly.cull = 0.05f;
+ cullOnly.hysteresis = 0.1f;
+ CHECK(lod::selectLevel(cullOnly, 0.5f, 0) == 0, "a cull-only group draws the mesh while it is big enough");
+ CHECK(lod::selectLevel(cullOnly, 0.01f, 0) == lod::kCulled, "a cull-only group draws nothing under its cull size");
+ }
lod::LevelSet noCull = set;
noCull.cull = 0.0f;