11#include " XACLoader.hpp"
22#include < cstdint>
3+ #include < vector>
34
45#include " XACUtilities.hpp"
56#include " godot_cpp/classes/array_mesh.hpp"
@@ -331,15 +332,10 @@ struct byte_array_wrapper : std::span<const T> {
331332 std::span<const T>(reinterpret_cast <T const * const >(source.data()), source.size() / sizeof (T)) {}
332333};
333334
334- MeshInstance3D* XacLoader::_build_mesh (mesh_t const & mesh_chunk, skinning_t * skin, std::vector<material_mapping> const & materials) {
335+ std::vector< MeshInstance3D*> XacLoader::_build_mesh (mesh_t const & mesh_chunk, skinning_t * skin, std::vector<material_mapping> const & materials) {
335336 static const uint32_t EXTRA_CULL_MARGIN = 2 ;
336337
337- Ref<ArrayMesh> mesh = Ref<ArrayMesh>();
338- mesh.instantiate ();
339-
340- MeshInstance3D* mesh_inst = memnew (MeshInstance3D);
341- mesh_inst->set_extra_cull_margin (EXTRA_CULL_MARGIN );
342- mesh_inst->set_mesh (mesh);
338+ std::vector<MeshInstance3D*> meshes = {}; // return value
343339
344340 byte_array_wrapper<vec3d_t > verts;
345341 byte_array_wrapper<vec3d_t > normals;
@@ -392,9 +388,17 @@ MeshInstance3D* XacLoader::_build_mesh(mesh_t const& mesh_chunk, skinning_t* ski
392388 Ref<SurfaceTool> st = Ref<SurfaceTool>();
393389 st.instantiate ();
394390
395- int32_t surface = 0 ;
396391 // for now we treat a submesh as a godot mesh surface
397392 for (submesh_t const & submesh : mesh_chunk.submeshes ) {
393+ // one mesh per submesh so that they can use different instance uniform parameters on the same material
394+ Ref<ArrayMesh> mesh = Ref<ArrayMesh>();
395+ mesh.instantiate ();
396+
397+ MeshInstance3D* mesh_inst = memnew (MeshInstance3D);
398+ mesh_inst->set_extra_cull_margin (EXTRA_CULL_MARGIN );
399+ mesh_inst->set_mesh (mesh);
400+
401+
398402 st->begin (Mesh::PRIMITIVE_TRIANGLES );
399403
400404 for (int j=0 ; j<submesh.relative_indices .size (); j++) {
@@ -432,29 +436,16 @@ MeshInstance3D* XacLoader::_build_mesh(mesh_t const& mesh_chunk, skinning_t* ski
432436 st->add_vertex (vec3d_to_godot (verts[rel_index],true ));
433437 }
434438
435- // Array arr = st->commit_to_arrays();
439+ material_mapping const & material_mapping = materials[submesh.packed .material_id ];
440+ st->set_material (material_mapping.godot_material );
436441
437442 mesh = st->commit (mesh); // add a new surface to the mesh
438-
439- // mesh_inst->set_mesh(mesh);
440443 st->clear ();
441444
442- // mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
443-
444445 vert_total += submesh.packed .vertices_count ;
445446
446447 // setup materials for the surface
447- material_mapping const & material_mapping = materials[submesh.packed .material_id ];
448- // mesh->get_surface_count()-1
449- mesh->surface_set_material (surface++, material_mapping.godot_material );
450-
451- // mesh_inst->set_surface_override_material(surface++, material_mapping.godot_material);
452448 if (material_mapping.diffuse_texture_index != -1 ) {
453- // mesh_inst->set_surface_override_material(int32_t p_surface, const Ref<Material> &p_material)
454- // material_mapping.godot_material.
455- // mesh->add_surface_from_arrays(Mesh::PrimitiveType p_primitive, const Array &p_arrays)
456- // mesh->surface_set_material(int32_t p_surf_idx, const Ref<Material> &p_material)
457- // mesh_inst->
458449 mesh_inst->set_instance_shader_parameter (key_diffuse, material_mapping.diffuse_texture_index );
459450 }
460451 if (material_mapping.specular_texture_index != -1 ) {
@@ -463,9 +454,11 @@ MeshInstance3D* XacLoader::_build_mesh(mesh_t const& mesh_chunk, skinning_t* ski
463454 if (material_mapping.scroll_index != -1 ) {
464455 mesh_inst->set_instance_shader_parameter (key_scroll, material_mapping.scroll_index );
465456 }
457+
458+ meshes.push_back (mesh_inst);
466459 }
467460
468- return mesh_inst ;
461+ return meshes ;
469462}
470463
471464/*
@@ -631,8 +624,23 @@ Node3D* XacLoader::load_xac_model(Ref<FileAccess> const& file, bool is_unit) {
631624
632625 if (skeleton != nullptr && mesh_skin == nullptr ) { continue ; }
633626
634- MeshInstance3D* mesh_inst = _build_mesh (mesh, mesh_skin, material_mappings);
635- base->add_child (mesh_inst);
627+ std::vector<MeshInstance3D*> mesh_instances = _build_mesh (mesh, mesh_skin, material_mappings);
628+ for (MeshInstance3D* mesh_inst : mesh_instances){
629+ base->add_child (mesh_inst);
630+ mesh_inst->set_owner (base);
631+
632+ if (skeleton != nullptr ) {
633+ mesh_inst->set_skeleton_path (mesh_inst->get_path_to (skeleton));
634+ int32_t node_id = mesh.packed .node_id ;
635+ if (hierarchy_read && node_id < hierarchy.node_data .size ()) {
636+ mesh_inst->set_name (hierarchy.node_data [node_id].name );
637+ }
638+ else if (!nodes.empty () && node_id < nodes.size ()) {
639+ mesh_inst->set_name (nodes[node_id].name );
640+ }
641+ }
642+ }
643+ /* base->add_child(mesh_inst);
636644 mesh_inst->set_owner(base);
637645
638646 if (skeleton != nullptr) {
@@ -644,7 +652,7 @@ Node3D* XacLoader::load_xac_model(Ref<FileAccess> const& file, bool is_unit) {
644652 else if (!nodes.empty() && node_id < nodes.size()) {
645653 mesh_inst->set_name(nodes[node_id].name);
646654 }
647- }
655+ } */
648656 }
649657
650658 return base;
0 commit comments