Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1a0b6d9

Browse files
committedFeb 6, 2025
feat: flexible LOD loading
1 parent 290848a commit 1a0b6d9

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
 

‎src/terrain_3d_mesh_asset.cpp

+43-1
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,46 @@ void Terrain3DMeshAsset::set_scene_file(const Ref<PackedScene> &p_scene_file) {
306306
_height_offset = 0.0f;
307307
}
308308
LOG(DEBUG, "Loaded scene with parent node: ", node);
309-
TypedArray<Node> mesh_instances = node->find_children("*", "MeshInstance3D");
310309
_meshes.clear();
310+
311+
// Look for MeshInstance3D nodes
312+
TypedArray<Node> mesh_instances;
313+
bool meshes_found = false;
314+
315+
// First look for XXXXLOD# meshes
316+
mesh_instances = node->find_children("*LOD?", "MeshInstance3D");
317+
if (mesh_instances.size() > 0) {
318+
LOG(INFO, "Found ", mesh_instances.size(), " meshes using LOD# naming convention, using the first ", MAX_LOD_COUNT);
319+
meshes_found = true;
320+
// mesh_instances.sort_custom(callable_mp_static(&Terrain3DMeshAsset::sort_lod_nodes));
321+
}
322+
323+
// Fallback to all the meshes
324+
if (!meshes_found) {
325+
mesh_instances = node->find_children("*", "MeshInstance3D");
326+
327+
if (mesh_instances.size() > 0) {
328+
LOG(INFO, "No LOD# meshes found, assuming the first ", MAX_LOD_COUNT, " are LOD0-LOD3");
329+
meshes_found = true;
330+
mesh_instances.sort();
331+
}
332+
}
333+
334+
// Fallback to the root mesh
335+
if (!meshes_found) {
336+
if (node->is_class("MeshInstance3D")) {
337+
LOG(INFO, "No LOD# meshes found, assuming the root mesh is LOD0");
338+
mesh_instances.push_back(node);
339+
meshes_found = true;
340+
}
341+
}
342+
343+
// Give up finding meshes
344+
if (!meshes_found) {
345+
LOG(ERROR, "No MeshInstance3D found in scene file");
346+
}
347+
348+
// Now process the meshes
311349
for (int i = 0; i < mesh_instances.size(); i++) {
312350
MeshInstance3D *mi = cast_to<MeshInstance3D>(mesh_instances[i]);
313351
LOG(DEBUG, "Found mesh: ", mi->get_name());
@@ -351,6 +389,10 @@ void Terrain3DMeshAsset::set_scene_file(const Ref<PackedScene> &p_scene_file) {
351389
emit_signal("instancer_setting_changed");
352390
}
353391

392+
bool Terrain3DMeshAsset::sort_lod_nodes(const Node &a, const Node &b) {
393+
return a.get_name().right(1) < b.get_name().right(1);
394+
}
395+
354396
void Terrain3DMeshAsset::set_material_override(const Ref<Material> &p_material) {
355397
_set_material_override(p_material);
356398
LOG(DEBUG, "Emitting setting_changed");

‎src/terrain_3d_mesh_asset.h

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class Terrain3DMeshAsset : public Terrain3DAssetResource {
125125
void set_mesh_count(const int p_count) {} // no-op, used to expose the property in the editor
126126
Ref<Texture2D> get_thumbnail() const { return _thumbnail; }
127127

128+
static bool sort_lod_nodes(const Node &a, const Node &b);
129+
128130
protected:
129131
void _validate_property(PropertyInfo &p_property) const;
130132
static void _bind_methods();

0 commit comments

Comments
 (0)
Please sign in to comment.