@@ -306,8 +306,46 @@ void Terrain3DMeshAsset::set_scene_file(const Ref<PackedScene> &p_scene_file) {
306
306
_height_offset = 0 .0f ;
307
307
}
308
308
LOG (DEBUG, " Loaded scene with parent node: " , node);
309
- TypedArray<Node> mesh_instances = node->find_children (" *" , " MeshInstance3D" );
310
309
_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
311
349
for (int i = 0 ; i < mesh_instances.size (); i++) {
312
350
MeshInstance3D *mi = cast_to<MeshInstance3D>(mesh_instances[i]);
313
351
LOG (DEBUG, " Found mesh: " , mi->get_name ());
@@ -351,6 +389,10 @@ void Terrain3DMeshAsset::set_scene_file(const Ref<PackedScene> &p_scene_file) {
351
389
emit_signal (" instancer_setting_changed" );
352
390
}
353
391
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
+
354
396
void Terrain3DMeshAsset::set_material_override (const Ref<Material> &p_material) {
355
397
_set_material_override (p_material);
356
398
LOG (DEBUG, " Emitting setting_changed" );
0 commit comments