2017-10-03 18:49:32 +02:00
|
|
|
/*************************************************************************/
|
2020-03-27 08:44:44 +01:00
|
|
|
/* skeleton_3d_editor_plugin.cpp */
|
2017-10-03 18:49:32 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-10-03 18:49:32 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
#include "skeleton_3d_editor_plugin.h"
|
2018-09-27 13:11:41 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
#include "node_3d_editor_plugin.h"
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "scene/3d/collision_shape_3d.h"
|
|
|
|
#include "scene/3d/physics_body_3d.h"
|
|
|
|
#include "scene/3d/physics_joint_3d.h"
|
|
|
|
#include "scene/resources/capsule_shape_3d.h"
|
|
|
|
#include "scene/resources/sphere_shape_3d.h"
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void Skeleton3DEditor::_on_click_option(int p_option) {
|
2017-10-03 18:49:32 +02:00
|
|
|
if (!skeleton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (p_option) {
|
|
|
|
case MENU_OPTION_CREATE_PHYSICAL_SKELETON: {
|
|
|
|
create_physical_skeleton();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void Skeleton3DEditor::create_physical_skeleton() {
|
2017-10-03 18:49:32 +02:00
|
|
|
UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
|
|
|
|
Node *owner = skeleton == get_tree()->get_edited_scene_root() ? skeleton : skeleton->get_owner();
|
|
|
|
|
|
|
|
const int bc = skeleton->get_bone_count();
|
|
|
|
|
|
|
|
if (!bc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<BoneInfo> bones_infos;
|
|
|
|
bones_infos.resize(bc);
|
|
|
|
|
|
|
|
for (int bone_id = 0; bc > bone_id; ++bone_id) {
|
|
|
|
|
|
|
|
const int parent = skeleton->get_bone_parent(bone_id);
|
|
|
|
|
|
|
|
if (parent < 0) {
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones_infos.write[bone_id].relative_rest = skeleton->get_bone_rest(bone_id);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2019-11-30 20:27:12 +01:00
|
|
|
const int parent_parent = skeleton->get_bone_parent(parent);
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones_infos.write[bone_id].relative_rest = bones_infos[parent].relative_rest * skeleton->get_bone_rest(bone_id);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
/// create physical bone on parent
|
|
|
|
if (!bones_infos[parent].physical_bone) {
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones_infos.write[parent].physical_bone = create_physical_bone(parent, bone_id, bones_infos);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
ur->create_action(TTR("Create physical bones"));
|
|
|
|
ur->add_do_method(skeleton, "add_child", bones_infos[parent].physical_bone);
|
|
|
|
ur->add_do_reference(bones_infos[parent].physical_bone);
|
|
|
|
ur->add_undo_method(skeleton, "remove_child", bones_infos[parent].physical_bone);
|
|
|
|
ur->commit_action();
|
|
|
|
|
|
|
|
bones_infos[parent].physical_bone->set_bone_name(skeleton->get_bone_name(parent));
|
|
|
|
bones_infos[parent].physical_bone->set_owner(owner);
|
|
|
|
bones_infos[parent].physical_bone->get_child(0)->set_owner(owner); // set shape owner
|
|
|
|
|
|
|
|
/// Create joint between parent of parent
|
|
|
|
if (-1 != parent_parent) {
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bones_infos[parent].physical_bone->set_joint_type(PhysicalBone3D::JOINT_TYPE_PIN);
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
PhysicalBone3D *Skeleton3DEditor::create_physical_bone(int bone_id, int bone_child_id, const Vector<BoneInfo> &bones_infos) {
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-02-09 09:41:18 +01:00
|
|
|
const Transform child_rest = skeleton->get_bone_rest(bone_child_id);
|
|
|
|
|
|
|
|
const real_t half_height(child_rest.origin.length() * 0.5);
|
|
|
|
const real_t radius(half_height * 0.2);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
CapsuleShape3D *bone_shape_capsule = memnew(CapsuleShape3D);
|
2017-10-03 18:49:32 +02:00
|
|
|
bone_shape_capsule->set_height((half_height - radius) * 2);
|
|
|
|
bone_shape_capsule->set_radius(radius);
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
CollisionShape3D *bone_shape = memnew(CollisionShape3D);
|
2017-10-03 18:49:32 +02:00
|
|
|
bone_shape->set_shape(bone_shape_capsule);
|
|
|
|
|
|
|
|
Transform body_transform;
|
2020-02-09 09:41:18 +01:00
|
|
|
body_transform.set_look_at(Vector3(0, 0, 0), child_rest.origin, Vector3(0, 1, 0));
|
|
|
|
body_transform.origin = body_transform.basis.xform(Vector3(0, 0, -half_height));
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
Transform joint_transform;
|
|
|
|
joint_transform.origin = Vector3(0, 0, half_height);
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicalBone3D *physical_bone = memnew(PhysicalBone3D);
|
2017-10-03 18:49:32 +02:00
|
|
|
physical_bone->add_child(bone_shape);
|
|
|
|
physical_bone->set_name("Physical Bone " + skeleton->get_bone_name(bone_id));
|
|
|
|
physical_bone->set_body_offset(body_transform);
|
|
|
|
physical_bone->set_joint_offset(joint_transform);
|
|
|
|
return physical_bone;
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void Skeleton3DEditor::edit(Skeleton3D *p_node) {
|
2018-08-24 13:28:53 +02:00
|
|
|
|
2017-10-03 18:49:32 +02:00
|
|
|
skeleton = p_node;
|
2018-08-24 13:28:53 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void Skeleton3DEditor::_notification(int p_what) {
|
2018-08-24 16:50:29 +02:00
|
|
|
if (p_what == NOTIFICATION_ENTER_TREE) {
|
2020-03-27 08:44:44 +01:00
|
|
|
get_tree()->connect("node_removed", callable_mp(this, &Skeleton3DEditor::_node_removed));
|
2018-08-24 13:28:53 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void Skeleton3DEditor::_node_removed(Node *p_node) {
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
if (p_node == skeleton) {
|
|
|
|
skeleton = NULL;
|
|
|
|
options->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void Skeleton3DEditor::_bind_methods() {
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
Skeleton3DEditor::Skeleton3DEditor() {
|
2018-11-11 18:32:47 +01:00
|
|
|
skeleton = NULL;
|
2017-10-03 18:49:32 +02:00
|
|
|
options = memnew(MenuButton);
|
2020-03-26 22:49:16 +01:00
|
|
|
Node3DEditor::get_singleton()->add_control_to_menu_panel(options);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
options->set_text(TTR("Skeleton3D"));
|
2020-03-26 22:49:16 +01:00
|
|
|
options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Skeleton3D", "EditorIcons"));
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
options->get_popup()->add_item(TTR("Create physical skeleton"), MENU_OPTION_CREATE_PHYSICAL_SKELETON);
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
options->get_popup()->connect("id_pressed", callable_mp(this, &Skeleton3DEditor::_on_click_option));
|
2017-10-03 18:49:32 +02:00
|
|
|
options->hide();
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
Skeleton3DEditor::~Skeleton3DEditor() {}
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void Skeleton3DEditorPlugin::edit(Object *p_object) {
|
2020-03-26 22:49:16 +01:00
|
|
|
skeleton_editor->edit(Object::cast_to<Skeleton3D>(p_object));
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
bool Skeleton3DEditorPlugin::handles(Object *p_object) const {
|
2020-03-26 22:49:16 +01:00
|
|
|
return p_object->is_class("Skeleton3D");
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void Skeleton3DEditorPlugin::make_visible(bool p_visible) {
|
2017-10-03 18:49:32 +02:00
|
|
|
if (p_visible) {
|
|
|
|
skeleton_editor->options->show();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
skeleton_editor->options->hide();
|
|
|
|
skeleton_editor->edit(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
Skeleton3DEditorPlugin::Skeleton3DEditorPlugin(EditorNode *p_node) {
|
2017-10-03 18:49:32 +02:00
|
|
|
editor = p_node;
|
2020-03-27 08:44:44 +01:00
|
|
|
skeleton_editor = memnew(Skeleton3DEditor);
|
2017-10-03 18:49:32 +02:00
|
|
|
editor->get_viewport()->add_child(skeleton_editor);
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
Skeleton3DEditorPlugin::~Skeleton3DEditorPlugin() {}
|