Author SHA1 Message Date
ActualHorse a359da1762 - 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.
2026-10-16 13:48:15 -04:00
ActualHorse 00332be60c Reorganised code files and moved functions to DamageableNode3D parent class. 2026-10-04 21:40:52 -04:00
ActualHorse 1a38b7bf19 Hitboxes now send multiple hitboxes per frame instead of just one. 2026-10-02 00:27:32 -04:00
ActualHorse 4e0b756000 Slice performance improved by setting time limits on loops. 2026-10-02 00:26:44 -04:00
24 changed files with 940 additions and 638 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ env.Append(CPPPATH="liborng/")
env.Append(CPPPATH="liborng/data_assets")
env.Append(CPPPATH="liborng/nodes/interfaces")
env.Append(CPPPATH="liborng/nodes")
env.Append(CPPPATH="liborng/nodes/character_nodes")
env.Append(CPPPATH="liborng/nodes/damageables")
env.Append(CPPPATH="liborng/resources")
env.Append(CPPPATH="liborng/singletons")
env.Append(CPPPATH="liborng/singletons/vendor_service")
-60
View File
@@ -1,60 +0,0 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#include "i_damageable.h"
#include "orng_macros.h"
#include "godot_cpp/classes/scene_tree.hpp"
#include "godot_cpp/classes/scene_tree_timer.hpp"
void IDamageable::_slice_meshes(Ref<MeshEditSliceInput> data)
{
this->_is_slicing = true;
this->_input_queue.emplace_back(data);
if (this->_slice_state_queue.empty())
this->_new_slice_loop();
}
void IDamageable::_new_slice_loop()
{
this->_slice_state_queue.emplace_back(MeshEditingLibrary::slice_mesh(this->_input_queue.front()));
this->_slice_loop();
}
void IDamageable::_slice_loop()
{
Ref<MeshSliceState> current_state = this->_slice_state_queue.front();
switch(current_state->current_stage)
{
case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_INIT:
MeshEditingLibrary::slice_mesh_init(current_state);
break;
case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_PLANE_SPLIT:
MeshEditingLibrary::slice_mesh_plane_split(current_state);
break;
case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_EDGE_SPLIT:
MeshEditingLibrary::slice_mesh_edge_split(current_state);
break;
case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_APPLY_CAP:
MeshEditingLibrary::slice_mesh_apply_cap(current_state);
break;
case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_COMPLETE:
this->_slice_complete(current_state->output);
this->_input_queue.erase(this->_input_queue.begin());
this->_slice_state_queue.erase(this->_slice_state_queue.begin());
if (!this->_input_queue.empty())
return this->_new_slice_loop();
break;
}
if (!this->_input_queue.empty())
CALL_NEXT_FRAME(callable_mp(this, &IDamageable::_slice_loop));
else
this->_is_slicing = false;
}
-38
View File
@@ -1,38 +0,0 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#pragma once
#include "nodes/mesh_editing_library.h"
#include "godot_cpp/classes/node.hpp"
using namespace godot;
class IDamageable : public Node3D
{
public:
virtual void take_damage(const struct HurtboxDamage &hit_data) = 0;
protected:
void _slice_meshes(Ref<MeshEditSliceInput> data);
virtual void _slice_complete(Ref<MeshEditSliceOutput> data) = 0;
private:
void _new_slice_loop();
void _slice_loop();
bool _is_slicing = false;
std::vector<Ref<MeshSliceState>> _slice_state_queue;
std::vector<Ref<MeshEditSliceInput>> _input_queue;
// Godot boilerplate below
protected:
static void _bind_methods() {}
};
@@ -1,192 +0,0 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#include "character_3d.h"
#include "orng_macros.h"
#include "nodes/debug_draw3d.h"
#include "nodes/mesh_editing_library.h"
#include "nodes/hurtbox/hurtbox_base.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/physical_bone3d.hpp>
#include <godot_cpp/classes/physical_bone_simulator3d.hpp>
#include <godot_cpp/classes/random_number_generator.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/scene_tree_timer.hpp>
#include <godot_cpp/classes/skin.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
using namespace godot;
void Character3D::_ready()
{
if (this->character_skeleton)
{
this->_rebuild_mesh_instances();
}
}
void Character3D::set_meshes(TypedArray<ArrayMesh> m)
{
this->meshes = m;
this->_full_aabb = AABB();
this->_rebuild_mesh_instances();
}
void Character3D::set_character_skeleton(CharacterSkeleton3D *cs)
{
if (cs)
{
if (this->character_skeleton)
{
this->skeleton_set.erase(this->character_skeleton);
this->character_skeleton->queue_free();
}
this->character_skeleton = cs;
this->skeleton_set.emplace(this->character_skeleton);
this->_rebuild_mesh_instances();
}
}
void Character3D::take_damage(const HurtboxDamage &hit_data)
{
if (this->_hurtbox_queue.empty())
CALL_NEXT_FRAME(callable_mp(this, &Character3D::_next_hurtbox_in_queue));
this->_hurtbox_queue.emplace_back(hit_data);
}
void Character3D::_next_hurtbox_in_queue()
{
HurtboxDamage &hit_data = this->_hurtbox_queue.front();
if (!hit_data.bones.empty())
{
CharacterSkeleton3D *character_skeleton = hit_data.bones.begin()->first;
const std::vector<int32_t> &bone_hits = hit_data.bones.begin()->second;
hit_data.bones.erase(this->_hurtbox_queue.begin()->bones.begin());
Skeleton3D *skeleton = character_skeleton->get_skeleton();
const Plane &local_slice_plane = skeleton->get_global_transform().inverse().xform(Plane(hit_data.global_hit_transform.get_basis().get_column(1), hit_data.global_hit_transform.origin));
TypedArray<MeshInstance3D> sliced_mesh_instances = character_skeleton->get_mesh_instances();
const uint32_t num_mesh_instances = sliced_mesh_instances.size();
TypedArray<ArrayMesh> first_half_meshes_to_add;
TypedArray<ArrayMesh> other_half_meshes_to_add;
Ref<MeshEditSliceInput> input; input.instantiate();
input->mesh_instance = cast_to<MeshInstance3D>(sliced_mesh_instances[0]);
input->skeleton = skeleton;
input->full_aabb = this->_full_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);
}
else
{
this->_hurtbox_queue.erase(this->_hurtbox_queue.begin());
}
if (!this->_hurtbox_queue.empty())
CALL_NEXT_FRAME(callable_mp(this, &Character3D::_next_hurtbox_in_queue));
}
void Character3D::_slice_complete(Ref<MeshEditSliceOutput> data)
{
if (data->main_half->get_surface_count() <= 0 && data->secondary_half->get_surface_count() > 0)
{
Ref<ArrayMesh> temp = data->main_half;
data->main_half = data->secondary_half;
data->secondary_half = temp;
}
TypedArray<ArrayMesh> first_half_meshes_to_add;
TypedArray<ArrayMesh> other_half_meshes_to_add;
if (data->main_half->get_surface_count() > 0)
{
first_half_meshes_to_add.append(data->main_half);
}
if (data->secondary_half->get_surface_count() > 0)
{
other_half_meshes_to_add.append(data->secondary_half);
}
for (CharacterSkeleton3D *skeleton : this->skeleton_set)
{
if (skeleton->get_skeleton() == data->original_skeleton)
{
skeleton->set_meshes(first_half_meshes_to_add);
break;
}
}
if (other_half_meshes_to_add.size() > 0)
{
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<CharacterSkeleton3D>(this->character_skeleton->duplicate());
this->character_skeleton->add_sibling(new_character_skeleton);
new_character_skeleton->set_position(Vector3(random_x, 0.0f, random_z));
PRINT_LOG(CHARACTER3D_TAG, "Adding new meshes:");
for (uint16_t i = 0; i < other_half_meshes_to_add.size(); i++)
{
const Ref<ArrayMesh> &new_array_mesh = other_half_meshes_to_add[i];
PRINT_LOG(CHARACTER3D_TAG, " Mesh ", i+1, " size: ", new_array_mesh->get_surface_count());
}
new_character_skeleton->set_meshes(other_half_meshes_to_add);
this->skeleton_set.emplace(new_character_skeleton);
}
}
void Character3D::_on_fell_out_of_world()
{
this->emit_signal("fell_out_of_world");
}
void Character3D::_rebuild_mesh_instances()
{
if (!this->character_skeleton || !this->is_node_ready())
return;
TypedArray<MeshInstance3D> mesh_instances = this->character_skeleton->set_meshes(this->meshes);
const uint32_t num_meshes = mesh_instances.size();
this->_full_aabb = AABB();
for (uint32_t i = 0; i < num_meshes; i++)
{
const MeshInstance3D *mesh_instance = cast_to<MeshInstance3D>(mesh_instances[i]);
const Vector3 &full_aabb_position = this->_full_aabb.get_position();
const Vector3 &full_aabb_size = this->_full_aabb.get_size();
const AABB &aabb = mesh_instance->get_aabb();
const Vector3 &aabb_position = aabb.get_position();
const Vector3 &aabb_size = aabb.get_size();
this->_full_aabb.set_position(Vector3(
UtilityFunctions::minf(full_aabb_position.x, aabb_position.x),
UtilityFunctions::minf(full_aabb_position.y, aabb_position.y),
UtilityFunctions::minf(full_aabb_position.z, aabb_position.z)
));
this->_full_aabb.set_size(Vector3(
UtilityFunctions::maxf(full_aabb_position.x, aabb_size.x),
UtilityFunctions::maxf(full_aabb_position.y, aabb_size.y),
UtilityFunctions::maxf(full_aabb_position.z, aabb_size.z)
));
}
}
@@ -1,78 +0,0 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#pragma once
#include "orng_macros.h"
#include "interfaces/i_damageable.h"
#include "nodes/character_nodes/character_skeleton3d.h"
#include <map>
#include <unordered_set>
#include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/material.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/variant/dictionary.hpp>
using namespace godot;
#define CHARACTER3D_TAG "Character3D"
class Character3D : public IDamageable
{
GDCLASS(Character3D, IDamageable);
public:
virtual void _ready() override;
virtual void take_damage(const HurtboxDamage &hit_data) override;
TypedArray<ArrayMesh> get_meshes() const { return this->meshes; }
CharacterSkeleton3D *get_character_skeleton() const { return this->character_skeleton; }
TypedArray<Material> get_inner_materials() const { return this->inner_materials; }
virtual void _on_fell_out_of_world();
protected:
void set_meshes(TypedArray<ArrayMesh> m);
void set_character_skeleton(CharacterSkeleton3D *cs);
void set_inner_materials(TypedArray<Material> materials) { this->inner_materials = materials; }
private:
void _rebuild_mesh_instances();
void _next_hurtbox_in_queue();
virtual void _slice_complete(Ref<MeshEditSliceOutput> data) override;
TypedArray<ArrayMesh> meshes;
CharacterSkeleton3D *character_skeleton = nullptr;
std::unordered_set<CharacterSkeleton3D*> skeleton_set;
AABB _full_aabb;
TypedArray<Material> inner_materials;
std::vector<HurtboxDamage> _hurtbox_queue;
// Godot boilerplate below
protected:
static void _bind_methods()
{
BIND_METHOD(Character3D, _on_fell_out_of_world);
ADD_SIGNAL(MethodInfo("fell_out_of_world"));
ADD_GETTER_SETTER_HINTED(Character3D, meshes, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, "ArrayMesh");
ADD_GETTER_SETTER_HINTED(Character3D, character_skeleton, Variant::OBJECT, PROPERTY_HINT_NODE_TYPE, "CharacterSkeleton3D");
ADD_GETTER_SETTER_HINTED(Character3D, inner_materials, Variant::ARRAY, PROPERTY_HINT_ARRAY_TYPE, "Material");
}
};
@@ -0,0 +1,146 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#include "character_3d.h"
#include "orng_macros.h"
#include "nodes/damageables/sliceable_rigid_body3d.h"
#include "nodes/hurtbox/hurtbox_base.h"
#include "singletons/mesh_editing_library.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/physical_bone3d.hpp>
#include <godot_cpp/classes/physical_bone_simulator3d.hpp>
#include <godot_cpp/classes/random_number_generator.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/scene_tree_timer.hpp>
#include <godot_cpp/classes/skin.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
using namespace godot;
void Character3D::set_character_skeleton(CharacterSkeleton3D *cs)
{
if (cs)
{
if (this->character_skeleton)
{
this->_skeleton_set.erase(this->character_skeleton);
this->character_skeleton->queue_free();
}
this->character_skeleton = cs;
this->_skeleton_set.emplace(this->character_skeleton);
this->_rebuild_mesh_instance();
}
}
void Character3D::take_damage(const HurtboxDamage &hit_data)
{
if (this->_hurtbox_queue.empty())
callable_mp(this, &Character3D::_next_hurtbox_in_queue).call_deferred();
this->_hurtbox_queue.emplace_back(hit_data);
}
void Character3D::_next_hurtbox_in_queue()
{
HurtboxDamage &hit_data = this->_hurtbox_queue.front();
if (!hit_data.bones.empty())
{
CharacterSkeleton3D *character_skeleton = hit_data.bones.begin()->first;
const std::vector<int32_t> &bone_hits = hit_data.bones.begin()->second;
hit_data.bones.erase(this->_hurtbox_queue.begin()->bones.begin());
Skeleton3D *skeleton = character_skeleton->get_skeleton();
const Plane &local_slice_plane = skeleton->get_global_transform().inverse().xform(Plane(hit_data.global_hit_transform.get_basis().get_column(1), hit_data.global_hit_transform.origin));
Ref<MeshEditSliceInput> input; input.instantiate();
input->mesh_instance = character_skeleton->get_mesh_instance();
input->skeleton = skeleton;
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);
}
else
{
this->_hurtbox_queue.erase(this->_hurtbox_queue.begin());
}
if (!this->_hurtbox_queue.empty())
CALL_NEXT_FRAME(callable_mp(this, &Character3D::_next_hurtbox_in_queue));
}
void Character3D::_slice_complete(Ref<MeshEditSliceOutput> data)
{
if (data->original_skeleton)
{
for (CharacterSkeleton3D *skeleton : this->_skeleton_set)
{
if (skeleton->get_skeleton() == data->original_skeleton)
{
MeshInstance3D *new_mesh_instance = memnew(MeshInstance3D);
new_mesh_instance->set_mesh(data->main_half);
skeleton->set_mesh(new_mesh_instance);
break;
}
}
if (data->secondary_half.is_valid())
{
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<CharacterSkeleton3D>(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");
}
}
}
}
void Character3D::_on_fell_out_of_world()
{
this->emit_signal("fell_out_of_world");
}
MeshInstance3D *Character3D::_rebuild_mesh_instance()
{
if (MeshInstance3D *returned_instance = DamageableNode3D::_rebuild_mesh_instance())
{
this->character_skeleton->set_mesh(returned_instance);
return returned_instance;
}
return nullptr;
}
@@ -0,0 +1,81 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#pragma once
#include "orng_macros.h"
#include "nodes/damageables/damageable_node3d.h"
#include "nodes/damageables/character_skeleton3d.h"
#include <map>
#include <unordered_set>
#include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/material.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/variant/dictionary.hpp>
using namespace godot;
#define CHARACTER3D_TAG "Character3D"
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; }
virtual void _on_fell_out_of_world();
protected:
void set_character_skeleton(CharacterSkeleton3D *cs);
virtual MeshInstance3D *_rebuild_mesh_instance() override;
private:
void _next_hurtbox_in_queue();
virtual void _slice_complete(Ref<MeshEditSliceOutput> data) override;
uint32_t loose_object_collision_layer = 0b1;
uint32_t loose_object_collision_mask = 0b1;
CharacterSkeleton3D *character_skeleton = nullptr;
std::unordered_set<CharacterSkeleton3D*> _skeleton_set;
std::vector<HurtboxDamage> _hurtbox_queue;
// Godot boilerplate below
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"));
ADD_GETTER_SETTER_HINTED(Character3D, character_skeleton, Variant::OBJECT, PROPERTY_HINT_NODE_TYPE, "CharacterSkeleton3D");
}
};
@@ -16,34 +16,23 @@
using namespace godot;
TypedArray<MeshInstance3D> CharacterSkeleton3D::set_meshes(TypedArray<ArrayMesh> meshes)
void CharacterSkeleton3D::set_mesh(MeshInstance3D *mesh)
{
if (this->skeleton)
{
this->_old_mesh_instances = this->_mesh_instances.duplicate(true);
this->_mesh_instances.clear();
this->_old_mesh_instance = this->_mesh_instance;
uint16_t num_new_meshes = meshes.size();
this->_mesh_instances.resize(num_new_meshes);
for (uint16_t i = 0; i < num_new_meshes; i++)
{
MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
mesh_instance->set_mesh(meshes[i]);
mesh_instance->set_skeleton_path(this->skeleton->get_path());
this->skeleton->add_child(mesh_instance, false, Node::INTERNAL_MODE_BACK);
this->_mesh_instances[i] = mesh_instance;
}
this->_mesh_instance = mesh;
_mesh_instance->set_skeleton_path(this->skeleton->get_path());
this->skeleton->add_child(this->_mesh_instance, false, Node::INTERNAL_MODE_BACK);
CALL_NEXT_FRAME(callable_mp(this, &CharacterSkeleton3D::_delete_old_meshes));
CALL_NEXT_FRAME(callable_mp(this, &CharacterSkeleton3D::_delete_old_mesh));
}
return this->_mesh_instances;
}
void CharacterSkeleton3D::_delete_old_meshes()
void CharacterSkeleton3D::_delete_old_mesh()
{
for (uint16_t i = 0; i < this->_old_mesh_instances.size(); i++)
cast_to<MeshInstance3D>(this->_old_mesh_instances[i])->queue_free();
this->_old_mesh_instances.clear();
if (this->_old_mesh_instance)
this->_old_mesh_instance->queue_free();
}
@@ -26,15 +26,14 @@ class CharacterSkeleton3D : public Node3D
GDCLASS(CharacterSkeleton3D, Node3D);
public:
TypedArray<MeshInstance3D> set_meshes(TypedArray<ArrayMesh> meshes);
void set_mesh(MeshInstance3D *mesh);
Skeleton3D *get_skeleton() const { return this->skeleton; }
PhysicalBoneSimulator3D *get_physical_bone_simulator() const { return this->physical_bone_simulator; }
AnimationPlayer *get_animation_player() const { return this->animation_player; }
AnimationTree *get_animation_tree() const { return this->animation_tree; }
void replace_mesh_instance(const uint16_t index, Ref<ArrayMesh> new_mesh) { cast_to<MeshInstance3D>(this->_mesh_instances[index])->set_mesh(new_mesh); }
TypedArray<MeshInstance3D> get_mesh_instances() const { return this->_mesh_instances; }
MeshInstance3D *get_mesh_instance() const { return this->_mesh_instance; }
protected:
void set_skeleton(Skeleton3D *s) { this->skeleton = s; }
@@ -44,7 +43,7 @@ protected:
private:
// void _refresh_skins_and_meshes();
void _delete_old_meshes();
void _delete_old_mesh();
void _activate_ragdoll();
@@ -53,8 +52,8 @@ private:
AnimationPlayer *animation_player = nullptr;
AnimationTree *animation_tree = nullptr;
TypedArray<MeshInstance3D> _mesh_instances;
TypedArray<MeshInstance3D> _old_mesh_instances;
MeshInstance3D *_mesh_instance = nullptr;
MeshInstance3D *_old_mesh_instance = nullptr;
// Godot boilerplate below
protected:
@@ -0,0 +1,125 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#include "damageable_node3d.h"
#include "orng_macros.h"
#include "godot_cpp/classes/scene_tree.hpp"
#include "godot_cpp/classes/scene_tree_timer.hpp"
void DamageableNode3D::_ready()
{
this->_rebuild_mesh_instance();
}
void DamageableNode3D::set_mesh(Ref<ArrayMesh> m)
{
this->mesh = m;
this->_rebuild_mesh_instance();
}
void DamageableNode3D::_slice_meshes(Ref<MeshEditSliceInput> data)
{
this->_is_slicing = true;
if (this->_validate_input(data))
{
this->_input_queue.emplace_back(data);
if (this->_slice_state_queue.empty())
this->_new_slice_loop();
}
}
void DamageableNode3D::_new_slice_loop()
{
Ref<MeshEditSliceInput> next_in_queue = this->_input_queue.front();
Ref<MeshEditSliceState> new_state = MeshEditingLibrary::slice_mesh(next_in_queue);
this->_slice_state_queue.emplace_back(new_state);
this->_slice_loop();
}
void DamageableNode3D::_slice_loop()
{
Ref<MeshEditSliceState> current_state = this->_slice_state_queue.front();
switch(current_state->current_stage)
{
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;
case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_EDGE_SPLIT:
if (MeshEditingLibrary::slice_mesh_edge_split(current_state) > MESH_EDITING_SLICE_LOOP_LIMIT)
break;
case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_APPLY_CAP:
if (MeshEditingLibrary::slice_mesh_apply_cap(current_state) > MESH_EDITING_SLICE_LOOP_LIMIT)
break;
case MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_COMPLETE:
if (this->_slice_loop_end(current_state))
return this->_new_slice_loop();
break;
}
if (!this->_input_queue.empty())
CALL_NEXT_FRAME(callable_mp(this, &DamageableNode3D::_slice_loop));
else
this->_is_slicing = false;
}
bool DamageableNode3D::_slice_loop_end(Ref<MeshEditSliceState> current_state)
{
this->_slice_complete(current_state->output);
this->_input_queue.erase(this->_input_queue.begin());
this->_slice_state_queue.erase(this->_slice_state_queue.begin());
if (!this->_input_queue.empty())
return true;
return false;
}
bool DamageableNode3D::_validate_input(Ref<MeshEditSliceInput> input)
{
if (this->_input_queue.size() >= MAX_SLICE_QUEUE_SIZE)
{
return false;
}
for (Ref<MeshEditSliceInput> &queued_input : this->_input_queue)
{
if (queued_input->skeleton == input->skeleton)
{
return false;
}
}
return true;
}
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);
if (this->aabb == AABB())
this->aabb = this->_active_mesh_instance->get_aabb();
return this->_active_mesh_instance;
}
@@ -0,0 +1,71 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#pragma once
#include "orng_macros.h"
#include "singletons/mesh_editing_library.h"
#include "godot_cpp/classes/node.hpp"
using namespace godot;
#define MAX_SLICE_QUEUE_SIZE 4
#define DAMAGEABLE_NODE3D_TAG "DamageableNode3D"
class DamageableNode3D : public Node3D
{
GDCLASS(DamageableNode3D, Node3D);
public:
virtual void _ready() override;
void set_mesh(Ref<ArrayMesh> m);
Ref<ArrayMesh> 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<Material> materials) { this->inner_materials = materials; }
TypedArray<Material> get_inner_materials() const { return this->inner_materials; }
virtual void take_damage(const struct HurtboxDamage &hit_data) = 0;
protected:
MeshInstance3D *_get_active_mesh_instance() { return this->_active_mesh_instance; }
virtual MeshInstance3D *_rebuild_mesh_instance();
void _slice_meshes(Ref<MeshEditSliceInput> data);
virtual void _slice_complete(Ref<MeshEditSliceOutput> data) = 0;
Ref<ArrayMesh> mesh;
AABB aabb;
TypedArray<Material> inner_materials;
private:
void _new_slice_loop();
void _slice_loop();
bool _slice_loop_end(Ref<MeshEditSliceState> current_state);
bool _validate_input(Ref<MeshEditSliceInput> input);
MeshInstance3D *_active_mesh_instance = nullptr;
bool _is_slicing = false;
std::vector<Ref<MeshEditSliceState>> _slice_state_queue;
std::vector<Ref<MeshEditSliceInput>> _input_queue;
// Godot boilerplate below
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");
}
};
@@ -0,0 +1,108 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#include "sliceable_rigid_body3d.h"
#include "orng_macros.h"
#include "nodes/hurtbox/hurtbox_base.h"
#include <godot_cpp/classes/convex_polygon_shape3d.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/scene_tree_timer.hpp>
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<MeshEditSliceInput> 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<MeshEditSliceOutput> 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<ArrayMesh> returned_mesh = returned_instance->get_mesh();
if (returned_mesh.is_valid())
{
Ref<ConvexPolygonShape3D> shape = returned_mesh->create_convex_shape(false, false);
this->collision_shape->set_shape(shape);
}
return returned_instance;
}
return nullptr;
}
@@ -0,0 +1,69 @@
/*
* ©2026 Batty Bovine Productions, LLC. All Rights Reserved.
*
* If this source code makes it to the public internet, this software can be
* considered to be protected by the MIT licence. Have fun with it.
*/
#pragma once
#include "orng_macros.h"
#include "nodes/damageables/damageable_node3d.h"
#include <map>
#include <unordered_set>
#include <godot_cpp/classes/collision_shape3d.hpp>
#include <godot_cpp/classes/material.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/rigid_body3d.hpp>
using namespace godot;
#define SLICEABLE_RIGID_BODY3D_TAG "SliceableRigidBody3D"
class SliceableRigidBody3D : public DamageableNode3D
{
GDCLASS(SliceableRigidBody3D, DamageableNode3D);
public:
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<MeshEditSliceOutput> 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()
{
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");
}
};
+33 -27
View File
@@ -8,7 +8,8 @@
#include "hurtbox_base.h"
#include "orng_macros.h"
#include "nodes/character_nodes/character_3d.h"
#include "nodes/damageables/character_3d.h"
#include "nodes/damageables/sliceable_rigid_body3d.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/physical_bone3d.hpp>
@@ -60,7 +61,7 @@ void HurtboxBase::_physics_process(double delta)
const TypedArray<Node3D> &bodies = this->get_overlapping_bodies();
const uint32_t num_bodies = bodies.size();
std::unordered_set<IDamageable*> new_hit_bodies;
std::unordered_set<DamageableNode3D*> new_hit_bodies;
std::vector<CharacterSkeleton3D*> new_hit_skeletons;
for (uint32_t i = 0; i < num_bodies; i++)
@@ -77,59 +78,64 @@ 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<PhysicalBone3D>(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<RigidBody3D>(bodies[i]))
{
if (SliceableRigidBody3D *sliceable_rigid_body = cast_to<SliceableRigidBody3D>(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 (IDamageable *damageable : new_hit_bodies)
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()
{
if (!this->hit_data_queue.empty())
for (uint8_t i = 0; i < MAX_HITS_PER_QUEUE_LOOP && !this->hit_data_queue.empty(); i++)
{
IDamageable *closest_damageable = nullptr;
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 = (Character3D*)(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));
}
}
+6 -4
View File
@@ -9,14 +9,16 @@
#include "orng_macros.h"
#include "nodes/character_nodes/character_3d.h"
#include "nodes/character_nodes/character_skeleton3d.h"
#include "nodes/damageables/character_3d.h"
#include "nodes/damageables/character_skeleton3d.h"
#include <godot_cpp/classes/collision_shape3d.hpp>
#include <godot_cpp/classes/shape3d.hpp>
#include <godot_cpp/classes/area3d.hpp>
using namespace godot;
#define MAX_HITS_PER_QUEUE_LOOP 1
#define HURTBOX_BASE_TAG "HurtboxBase"
@@ -48,7 +50,7 @@ public:
int32_t get_hit_points() const { return this->hit_points; }
protected:
std::map<class IDamageable*, std::vector<HurtboxDamage>> hit_data_queue;
std::map<class DamageableNode3D*, std::vector<HurtboxDamage>> hit_data_queue;
private:
Dictionary _create_hit_data_struct() const;
@@ -61,7 +63,7 @@ private:
float remaining_life = 0.0f;
int32_t hit_points = 0;
std::vector<class IDamageable*> hit_bodies;
std::vector<class DamageableNode3D*> hit_bodies;
// Godot boilerplate below
+2 -2
View File
@@ -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);
}
-2
View File
@@ -15,8 +15,6 @@
#include <godot_cpp/classes/resource.hpp>
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"
+1 -1
View File
@@ -68,7 +68,7 @@ bool PlayerOrigin::spawn_player(Node3D *parent, Ref<PackedScene> 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
}
-2
View File
@@ -14,8 +14,6 @@
#include <godot_cpp/classes/packed_scene.hpp>
using namespace godot;
#define PLAYERSPAWN_LOG_TAG "PlayerSpawn"
#define PLAYER_ORIGIN_TAG "origin"
+6 -6
View File
@@ -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
+8 -5
View File
@@ -21,17 +21,17 @@
#include "nodes/player_spawn.h"
#include "nodes/sublevel_loader.h"
#include "nodes/sublevel_scene.h"
#include "nodes/character_nodes/character_3d.h"
#include "nodes/character_nodes/character_skeleton3d.h"
#include "nodes/damageables/character_3d.h"
#include "nodes/damageables/character_skeleton3d.h"
#include "nodes/damageables/sliceable_rigid_body3d.h"
#include "nodes/hurtbox/hurtbox_base.h"
#include "resources/level_metadata_map.h"
#include "resources/level_metadata_resource.h"
#include "nodes/mesh_editing_library.h"
#include "singletons/input_handler.h"
#include "singletons/mesh_editing_library.h"
#include "singletons/save_manager.h"
#include "singletons/scene_loader.h"
// #include "singletons/vendor_service/vendor_service.h"
@@ -66,7 +66,10 @@ void initialize_orng_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<MovementHandler>();
ClassDB::register_abstract_class<DamageableNode3D>();
ClassDB::register_class<Character3D>();
ClassDB::register_class<SliceableRigidBody3D>();
ClassDB::register_class<CharacterSkeleton3D>();
ClassDB::register_class<GameClasses>();
ClassDB::register_class<LevelScene>();
@@ -80,7 +83,7 @@ void initialize_orng_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<MeshEditingLibrary>();
ClassDB::register_class<MeshEditSliceInput>();
ClassDB::register_class<MeshEditSliceOutput>();
ClassDB::register_class<MeshSliceState>();
ClassDB::register_class<MeshEditSliceState>();
GDSINGLETON_REGISTER_CLASS(InputHandler, _input_handler_singleton);
GDSINGLETON_REGISTER_CLASS(SaveManager, _save_manager_singleton);
@@ -16,9 +16,11 @@
using namespace godot;
Ref<MeshSliceState> MeshEditingLibrary::slice_mesh(const Ref<MeshEditSliceInput> input)
Ref<MeshEditSliceState> MeshEditingLibrary::slice_mesh(const Ref<MeshEditSliceInput> input)
{
Ref<MeshSliceState> fresh_state;
const Time *time = Time::get_singleton();
Ref<MeshEditSliceState> fresh_state;
fresh_state.instantiate();
fresh_state->input = input;
@@ -28,69 +30,67 @@ Ref<MeshSliceState> MeshEditingLibrary::slice_mesh(const Ref<MeshEditSliceInput>
fresh_state->first_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;
}
void MeshEditingLibrary::slice_mesh_init(Ref<MeshSliceState> state)
float MeshEditingLibrary::slice_mesh_init(Ref<MeshEditSliceState> state)
{
{ // Initialise the state here
// const Time *time = Time::get_singleton();
// 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();
// PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Got skinned vertices at ", time->get_ticks_msec(), "ms");
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);
}
START_LOOP_TIMER();
PRINT_LOG(MeshEditingLibrary, "Started slicing mesh at ", time->get_ticks_msec(), "ms");
for (state->surface; state->surface < state->num_surfaces; state->surface++)
{
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]);
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];
@@ -127,15 +127,106 @@ void MeshEditingLibrary::slice_mesh_init(Ref<MeshSliceState> state)
state->num_custom0_per_vertex[state->surface] = surface_num_custom0_per_vertex;
state->vertex_distances[state->surface].resize(num_vertices);
state->skinned_vertex_positions[state->surface].resize(num_vertices);
state->skinned_vertex_transforms[state->surface].resize(num_vertices);
}
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<MeshEditSliceState> 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_LOOP_TIME();
}
void MeshEditingLibrary::slice_mesh_plane_split(Ref<MeshSliceState> state)
float MeshEditingLibrary::slice_mesh_plane_split(Ref<MeshEditSliceState> state)
{
START_LOOP_TIMER();
for (state->surface; state->surface < state->num_surfaces; state->surface++)
{
const PackedVector3Array &surface_vertex_array = state->surface_arrays_list[state->surface][ArrayMesh::ARRAY_VERTEX];
@@ -170,6 +261,8 @@ void MeshEditingLibrary::slice_mesh_plane_split(Ref<MeshSliceState> state)
for (state->element; state->element < num_vertices; state->element++)
{
RETURN_IF_TIME_IS_UP();
vertex_distances[state->element] = state->input->local_plane.distance_to(state->skinned_vertex_positions[state->surface][state->element]);
if (vertex_distances[state->element] >= 0.0f)
@@ -197,7 +290,7 @@ void MeshEditingLibrary::slice_mesh_plane_split(Ref<MeshSliceState> 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)
{
@@ -233,7 +326,7 @@ void MeshEditingLibrary::slice_mesh_plane_split(Ref<MeshSliceState> 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)
{
@@ -246,7 +339,7 @@ void MeshEditingLibrary::slice_mesh_plane_split(Ref<MeshSliceState> state)
}
}
// PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Surface ", 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;
}
@@ -255,14 +348,16 @@ void MeshEditingLibrary::slice_mesh_plane_split(Ref<MeshSliceState> state)
state->surface = 0;
state->current_stage = MeshEditSliceStage::SLICE_STAGE_EDGE_SPLIT;
RETURN_LOOP_TIME();
}
void MeshEditingLibrary::slice_mesh_edge_split(Ref<MeshSliceState> state)
float MeshEditingLibrary::slice_mesh_edge_split(Ref<MeshEditSliceState> state)
{
START_LOOP_TIMER();
for (state->surface; state->surface < state->num_surfaces; state->surface++)
{
Ref<Material> original_material = state->original_mesh->surface_get_material(state->surface);
const PackedVector3Array &surface_vertex_array = state->surface_arrays_list[state->surface][ArrayMesh::ARRAY_VERTEX];
const PackedVector3Array &surface_normal_array = state->surface_arrays_list[state->surface][ArrayMesh::ARRAY_NORMAL];
const PackedFloat32Array &surface_tangent_array = state->surface_arrays_list[state->surface][ArrayMesh::ARRAY_TANGENT];
@@ -307,6 +402,8 @@ void MeshEditingLibrary::slice_mesh_edge_split(Ref<MeshSliceState> state)
const uint32_t num_triangles = surface_index_array.size();
for (state->element; state->element < num_triangles; state->element += 3)
{
RETURN_IF_TIME_IS_UP();
uint32_t base_v[3];
std::map<uint32_t, uint32_t>::iterator sliced_v[3];
std::map<uint32_t, uint32_t>::iterator sliced_other_v[3];
@@ -408,23 +505,21 @@ void MeshEditingLibrary::slice_mesh_edge_split(Ref<MeshSliceState> 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)
@@ -541,9 +636,12 @@ void MeshEditingLibrary::slice_mesh_edge_split(Ref<MeshSliceState> state)
}
}
const Transform3D &interp_bone_transform = surface_skinned_vertex_transforms[base_v[this_vert]].interpolate_with(
surface_skinned_vertex_transforms[base_v[next_vert]], alpha);
surface_skinned_vertex_transforms.emplace_back(interp_bone_transform);
if (state->input->skeleton)
{
const Transform3D &interp_bone_transform = surface_skinned_vertex_transforms[base_v[this_vert]].interpolate_with(
surface_skinned_vertex_transforms[base_v[next_vert]], alpha);
surface_skinned_vertex_transforms.emplace_back(interp_bone_transform);
}
}
}
@@ -570,6 +668,8 @@ void MeshEditingLibrary::slice_mesh_edge_split(Ref<MeshSliceState> state)
state->element = 0;
}
Ref<Material> original_material = state->original_mesh->surface_get_material(state->surface);
int64_t array_format_bit_mask = Mesh::ARRAY_FORMAT_VERTEX;
if (has_normal) array_format_bit_mask |= Mesh::ARRAY_FORMAT_NORMAL;
if (has_tangent) array_format_bit_mask |= Mesh::ARRAY_FORMAT_TANGENT;
@@ -621,28 +721,29 @@ void MeshEditingLibrary::slice_mesh_edge_split(Ref<MeshSliceState> state)
state->other_half->surface_set_material(next_surface, original_material);
}
// PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Surface ", 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_LOOP_TIME();
}
void MeshEditingLibrary::slice_mesh_apply_cap(Ref<MeshSliceState> state)
float MeshEditingLibrary::slice_mesh_apply_cap(Ref<MeshEditSliceState> state)
{
const Time *time = Time::get_singleton();
const float start_time = time->get_ticks_msec();
if (state->clip_edges.size() > 0)
{
MeshSlicePlaneOrientation uv_plane = MeshSlicePlaneOrientation::Z;
const Basis &mesh_instance_basis = state->input->mesh_instance->get_basis();
const Vector3 &local_plane_normal = state->input->local_plane.get_normal();
if (UtilityFunctions::absf(local_plane_normal.dot(mesh_instance_basis.xform(Vector3(0.0f, 1.0f, 0.0f)))) > 0.5f)
{
uv_plane = MeshSlicePlaneOrientation::Y;
}
else if (UtilityFunctions::absf(local_plane_normal.dot(mesh_instance_basis.xform(Vector3(1.0f, 0.0f, 0.0f)))) > 0.5f)
{
uv_plane = MeshSlicePlaneOrientation::X;
}
SurfaceTool *surface_tool_first_half = memnew(SurfaceTool);
SurfaceTool *surface_tool_other_half = memnew(SurfaceTool);
@@ -677,8 +778,8 @@ void MeshEditingLibrary::slice_mesh_apply_cap(Ref<MeshSliceState> state)
for (const MeshEditVert2D &vertex : polygon_set[state->element].vertices)
{
const Vector3 &position = state->first_half_vertices[vertex.surface][vertex.index];
const Transform3D &skinned_vertex_transform = state->skinned_vertex_transforms[vertex.surface][vertex.index];
// const Vector3 &skinned_position = state->skinned_vertex_positions[vertex.surface][vertex.index];
// const Transform3D &skinned_vertex_transform = state->skinned_vertex_transforms[vertex.surface][vertex.index];
const Vector2 &uv = MeshEditingLibrary::calculate_planar_uv(position, mesh_bounds, uv_plane);
surface_tool_first_half->set_uv(uv);
@@ -770,107 +871,67 @@ void MeshEditingLibrary::slice_mesh_apply_cap(Ref<MeshSliceState> state)
memdelete(surface_tool_first_half);
memdelete(surface_tool_other_half);
// PRINT_LOG(MESH_EDITING_LIBRARY_TAG, "Created cap surfaces at ", time->get_ticks_msec(), "ms");
}
// 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;
}
SkinnedVertices MeshEditingLibrary::get_skinned_vertex_positions(const uint8_t surface, const Ref<ArrayMesh> mesh, const Skeleton3D *skeleton, std::vector<Transform3D> &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!");
}
SkinnedVertices skinned_vertex_array;
if (skeleton)
{
const uint16_t bone_count = skeleton->get_bone_count();
std::vector<Transform3D> 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
{
for (uint8_t surface = 0; surface < num_surfaces; surface++)
{
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<ArrayMesh> 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;
}
@@ -18,10 +18,14 @@ using namespace godot;
#include <map>
#include <vector>
#define MESH_EDITING_LIBRARY_TAG "MeshEditingLibrary"
#define MESH_EDITING_SLICE_LOOP_LIMIT 5.555555555555555f
#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<SkinnedVertices> SkinnedSurfaces;
typedef std::vector<Transform3D> SkinnedVertexTransforms;
struct MeshEditVert3D
@@ -58,7 +62,7 @@ struct MeshEditPolygon2D
class MeshEditSliceInput;
class MeshEditSliceOutput;
class MeshSliceState;
class MeshEditSliceState;
class MeshEditingLibrary : public Node
@@ -72,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,
@@ -85,12 +90,15 @@ public:
CAP_UV_TILED
};
static Ref<class MeshSliceState> slice_mesh(const Ref<class MeshEditSliceInput> input);
static Ref<class MeshEditSliceState> slice_mesh(const Ref<class MeshEditSliceInput> input);
static void slice_mesh_init(Ref<class MeshSliceState> state);
static void slice_mesh_plane_split(Ref<class MeshSliceState> state);
static void slice_mesh_edge_split(Ref<class MeshSliceState> state);
static void slice_mesh_apply_cap(Ref<class MeshSliceState> state);
static float slice_mesh_init(Ref<class MeshEditSliceState> state);
static float slice_mesh_get_skinned_verts(Ref<class MeshEditSliceState> state);
static float slice_mesh_plane_split(Ref<class MeshEditSliceState> state);
static float slice_mesh_edge_split(Ref<class MeshEditSliceState> state);
static float slice_mesh_apply_cap(Ref<class MeshEditSliceState> state);
static Dictionary MeshEditingLibrary::split_mesh_by_body_part(Ref<ArrayMesh> mesh);
protected:
enum MeshSlicePlaneOrientation
@@ -103,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<ArrayMesh> mesh, const Skeleton3D *skeleton = nullptr, std::vector<Transform3D> &bone_transforms = std::vector<Transform3D>());
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); }
@@ -121,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);
@@ -161,8 +169,14 @@ class MeshEditSliceOutput : public RefCounted
public:
const Skeleton3D *original_skeleton = nullptr;
AABB original_full_aabb;
Plane original_local_plane;
Ref<ArrayMesh> main_half;
uint32_t main_half_split_map = 0b0000000000000000000000000;
Ref<ArrayMesh> secondary_half;
uint32_t secondary_half_split_map = 0b0000000000000000000000000;
PackedVector3Array impact_points;
uint32_t hit_map = 0b0000000000000000000000000;
@@ -172,9 +186,9 @@ protected:
};
class MeshSliceState : public RefCounted
class MeshEditSliceState : public RefCounted
{
GDCLASS(MeshSliceState, RefCounted);
GDCLASS(MeshEditSliceState, RefCounted);
public:
MeshEditingLibrary::MeshEditSliceStage current_stage = MeshEditingLibrary::MeshEditSliceStage::SLICE_STAGE_INIT;
@@ -193,7 +207,7 @@ public:
std::vector<Array> surface_arrays_list;
std::vector<SkinnedVertices> skinned_vertex_positions;
std::vector<std::vector<Transform3D>> skinned_vertex_transforms;
std::vector<SkinnedVertexTransforms> skinned_vertex_transforms;
std::vector<bool> has_normal;
std::vector<bool> has_tangent;
@@ -235,8 +249,8 @@ public:
std::vector<std::map<uint32_t, uint32_t>> base_to_first_sliced_vert_indices;
std::vector<std::map<uint32_t, uint32_t>> 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<MeshEditEdge3D> clip_edges;
+1 -1
View File
@@ -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;
}