Skip to content

Commit 1a7ccbf

Browse files
author
Nemrav
committed
separate surfaces to meshes
1 parent 9df2c43 commit 1a7ccbf

3 files changed

Lines changed: 40 additions & 31 deletions

File tree

extension/src/openvic-extension/utility/XACLoader.cpp

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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;

extension/src/openvic-extension/utility/XACLoader.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ namespace OpenVic {
308308

309309
model_texture_set _get_model_textures(material_definition_t const& material);
310310
std::vector<material_mapping> _build_materials(std::vector<material_definition_t> const& materials);
311-
godot::MeshInstance3D* _build_mesh(mesh_t const& mesh_chunk, skinning_t* skin, std::vector<material_mapping> const& materials);
311+
std::vector<godot::MeshInstance3D*> _build_mesh(mesh_t const& mesh_chunk, skinning_t* skin, std::vector<material_mapping> const& materials);
312312

313313
public:
314314
static godot::Ref<godot::ImageTexture> get_model_texture(godot::String name) {

game/src/Game/GameSession/ModelManager.gd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ class_name ModelManager
22
extends Node3D
33

44
@export var _map_view : MapView
5+
@export var _buildings : Node3D
6+
@export var _units : Node3D
57

68
const MODEL_SCALE : float = 1.0 / 256.0
79

@@ -61,7 +63,7 @@ func _generate_unit(unit_dict : Dictionary) -> void:
6163
model.secondary_colour = unit_dict[secondary_colour_key]
6264
model.tertiary_colour = unit_dict[tertiary_colour_key]
6365

64-
add_child(model)
66+
_units.add_child(model)
6567

6668
func generate_buildings() -> void:
6769
for building : Dictionary in ModelSingleton.get_buildings():
@@ -80,7 +82,7 @@ func _generate_building(building_dict : Dictionary) -> void:
8082
model.rotate_y(PI + building_dict.get(rotation_key, 0.0))
8183
model.set_position(_map_view._map_to_world_coords(building_dict[position_key]) + Vector3(0, 0.1 * MODEL_SCALE, 0))
8284

83-
add_child(model)
85+
_buildings.add_child(model)
8486

8587
func _generate_model(model_dict : Dictionary, culture : String = "", is_unit : bool = false) -> Node3D:
8688
const file_key : StringName = &"file"
@@ -103,7 +105,6 @@ func _generate_model(model_dict : Dictionary, culture : String = "", is_unit : b
103105
# Currently needs UnitModel's attach_model helper function
104106
or attachments_key in model_dict
105107
)
106-
#var model : Node3D = XACLoader.get_xac_model(model_dict[file_key], is_unit)
107108
var model : Node3D = ModelSingleton.get_xac_model(model_dict[file_key], is_unit)
108109
if not model:
109110
return null

0 commit comments

Comments
 (0)