2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-03-26 22:49:16 +01:00
|
|
|
/* skeleton_3d.cpp */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
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). */
|
2014-02-10 02:10:30 +01: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "skeleton_3d.h"
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-02-08 09:52:58 +01:00
|
|
|
#include "core/engine.h"
|
2020-02-21 23:26:13 +01:00
|
|
|
#include "core/message_queue.h"
|
2017-07-19 22:00:46 +02:00
|
|
|
#include "core/project_settings.h"
|
2020-04-21 17:16:45 +02:00
|
|
|
#include "core/type_info.h"
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "scene/3d/physics_body_3d.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "scene/resources/surface_tool.h"
|
2020-02-19 20:48:20 +01:00
|
|
|
#include "scene/scene_string_names.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-09-19 00:46:32 +02:00
|
|
|
void SkinReference::_skin_changed() {
|
|
|
|
if (skeleton_node) {
|
|
|
|
skeleton_node->_make_dirty();
|
|
|
|
}
|
2020-02-21 11:27:48 +01:00
|
|
|
skeleton_version = 0;
|
2019-09-19 00:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkinReference::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("_skin_changed"), &SkinReference::_skin_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_skeleton"), &SkinReference::get_skeleton);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_skin"), &SkinReference::get_skin);
|
|
|
|
}
|
|
|
|
|
|
|
|
RID SkinReference::get_skeleton() const {
|
|
|
|
return skeleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Skin> SkinReference::get_skin() const {
|
|
|
|
return skin;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkinReference::~SkinReference() {
|
|
|
|
if (skeleton_node) {
|
|
|
|
skeleton_node->skin_bindings.erase(this);
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
RS::get_singleton()->free(skeleton);
|
2019-09-19 00:46:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool Skeleton3D::_set(const StringName &p_path, const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
String path = p_path;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!path.begins_with("bones/")) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int which = path.get_slicec('/', 1).to_int();
|
|
|
|
String what = path.get_slicec('/', 2);
|
2014-02-23 14:35:05 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (which == bones.size() && what == "name") {
|
2014-02-10 02:10:30 +01:00
|
|
|
add_bone(p_value);
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(which, bones.size(), false);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (what == "parent") {
|
2017-03-05 16:44:50 +01:00
|
|
|
set_bone_parent(which, p_value);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "rest") {
|
2014-02-10 02:10:30 +01:00
|
|
|
set_bone_rest(which, p_value);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "enabled") {
|
2014-02-10 02:10:30 +01:00
|
|
|
set_bone_enabled(which, p_value);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "pose") {
|
2014-02-10 02:10:30 +01:00
|
|
|
set_bone_pose(which, p_value);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "bound_children") {
|
2017-03-05 16:44:50 +01:00
|
|
|
Array children = p_value;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-06-29 23:38:32 +02:00
|
|
|
if (is_inside_tree()) {
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[which].nodes_bound.clear();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2019-02-12 21:10:08 +01:00
|
|
|
NodePath npath = children[i];
|
|
|
|
ERR_CONTINUE(npath.operator String() == "");
|
|
|
|
Node *node = get_node(npath);
|
2016-06-29 23:38:32 +02:00
|
|
|
ERR_CONTINUE(!node);
|
2017-03-05 16:44:50 +01:00
|
|
|
bind_child_node_to_bone(which, node);
|
2016-06-29 23:38:32 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool Skeleton3D::_get(const StringName &p_path, Variant &r_ret) const {
|
2017-08-11 21:10:05 +02:00
|
|
|
String path = p_path;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!path.begins_with("bones/")) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int which = path.get_slicec('/', 1).to_int();
|
|
|
|
String what = path.get_slicec('/', 2);
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(which, bones.size(), false);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (what == "name") {
|
2017-03-05 16:44:50 +01:00
|
|
|
r_ret = get_bone_name(which);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "parent") {
|
2017-03-05 16:44:50 +01:00
|
|
|
r_ret = get_bone_parent(which);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "rest") {
|
2017-03-05 16:44:50 +01:00
|
|
|
r_ret = get_bone_rest(which);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "enabled") {
|
2017-03-05 16:44:50 +01:00
|
|
|
r_ret = is_bone_enabled(which);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "pose") {
|
2017-03-05 16:44:50 +01:00
|
|
|
r_ret = get_bone_pose(which);
|
2020-05-14 16:41:43 +02:00
|
|
|
} else if (what == "bound_children") {
|
2014-02-10 02:10:30 +01:00
|
|
|
Array children;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
for (const List<ObjectID>::Element *E = bones[which].nodes_bound.front(); E; E = E->next()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Object *obj = ObjectDB::get_instance(E->get());
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_CONTINUE(!obj);
|
2017-08-24 22:58:51 +02:00
|
|
|
Node *node = Object::cast_to<Node>(obj);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_CONTINUE(!node);
|
2019-02-12 21:10:08 +01:00
|
|
|
NodePath npath = get_path_to(node);
|
|
|
|
children.push_back(npath);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
r_ret = children;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2014-02-10 02:10:30 +01:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return true;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::_get_property_list(List<PropertyInfo> *p_list) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < bones.size(); i++) {
|
|
|
|
String prep = "bones/" + itos(i) + "/";
|
2020-02-19 20:48:20 +01:00
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, prep + "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, prep + "parent", PROPERTY_HINT_RANGE, "-1," + itos(bones.size() - 1) + ",1", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + "rest", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::BOOL, prep + "enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + "pose", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::ARRAY, prep + "bound_children", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::_update_process_order() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!process_order_dirty) {
|
2018-08-07 03:35:09 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-07 03:35:09 +02:00
|
|
|
|
|
|
|
Bone *bonesptr = bones.ptrw();
|
|
|
|
int len = bones.size();
|
|
|
|
|
|
|
|
process_order.resize(len);
|
|
|
|
int *order = process_order.ptrw();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
if (bonesptr[i].parent >= len) {
|
|
|
|
//validate this just in case
|
2019-11-06 17:03:04 +01:00
|
|
|
ERR_PRINT("Bone " + itos(i) + " has invalid parent: " + itos(bonesptr[i].parent));
|
2018-08-07 03:35:09 +02:00
|
|
|
bonesptr[i].parent = -1;
|
|
|
|
}
|
|
|
|
order[i] = i;
|
|
|
|
bonesptr[i].sort_index = i;
|
|
|
|
}
|
|
|
|
//now check process order
|
|
|
|
int pass_count = 0;
|
|
|
|
while (pass_count < len * len) {
|
2019-05-19 12:34:40 +02:00
|
|
|
//using bubblesort because of simplicity, it won't run every frame though.
|
2018-08-07 03:35:09 +02:00
|
|
|
//bublesort worst case is O(n^2), and this may be an infinite loop if cyclic
|
|
|
|
bool swapped = false;
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
int parent_idx = bonesptr[order[i]].parent;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (parent_idx < 0) {
|
2018-08-07 03:35:09 +02:00
|
|
|
continue; //do nothing because it has no parent
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-07 03:35:09 +02:00
|
|
|
//swap indices
|
|
|
|
int parent_order = bonesptr[parent_idx].sort_index;
|
|
|
|
if (parent_order > i) {
|
|
|
|
bonesptr[order[i]].sort_index = parent_order;
|
|
|
|
bonesptr[parent_idx].sort_index = i;
|
|
|
|
//swap order
|
|
|
|
SWAP(order[i], order[parent_order]);
|
|
|
|
swapped = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!swapped) {
|
2018-08-07 03:35:09 +02:00
|
|
|
break;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-08-07 03:35:09 +02:00
|
|
|
pass_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pass_count == len * len) {
|
2020-02-19 20:48:20 +01:00
|
|
|
ERR_PRINT("Skeleton3D parenthood graph is cyclic");
|
2018-08-07 03:35:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
process_order_dirty = false;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::_notification(int p_what) {
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case NOTIFICATION_UPDATE_SKELETON: {
|
2020-02-19 20:48:20 +01:00
|
|
|
RenderingServer *rs = RenderingServer::get_singleton();
|
2018-07-25 03:11:03 +02:00
|
|
|
Bone *bonesptr = bones.ptrw();
|
2017-03-05 16:44:50 +01:00
|
|
|
int len = bones.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-08-07 03:35:09 +02:00
|
|
|
_update_process_order();
|
|
|
|
|
|
|
|
const int *order = process_order.ptr();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < len; i++) {
|
2018-08-07 03:35:09 +02:00
|
|
|
Bone &b = bonesptr[order[i]];
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2019-09-19 00:46:32 +02:00
|
|
|
if (b.global_pose_override_amount >= 0.999) {
|
|
|
|
b.pose_global = b.global_pose_override;
|
|
|
|
} else {
|
|
|
|
if (b.disable_rest) {
|
|
|
|
if (b.enabled) {
|
|
|
|
Transform pose = b.pose;
|
2019-09-26 13:06:47 +02:00
|
|
|
if (b.custom_pose_enable) {
|
|
|
|
pose = b.custom_pose * pose;
|
|
|
|
}
|
2019-09-19 00:46:32 +02:00
|
|
|
if (b.parent >= 0) {
|
|
|
|
b.pose_global = bonesptr[b.parent].pose_global * pose;
|
|
|
|
} else {
|
|
|
|
b.pose_global = pose;
|
|
|
|
}
|
2015-09-24 23:06:15 +02:00
|
|
|
} else {
|
2019-09-19 00:46:32 +02:00
|
|
|
if (b.parent >= 0) {
|
|
|
|
b.pose_global = bonesptr[b.parent].pose_global;
|
|
|
|
} else {
|
|
|
|
b.pose_global = Transform();
|
|
|
|
}
|
2015-09-24 23:06:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 00:46:32 +02:00
|
|
|
} else {
|
|
|
|
if (b.enabled) {
|
|
|
|
Transform pose = b.pose;
|
2019-09-26 13:06:47 +02:00
|
|
|
if (b.custom_pose_enable) {
|
|
|
|
pose = b.custom_pose * pose;
|
|
|
|
}
|
2019-09-19 00:46:32 +02:00
|
|
|
if (b.parent >= 0) {
|
|
|
|
b.pose_global = bonesptr[b.parent].pose_global * (b.rest * pose);
|
|
|
|
} else {
|
|
|
|
b.pose_global = b.rest * pose;
|
|
|
|
}
|
2015-09-24 23:06:15 +02:00
|
|
|
} else {
|
2019-09-19 00:46:32 +02:00
|
|
|
if (b.parent >= 0) {
|
|
|
|
b.pose_global = bonesptr[b.parent].pose_global * b.rest;
|
|
|
|
} else {
|
|
|
|
b.pose_global = b.rest;
|
|
|
|
}
|
2015-09-24 23:06:15 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-19 00:46:32 +02:00
|
|
|
|
|
|
|
if (b.global_pose_override_amount >= CMP_EPSILON) {
|
|
|
|
b.pose_global = b.pose_global.interpolate_with(b.global_pose_override, b.global_pose_override_amount);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2019-09-19 00:46:32 +02:00
|
|
|
if (b.global_pose_override_reset) {
|
|
|
|
b.global_pose_override_amount = 0.0;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
for (List<ObjectID>::Element *E = b.nodes_bound.front(); E; E = E->next()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Object *obj = ObjectDB::get_instance(E->get());
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_CONTINUE(!obj);
|
2020-02-19 20:48:20 +01:00
|
|
|
Node3D *node_3d = Object::cast_to<Node3D>(obj);
|
|
|
|
ERR_CONTINUE(!node_3d);
|
|
|
|
node_3d->set_transform(b.pose_global);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 00:46:32 +02:00
|
|
|
//update skins
|
|
|
|
for (Set<SkinReference *>::Element *E = skin_bindings.front(); E; E = E->next()) {
|
|
|
|
const Skin *skin = E->get()->skin.operator->();
|
|
|
|
RID skeleton = E->get()->skeleton;
|
|
|
|
uint32_t bind_count = skin->get_bind_count();
|
|
|
|
|
|
|
|
if (E->get()->bind_count != bind_count) {
|
2020-03-27 19:21:27 +01:00
|
|
|
RS::get_singleton()->skeleton_allocate(skeleton, bind_count);
|
2019-09-19 00:46:32 +02:00
|
|
|
E->get()->bind_count = bind_count;
|
2020-02-21 11:27:48 +01:00
|
|
|
E->get()->skin_bone_indices.resize(bind_count);
|
|
|
|
E->get()->skin_bone_indices_ptrs = E->get()->skin_bone_indices.ptrw();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (E->get()->skeleton_version != version) {
|
|
|
|
for (uint32_t i = 0; i < bind_count; i++) {
|
|
|
|
StringName bind_name = skin->get_bind_name(i);
|
|
|
|
|
|
|
|
if (bind_name != StringName()) {
|
|
|
|
//bind name used, use this
|
|
|
|
bool found = false;
|
|
|
|
for (int j = 0; j < len; j++) {
|
|
|
|
if (bonesptr[j].name == bind_name) {
|
|
|
|
E->get()->skin_bone_indices_ptrs[i] = j;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
2020-02-19 20:48:20 +01:00
|
|
|
ERR_PRINT("Skin bind #" + itos(i) + " contains named bind '" + String(bind_name) + "' but Skeleton3D has no bone by that name.");
|
2020-02-21 11:27:48 +01:00
|
|
|
E->get()->skin_bone_indices_ptrs[i] = 0;
|
|
|
|
}
|
|
|
|
} else if (skin->get_bind_bone(i) >= 0) {
|
|
|
|
int bind_index = skin->get_bind_bone(i);
|
|
|
|
if (bind_index >= len) {
|
|
|
|
ERR_PRINT("Skin bind #" + itos(i) + " contains bone index bind: " + itos(bind_index) + " , which is greater than the skeleton bone count: " + itos(len) + ".");
|
|
|
|
E->get()->skin_bone_indices_ptrs[i] = 0;
|
|
|
|
} else {
|
|
|
|
E->get()->skin_bone_indices_ptrs[i] = bind_index;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ERR_PRINT("Skin bind #" + itos(i) + " does not contain a name nor a bone index.");
|
|
|
|
E->get()->skin_bone_indices_ptrs[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
E->get()->skeleton_version = version;
|
2019-09-19 00:46:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < bind_count; i++) {
|
2020-02-21 11:27:48 +01:00
|
|
|
uint32_t bone_index = E->get()->skin_bone_indices_ptrs[i];
|
2019-09-19 00:46:32 +02:00
|
|
|
ERR_CONTINUE(bone_index >= (uint32_t)len);
|
2020-02-19 20:48:20 +01:00
|
|
|
rs->skeleton_bone_set_transform(skeleton, i, bonesptr[bone_index].pose_global * skin->get_bind_pose(i));
|
2019-09-19 00:46:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
dirty = false;
|
2020-02-19 20:48:20 +01:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
emit_signal(SceneStringNames::get_singleton()->pose_updated);
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
} break;
|
2020-02-08 09:52:58 +01:00
|
|
|
|
|
|
|
#ifndef _3D_DISABLED
|
|
|
|
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
|
|
|
|
// This is active only if the skeleton animates the physical bones
|
|
|
|
// and the state of the bone is not active.
|
|
|
|
if (animate_physical_bones) {
|
|
|
|
for (int i = 0; i < bones.size(); i += 1) {
|
|
|
|
if (bones[i].physical_bone) {
|
|
|
|
if (bones[i].physical_bone->is_simulating_physics() == false) {
|
|
|
|
bones[i].physical_bone->reset_to_rest_position();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_READY: {
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
set_physics_process_internal(true);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::clear_bones_global_pose_override() {
|
2020-03-24 09:59:32 +01:00
|
|
|
for (int i = 0; i < bones.size(); i += 1) {
|
|
|
|
bones.write[i].global_pose_override_amount = 0;
|
|
|
|
}
|
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::set_bone_global_pose_override(int p_bone, const Transform &p_pose, float p_amount, bool p_persistent) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2019-09-19 00:46:32 +02:00
|
|
|
bones.write[p_bone].global_pose_override_amount = p_amount;
|
|
|
|
bones.write[p_bone].global_pose_override = p_pose;
|
|
|
|
bones.write[p_bone].global_pose_override_reset = !p_persistent;
|
|
|
|
_make_dirty();
|
2014-10-28 02:54:32 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Transform Skeleton3D::get_bone_global_pose(int p_bone) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
2020-05-14 16:41:43 +02:00
|
|
|
if (dirty) {
|
2020-03-26 22:49:16 +01:00
|
|
|
const_cast<Skeleton3D *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
return bones[p_bone].pose_global;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
// skeleton creation api
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::add_bone(const String &p_name) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND(p_name == "" || p_name.find(":") != -1 || p_name.find("/") != -1);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < bones.size(); i++) {
|
2018-08-07 03:35:09 +02:00
|
|
|
ERR_FAIL_COND(bones[i].name == p_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2014-02-23 14:35:05 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
Bone b;
|
2017-03-05 16:44:50 +01:00
|
|
|
b.name = p_name;
|
2014-02-10 02:10:30 +01:00
|
|
|
bones.push_back(b);
|
2018-08-07 03:35:09 +02:00
|
|
|
process_order_dirty = true;
|
2020-02-21 11:27:48 +01:00
|
|
|
version++;
|
2014-02-10 02:10:30 +01:00
|
|
|
_make_dirty();
|
|
|
|
update_gizmo();
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
int Skeleton3D::find_bone(const String &p_name) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < bones.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (bones[i].name == p_name) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return i;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
String Skeleton3D::get_bone_name(int p_bone) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), "");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return bones[p_bone].name;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool Skeleton3D::is_bone_parent_of(int p_bone, int p_parent_bone_id) const {
|
2018-05-10 13:23:05 +02:00
|
|
|
int parent_of_bone = get_bone_parent(p_bone);
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (-1 == parent_of_bone) {
|
2018-05-10 13:23:05 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-10 13:23:05 +02:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (parent_of_bone == p_parent_bone_id) {
|
2018-05-10 13:23:05 +02:00
|
|
|
return true;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-10 13:23:05 +02:00
|
|
|
|
|
|
|
return is_bone_parent_of(parent_of_bone, p_parent_bone_id);
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
int Skeleton3D::get_bone_count() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
return bones.size();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::set_bone_parent(int p_bone, int p_parent) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2018-08-07 03:35:09 +02:00
|
|
|
ERR_FAIL_COND(p_parent != -1 && (p_parent < 0));
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].parent = p_parent;
|
2018-08-07 03:35:09 +02:00
|
|
|
process_order_dirty = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::unparent_bone_and_rest(int p_bone) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2018-08-07 03:35:09 +02:00
|
|
|
_update_process_order();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int parent = bones[p_bone].parent;
|
|
|
|
while (parent >= 0) {
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].rest = bones[parent].rest * bones[p_bone].rest;
|
2017-03-05 16:44:50 +01:00
|
|
|
parent = bones[parent].parent;
|
2015-09-24 23:06:15 +02:00
|
|
|
}
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].parent = -1;
|
2018-08-07 03:35:09 +02:00
|
|
|
process_order_dirty = true;
|
2015-09-24 23:06:15 +02:00
|
|
|
|
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::set_bone_disable_rest(int p_bone, bool p_disable) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].disable_rest = p_disable;
|
2015-09-24 23:06:15 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool Skeleton3D::is_bone_rest_disabled(int p_bone) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
|
2015-09-24 23:06:15 +02:00
|
|
|
return bones[p_bone].disable_rest;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
int Skeleton3D::get_bone_parent(int p_bone) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), -1);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return bones[p_bone].parent;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::set_bone_rest(int p_bone, const Transform &p_rest) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].rest = p_rest;
|
2014-02-10 02:10:30 +01:00
|
|
|
_make_dirty();
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Transform Skeleton3D::get_bone_rest(int p_bone) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return bones[p_bone].rest;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::set_bone_enabled(int p_bone, bool p_enabled) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].enabled = p_enabled;
|
2014-02-10 02:10:30 +01:00
|
|
|
_make_dirty();
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool Skeleton3D::is_bone_enabled(int p_bone) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
|
2014-02-10 02:10:30 +01:00
|
|
|
return bones[p_bone].enabled;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::bind_child_node_to_bone(int p_bone, Node *p_node) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_NULL(p_node);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
ObjectID id = p_node->get_instance_id();
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
for (const List<ObjectID>::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (E->get() == id) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return; // already here
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].nodes_bound.push_back(id);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::unbind_child_node_from_bone(int p_bone, Node *p_node) {
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_NULL(p_node);
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
ObjectID id = p_node->get_instance_id();
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].nodes_bound.erase(id);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::get_bound_child_nodes_to_bone(int p_bone, List<Node *> *p_bound) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-02-12 18:24:06 +01:00
|
|
|
for (const List<ObjectID>::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Object *obj = ObjectDB::get_instance(E->get());
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_CONTINUE(!obj);
|
2017-08-24 22:58:51 +02:00
|
|
|
p_bound->push_back(Object::cast_to<Node>(obj));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::clear_bones() {
|
2014-02-10 02:10:30 +01:00
|
|
|
bones.clear();
|
2018-08-07 03:35:09 +02:00
|
|
|
process_order_dirty = true;
|
2020-02-21 11:27:48 +01:00
|
|
|
version++;
|
2014-02-10 02:10:30 +01:00
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// posing api
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::set_bone_pose(int p_bone, const Transform &p_pose) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].pose = p_pose;
|
2019-04-13 20:19:26 +02:00
|
|
|
if (is_inside_tree()) {
|
|
|
|
_make_dirty();
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Transform Skeleton3D::get_bone_pose(int p_bone) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
2014-02-10 02:10:30 +01:00
|
|
|
return bones[p_bone].pose;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::set_bone_custom_pose(int p_bone, const Transform &p_custom_pose) {
|
2019-09-26 13:06:47 +02:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
|
|
|
//ERR_FAIL_COND( !is_inside_scene() );
|
|
|
|
|
|
|
|
bones.write[p_bone].custom_pose_enable = (p_custom_pose != Transform());
|
|
|
|
bones.write[p_bone].custom_pose = p_custom_pose;
|
|
|
|
|
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Transform Skeleton3D::get_bone_custom_pose(int p_bone) const {
|
2019-09-26 13:06:47 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
|
|
|
return bones[p_bone].custom_pose;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::_make_dirty() {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (dirty) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
MessageQueue::get_singleton()->push_notification(this, NOTIFICATION_UPDATE_SKELETON);
|
|
|
|
dirty = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
int Skeleton3D::get_process_order(int p_idx) {
|
2018-08-07 03:35:09 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, bones.size(), -1);
|
|
|
|
_update_process_order();
|
|
|
|
return process_order[p_idx];
|
|
|
|
}
|
|
|
|
|
2020-02-19 20:48:20 +01:00
|
|
|
Vector<int> Skeleton3D::get_bone_process_orders() {
|
|
|
|
_update_process_order();
|
|
|
|
return process_order;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::localize_rests() {
|
2018-08-07 03:35:09 +02:00
|
|
|
_update_process_order();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-08-07 03:35:09 +02:00
|
|
|
for (int i = bones.size() - 1; i >= 0; i--) {
|
|
|
|
int idx = process_order[i];
|
|
|
|
if (bones[idx].parent >= 0) {
|
|
|
|
set_bone_rest(idx, bones[bones[idx].parent].rest.affine_inverse() * bones[idx].rest);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 11:54:13 +02:00
|
|
|
#ifndef _3D_DISABLED
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::set_animate_physical_bones(bool p_animate) {
|
2020-02-08 09:52:58 +01:00
|
|
|
animate_physical_bones = p_animate;
|
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint() == false) {
|
|
|
|
bool sim = false;
|
|
|
|
for (int i = 0; i < bones.size(); i += 1) {
|
|
|
|
if (bones[i].physical_bone) {
|
|
|
|
bones[i].physical_bone->reset_physics_simulation_state();
|
|
|
|
if (bones[i].physical_bone->is_simulating_physics()) {
|
|
|
|
sim = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_physics_process_internal(sim == false && p_animate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
bool Skeleton3D::get_animate_physical_bones() const {
|
2020-02-08 09:52:58 +01:00
|
|
|
return animate_physical_bones;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::bind_physical_bone_to_bone(int p_bone, PhysicalBone3D *p_physical_bone) {
|
2017-10-03 18:49:32 +02:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
|
|
|
ERR_FAIL_COND(bones[p_bone].physical_bone);
|
|
|
|
ERR_FAIL_COND(!p_physical_bone);
|
2018-07-25 03:11:03 +02:00
|
|
|
bones.write[p_bone].physical_bone = p_physical_bone;
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
_rebuild_physical_bones_cache();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::unbind_physical_bone_from_bone(int p_bone) {
|
2017-10-03 18:49:32 +02:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2020-04-02 01:20:12 +02:00
|
|
|
bones.write[p_bone].physical_bone = nullptr;
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
_rebuild_physical_bones_cache();
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicalBone3D *Skeleton3D::get_physical_bone(int p_bone) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), nullptr);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
return bones[p_bone].physical_bone;
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicalBone3D *Skeleton3D::get_physical_bone_parent(int p_bone) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), nullptr);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
if (bones[p_bone].cache_parent_physical_bone) {
|
|
|
|
return bones[p_bone].cache_parent_physical_bone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _get_physical_bone_parent(p_bone);
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicalBone3D *Skeleton3D::_get_physical_bone_parent(int p_bone) {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), nullptr);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
|
|
|
const int parent_bone = bones[p_bone].parent;
|
|
|
|
if (0 > parent_bone) {
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicalBone3D *pb = bones[parent_bone].physical_bone;
|
2017-10-03 18:49:32 +02:00
|
|
|
if (pb) {
|
|
|
|
return pb;
|
|
|
|
} else {
|
|
|
|
return get_physical_bone_parent(parent_bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::_rebuild_physical_bones_cache() {
|
2017-10-03 18:49:32 +02:00
|
|
|
const int b_size = bones.size();
|
|
|
|
for (int i = 0; i < b_size; ++i) {
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicalBone3D *parent_pb = _get_physical_bone_parent(i);
|
2017-10-03 18:49:32 +02:00
|
|
|
if (parent_pb != bones[i].physical_bone) {
|
|
|
|
bones.write[i].cache_parent_physical_bone = parent_pb;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (bones[i].physical_bone) {
|
2017-10-03 18:49:32 +02:00
|
|
|
bones[i].physical_bone->_on_bone_parent_changed();
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 13:23:05 +02:00
|
|
|
void _pb_stop_simulation(Node *p_node) {
|
|
|
|
for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
|
|
|
|
_pb_stop_simulation(p_node->get_child(i));
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node);
|
2018-05-10 13:23:05 +02:00
|
|
|
if (pb) {
|
|
|
|
pb->set_simulate_physics(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::physical_bones_stop_simulation() {
|
2018-05-10 13:23:05 +02:00
|
|
|
_pb_stop_simulation(this);
|
2020-02-08 09:52:58 +01:00
|
|
|
if (Engine::get_singleton()->is_editor_hint() == false && animate_physical_bones) {
|
|
|
|
set_physics_process_internal(true);
|
|
|
|
}
|
2018-05-10 13:23:05 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void _pb_start_simulation(const Skeleton3D *p_skeleton, Node *p_node, const Vector<int> &p_sim_bones) {
|
2018-05-10 13:23:05 +02:00
|
|
|
for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
|
|
|
|
_pb_start_simulation(p_skeleton, p_node->get_child(i), p_sim_bones);
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicalBone3D *pb = Object::cast_to<PhysicalBone3D>(p_node);
|
2018-05-10 13:23:05 +02:00
|
|
|
if (pb) {
|
|
|
|
for (int i = p_sim_bones.size() - 1; 0 <= i; --i) {
|
|
|
|
if (p_sim_bones[i] == pb->get_bone_id() || p_skeleton->is_bone_parent_of(pb->get_bone_id(), p_sim_bones[i])) {
|
2020-02-08 09:52:58 +01:00
|
|
|
pb->set_simulate_physics(true);
|
2018-05-10 13:23:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 17:16:45 +02:00
|
|
|
void Skeleton3D::physical_bones_start_simulation_on(const TypedArray<StringName> &p_bones) {
|
2020-02-08 09:52:58 +01:00
|
|
|
set_physics_process_internal(false);
|
2018-05-10 13:23:05 +02:00
|
|
|
|
|
|
|
Vector<int> sim_bones;
|
|
|
|
if (p_bones.size() <= 0) {
|
|
|
|
sim_bones.push_back(0); // if no bones is specified, activate ragdoll on full body
|
|
|
|
} else {
|
|
|
|
sim_bones.resize(p_bones.size());
|
|
|
|
int c = 0;
|
|
|
|
for (int i = sim_bones.size() - 1; 0 <= i; --i) {
|
2020-04-21 17:16:45 +02:00
|
|
|
int bone_id = find_bone(p_bones[i]);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (bone_id != -1) {
|
2020-04-21 17:16:45 +02:00
|
|
|
sim_bones.write[c++] = bone_id;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-05-10 13:23:05 +02:00
|
|
|
}
|
|
|
|
sim_bones.resize(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
_pb_start_simulation(this, this, sim_bones);
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _physical_bones_add_remove_collision_exception(bool p_add, Node *p_node, RID p_exception) {
|
|
|
|
for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
|
|
|
|
_physical_bones_add_remove_collision_exception(p_add, p_node->get_child(i), p_exception);
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_node);
|
2017-10-03 18:49:32 +02:00
|
|
|
if (co) {
|
|
|
|
if (p_add) {
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::get_singleton()->body_add_collision_exception(co->get_rid(), p_exception);
|
2017-10-03 18:49:32 +02:00
|
|
|
} else {
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsServer3D::get_singleton()->body_remove_collision_exception(co->get_rid(), p_exception);
|
2017-10-03 18:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::physical_bones_add_collision_exception(RID p_exception) {
|
2017-10-03 18:49:32 +02:00
|
|
|
_physical_bones_add_remove_collision_exception(true, this, p_exception);
|
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::physical_bones_remove_collision_exception(RID p_exception) {
|
2017-10-03 18:49:32 +02:00
|
|
|
_physical_bones_add_remove_collision_exception(false, this, p_exception);
|
|
|
|
}
|
|
|
|
|
2018-07-01 11:54:13 +02:00
|
|
|
#endif // _3D_DISABLED
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::_skin_changed() {
|
2019-09-19 00:46:32 +02:00
|
|
|
_make_dirty();
|
2019-03-03 16:23:03 +01:00
|
|
|
}
|
2019-09-19 00:46:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Ref<SkinReference> Skeleton3D::register_skin(const Ref<Skin> &p_skin) {
|
2019-09-19 00:46:32 +02:00
|
|
|
for (Set<SkinReference *>::Element *E = skin_bindings.front(); E; E = E->next()) {
|
|
|
|
if (E->get()->skin == p_skin) {
|
|
|
|
return Ref<SkinReference>(E->get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Skin> skin = p_skin;
|
|
|
|
|
|
|
|
if (skin.is_null()) {
|
|
|
|
//need to create one from existing code, this is for compatibility only
|
|
|
|
//when skeletons did not support skins. It is also used by gizmo
|
|
|
|
//to display the skeleton.
|
|
|
|
|
|
|
|
skin.instance();
|
|
|
|
skin->set_bind_count(bones.size());
|
|
|
|
_update_process_order(); //just in case
|
|
|
|
|
|
|
|
// pose changed, rebuild cache of inverses
|
|
|
|
const Bone *bonesptr = bones.ptr();
|
|
|
|
int len = bones.size();
|
|
|
|
const int *order = process_order.ptr();
|
|
|
|
|
|
|
|
// calculate global rests and invert them
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
const Bone &b = bonesptr[order[i]];
|
|
|
|
if (b.parent >= 0) {
|
|
|
|
skin->set_bind_pose(order[i], skin->get_bind_pose(b.parent) * b.rest);
|
|
|
|
} else {
|
|
|
|
skin->set_bind_pose(order[i], b.rest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
//the inverse is what is actually required
|
|
|
|
skin->set_bind_bone(i, i);
|
|
|
|
skin->set_bind_pose(i, skin->get_bind_pose(i).affine_inverse());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(skin.is_null(), Ref<SkinReference>());
|
|
|
|
|
|
|
|
Ref<SkinReference> skin_ref;
|
|
|
|
skin_ref.instance();
|
|
|
|
|
|
|
|
skin_ref->skeleton_node = this;
|
|
|
|
skin_ref->bind_count = 0;
|
2020-03-27 19:21:27 +01:00
|
|
|
skin_ref->skeleton = RenderingServer::get_singleton()->skeleton_create();
|
2019-09-19 00:46:32 +02:00
|
|
|
skin_ref->skeleton_node = this;
|
|
|
|
skin_ref->skin = skin;
|
|
|
|
|
|
|
|
skin_bindings.insert(skin_ref.operator->());
|
|
|
|
|
2020-02-19 20:27:19 +01:00
|
|
|
skin->connect_compat("changed", skin_ref.operator->(), "_skin_changed");
|
2019-09-25 21:44:44 +02:00
|
|
|
|
|
|
|
_make_dirty(); //skin needs to be updated, so update skeleton
|
|
|
|
|
2019-09-19 00:46:32 +02:00
|
|
|
return skin_ref;
|
2019-03-03 16:23:03 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
void Skeleton3D::_bind_methods() {
|
2020-02-19 20:48:20 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_process_orders"), &Skeleton3D::get_bone_process_orders);
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("add_bone", "name"), &Skeleton3D::add_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("find_bone", "name"), &Skeleton3D::find_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_name", "bone_idx"), &Skeleton3D::get_bone_name);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_parent", "bone_idx"), &Skeleton3D::get_bone_parent);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_parent", "bone_idx", "parent_idx"), &Skeleton3D::set_bone_parent);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton3D::get_bone_count);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("unparent_bone_and_rest", "bone_idx"), &Skeleton3D::unparent_bone_and_rest);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_rest", "bone_idx"), &Skeleton3D::get_bone_rest);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_rest", "bone_idx", "rest"), &Skeleton3D::set_bone_rest);
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("register_skin", "skin"), &Skeleton3D::register_skin);
|
2019-09-19 00:46:32 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("localize_rests"), &Skeleton3D::localize_rests);
|
2019-04-16 20:38:13 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_disable_rest", "bone_idx", "disable"), &Skeleton3D::set_bone_disable_rest);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_bone_rest_disabled", "bone_idx"), &Skeleton3D::is_bone_rest_disabled);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("bind_child_node_to_bone", "bone_idx", "node"), &Skeleton3D::bind_child_node_to_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("unbind_child_node_from_bone", "bone_idx", "node"), &Skeleton3D::unbind_child_node_from_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bound_child_nodes_to_bone", "bone_idx"), &Skeleton3D::_get_bound_child_nodes_to_bone);
|
2015-09-24 23:06:15 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("clear_bones"), &Skeleton3D::clear_bones);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_pose", "bone_idx"), &Skeleton3D::get_bone_pose);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_pose", "bone_idx", "pose"), &Skeleton3D::set_bone_pose);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("clear_bones_global_pose_override"), &Skeleton3D::clear_bones_global_pose_override);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_global_pose_override", "bone_idx", "pose", "amount", "persistent"), &Skeleton3D::set_bone_global_pose_override, DEFVAL(false));
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_global_pose", "bone_idx"), &Skeleton3D::get_bone_global_pose);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_custom_pose", "bone_idx"), &Skeleton3D::get_bone_custom_pose);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_custom_pose", "bone_idx", "custom_pose"), &Skeleton3D::set_bone_custom_pose);
|
2019-09-26 13:06:47 +02:00
|
|
|
|
2018-07-01 11:54:13 +02:00
|
|
|
#ifndef _3D_DISABLED
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_animate_physical_bones"), &Skeleton3D::set_animate_physical_bones);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_animate_physical_bones"), &Skeleton3D::get_animate_physical_bones);
|
2020-02-08 09:52:58 +01:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("physical_bones_stop_simulation"), &Skeleton3D::physical_bones_stop_simulation);
|
|
|
|
ClassDB::bind_method(D_METHOD("physical_bones_start_simulation", "bones"), &Skeleton3D::physical_bones_start_simulation_on, DEFVAL(Array()));
|
|
|
|
ClassDB::bind_method(D_METHOD("physical_bones_add_collision_exception", "exception"), &Skeleton3D::physical_bones_add_collision_exception);
|
|
|
|
ClassDB::bind_method(D_METHOD("physical_bones_remove_collision_exception", "exception"), &Skeleton3D::physical_bones_remove_collision_exception);
|
2017-10-03 18:49:32 +02:00
|
|
|
|
2020-02-08 09:52:58 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "animate_physical_bones"), "set_animate_physical_bones", "get_animate_physical_bones");
|
2018-07-01 11:54:13 +02:00
|
|
|
#endif // _3D_DISABLED
|
|
|
|
|
2020-02-19 20:48:20 +01:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
ADD_SIGNAL(MethodInfo("pose_updated"));
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
BIND_CONSTANT(NOTIFICATION_UPDATE_SKELETON);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Skeleton3D::Skeleton3D() {
|
2020-02-08 09:52:58 +01:00
|
|
|
animate_physical_bones = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
dirty = false;
|
2020-02-21 11:27:48 +01:00
|
|
|
version = 1;
|
2018-08-07 03:35:09 +02:00
|
|
|
process_order_dirty = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
Skeleton3D::~Skeleton3D() {
|
2019-09-19 00:46:32 +02:00
|
|
|
//some skins may remain bound
|
|
|
|
for (Set<SkinReference *>::Element *E = skin_bindings.front(); E; E = E->next()) {
|
|
|
|
E->get()->skeleton_node = nullptr;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|