From a359da1762b72d5328120d682c7eba07ab4bfb42 Mon Sep 17 00:00:00 2001 From: Jamie Greunbaum Date: Fri, 16 Oct 2026 13:48:15 -0400 Subject: [PATCH] - SliceableRigidBody3D is now implemented. - Skinned vertex generation is now its own interruptible slicing step. - Hurtbox queue is implemented in a more performant manner. - Print macros now use class names instead of special tags. --- .../nodes/damageables/character_3d.cpp | 65 +-- src/liborng/nodes/damageables/character_3d.h | 17 + .../nodes/damageables/damageable_node3d.cpp | 12 +- .../nodes/damageables/damageable_node3d.h | 16 +- .../damageables/sliceable_rigid_body3d.cpp | 94 +++++ .../damageables/sliceable_rigid_body3d.h | 36 +- src/liborng/nodes/hurtbox/hurtbox_base.cpp | 50 ++- src/liborng/nodes/hurtbox/hurtbox_base.h | 2 +- src/liborng/nodes/level_scene.cpp | 4 +- src/liborng/nodes/level_scene.h | 2 - src/liborng/nodes/player_spawn.cpp | 2 +- src/liborng/nodes/player_spawn.h | 2 - src/liborng/orng_macros.h | 12 +- .../singletons/mesh_editing_library.cpp | 385 ++++++++++-------- src/liborng/singletons/mesh_editing_library.h | 21 +- src/liborng/singletons/scene_loader.cpp | 2 +- 16 files changed, 464 insertions(+), 258 deletions(-) diff --git a/src/liborng/nodes/damageables/character_3d.cpp b/src/liborng/nodes/damageables/character_3d.cpp index bb5efbf..a660492 100644 --- a/src/liborng/nodes/damageables/character_3d.cpp +++ b/src/liborng/nodes/damageables/character_3d.cpp @@ -9,7 +9,7 @@ #include "orng_macros.h" -#include "nodes/debug_draw3d.h" +#include "nodes/damageables/sliceable_rigid_body3d.h" #include "nodes/hurtbox/hurtbox_base.h" #include "singletons/mesh_editing_library.h" @@ -44,7 +44,7 @@ void Character3D::set_character_skeleton(CharacterSkeleton3D *cs) void Character3D::take_damage(const HurtboxDamage &hit_data) { if (this->_hurtbox_queue.empty()) - CALL_NEXT_FRAME(callable_mp(this, &Character3D::_next_hurtbox_in_queue)); + callable_mp(this, &Character3D::_next_hurtbox_in_queue).call_deferred(); this->_hurtbox_queue.emplace_back(hit_data); } @@ -63,7 +63,7 @@ void Character3D::_next_hurtbox_in_queue() Ref input; input.instantiate(); input->mesh_instance = character_skeleton->get_mesh_instance(); input->skeleton = skeleton; - input->full_aabb = this->_aabb; + input->full_aabb = this->aabb; input->local_plane = local_slice_plane; input->cap_option = MeshEditingLibrary::CAP_UV_FILL_MESH_BOUNDS; input->cap_materials = this->inner_materials; @@ -80,18 +80,6 @@ void Character3D::_next_hurtbox_in_queue() void Character3D::_slice_complete(Ref data) { - if (data->main_half->get_surface_count() <= 0) - { - if (data->secondary_half->get_surface_count() <= 0) - { - return; - } - - Ref temp = data->main_half; - data->main_half = data->secondary_half; - data->secondary_half = temp; - } - if (data->original_skeleton) { for (CharacterSkeleton3D *skeleton : this->_skeleton_set) @@ -106,25 +94,38 @@ void Character3D::_slice_complete(Ref data) } if (data->secondary_half.is_valid()) { - RandomNumberGenerator *rng = memnew(RandomNumberGenerator); - const float random_x = rng->randf_range(-0.01f, 0.01f); - const float random_z = rng->randf_range(-0.01f, 0.01f); - - CharacterSkeleton3D *new_character_skeleton = cast_to(this->character_skeleton->duplicate()); - this->character_skeleton->add_sibling(new_character_skeleton); - new_character_skeleton->set_position(Vector3(random_x, 0.0f, random_z)); - - MeshInstance3D *new_mesh_instance = memnew(MeshInstance3D); - new_mesh_instance->set_mesh(data->secondary_half); - new_character_skeleton->set_mesh(new_mesh_instance); - - this->_skeleton_set.emplace(new_character_skeleton); + if (this->_skeleton_set.size() < 4) + { + RandomNumberGenerator *rng = memnew(RandomNumberGenerator); + const float random_x = rng->randf_range(-0.05f, 0.05f); + const float random_z = rng->randf_range(-0.05f, 0.05f); + + CharacterSkeleton3D *new_character_skeleton = cast_to(this->character_skeleton->duplicate()); + this->character_skeleton->add_sibling(new_character_skeleton); + new_character_skeleton->set_position(Vector3(random_x, 0.0f, random_z)); + + MeshInstance3D *new_mesh_instance = memnew(MeshInstance3D); + new_mesh_instance->set_mesh(data->secondary_half); + new_character_skeleton->set_mesh(new_mesh_instance); + + this->_skeleton_set.emplace(new_character_skeleton); + + PRINT_LOG(Character3D, "Number of skeletons is now ", this->_skeleton_set.size()); + } + else + { + SliceableRigidBody3D *new_rigid_body = memnew(SliceableRigidBody3D); + this->add_sibling(new_rigid_body); + new_rigid_body->set_mesh(data->secondary_half); + new_rigid_body->set_global_transform(this->get_global_transform()); + new_rigid_body->set_collision_layer(this->loose_object_collision_layer); + new_rigid_body->set_collision_mask(this->loose_object_collision_mask); + new_rigid_body->set_inner_materials(this->inner_materials); + + PRINT_LOG(Character3D, "Not creating skeletons any more. >:c"); + } } } - else - { - - } } diff --git a/src/liborng/nodes/damageables/character_3d.h b/src/liborng/nodes/damageables/character_3d.h index 90ce3d8..fbe7f08 100644 --- a/src/liborng/nodes/damageables/character_3d.h +++ b/src/liborng/nodes/damageables/character_3d.h @@ -30,6 +30,12 @@ class Character3D : public DamageableNode3D GDCLASS(Character3D, DamageableNode3D); public: + void set_loose_object_collision_layer(const uint32_t layer) { this->loose_object_collision_layer = layer; } + uint32_t get_loose_object_collision_layer() { return this->loose_object_collision_layer; } + + void set_loose_object_collision_mask(const uint32_t mask) { this->loose_object_collision_mask = mask; } + uint32_t get_loose_object_collision_mask() { return this->loose_object_collision_mask; } + virtual void take_damage(const HurtboxDamage &hit_data) override; CharacterSkeleton3D *get_character_skeleton() const { return this->character_skeleton; } @@ -46,6 +52,9 @@ private: virtual void _slice_complete(Ref data) override; + uint32_t loose_object_collision_layer = 0b1; + uint32_t loose_object_collision_mask = 0b1; + CharacterSkeleton3D *character_skeleton = nullptr; std::unordered_set _skeleton_set; @@ -55,6 +64,14 @@ private: protected: static void _bind_methods() { + ClassDB::bind_method(D_METHOD("set_loose_object_collision_layer", "layer"), &Character3D::set_loose_object_collision_layer); + ClassDB::bind_method(D_METHOD("get_loose_object_collision_layer"), &Character3D::get_loose_object_collision_layer); + ClassDB::add_property(Character3D::get_class_static(), PropertyInfo(Variant::INT, "loose_object_collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_loose_object_collision_layer", "get_loose_object_collision_layer"); + + ClassDB::bind_method(D_METHOD("set_loose_object_collision_mask", "mask"), &Character3D::set_loose_object_collision_mask); + ClassDB::bind_method(D_METHOD("get_loose_object_collision_mask"), &Character3D::get_loose_object_collision_mask); + ClassDB::add_property(Character3D::get_class_static(), PropertyInfo(Variant::INT, "loose_object_collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_loose_object_collision_mask", "get_loose_object_collision_mask"); + BIND_METHOD(Character3D, _on_fell_out_of_world); ADD_SIGNAL(MethodInfo("fell_out_of_world")); diff --git a/src/liborng/nodes/damageables/damageable_node3d.cpp b/src/liborng/nodes/damageables/damageable_node3d.cpp index adee74e..1e52f91 100644 --- a/src/liborng/nodes/damageables/damageable_node3d.cpp +++ b/src/liborng/nodes/damageables/damageable_node3d.cpp @@ -21,7 +21,6 @@ void DamageableNode3D::_ready() void DamageableNode3D::set_mesh(Ref m) { this->mesh = m; - this->_aabb = AABB(); this->_rebuild_mesh_instance(); } @@ -53,6 +52,9 @@ void DamageableNode3D::_slice_loop() case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_INIT: if (MeshEditingLibrary::slice_mesh_init(current_state) > MESH_EDITING_SLICE_LOOP_LIMIT) break; + case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_GET_SKINNED_VERTS: + if (MeshEditingLibrary::slice_mesh_get_skinned_verts(current_state) > MESH_EDITING_SLICE_LOOP_LIMIT) + break; case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_PLANE_SPLIT: if (MeshEditingLibrary::slice_mesh_plane_split(current_state) > MESH_EDITING_SLICE_LOOP_LIMIT) break; @@ -109,9 +111,15 @@ MeshInstance3D *DamageableNode3D::_rebuild_mesh_instance() if (!this->is_node_ready()) return nullptr; + if (this->_active_mesh_instance) + { + this->_active_mesh_instance->queue_free(); + } + this->_active_mesh_instance = memnew(MeshInstance3D); this->_active_mesh_instance->set_mesh(this->mesh); - this->_aabb = this->_active_mesh_instance->get_aabb(); + if (this->aabb == AABB()) + this->aabb = this->_active_mesh_instance->get_aabb(); return this->_active_mesh_instance; } diff --git a/src/liborng/nodes/damageables/damageable_node3d.h b/src/liborng/nodes/damageables/damageable_node3d.h index d8940e7..6eb503c 100644 --- a/src/liborng/nodes/damageables/damageable_node3d.h +++ b/src/liborng/nodes/damageables/damageable_node3d.h @@ -13,7 +13,7 @@ #include "godot_cpp/classes/node.hpp" using namespace godot; -#define MAX_SLICE_QUEUE_SIZE 3 +#define MAX_SLICE_QUEUE_SIZE 4 #define DAMAGEABLE_NODE3D_TAG "DamageableNode3D" @@ -25,24 +25,27 @@ class DamageableNode3D : public Node3D public: virtual void _ready() override; + void set_mesh(Ref m); Ref get_mesh() const { return this->mesh; } + + void set_aabb(const AABB &aabb) { this->aabb = aabb; } + AABB get_aabb() const { return this->aabb; } + + void set_inner_materials(TypedArray materials) { this->inner_materials = materials; } TypedArray get_inner_materials() const { return this->inner_materials; } virtual void take_damage(const struct HurtboxDamage &hit_data) = 0; protected: - void set_mesh(Ref m); - void set_inner_materials(TypedArray materials) { this->inner_materials = materials; } - + MeshInstance3D *_get_active_mesh_instance() { return this->_active_mesh_instance; } virtual MeshInstance3D *_rebuild_mesh_instance(); void _slice_meshes(Ref data); virtual void _slice_complete(Ref data) = 0; Ref mesh; + AABB aabb; TypedArray inner_materials; - - AABB _aabb; private: void _new_slice_loop(); @@ -62,6 +65,7 @@ protected: static void _bind_methods() { ADD_GETTER_SETTER_HINTED(DamageableNode3D, mesh, Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "ArrayMesh"); + ADD_GETTER_SETTER(DamageableNode3D, aabb, Variant::AABB); ADD_GETTER_SETTER_HINTED(DamageableNode3D, inner_materials, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, "Material"); } }; diff --git a/src/liborng/nodes/damageables/sliceable_rigid_body3d.cpp b/src/liborng/nodes/damageables/sliceable_rigid_body3d.cpp index 3b196ef..806bb80 100644 --- a/src/liborng/nodes/damageables/sliceable_rigid_body3d.cpp +++ b/src/liborng/nodes/damageables/sliceable_rigid_body3d.cpp @@ -9,6 +9,100 @@ #include "orng_macros.h" +#include "nodes/hurtbox/hurtbox_base.h" + +#include #include #include using namespace godot; + + +void SliceableRigidBody3D::_enter_tree() +{ + this->rigid_body = memnew(RigidBody3D); + this->collision_shape = memnew(CollisionShape3D); + this->rigid_body->add_child(this->collision_shape); + this->add_child(this->rigid_body); +} + +void SliceableRigidBody3D::_ready() +{ + this->rigid_body->set_collision_layer(this->collision_layer); + this->rigid_body->set_collision_mask(this->collision_mask); + + DamageableNode3D::_ready(); +} + + +void SliceableRigidBody3D::set_collision_layer(const uint32_t layer) +{ + this->collision_layer = layer; + if (this->rigid_body) + this->rigid_body->set_collision_layer(this->collision_layer); +} + +void SliceableRigidBody3D::set_collision_mask(const uint32_t mask) +{ + this->collision_mask = mask; + if (this->rigid_body) + this->rigid_body->set_collision_mask(this->collision_mask); +} + + +void SliceableRigidBody3D::take_damage(const HurtboxDamage &hit_data) +{ + const Plane &local_slice_plane = this->rigid_body->get_global_transform().inverse().xform(Plane(hit_data.global_hit_transform.get_basis().get_column(1), hit_data.global_hit_transform.origin)); + Ref input; input.instantiate(); + input->mesh_instance = this->_get_active_mesh_instance(); + input->skeleton = nullptr; + input->full_aabb = this->aabb; + input->local_plane = local_slice_plane; + input->cap_option = MeshEditingLibrary::CAP_UV_FILL_MESH_BOUNDS; + input->cap_materials = this->inner_materials; + this->_slice_meshes(input); +} + +void SliceableRigidBody3D::_slice_complete(Ref data) +{ + // This is here so that the root node and its child RigidBody3D can share the same position, + // making the transform of the new slices much easier to calculate. + if (this->rigid_body) + { + const Transform3D rigid_body_transform = this->rigid_body->get_global_transform(); + this->set_global_transform(rigid_body_transform); + this->rigid_body->set_transform(Transform3D()); + } + this->set_mesh(data->main_half); + this->apply_central_impulse(data->original_local_plane.get_normal() * 1.0f); + + if (data->secondary_half.is_valid()) + { + SliceableRigidBody3D *new_rigid_body = memnew(SliceableRigidBody3D); + this->add_sibling(new_rigid_body); + new_rigid_body->set_mesh(data->secondary_half); + new_rigid_body->set_global_transform(this->get_global_transform()); + new_rigid_body->set_aabb(data->original_full_aabb); + new_rigid_body->set_collision_layer(this->collision_layer); + new_rigid_body->set_collision_mask(this->collision_mask); + new_rigid_body->set_inner_materials(this->inner_materials); + + new_rigid_body->apply_central_impulse(data->original_local_plane.get_normal() * -1.0f); + } +} + + +MeshInstance3D *SliceableRigidBody3D::_rebuild_mesh_instance() +{ + if (MeshInstance3D *returned_instance = DamageableNode3D::_rebuild_mesh_instance()) + { + this->rigid_body->add_child(returned_instance); + Ref returned_mesh = returned_instance->get_mesh(); + if (returned_mesh.is_valid()) + { + Ref shape = returned_mesh->create_convex_shape(false, false); + this->collision_shape->set_shape(shape); + } + return returned_instance; + } + return nullptr; +} \ No newline at end of file diff --git a/src/liborng/nodes/damageables/sliceable_rigid_body3d.h b/src/liborng/nodes/damageables/sliceable_rigid_body3d.h index c579763..1dc9cd3 100644 --- a/src/liborng/nodes/damageables/sliceable_rigid_body3d.h +++ b/src/liborng/nodes/damageables/sliceable_rigid_body3d.h @@ -14,8 +14,10 @@ #include #include +#include #include #include +#include using namespace godot; #define SLICEABLE_RIGID_BODY3D_TAG "SliceableRigidBody3D" @@ -26,14 +28,42 @@ class SliceableRigidBody3D : public DamageableNode3D GDCLASS(SliceableRigidBody3D, DamageableNode3D); public: - virtual void take_damage(const HurtboxDamage &hit_data) override {} + virtual void _enter_tree() override; + virtual void _ready() override; + + void set_collision_layer(const uint32_t layer); + uint32_t get_collision_layer() const { return this->collision_layer; } + + void set_collision_mask(const uint32_t mask); + uint32_t get_collision_mask() const { return this->collision_mask; } + + virtual void take_damage(const HurtboxDamage &hit_data) override; + + void apply_impulse(const Vector3 &impulse, const Vector3 &position = Vector3(0.0f, 0.0f, 0.0f)) { if (this->rigid_body) this->rigid_body->apply_impulse(impulse, position); } + void apply_central_impulse(const Vector3 &impulse) { if (this->rigid_body) this->rigid_body->apply_central_impulse(impulse); } protected: + virtual MeshInstance3D *_rebuild_mesh_instance() override; private: - virtual void _slice_complete(Ref data) override {} + virtual void _slice_complete(Ref data) override; + + uint32_t collision_layer; + uint32_t collision_mask; + + RigidBody3D *rigid_body = nullptr; + CollisionShape3D *collision_shape = nullptr; // Godot boilerplate below protected: - static void _bind_methods(){} + static void _bind_methods() + { + ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &SliceableRigidBody3D::set_collision_layer); + ClassDB::bind_method(D_METHOD("get_collision_layer"), &SliceableRigidBody3D::get_collision_layer); + ClassDB::add_property(SliceableRigidBody3D::get_class_static(), PropertyInfo(Variant::INT, "collision_layers", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer"); + + ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &SliceableRigidBody3D::set_collision_mask); + ClassDB::bind_method(D_METHOD("get_collision_mask"), &SliceableRigidBody3D::get_collision_mask); + ClassDB::add_property(SliceableRigidBody3D::get_class_static(), PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask"); + } }; diff --git a/src/liborng/nodes/hurtbox/hurtbox_base.cpp b/src/liborng/nodes/hurtbox/hurtbox_base.cpp index 339c6fa..cd2cb94 100644 --- a/src/liborng/nodes/hurtbox/hurtbox_base.cpp +++ b/src/liborng/nodes/hurtbox/hurtbox_base.cpp @@ -9,6 +9,7 @@ #include "orng_macros.h" #include "nodes/damageables/character_3d.h" +#include "nodes/damageables/sliceable_rigid_body3d.h" #include #include @@ -77,35 +78,49 @@ void HurtboxBase::_physics_process(double delta) if (std::find(new_hit_skeletons.begin(), new_hit_skeletons.end(), character_skeleton) == new_hit_skeletons.end()) { - HurtboxDamage character_damage; - character_damage.hit_points = this->hit_points; - character_damage.global_hit_transform = this->get_global_transform(); + HurtboxDamage damage; + damage.hit_points = this->hit_points; + damage.global_hit_transform = this->get_global_transform(); for (uint32_t bone_index = 0; bone_index < num_bodies; bone_index++) { if (PhysicalBone3D *bone = cast_to(bodies[bone_index])) { if (String(bone->get_path()).begins_with(character_skeleton->get_path())) - character_damage.bones[character_skeleton].emplace_back(bone->get_bone_id()); + damage.bones[character_skeleton].emplace_back(bone->get_bone_id()); } } - this->hit_data_queue[body].emplace_back(character_damage); + this->hit_data_queue[body].emplace_back(damage); new_hit_skeletons.emplace_back(character_skeleton); } } } } } + else if (RigidBody3D *rigid_body = cast_to(bodies[i])) + { + if (SliceableRigidBody3D *sliceable_rigid_body = cast_to(rigid_body->get_parent())) + { + if (std::find(this->hit_bodies.begin(), this->hit_bodies.end(), sliceable_rigid_body) == this->hit_bodies.end()) + { + HurtboxDamage damage; + damage.hit_points = this->hit_points; + damage.global_hit_transform = this->get_global_transform(); + this->hit_data_queue[sliceable_rigid_body].emplace_back(damage); + new_hit_bodies.emplace(sliceable_rigid_body); + } + } + } } for (DamageableNode3D *damageable : new_hit_bodies) this->hit_bodies.emplace_back(damageable); + this->remaining_life -= delta; + if (this->remaining_life <= 0.0f) this->collision_shape->set_disabled(true); - else - this->remaining_life -= delta; } void HurtboxBase::_send_next_in_queue() @@ -114,22 +129,13 @@ void HurtboxBase::_send_next_in_queue() { DamageableNode3D *closest_damageable = nullptr; float shortest_distance_from_hurtbox = FLT_MAX; - bool found_character_3d = false; for (const auto &[damageable, hit_data_list] : this->hit_data_queue) { - if (const Character3D *node = cast_to(damageable)) + const float distance_to_hurtbox = damageable->get_global_position().distance_squared_to(this->get_global_position()); + if (distance_to_hurtbox < shortest_distance_from_hurtbox) { - const float distance_to_hurtbox = node->get_global_position().distance_squared_to(this->get_global_position()); - if (distance_to_hurtbox < shortest_distance_from_hurtbox) - { - closest_damageable = damageable; - shortest_distance_from_hurtbox = distance_to_hurtbox; - found_character_3d = true; - } - // if (!found_character_3d && const SliceablePhysicsObject3D *node = (SliceablePhysicsObject3D*)(damageable) ) - // { - // // This is where we handle regular physics objects. - // } + closest_damageable = damageable; + shortest_distance_from_hurtbox = distance_to_hurtbox; } } if (closest_damageable) @@ -140,12 +146,12 @@ void HurtboxBase::_send_next_in_queue() } } - if (this->hit_data_queue.empty() && this->remaining_life <= 0.0f) + if (this->remaining_life <= -this->lifetime) { this->queue_free(); } else { - CALL_ON_TIMEOUT(1.0f / 8.0f, callable_mp(this, &HurtboxBase::_send_next_in_queue)); + CALL_NEXT_FRAME(callable_mp(this, &HurtboxBase::_send_next_in_queue)); } } diff --git a/src/liborng/nodes/hurtbox/hurtbox_base.h b/src/liborng/nodes/hurtbox/hurtbox_base.h index edf6ca3..f5fe8a5 100644 --- a/src/liborng/nodes/hurtbox/hurtbox_base.h +++ b/src/liborng/nodes/hurtbox/hurtbox_base.h @@ -17,7 +17,7 @@ #include using namespace godot; -#define MAX_HITS_PER_QUEUE_LOOP 3 +#define MAX_HITS_PER_QUEUE_LOOP 1 #define HURTBOX_BASE_TAG "HurtboxBase" diff --git a/src/liborng/nodes/level_scene.cpp b/src/liborng/nodes/level_scene.cpp index 3d7ba1c..5cf5b9f 100644 --- a/src/liborng/nodes/level_scene.cpp +++ b/src/liborng/nodes/level_scene.cpp @@ -39,7 +39,7 @@ void LevelScene::_scene_ready() #ifdef DEBUG_ENABLED if (!this->game_classes.is_valid()) { - PRINT_ERROR(LEVEL_SCENE_TAG, "No GameClasses resource attached to level."); + PRINT_ERROR(LevelScene, "No GameClasses resource attached to level."); return; } #endif @@ -63,7 +63,7 @@ void LevelScene::_scene_ready() #ifdef DEBUG_ENABLED if (!spawn_point) { - PRINT_ERROR(LEVEL_SCENE_TAG, "Could not find a spawn point named ", current_checkpoint, "; defaulting to origin..."); + PRINT_ERROR(LevelScene, "Could not find a spawn point named ", current_checkpoint, "; defaulting to origin..."); spawn_point = spawn_points[PLAYER_ORIGIN_TAG]; DEV_ASSERT(spawn_point); } diff --git a/src/liborng/nodes/level_scene.h b/src/liborng/nodes/level_scene.h index aeaf151..a530f75 100644 --- a/src/liborng/nodes/level_scene.h +++ b/src/liborng/nodes/level_scene.h @@ -15,8 +15,6 @@ #include using namespace godot; -#define LEVEL_SCENE_TAG "LevelScene" - #define FELL_OUT_OF_WORLD_SIGNAL "fell_out_of_world" #define FELL_OUT_OF_WORLD_METHOD "_on_fell_out_of_world" diff --git a/src/liborng/nodes/player_spawn.cpp b/src/liborng/nodes/player_spawn.cpp index c9806d2..fd7102b 100644 --- a/src/liborng/nodes/player_spawn.cpp +++ b/src/liborng/nodes/player_spawn.cpp @@ -68,7 +68,7 @@ bool PlayerOrigin::spawn_player(Node3D *parent, Ref player) const #ifdef DEBUG_ENABLED else { - PRINT_ERROR(PLAYERSPAWN_LOG_TAG, "Empty sublevel in PlayerSpawn object \"", this->tag, "\""); + PRINT_ERROR(PlayerOrigin, "Empty sublevel in PlayerSpawn object \"", this->tag, "\""); } #endif } diff --git a/src/liborng/nodes/player_spawn.h b/src/liborng/nodes/player_spawn.h index d67c1e1..c325cab 100644 --- a/src/liborng/nodes/player_spawn.h +++ b/src/liborng/nodes/player_spawn.h @@ -14,8 +14,6 @@ #include using namespace godot; -#define PLAYERSPAWN_LOG_TAG "PlayerSpawn" - #define PLAYER_ORIGIN_TAG "origin" diff --git a/src/liborng/orng_macros.h b/src/liborng/orng_macros.h index 028e117..da22ff9 100644 --- a/src/liborng/orng_macros.h +++ b/src/liborng/orng_macros.h @@ -54,11 +54,11 @@ * Logging helpers */ #ifdef DEBUG_ENABLED - #define PRINT_LOG(tag, ...) UtilityFunctions::print("[" tag "] ", __VA_ARGS__) - #define PRINT_WARNING(tag, ...) UtilityFunctions::push_warning("[" tag "] ", __VA_ARGS__) - #define PRINT_ERROR(tag, ...) UtilityFunctions::printerr("[" tag "] ", __VA_ARGS__) + #define PRINT_LOG(class, ...) UtilityFunctions::print("[", ##class::get_class_static(), "] ", __VA_ARGS__) + #define PRINT_WARNING(class, ...) UtilityFunctions::push_warning("[", ##class::get_class_static(), "] ", __VA_ARGS__) + #define PRINT_ERROR(class, ...) UtilityFunctions::printerr("[", ##class::get_class_static(), "] ", __VA_ARGS__) #else - #define PRINT_LOG(tag, ...) - #define PRINT_WARNING(tag, ...) - #define PRINT_ERROR(tag, ...) + #define PRINT_LOG(class, ...) + #define PRINT_WARNING(class, ...) + #define PRINT_ERROR(class, ...) #endif // DEBUG_ENABLED diff --git a/src/liborng/singletons/mesh_editing_library.cpp b/src/liborng/singletons/mesh_editing_library.cpp index c66cecd..c60d1a7 100644 --- a/src/liborng/singletons/mesh_editing_library.cpp +++ b/src/liborng/singletons/mesh_editing_library.cpp @@ -18,6 +18,8 @@ using namespace godot; Ref MeshEditingLibrary::slice_mesh(const Ref input) { + const Time *time = Time::get_singleton(); + Ref fresh_state; fresh_state.instantiate(); @@ -28,69 +30,67 @@ Ref MeshEditingLibrary::slice_mesh(const Reffirst_half.instantiate(); fresh_state->other_half.instantiate(); + { // Initialise the state here + fresh_state->original_mesh = fresh_state->input->mesh_instance->get_mesh(); + fresh_state->num_surfaces = fresh_state->original_mesh->get_surface_count(); + + fresh_state->surface_arrays_list.resize(fresh_state->num_surfaces); + fresh_state->skinned_vertex_positions.resize(fresh_state->num_surfaces); + fresh_state->skinned_vertex_transforms.resize(fresh_state->num_surfaces); + + fresh_state->has_normal.resize(fresh_state->num_surfaces); + fresh_state->has_tangent.resize(fresh_state->num_surfaces); + fresh_state->has_uv.resize(fresh_state->num_surfaces); + fresh_state->has_uv2.resize(fresh_state->num_surfaces); + fresh_state->has_colour.resize(fresh_state->num_surfaces); + fresh_state->has_bone.resize(fresh_state->num_surfaces); + fresh_state->has_weight.resize(fresh_state->num_surfaces); + fresh_state->has_custom0.resize(fresh_state->num_surfaces); + + fresh_state->num_bones_per_vertex.resize(fresh_state->num_surfaces); + fresh_state->num_custom0_per_vertex.resize(fresh_state->num_surfaces); + + fresh_state->first_half_vertices.resize(fresh_state->num_surfaces); + fresh_state->first_half_normals.resize(fresh_state->num_surfaces); + fresh_state->first_half_tangents.resize(fresh_state->num_surfaces); + fresh_state->first_half_uvs.resize(fresh_state->num_surfaces); + fresh_state->first_half_uv2s.resize(fresh_state->num_surfaces); + fresh_state->first_half_colours.resize(fresh_state->num_surfaces); + fresh_state->first_half_bones.resize(fresh_state->num_surfaces); + fresh_state->first_half_weights.resize(fresh_state->num_surfaces); + fresh_state->first_half_custom0.resize(fresh_state->num_surfaces); + + fresh_state->other_half_vertices.resize(fresh_state->num_surfaces); + fresh_state->other_half_normals.resize(fresh_state->num_surfaces); + fresh_state->other_half_tangents.resize(fresh_state->num_surfaces); + fresh_state->other_half_uvs.resize(fresh_state->num_surfaces); + fresh_state->other_half_uv2s.resize(fresh_state->num_surfaces); + fresh_state->other_half_colours.resize(fresh_state->num_surfaces); + fresh_state->other_half_bones.resize(fresh_state->num_surfaces); + fresh_state->other_half_weights.resize(fresh_state->num_surfaces); + fresh_state->other_half_custom0.resize(fresh_state->num_surfaces); + + fresh_state->first_half_indices.resize(fresh_state->num_surfaces); + fresh_state->other_half_indices.resize(fresh_state->num_surfaces); + + fresh_state->vertex_distances.resize(fresh_state->num_surfaces); + + fresh_state->base_to_first_sliced_vert_indices.resize(fresh_state->num_surfaces); + fresh_state->base_to_other_sliced_vert_indices.resize(fresh_state->num_surfaces); + } + return fresh_state; } float MeshEditingLibrary::slice_mesh_init(Ref state) { - const Time *time = Time::get_singleton(); - const float start_time = time->get_ticks_msec(); + START_LOOP_TIMER(); - { // Initialise the state here - PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Started slicing mesh at ", time->get_ticks_msec(), "ms"); - - state->original_mesh = state->input->mesh_instance->get_mesh(); - state->num_surfaces = state->original_mesh->get_surface_count(); - - state->surface_arrays_list.resize(state->num_surfaces); - state->skinned_vertex_positions.resize(state->num_surfaces); - state->skinned_vertex_transforms.resize(state->num_surfaces); - - state->has_normal.resize(state->num_surfaces); - state->has_tangent.resize(state->num_surfaces); - state->has_uv.resize(state->num_surfaces); - state->has_uv2.resize(state->num_surfaces); - state->has_colour.resize(state->num_surfaces); - state->has_bone.resize(state->num_surfaces); - state->has_weight.resize(state->num_surfaces); - state->has_custom0.resize(state->num_surfaces); - - state->num_bones_per_vertex.resize(state->num_surfaces); - state->num_custom0_per_vertex.resize(state->num_surfaces); - - state->first_half_vertices.resize(state->num_surfaces); - state->first_half_normals.resize(state->num_surfaces); - state->first_half_tangents.resize(state->num_surfaces); - state->first_half_uvs.resize(state->num_surfaces); - state->first_half_uv2s.resize(state->num_surfaces); - state->first_half_colours.resize(state->num_surfaces); - state->first_half_bones.resize(state->num_surfaces); - state->first_half_weights.resize(state->num_surfaces); - state->first_half_custom0.resize(state->num_surfaces); - - state->other_half_vertices.resize(state->num_surfaces); - state->other_half_normals.resize(state->num_surfaces); - state->other_half_tangents.resize(state->num_surfaces); - state->other_half_uvs.resize(state->num_surfaces); - state->other_half_uv2s.resize(state->num_surfaces); - state->other_half_colours.resize(state->num_surfaces); - state->other_half_bones.resize(state->num_surfaces); - state->other_half_weights.resize(state->num_surfaces); - state->other_half_custom0.resize(state->num_surfaces); - - state->first_half_indices.resize(state->num_surfaces); - state->other_half_indices.resize(state->num_surfaces); - - state->vertex_distances.resize(state->num_surfaces); - - state->base_to_first_sliced_vert_indices.resize(state->num_surfaces); - state->base_to_other_sliced_vert_indices.resize(state->num_surfaces); - } + PRINT_LOG(MeshEditingLibrary, "Started slicing mesh at ", time->get_ticks_msec(), "ms"); for (state->surface; state->surface < state->num_surfaces; state->surface++) { - // if ((time->get_ticks_msec() - start_time) > MESH_EDITING_SLICE_LOOP_LIMIT) - // return time->get_ticks_msec() - start_time; + RETURN_IF_TIME_IS_UP(); state->surface_arrays_list[state->surface] = state->original_mesh->surface_get_arrays(state->surface); const PackedVector3Array &surface_vertex_array = state->surface_arrays_list[state->surface][ArrayMesh::ARRAY_VERTEX]; @@ -128,22 +128,104 @@ float MeshEditingLibrary::slice_mesh_init(Ref state) state->vertex_distances[state->surface].resize(num_vertices); - state->skinned_vertex_positions[state->surface] = MeshEditingLibrary::get_skinned_vertex_positions(state->surface, state->original_mesh, state->input->skeleton, state->skinned_vertex_transforms[state->surface]); + state->skinned_vertex_positions[state->surface].resize(num_vertices); + state->skinned_vertex_transforms[state->surface].resize(num_vertices); } - PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Got skinned vertices at ", time->get_ticks_msec(), "ms"); + PRINT_LOG(MeshEditingLibrary, "Initialised arrays at ", time->get_ticks_msec(), "ms"); + + state->surface = 0; + state->element = 0; + state->current_stage = MeshEditSliceStage::SLICE_STAGE_GET_SKINNED_VERTS; + + RETURN_LOOP_TIME(); +} + +float MeshEditingLibrary::slice_mesh_get_skinned_verts(Ref state) +{ + START_LOOP_TIMER(); + + const uint8_t num_surfaces = state->original_mesh->get_surface_count(); + + for (state->surface; state->surface < state->num_surfaces; state->surface++) + { + const Array &mesh_arrays = state->surface_arrays_list[state->surface]; + const PackedVector3Array &mesh_vertex_array = mesh_arrays[ArrayMesh::ARRAY_VERTEX]; + const uint32_t num_elements = mesh_vertex_array.size(); + + if (state->input->skeleton) + { + const Skeleton3D *skeleton = state->input->skeleton; + const uint16_t bone_count = skeleton->get_bone_count(); + SkinnedVertexTransforms bone_transforms; + bone_transforms.resize(bone_count); + for (uint8_t bone_index = 0; bone_index < bone_count; bone_index++) + { + bone_transforms[bone_index] = skeleton->get_bone_global_pose(bone_index) * skeleton->get_bone_global_rest(bone_index).inverse(); + } + + const PackedInt32Array &mesh_bones_array = mesh_arrays[ArrayMesh::ARRAY_BONES]; + const PackedFloat32Array &mesh_weights_array = mesh_arrays[ArrayMesh::ARRAY_WEIGHTS]; + const uint8_t num_bones_per_vertex = mesh_bones_array.size() / num_elements; + for (state->element; state->element < num_elements; state->element++) + { + RETURN_IF_TIME_IS_UP(); + + const Vector3 &vertex = mesh_vertex_array[state->element]; + Transform3D *transforms = new Transform3D[num_bones_per_vertex]; + const uint32_t bone_index_start = (state->element * num_bones_per_vertex); + for (uint8_t bone_index_offset = 0; bone_index_offset < num_bones_per_vertex; bone_index_offset++) + { + const float weight = mesh_weights_array[bone_index_start + bone_index_offset]; + const uint32_t bone = mesh_bones_array[bone_index_start + bone_index_offset]; + transforms[bone_index_offset] = ((Transform3D)bone_transforms[bone]) * weight; + } + + Vector3 x_basis, y_basis, z_basis, origin; + for (uint8_t transform = 0; transform < num_bones_per_vertex; transform++) + { + x_basis += transforms[transform].basis.rows[0]; + y_basis += transforms[transform].basis.rows[1]; + z_basis += transforms[transform].basis.rows[2]; + origin += transforms[transform].origin; + } + + const Transform3D &final_bone_transform = Transform3D( + x_basis.x, x_basis.y, x_basis.z, + y_basis.x, y_basis.y, y_basis.z, + z_basis.x, z_basis.y, z_basis.z, + origin.x, origin.y, origin.z); + state->skinned_vertex_transforms[state->surface][state->element] = final_bone_transform; + state->skinned_vertex_positions[state->surface][state->element] = final_bone_transform.xform(vertex); + + delete transforms; + } + } + else + { + for (state->element; state->element < num_elements; state->element++) + { + RETURN_IF_TIME_IS_UP(); + + state->skinned_vertex_positions[state->surface][state->element] = mesh_vertex_array[state->element]; + } + } + + PRINT_LOG(MeshEditingLibrary, "Surface ", state->surface, " skinned vertices calculated at ", time->get_ticks_msec(), "ms"); + + state->element = 0; + } state->surface = 0; state->element = 0; state->current_stage = MeshEditSliceStage::SLICE_STAGE_PLANE_SPLIT; - return time->get_ticks_msec() - start_time; + RETURN_LOOP_TIME(); } float MeshEditingLibrary::slice_mesh_plane_split(Ref state) { - const Time *time = Time::get_singleton(); - const float start_time = time->get_ticks_msec(); + START_LOOP_TIMER(); for (state->surface; state->surface < state->num_surfaces; state->surface++) { @@ -179,8 +261,7 @@ float MeshEditingLibrary::slice_mesh_plane_split(Ref state) for (state->element; state->element < num_vertices; state->element++) { - if ((time->get_ticks_msec() - start_time) > MESH_EDITING_SLICE_LOOP_LIMIT) - return time->get_ticks_msec() - start_time; + RETURN_IF_TIME_IS_UP(); vertex_distances[state->element] = state->input->local_plane.distance_to(state->skinned_vertex_positions[state->surface][state->element]); @@ -209,7 +290,7 @@ float MeshEditingLibrary::slice_mesh_plane_split(Ref state) state->first_half_custom0[state->surface].append(value); hit_grid[i] = value; } - state->first_half_split_map |= (0x00000001 << MeshEditingLibrary::uv_to_body_part_shift(hit_grid[0], hit_grid[1])); + state->first_half_split_map |= (0x1 << MeshEditingLibrary::uv_to_body_part_shift(hit_grid[0], hit_grid[1])); } if (has_bone) { @@ -245,7 +326,7 @@ float MeshEditingLibrary::slice_mesh_plane_split(Ref state) state->other_half_custom0[state->surface].append(value); hit_grid[i] = value; } - state->other_half_split_map |= (0x00000001 << MeshEditingLibrary::uv_to_body_part_shift(hit_grid[0], hit_grid[1])); + state->other_half_split_map |= (0x1 << MeshEditingLibrary::uv_to_body_part_shift(hit_grid[0], hit_grid[1])); } if (has_bone) { @@ -258,7 +339,7 @@ float MeshEditingLibrary::slice_mesh_plane_split(Ref state) } } - PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Surface ", state->surface, " sorted by plane distance at ", time->get_ticks_msec(), "ms"); + PRINT_LOG(MeshEditingLibrary, "Surface ", state->surface, " sorted by plane distance at ", time->get_ticks_msec(), "ms"); state->element = 0; } @@ -268,13 +349,12 @@ float MeshEditingLibrary::slice_mesh_plane_split(Ref state) state->surface = 0; state->current_stage = MeshEditSliceStage::SLICE_STAGE_EDGE_SPLIT; - return time->get_ticks_msec() - start_time; + RETURN_LOOP_TIME(); } float MeshEditingLibrary::slice_mesh_edge_split(Ref state) { - const Time *time = Time::get_singleton(); - const float start_time = time->get_ticks_msec(); + START_LOOP_TIMER(); for (state->surface; state->surface < state->num_surfaces; state->surface++) { @@ -322,8 +402,7 @@ float MeshEditingLibrary::slice_mesh_edge_split(Ref state) const uint32_t num_triangles = surface_index_array.size(); for (state->element; state->element < num_triangles; state->element += 3) { - if ((time->get_ticks_msec() - start_time) > MESH_EDITING_SLICE_LOOP_LIMIT) - return time->get_ticks_msec() - start_time; + RETURN_IF_TIME_IS_UP(); uint32_t base_v[3]; std::map::iterator sliced_v[3]; @@ -426,23 +505,21 @@ float MeshEditingLibrary::slice_mesh_edge_split(Ref state) { const float *this_tangent_start = (surface_tangent_array.ptr() + (base_v[this_vert] * 4)); const float *next_tangent_start = (surface_tangent_array.ptr() + (base_v[next_vert] * 4)); - const Vector3 &this_tangent = Vector3(*this_tangent_start, *(this_tangent_start + 1), *(this_tangent_start + 2)); - const Vector3 &next_tangent = Vector3(*next_tangent_start, *(next_tangent_start + 1), *(next_tangent_start + 2)); - const float this_binormal = *(this_tangent_start + 3); - const float next_binormal = *(next_tangent_start + 3); - const Vector3 &interp_tangent = this_tangent.slerp(next_tangent, alpha); - const uint8_t &interp_binormal = UtilityFunctions::roundi(UtilityFunctions::lerpf(this_binormal, next_binormal, alpha)); + const Vector4 this_tangent = Vector4(*this_tangent_start, *(this_tangent_start + 1), *(this_tangent_start + 2), *(this_tangent_start + 3)); + const Vector4 next_tangent = Vector4(*next_tangent_start, *(next_tangent_start + 1), *(next_tangent_start + 2), *(next_tangent_start + 3)); - state->first_half_tangents[state->surface].append(interp_tangent.x); - state->first_half_tangents[state->surface].append(interp_tangent.y); - state->first_half_tangents[state->surface].append(interp_tangent.z); - state->first_half_tangents[state->surface].append(interp_binormal); + Vector4 lerped_tangent = this_tangent.lerp(next_tangent, alpha); - state->other_half_tangents[state->surface].append(interp_tangent.x); - state->other_half_tangents[state->surface].append(interp_tangent.y); - state->other_half_tangents[state->surface].append(interp_tangent.z); - state->other_half_tangents[state->surface].append(interp_binormal); + state->first_half_tangents[state->surface].append(lerped_tangent.x); + state->first_half_tangents[state->surface].append(lerped_tangent.y); + state->first_half_tangents[state->surface].append(lerped_tangent.z); + state->first_half_tangents[state->surface].append(lerped_tangent.w); + + state->other_half_tangents[state->surface].append(lerped_tangent.x); + state->other_half_tangents[state->surface].append(lerped_tangent.y); + state->other_half_tangents[state->surface].append(lerped_tangent.z); + state->other_half_tangents[state->surface].append(lerped_tangent.w); } if (has_uv) @@ -644,13 +721,13 @@ float MeshEditingLibrary::slice_mesh_edge_split(Ref state) state->other_half->surface_set_material(next_surface, original_material); } - PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Surface ", state->surface, " split edges at ", time->get_ticks_msec(), "ms"); + PRINT_LOG(MeshEditingLibrary, "Surface ", state->surface, " split edges at ", time->get_ticks_msec(), "ms"); } state->surface = 0; state->current_stage = MeshEditSliceStage::SLICE_STAGE_APPLY_CAP; - return time->get_ticks_msec() - start_time; + RETURN_LOOP_TIME(); } float MeshEditingLibrary::slice_mesh_apply_cap(Ref state) @@ -796,103 +873,65 @@ float MeshEditingLibrary::slice_mesh_apply_cap(Ref state) memdelete(surface_tool_other_half); } - PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Generated final cap surface at ", time->get_ticks_msec(), "ms"); + PRINT_LOG(MeshEditingLibrary, "Generated final cap surface at ", time->get_ticks_msec(), "ms"); state->output->original_skeleton = state->input->skeleton; - state->output->main_half = state->first_half; - state->output->secondary_half = state->other_half; + state->output->original_full_aabb = state->input->full_aabb; + state->output->original_local_plane = state->input->local_plane; - state->current_stage = MeshEditSliceStage::SLICE_STAGE_COMPLETE; - - return time->get_ticks_msec() - start_time; -} - - -SkinnedVertices MeshEditingLibrary::get_skinned_vertex_positions(const uint8_t surface, const Ref mesh, const Skeleton3D *skeleton, std::vector &vertex_transforms) -{ - const uint8_t num_surfaces = mesh->get_surface_count(); - if (surface > num_surfaces) + if (state->first_half->get_surface_count() <= 0 && state->other_half->get_surface_count() > 0) { - PRINT_ERROR(MESH_EDITING_LIBRARY_TAG, "This mesh doesn't have that many surfaces! You wrote ", surface, ", but there's only ", num_surfaces, "here!"); - return SkinnedVertices(); - } - - SkinnedVertices skinned_vertex_array; - - if (skeleton) - { - const uint16_t bone_count = skeleton->get_bone_count(); - SkinnedVertexTransforms bone_transforms; - bone_transforms.resize(bone_count); - for (uint8_t bone_index = 0; bone_index < bone_count; bone_index++) - { - bone_transforms[bone_index] = skeleton->get_bone_global_pose(bone_index) * skeleton->get_bone_global_rest(bone_index).inverse(); - } - - const Array &mesh_arrays = mesh->surface_get_arrays(surface); - - const PackedVector3Array &mesh_vertex_array = mesh_arrays[ArrayMesh::ARRAY_VERTEX]; - const uint32_t mesh_vertex_array_size = mesh_vertex_array.size(); - - skinned_vertex_array.resize(mesh_vertex_array_size); - vertex_transforms.resize(mesh_vertex_array_size); - - const PackedInt32Array &mesh_bones_array = mesh_arrays[ArrayMesh::ARRAY_BONES]; - const PackedFloat32Array &mesh_weights_array = mesh_arrays[ArrayMesh::ARRAY_WEIGHTS]; - const uint8_t num_bones_per_vertex = mesh_bones_array.size() / mesh_vertex_array_size; - for (uint32_t vertex_index = 0; vertex_index < mesh_vertex_array_size; vertex_index++) - { - const Vector3 &vertex = mesh_vertex_array[vertex_index]; - Transform3D *transforms = new Transform3D[num_bones_per_vertex]; - const uint32_t bone_index_start = (vertex_index * num_bones_per_vertex); - for (uint8_t bone_index_offset = 0; bone_index_offset < num_bones_per_vertex; bone_index_offset++) - { - const float weight = mesh_weights_array[bone_index_start + bone_index_offset]; - const uint32_t bone = mesh_bones_array[bone_index_start + bone_index_offset]; - transforms[bone_index_offset] = ((Transform3D)bone_transforms[bone]) * weight; - } - - Vector3 x_basis, y_basis, z_basis, origin; - for (uint8_t transform = 0; transform < num_bones_per_vertex; transform++) - { - x_basis += transforms[transform].basis.rows[0]; - y_basis += transforms[transform].basis.rows[1]; - z_basis += transforms[transform].basis.rows[2]; - origin += transforms[transform].origin; - } - - const Transform3D &final_bone_transform = Transform3D( - x_basis.x, x_basis.y, x_basis.z, - y_basis.x, y_basis.y, y_basis.z, - z_basis.x, z_basis.y, z_basis.z, - origin.x, origin.y, origin.z); - vertex_transforms[vertex_index] = final_bone_transform; - skinned_vertex_array[vertex_index] = final_bone_transform.xform(vertex); - - delete transforms; - } - - return skinned_vertex_array; + state->output->main_half = state->other_half; + state->output->secondary_half = state->first_half; + state->output->main_half_split_map = state->other_half_split_map; + state->output->secondary_half_split_map = state->first_half_split_map; } else { - const Array &mesh_arrays = mesh->surface_get_arrays(surface); - - const PackedVector3Array &mesh_vertex_array = mesh_arrays[ArrayMesh::ARRAY_VERTEX]; - const uint32_t mesh_vertex_array_size = mesh_vertex_array.size(); - - SkinnedVertices skinned_vertex_array; - skinned_vertex_array.resize(mesh_vertex_array_size); - - for (uint32_t vertex_index = 0; vertex_index < mesh_vertex_array_size; vertex_index++) - { - skinned_vertex_array[vertex_index] = mesh_vertex_array[vertex_index]; - } - - return skinned_vertex_array; + state->output->main_half = state->first_half; + state->output->secondary_half = state->other_half; + state->output->main_half_split_map = state->first_half_split_map; + state->output->secondary_half_split_map = state->other_half_split_map; } - return SkinnedVertices(); + state->current_stage = MeshEditSliceStage::SLICE_STAGE_COMPLETE; + + RETURN_LOOP_TIME(); +} + + +Dictionary MeshEditingLibrary::split_mesh_by_body_part(Ref mesh) +{ + Dictionary array_mesh_map; + + const uint8_t num_surfaces = mesh->get_surface_count(); + for (uint8_t surface = 0; surface < num_surfaces; surface++) + { + const int64_t &original_mesh_format_mask = (int64_t)mesh->surface_get_format(surface); + + const bool has_custom0 = original_mesh_format_mask & Mesh::ARRAY_FORMAT_CUSTOM0; + const bool has_bone = original_mesh_format_mask & Mesh::ARRAY_FORMAT_BONES; + const bool has_weight = original_mesh_format_mask & Mesh::ARRAY_FORMAT_WEIGHTS; + + ERR_FAIL_COND_V_MSG(!has_custom0, Dictionary(), "Mesh must have all sections mapped to a grid on UV3 in order to be split into parts."); + + const Array &surface_array = mesh->surface_get_arrays(surface); + const PackedVector3Array &surface_vertex_array = surface_array[ArrayMesh::ARRAY_VERTEX]; + const PackedVector3Array &surface_normal_array = surface_array[ArrayMesh::ARRAY_NORMAL]; + const PackedFloat32Array &surface_tangent_array = surface_array[ArrayMesh::ARRAY_TANGENT]; + const PackedVector2Array &surface_uv_array = surface_array[ArrayMesh::ARRAY_TEX_UV]; + const PackedVector2Array &surface_uv2_array = surface_array[ArrayMesh::ARRAY_TEX_UV2]; + const PackedColorArray &surface_colour_array = surface_array[ArrayMesh::ARRAY_COLOR]; + const PackedInt32Array &surface_bone_array = surface_array[ArrayMesh::ARRAY_BONES]; + const PackedFloat32Array &surface_weight_array = surface_array[ArrayMesh::ARRAY_WEIGHTS]; + const PackedFloat32Array &surface_custom0_array = surface_array[ArrayMesh::ARRAY_CUSTOM0]; + const PackedFloat32Array &surface_index_array = surface_array[ArrayMesh::ARRAY_INDEX]; + + const uint8_t surface_num_bones_per_vertex = (has_bone && has_weight) ? + ((original_mesh_format_mask & Mesh::ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? 8 : 4 ) : 0; + } + + return array_mesh_map; } diff --git a/src/liborng/singletons/mesh_editing_library.h b/src/liborng/singletons/mesh_editing_library.h index 3e85161..0e63efe 100644 --- a/src/liborng/singletons/mesh_editing_library.h +++ b/src/liborng/singletons/mesh_editing_library.h @@ -19,8 +19,9 @@ using namespace godot; #include #define MESH_EDITING_SLICE_LOOP_LIMIT 5.555555555555555f - -#define MESH_EDITING_LIBRARY_TAG "MeshEditingLibrary" +#define START_LOOP_TIMER() const Time *time = Time::get_singleton(); const float start_time = time->get_ticks_msec() +#define RETURN_IF_TIME_IS_UP() if ((time->get_ticks_msec() - start_time) > MESH_EDITING_SLICE_LOOP_LIMIT) return time->get_ticks_msec() - start_time +#define RETURN_LOOP_TIME() return time->get_ticks_msec() - start_time typedef PackedVector3Array SkinnedVertices; typedef std::vector SkinnedSurfaces; @@ -75,6 +76,7 @@ public: enum MeshEditSliceStage { SLICE_STAGE_INIT, + SLICE_STAGE_GET_SKINNED_VERTS, SLICE_STAGE_PLANE_SPLIT, SLICE_STAGE_EDGE_SPLIT, SLICE_STAGE_APPLY_CAP, @@ -91,10 +93,13 @@ public: static Ref slice_mesh(const Ref input); static float slice_mesh_init(Ref state); + static float slice_mesh_get_skinned_verts(Ref state); static float slice_mesh_plane_split(Ref state); static float slice_mesh_edge_split(Ref state); static float slice_mesh_apply_cap(Ref state); + static Dictionary MeshEditingLibrary::split_mesh_by_body_part(Ref mesh); + protected: enum MeshSlicePlaneOrientation { @@ -106,7 +111,6 @@ protected: private: static const Vector2 calculate_planar_uv(const Vector3 &vertex, const Vector3 &mesh_bounds, const MeshSlicePlaneOrientation &axis); - static SkinnedVertices get_skinned_vertex_positions(const uint8_t surface, const Ref mesh, const Skeleton3D *skeleton = nullptr, std::vector &bone_transforms = std::vector()); static constexpr const uint8_t uv_to_body_part_shift(const float x, const float y) { return (uint8_t(y * 5.0f) * 5) + uint8_t(x * 5.0f); } @@ -124,6 +128,7 @@ protected: ClassDB::bind_static_method("MeshEditingLibrary", D_METHOD("slice_mesh", "input"), &MeshEditingLibrary::slice_mesh); BIND_ENUM_CONSTANT(SLICE_STAGE_INIT); + BIND_ENUM_CONSTANT(SLICE_STAGE_GET_SKINNED_VERTS); BIND_ENUM_CONSTANT(SLICE_STAGE_PLANE_SPLIT); BIND_ENUM_CONSTANT(SLICE_STAGE_EDGE_SPLIT); BIND_ENUM_CONSTANT(SLICE_STAGE_APPLY_CAP); @@ -164,8 +169,14 @@ class MeshEditSliceOutput : public RefCounted public: const Skeleton3D *original_skeleton = nullptr; + AABB original_full_aabb; + Plane original_local_plane; + Ref main_half; + uint32_t main_half_split_map = 0b0000000000000000000000000; + Ref secondary_half; + uint32_t secondary_half_split_map = 0b0000000000000000000000000; PackedVector3Array impact_points; uint32_t hit_map = 0b0000000000000000000000000; @@ -238,8 +249,8 @@ public: std::vector> base_to_first_sliced_vert_indices; std::vector> base_to_other_sliced_vert_indices; - uint32_t first_half_split_map = 0x00000000; - uint32_t other_half_split_map = 0x00000000; + uint32_t first_half_split_map = 0b0000000000000000000000000; + uint32_t other_half_split_map = 0b0000000000000000000000000; std::vector clip_edges; diff --git a/src/liborng/singletons/scene_loader.cpp b/src/liborng/singletons/scene_loader.cpp index bc4e47c..f69353a 100644 --- a/src/liborng/singletons/scene_loader.cpp +++ b/src/liborng/singletons/scene_loader.cpp @@ -168,7 +168,7 @@ void SceneLoader::load_scene(const StringName path, Callable callback) if (!this->resource_loader) return; if (this->resource_loader->load_threaded_request(path, "PackedScene", true) != Error::OK) { - PRINT_ERROR("SceneLoader", "Scene ", path, " does not exist."); + PRINT_ERROR(SceneLoader, "Scene ", path, " does not exist."); return; }