diff --git a/src/liborng/nodes/character_nodes/character_3d.cpp b/src/liborng/nodes/character_nodes/character_3d.cpp new file mode 100644 index 0000000..29a132c --- /dev/null +++ b/src/liborng/nodes/character_nodes/character_3d.cpp @@ -0,0 +1,28 @@ +/* + * ©2023 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 +#include + +using namespace godot; + + +void Character3D::_ready() +{ + if (this->animation_tree = cast_to(Node::get_node_or_null(this->animation_tree_path))) + { + UtilityFunctions::print("Finally found the god damn animation tree"); + } +} + + +void Character3D::_on_fell_out_of_world() +{ + this->emit_signal("fell_out_of_world"); +} diff --git a/src/liborng/nodes/character_nodes/character_3d.h b/src/liborng/nodes/character_nodes/character_3d.h new file mode 100644 index 0000000..69bcb8f --- /dev/null +++ b/src/liborng/nodes/character_nodes/character_3d.h @@ -0,0 +1,51 @@ +/* + * ©2023 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 +#include +#include +using namespace godot; + + +class Character3D : public CharacterBody3D +{ + GDCLASS(Character3D, CharacterBody3D); + +public: + virtual void _ready() override; + + virtual void _on_fell_out_of_world(); + +protected: + void set_animation_tree_path(NodePath at) { this->animation_tree_path = at; } + NodePath get_animation_tree_path() const { return this->animation_tree_path; } + +private: + AnimationTree *get_animation_tree() const { return this->animation_tree; } + + NodePath animation_tree_path; + AnimationTree *animation_tree = nullptr; + +// Godot boilerplate below +protected: + static void _bind_methods() + { + BIND_METHOD(Character3D, _on_fell_out_of_world); + + ADD_SIGNAL(MethodInfo("fell_out_of_world")); + + ClassDB::bind_method(D_METHOD("get_animation_tree_path"), &Character3D::get_animation_tree_path); + ClassDB::bind_method(D_METHOD("set_animation_tree_path", "animation_tree"), &Character3D::set_animation_tree_path); + ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "animation_tree", PROPERTY_HINT_NODE_TYPE, "AnimationTree"), "set_animation_tree_path", "get_animation_tree_path"); + + ClassDB::bind_method(D_METHOD("get_animation_tree"), &Character3D::get_animation_tree); + } +}; diff --git a/src/liborng/nodes/level_scene.cpp b/src/liborng/nodes/level_scene.cpp index 1adfe31..e73a802 100644 --- a/src/liborng/nodes/level_scene.cpp +++ b/src/liborng/nodes/level_scene.cpp @@ -7,6 +7,7 @@ #include "level_scene.h" +#include "character_3d.h" #include "nodes/player_spawn.h" #include "singletons/save_manager.h" @@ -97,12 +98,12 @@ void LevelScene::_find_player_spawns_recursive(const Node *node, SpawnPointMap & void LevelScene::_body_fell_out_of_world(Node3D *body) { - if (body->has_method("on_fell_out_of_world")) + if (Character3D *character3d = cast_to(body)) { - body->call("on_fell_out_of_world"); + character3d->emit_signal(FELL_OUT_OF_WORLD_SIGNAL); } - else + else if (body->has_method(FELL_OUT_OF_WORLD_METHOD)) { - body->queue_free(); + body->call(FELL_OUT_OF_WORLD_METHOD); } } diff --git a/src/liborng/nodes/level_scene.h b/src/liborng/nodes/level_scene.h index 25954ee..a530f75 100644 --- a/src/liborng/nodes/level_scene.h +++ b/src/liborng/nodes/level_scene.h @@ -15,6 +15,9 @@ #include using namespace godot; +#define FELL_OUT_OF_WORLD_SIGNAL "fell_out_of_world" +#define FELL_OUT_OF_WORLD_METHOD "_on_fell_out_of_world" + typedef std::map SpawnPointMap; diff --git a/src/liborng/nodes/mesh_editing_library.cpp b/src/liborng/nodes/mesh_editing_library.cpp index c269e6a..c2970f1 100644 --- a/src/liborng/nodes/mesh_editing_library.cpp +++ b/src/liborng/nodes/mesh_editing_library.cpp @@ -33,8 +33,8 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original const Mesh *original_mesh = original_mesh_instance->get_mesh().ptr(); const uint8_t num_surfaces = original_mesh->get_surface_count(); - TypedArray bone_transforms; - const SkinnedSurfaces &skinned_vertex_positions = MeshEditingLibrary::get_skinned_vertex_positions(original_mesh, original_skeleton, bone_transforms); + std::vector> skinned_vertex_transforms; + const SkinnedSurfaces &skinned_vertex_positions = MeshEditingLibrary::get_skinned_vertex_positions(original_mesh, original_skeleton, skinned_vertex_transforms); UtilityFunctions::print("Got skinned vertices at ", time->get_ticks_msec(), "ms"); @@ -501,9 +501,10 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original for (const MeshEditVert2D &vertex : polygon_set[polygon_index].vertices) { const Vector3 &position = first_half_section_vertices[vertex.index]; + const Vector3 &local_plane_transformed_normal = skinned_vertex_transforms[surface][vertex.index].xform(local_plane_normal).normalized(); cap_section_vertices.append(position); - if (has_normal) { cap_section_normals.append(local_plane_normal * -1.0f); cap_section_flipped_normals.append(local_plane_normal); } + if (has_normal) { cap_section_normals.append(local_plane_transformed_normal); cap_section_flipped_normals.append(local_plane_transformed_normal * -1.0f); } if (has_colour) { cap_section_colours.append(first_half_section_colours[vertex.index]); } if (has_uv) { cap_section_uvs.append(MeshEditingLibrary::calculate_planar_uv(position, mesh_bounds, uv_plane)); } if (has_uv2) { cap_section_uv2s.append(first_half_section_uv2s[vertex.index]); } @@ -595,7 +596,7 @@ PackedVector3Array MeshEditingLibrary::slice_mesh(const MeshInstance3D *original return impact_points; } -SkinnedSurfaces MeshEditingLibrary::get_skinned_vertex_positions(const Mesh *mesh, const Skeleton3D *skeleton, TypedArray &bone_transforms) +SkinnedSurfaces MeshEditingLibrary::get_skinned_vertex_positions(const Mesh *mesh, const Skeleton3D *skeleton, std::vector> &vertex_transforms) { const uint8_t num_surfaces = mesh->get_surface_count(); SkinnedSurfaces skinned_vertex_surfaces; @@ -604,12 +605,15 @@ SkinnedSurfaces MeshEditingLibrary::get_skinned_vertex_positions(const Mesh *mes if (skeleton) { const uint16_t bone_count = skeleton->get_bone_count(); + std::vector 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(); } + vertex_transforms.resize(num_surfaces); + for (uint8_t surface = 0; surface < num_surfaces; surface++) { const Array &mesh_arrays = mesh->surface_get_arrays(surface); @@ -620,6 +624,9 @@ SkinnedSurfaces MeshEditingLibrary::get_skinned_vertex_positions(const Mesh *mes SkinnedVertices skinned_vertex_array; skinned_vertex_array.resize(mesh_vertex_array_size); + std::vector skinned_vertex_transforms; + skinned_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; @@ -644,21 +651,19 @@ SkinnedSurfaces MeshEditingLibrary::get_skinned_vertex_positions(const Mesh *mes origin += transforms[transform].origin; } - const Vector3 &transformed_vertex = Vector3( - x_basis.dot(vertex) + origin.x, - y_basis.dot(vertex) + origin.y, - z_basis.dot(vertex) + origin.z); - skinned_vertex_array[vertex_index] = transformed_vertex; - // The above is identical to the code seen below, except the below code does not work. - // This is fucking stupid. - // - // const Transform3D &final_transform = Transform3D(x_basis, y_basis, z_basis, origin); - // skinned_vertex_array[vertex_index] = final_transform.xform(vertex); + 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); + skinned_vertex_transforms[vertex_index] = final_bone_transform; + skinned_vertex_array[vertex_index] = final_bone_transform.xform(vertex); delete transforms; } skinned_vertex_surfaces[surface] = skinned_vertex_array; + vertex_transforms[surface] = skinned_vertex_transforms; } return skinned_vertex_surfaces; diff --git a/src/liborng/nodes/mesh_editing_library.h b/src/liborng/nodes/mesh_editing_library.h index d20c9fb..d6dae08 100644 --- a/src/liborng/nodes/mesh_editing_library.h +++ b/src/liborng/nodes/mesh_editing_library.h @@ -79,7 +79,7 @@ protected: private: static const Vector2 calculate_planar_uv(const Vector3 &vertex, const Vector3 &mesh_bounds, const MeshSlicePlaneOrientation &axis); - static SkinnedSurfaces get_skinned_vertex_positions(const Mesh *mesh, const Skeleton3D *skeleton = nullptr, TypedArray &bone_transforms = TypedArray()); + static SkinnedSurfaces get_skinned_vertex_positions(const Mesh *mesh, const Skeleton3D *skeleton = nullptr, std::vector> &bone_transforms = std::vector>()); static void project_edges(std::vector &out_2d_edges, const Transform3D &to_node_space, const std::vector &in_3d_edges, const Plane &plane); static void build_2d_polygons_from_edges(std::vector &out_polygons, const std::vector &in_edges, std::vector &error_polygons); diff --git a/src/liborng/register_types.cpp b/src/liborng/register_types.cpp index 2c03116..7ad5261 100644 --- a/src/liborng/register_types.cpp +++ b/src/liborng/register_types.cpp @@ -21,6 +21,7 @@ #include "nodes/player_spawn.h" #include "nodes/sublevel_loader.h" #include "nodes/sublevel_scene.h" +#include "nodes/character_nodes/character_3d.h" #include "resources/level_metadata_map.h" #include "resources/level_metadata_resource.h" @@ -62,6 +63,7 @@ void initialize_orng_module(ModuleInitializationLevel p_level) { ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class();