2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* skeleton.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2019-01-01 11:53:14 +00:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00: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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "skeleton.h"
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/message_queue.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
#include "core/project_settings.h"
|
2017-10-03 16:49:32 +00:00
|
|
|
#include "scene/3d/physics_body.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "scene/resources/surface_tool.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Skeleton::_set(const StringName &p_path, const Variant &p_value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
String path = p_path;
|
|
|
|
|
|
|
|
if (!path.begins_with("bones/"))
|
|
|
|
return false;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int which = path.get_slicec('/', 1).to_int();
|
|
|
|
String what = path.get_slicec('/', 2);
|
2014-02-23 13:35:05 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (which == bones.size() && what == "name") {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
add_bone(p_value);
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(which, bones.size(), false);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (what == "parent")
|
|
|
|
set_bone_parent(which, p_value);
|
|
|
|
else if (what == "rest")
|
2014-02-10 01:10:30 +00:00
|
|
|
set_bone_rest(which, p_value);
|
2017-03-05 15:44:50 +00:00
|
|
|
else if (what == "enabled")
|
2014-02-10 01:10:30 +00:00
|
|
|
set_bone_enabled(which, p_value);
|
2017-03-05 15:44:50 +00:00
|
|
|
else if (what == "pose")
|
2014-02-10 01:10:30 +00:00
|
|
|
set_bone_pose(which, p_value);
|
2018-01-18 20:37:17 +00:00
|
|
|
else if (what == "bound_children") {
|
2017-03-05 15:44:50 +00:00
|
|
|
Array children = p_value;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2016-06-29 21:38:32 +00:00
|
|
|
if (is_inside_tree()) {
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[which].nodes_bound.clear();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-02-12 20:10:08 +00:00
|
|
|
NodePath npath = children[i];
|
|
|
|
ERR_CONTINUE(npath.operator String() == "");
|
|
|
|
Node *node = get_node(npath);
|
2016-06-29 21:38:32 +00:00
|
|
|
ERR_CONTINUE(!node);
|
2017-03-05 15:44:50 +00:00
|
|
|
bind_child_node_to_bone(which, node);
|
2016-06-29 21:38:32 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
bool Skeleton::_get(const StringName &p_path, Variant &r_ret) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
String path = p_path;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!path.begins_with("bones/"))
|
|
|
|
return false;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int which = path.get_slicec('/', 1).to_int();
|
|
|
|
String what = path.get_slicec('/', 2);
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(which, bones.size(), false);
|
|
|
|
|
|
|
|
if (what == "name")
|
|
|
|
r_ret = get_bone_name(which);
|
|
|
|
else if (what == "parent")
|
|
|
|
r_ret = get_bone_parent(which);
|
|
|
|
else if (what == "rest")
|
|
|
|
r_ret = get_bone_rest(which);
|
|
|
|
else if (what == "enabled")
|
|
|
|
r_ret = is_bone_enabled(which);
|
|
|
|
else if (what == "pose")
|
|
|
|
r_ret = get_bone_pose(which);
|
2018-01-18 20:37:17 +00:00
|
|
|
else if (what == "bound_children") {
|
2014-02-10 01:10:30 +00:00
|
|
|
Array children;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (const List<uint32_t>::Element *E = bones[which].nodes_bound.front(); E; E = E->next()) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Object *obj = ObjectDB::get_instance(E->get());
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_CONTINUE(!obj);
|
2017-08-24 20:58:51 +00:00
|
|
|
Node *node = Object::cast_to<Node>(obj);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_CONTINUE(!node);
|
2019-02-12 20:10:08 +00:00
|
|
|
NodePath npath = get_path_to(node);
|
|
|
|
children.push_back(npath);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
r_ret = children;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else
|
|
|
|
return false;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::_get_property_list(List<PropertyInfo> *p_list) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < bones.size(); i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String prep = "bones/" + itos(i) + "/";
|
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, prep + "name"));
|
2018-08-07 01:35:09 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, prep + "parent", PROPERTY_HINT_RANGE, "-1," + itos(bones.size() - 1) + ",1"));
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + "rest"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::BOOL, prep + "enabled"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::TRANSFORM, prep + "pose", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
|
2018-01-18 20:37:17 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::ARRAY, prep + "bound_children"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:35:09 +00:00
|
|
|
void Skeleton::_update_process_order() {
|
|
|
|
|
|
|
|
if (!process_order_dirty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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
|
|
|
|
ERR_PRINTS("Bone " + itos(i) + " has invalid parent: " + itos(bonesptr[i].parent));
|
|
|
|
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) {
|
|
|
|
//using bubblesort because of simplicity, it wont run every frame though.
|
|
|
|
//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;
|
|
|
|
if (parent_idx < 0)
|
|
|
|
continue; //do nothing because it has no parent
|
|
|
|
//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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!swapped)
|
|
|
|
break;
|
|
|
|
pass_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pass_count == len * len) {
|
|
|
|
ERR_PRINT("Skeleton parenthood graph is cyclic");
|
|
|
|
}
|
|
|
|
|
|
|
|
process_order_dirty = false;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Skeleton::_notification(int p_what) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_ENTER_WORLD: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-03-03 15:23:03 +00:00
|
|
|
VS::get_singleton()->skeleton_set_world_transform(skeleton, use_bones_in_world_transform, get_global_transform());
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_WORLD: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-12-29 13:54:01 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
2017-12-29 14:34:36 +00:00
|
|
|
|
2019-03-03 15:23:03 +00:00
|
|
|
VS::get_singleton()->skeleton_set_world_transform(skeleton, use_bones_in_world_transform, get_global_transform());
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_UPDATE_SKELETON: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualServer *vs = VisualServer::get_singleton();
|
2018-07-25 01:11:03 +00:00
|
|
|
Bone *bonesptr = bones.ptrw();
|
2017-03-05 15:44:50 +00:00
|
|
|
int len = bones.size();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-02-13 08:23:29 +00:00
|
|
|
vs->skeleton_allocate(skeleton, len); // if same size, nothing really happens
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-08-07 01:35:09 +00:00
|
|
|
_update_process_order();
|
|
|
|
|
|
|
|
const int *order = process_order.ptr();
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
// pose changed, rebuild cache of inverses
|
|
|
|
if (rest_global_inverse_dirty) {
|
|
|
|
|
|
|
|
// calculate global rests and invert them
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
2018-08-07 01:35:09 +00:00
|
|
|
Bone &b = bonesptr[order[i]];
|
2017-03-05 15:44:50 +00:00
|
|
|
if (b.parent >= 0)
|
|
|
|
b.rest_global_inverse = bonesptr[b.parent].rest_global_inverse * b.rest;
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
b.rest_global_inverse = b.rest;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
2018-08-07 01:35:09 +00:00
|
|
|
Bone &b = bonesptr[order[i]];
|
2014-02-10 01:10:30 +00:00
|
|
|
b.rest_global_inverse.affine_invert();
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
rest_global_inverse_dirty = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < len; i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-08-07 01:35:09 +00:00
|
|
|
Bone &b = bonesptr[order[i]];
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2015-09-24 21:06:15 +00:00
|
|
|
if (b.disable_rest) {
|
|
|
|
if (b.enabled) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Transform pose = b.pose;
|
2015-09-24 21:06:15 +00:00
|
|
|
if (b.custom_pose_enable) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-09-24 21:06:15 +00:00
|
|
|
pose = b.custom_pose * pose;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (b.parent >= 0) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b.pose_global = bonesptr[b.parent].pose_global * pose;
|
2015-09-24 21:06:15 +00:00
|
|
|
} else {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b.pose_global = pose;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (b.parent >= 0) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b.pose_global = bonesptr[b.parent].pose_global;
|
2015-09-24 21:06:15 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b.pose_global = Transform();
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2015-09-24 21:06:15 +00:00
|
|
|
if (b.enabled) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Transform pose = b.pose;
|
2015-09-24 21:06:15 +00:00
|
|
|
if (b.custom_pose_enable) {
|
|
|
|
|
|
|
|
pose = b.custom_pose * pose;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (b.parent >= 0) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b.pose_global = bonesptr[b.parent].pose_global * (b.rest * pose);
|
2015-09-24 21:06:15 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b.pose_global = b.rest * pose;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (b.parent >= 0) {
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b.pose_global = bonesptr[b.parent].pose_global * b.rest;
|
2015-09-24 21:06:15 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
b.pose_global = b.rest;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-12-29 14:34:36 +00:00
|
|
|
b.transform_final = b.pose_global * b.rest_global_inverse;
|
2019-03-03 15:23:03 +00:00
|
|
|
vs->skeleton_bone_set_transform(skeleton, order[i], b.transform_final);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<uint32_t>::Element *E = b.nodes_bound.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Object *obj = ObjectDB::get_instance(E->get());
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_CONTINUE(!obj);
|
2017-08-24 20:58:51 +00:00
|
|
|
Spatial *sp = Object::cast_to<Spatial>(obj);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_CONTINUE(!sp);
|
2015-08-09 16:43:44 +00:00
|
|
|
sp->set_transform(b.pose_global);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
dirty = false;
|
2016-03-08 23:00:52 +00:00
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform Skeleton::get_bone_transform(int p_bone) const {
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
2014-02-10 01:10:30 +00:00
|
|
|
if (dirty)
|
2017-03-05 15:44:50 +00:00
|
|
|
const_cast<Skeleton *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
|
2014-02-10 01:10:30 +00:00
|
|
|
return bones[p_bone].pose_global * bones[p_bone].rest_global_inverse;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::set_bone_global_pose(int p_bone, const Transform &p_pose) {
|
2014-10-28 01:54:32 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
|
|
|
if (bones[p_bone].parent == -1) {
|
2014-10-28 01:54:32 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
set_bone_pose(p_bone, bones[p_bone].rest_global_inverse * p_pose); //fast
|
2014-10-28 01:54:32 +00:00
|
|
|
} else {
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
set_bone_pose(p_bone, bones[p_bone].rest.affine_inverse() * (get_bone_global_pose(bones[p_bone].parent).affine_inverse() * p_pose)); //slow
|
2014-10-28 01:54:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 13:31:38 +00:00
|
|
|
Transform Skeleton::get_bone_global_pose(int p_bone) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
2014-08-14 13:31:38 +00:00
|
|
|
if (dirty)
|
2017-03-05 15:44:50 +00:00
|
|
|
const_cast<Skeleton *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
|
2014-08-14 13:31:38 +00:00
|
|
|
return bones[p_bone].pose_global;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
RID Skeleton::get_skeleton() const {
|
|
|
|
|
|
|
|
return skeleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skeleton creation api
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::add_bone(const String &p_name) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_name == "" || p_name.find(":") != -1 || p_name.find("/") != -1);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < bones.size(); i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-08-07 01:35:09 +00:00
|
|
|
ERR_FAIL_COND(bones[i].name == p_name);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2014-02-23 13:35:05 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Bone b;
|
2017-03-05 15:44:50 +00:00
|
|
|
b.name = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
bones.push_back(b);
|
2018-08-07 01:35:09 +00:00
|
|
|
process_order_dirty = true;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
rest_global_inverse_dirty = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_dirty();
|
|
|
|
update_gizmo();
|
|
|
|
}
|
2018-05-10 11:23:05 +00:00
|
|
|
int Skeleton::find_bone(const String &p_name) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < bones.size(); i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (bones[i].name == p_name)
|
2014-02-10 01:10:30 +00:00
|
|
|
return i;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
String Skeleton::get_bone_name(int p_bone) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), "");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return bones[p_bone].name;
|
|
|
|
}
|
|
|
|
|
2018-05-10 11:23:05 +00:00
|
|
|
bool Skeleton::is_bone_parent_of(int p_bone, int p_parent_bone_id) const {
|
|
|
|
|
|
|
|
int parent_of_bone = get_bone_parent(p_bone);
|
|
|
|
|
|
|
|
if (-1 == parent_of_bone)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (parent_of_bone == p_parent_bone_id)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return is_bone_parent_of(parent_of_bone, p_parent_bone_id);
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int Skeleton::get_bone_count() const {
|
|
|
|
|
|
|
|
return bones.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::set_bone_parent(int p_bone, int p_parent) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2018-08-07 01:35:09 +00:00
|
|
|
ERR_FAIL_COND(p_parent != -1 && (p_parent < 0));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].parent = p_parent;
|
2017-03-05 15:44:50 +00:00
|
|
|
rest_global_inverse_dirty = true;
|
2018-08-07 01:35:09 +00:00
|
|
|
process_order_dirty = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:06:15 +00:00
|
|
|
void Skeleton::unparent_bone_and_rest(int p_bone) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2018-08-07 01:35:09 +00:00
|
|
|
_update_process_order();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int parent = bones[p_bone].parent;
|
|
|
|
while (parent >= 0) {
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].rest = bones[parent].rest * bones[p_bone].rest;
|
2017-03-05 15:44:50 +00:00
|
|
|
parent = bones[parent].parent;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].parent = -1;
|
|
|
|
bones.write[p_bone].rest_global_inverse = bones[p_bone].rest.affine_inverse(); //same thing
|
2018-08-07 01:35:09 +00:00
|
|
|
process_order_dirty = true;
|
2015-09-24 21:06:15 +00:00
|
|
|
|
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
2017-10-03 16:49:32 +00:00
|
|
|
void Skeleton::set_bone_ignore_animation(int p_bone, bool p_ignore) {
|
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].ignore_animation = p_ignore;
|
2017-10-03 16:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Skeleton::is_bone_ignore_animation(int p_bone) const {
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
|
|
|
|
return bones[p_bone].ignore_animation;
|
|
|
|
}
|
|
|
|
|
2015-09-24 21:06:15 +00:00
|
|
|
void Skeleton::set_bone_disable_rest(int p_bone, bool p_disable) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].disable_rest = p_disable;
|
2015-09-24 21:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Skeleton::is_bone_rest_disabled(int p_bone) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
|
2015-09-24 21:06:15 +00:00
|
|
|
return bones[p_bone].disable_rest;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
int Skeleton::get_bone_parent(int p_bone) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), -1);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return bones[p_bone].parent;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::set_bone_rest(int p_bone, const Transform &p_rest) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].rest = p_rest;
|
2017-03-05 15:44:50 +00:00
|
|
|
rest_global_inverse_dirty = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
Transform Skeleton::get_bone_rest(int p_bone) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return bones[p_bone].rest;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::set_bone_enabled(int p_bone, bool p_enabled) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].enabled = p_enabled;
|
2017-03-05 15:44:50 +00:00
|
|
|
rest_global_inverse_dirty = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
bool Skeleton::is_bone_enabled(int p_bone) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), false);
|
2014-02-10 01:10:30 +00:00
|
|
|
return bones[p_bone].enabled;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::bind_child_node_to_bone(int p_bone, Node *p_node) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_NULL(p_node);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-07 10:17:31 +00:00
|
|
|
uint32_t id = p_node->get_instance_id();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
for (const List<uint32_t>::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (E->get() == id)
|
2014-02-10 01:10:30 +00:00
|
|
|
return; // already here
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].nodes_bound.push_back(id);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::unbind_child_node_from_bone(int p_bone, Node *p_node) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_NULL(p_node);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-07 10:17:31 +00:00
|
|
|
uint32_t id = p_node->get_instance_id();
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].nodes_bound.erase(id);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::get_bound_child_nodes_to_bone(int p_bone, List<Node *> *p_bound) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (const List<uint32_t>::Element *E = bones[p_bone].nodes_bound.front(); E; E = E->next()) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Object *obj = ObjectDB::get_instance(E->get());
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_CONTINUE(!obj);
|
2017-08-24 20:58:51 +00:00
|
|
|
p_bound->push_back(Object::cast_to<Node>(obj));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::clear_bones() {
|
|
|
|
|
|
|
|
bones.clear();
|
2017-03-05 15:44:50 +00:00
|
|
|
rest_global_inverse_dirty = true;
|
2018-08-07 01:35:09 +00:00
|
|
|
process_order_dirty = true;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
// posing api
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::set_bone_pose(int p_bone, const Transform &p_pose) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
|
|
|
ERR_FAIL_COND(!is_inside_tree());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].pose = p_pose;
|
2014-02-10 01:10:30 +00:00
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
Transform Skeleton::get_bone_pose(int p_bone) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
2014-02-10 01:10:30 +00:00
|
|
|
return bones[p_bone].pose;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Skeleton::set_bone_custom_pose(int p_bone, const Transform &p_custom_pose) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2017-01-14 11:26:56 +00:00
|
|
|
//ERR_FAIL_COND( !is_inside_scene() );
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].custom_pose_enable = (p_custom_pose != Transform());
|
|
|
|
bones.write[p_bone].custom_pose = p_custom_pose;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_make_dirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform Skeleton::get_bone_custom_pose(int p_bone) const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), Transform());
|
2014-02-10 01:10:30 +00:00
|
|
|
return bones[p_bone].custom_pose;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::_make_dirty() {
|
|
|
|
|
|
|
|
if (dirty)
|
|
|
|
return;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
MessageQueue::get_singleton()->push_notification(this, NOTIFICATION_UPDATE_SKELETON);
|
|
|
|
dirty = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 01:35:09 +00:00
|
|
|
int Skeleton::get_process_order(int p_idx) {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, bones.size(), -1);
|
|
|
|
_update_process_order();
|
|
|
|
return process_order[p_idx];
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Skeleton::localize_rests() {
|
|
|
|
|
2018-08-07 01:35:09 +00:00
|
|
|
_update_process_order();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-08-07 01:35:09 +00: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 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 09:54:13 +00:00
|
|
|
#ifndef _3D_DISABLED
|
|
|
|
|
2017-10-03 16:49:32 +00:00
|
|
|
void Skeleton::bind_physical_bone_to_bone(int p_bone, PhysicalBone *p_physical_bone) {
|
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
|
|
|
ERR_FAIL_COND(bones[p_bone].physical_bone);
|
|
|
|
ERR_FAIL_COND(!p_physical_bone);
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].physical_bone = p_physical_bone;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
_rebuild_physical_bones_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::unbind_physical_bone_from_bone(int p_bone) {
|
|
|
|
ERR_FAIL_INDEX(p_bone, bones.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
bones.write[p_bone].physical_bone = NULL;
|
2017-10-03 16:49:32 +00:00
|
|
|
|
|
|
|
_rebuild_physical_bones_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicalBone *Skeleton::get_physical_bone(int p_bone) {
|
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), NULL);
|
|
|
|
|
|
|
|
return bones[p_bone].physical_bone;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicalBone *Skeleton::get_physical_bone_parent(int p_bone) {
|
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), NULL);
|
|
|
|
|
|
|
|
if (bones[p_bone].cache_parent_physical_bone) {
|
|
|
|
return bones[p_bone].cache_parent_physical_bone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _get_physical_bone_parent(p_bone);
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicalBone *Skeleton::_get_physical_bone_parent(int p_bone) {
|
|
|
|
ERR_FAIL_INDEX_V(p_bone, bones.size(), NULL);
|
|
|
|
|
|
|
|
const int parent_bone = bones[p_bone].parent;
|
|
|
|
if (0 > parent_bone) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicalBone *pb = bones[parent_bone].physical_bone;
|
|
|
|
if (pb) {
|
|
|
|
return pb;
|
|
|
|
} else {
|
|
|
|
return get_physical_bone_parent(parent_bone);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::_rebuild_physical_bones_cache() {
|
|
|
|
const int b_size = bones.size();
|
|
|
|
for (int i = 0; i < b_size; ++i) {
|
2017-10-03 16:49:32 +00:00
|
|
|
PhysicalBone *parent_pb = _get_physical_bone_parent(i);
|
|
|
|
if (parent_pb != bones[i].physical_bone) {
|
|
|
|
bones.write[i].cache_parent_physical_bone = parent_pb;
|
|
|
|
if (bones[i].physical_bone)
|
|
|
|
bones[i].physical_bone->_on_bone_parent_changed();
|
|
|
|
}
|
2017-10-03 16:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 11:23:05 +00: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));
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicalBone *pb = Object::cast_to<PhysicalBone>(p_node);
|
|
|
|
if (pb) {
|
|
|
|
pb->set_simulate_physics(false);
|
|
|
|
pb->set_static_body(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::physical_bones_stop_simulation() {
|
|
|
|
_pb_stop_simulation(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _pb_start_simulation(const Skeleton *p_skeleton, Node *p_node, const Vector<int> &p_sim_bones) {
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicalBone *pb = Object::cast_to<PhysicalBone>(p_node);
|
|
|
|
if (pb) {
|
|
|
|
bool sim = false;
|
|
|
|
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])) {
|
|
|
|
sim = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pb->set_simulate_physics(true);
|
|
|
|
if (sim) {
|
|
|
|
pb->set_static_body(false);
|
|
|
|
} else {
|
|
|
|
pb->set_static_body(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::physical_bones_start_simulation_on(const Array &p_bones) {
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (Variant::STRING == p_bones.get(i).get_type()) {
|
|
|
|
int bone_id = find_bone(p_bones.get(i));
|
|
|
|
if (bone_id != -1)
|
2018-07-25 01:11:03 +00:00
|
|
|
sim_bones.write[c++] = bone_id;
|
2018-05-10 11:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sim_bones.resize(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
_pb_start_simulation(this, this, sim_bones);
|
2017-10-03 16:49:32 +00: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);
|
|
|
|
}
|
|
|
|
|
|
|
|
CollisionObject *co = Object::cast_to<CollisionObject>(p_node);
|
|
|
|
if (co) {
|
|
|
|
if (p_add) {
|
|
|
|
PhysicsServer::get_singleton()->body_add_collision_exception(co->get_rid(), p_exception);
|
|
|
|
} else {
|
|
|
|
PhysicsServer::get_singleton()->body_remove_collision_exception(co->get_rid(), p_exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::physical_bones_add_collision_exception(RID p_exception) {
|
|
|
|
_physical_bones_add_remove_collision_exception(true, this, p_exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skeleton::physical_bones_remove_collision_exception(RID p_exception) {
|
|
|
|
_physical_bones_add_remove_collision_exception(false, this, p_exception);
|
|
|
|
}
|
|
|
|
|
2018-07-01 09:54:13 +00:00
|
|
|
#endif // _3D_DISABLED
|
|
|
|
|
2019-03-03 15:23:03 +00:00
|
|
|
void Skeleton::set_use_bones_in_world_transform(bool p_enable) {
|
|
|
|
use_bones_in_world_transform = p_enable;
|
|
|
|
if (is_inside_tree()) {
|
|
|
|
VS::get_singleton()->skeleton_set_world_transform(skeleton, use_bones_in_world_transform, get_global_transform());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool Skeleton::is_using_bones_in_world_transform() const {
|
|
|
|
return use_bones_in_world_transform;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Skeleton::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("add_bone", "name"), &Skeleton::add_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("find_bone", "name"), &Skeleton::find_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_name", "bone_idx"), &Skeleton::get_bone_name);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_parent", "bone_idx"), &Skeleton::get_bone_parent);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_parent", "bone_idx", "parent_idx"), &Skeleton::set_bone_parent);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_count"), &Skeleton::get_bone_count);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("unparent_bone_and_rest", "bone_idx"), &Skeleton::unparent_bone_and_rest);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_rest", "bone_idx"), &Skeleton::get_bone_rest);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_rest", "bone_idx", "rest"), &Skeleton::set_bone_rest);
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_disable_rest", "bone_idx", "disable"), &Skeleton::set_bone_disable_rest);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_bone_rest_disabled", "bone_idx"), &Skeleton::is_bone_rest_disabled);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("bind_child_node_to_bone", "bone_idx", "node"), &Skeleton::bind_child_node_to_bone);
|
|
|
|
ClassDB::bind_method(D_METHOD("unbind_child_node_from_bone", "bone_idx", "node"), &Skeleton::unbind_child_node_from_bone);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bound_child_nodes_to_bone", "bone_idx"), &Skeleton::_get_bound_child_nodes_to_bone);
|
2015-09-24 21:06:15 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("clear_bones"), &Skeleton::clear_bones);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_pose", "bone_idx"), &Skeleton::get_bone_pose);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_pose", "bone_idx", "pose"), &Skeleton::set_bone_pose);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_global_pose", "bone_idx", "pose"), &Skeleton::set_bone_global_pose);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_global_pose", "bone_idx"), &Skeleton::get_bone_global_pose);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_custom_pose", "bone_idx"), &Skeleton::get_bone_custom_pose);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_custom_pose", "bone_idx", "custom_pose"), &Skeleton::set_bone_custom_pose);
|
2014-08-14 13:31:38 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_transform", "bone_idx"), &Skeleton::get_bone_transform);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-03-03 15:23:03 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_bones_in_world_transform", "enable"), &Skeleton::set_use_bones_in_world_transform);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_using_bones_in_world_transform"), &Skeleton::is_using_bones_in_world_transform);
|
|
|
|
|
2018-07-01 09:54:13 +00:00
|
|
|
#ifndef _3D_DISABLED
|
|
|
|
|
2018-05-10 11:23:05 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("physical_bones_stop_simulation"), &Skeleton::physical_bones_stop_simulation);
|
|
|
|
ClassDB::bind_method(D_METHOD("physical_bones_start_simulation", "bones"), &Skeleton::physical_bones_start_simulation_on, DEFVAL(Array()));
|
2017-10-03 16:49:32 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("physical_bones_add_collision_exception", "exception"), &Skeleton::physical_bones_add_collision_exception);
|
|
|
|
ClassDB::bind_method(D_METHOD("physical_bones_remove_collision_exception", "exception"), &Skeleton::physical_bones_remove_collision_exception);
|
|
|
|
|
2018-07-01 09:54:13 +00:00
|
|
|
#endif // _3D_DISABLED
|
|
|
|
|
2017-10-03 16:49:32 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_ignore_animation", "bone", "ignore"), &Skeleton::set_bone_ignore_animation);
|
|
|
|
|
2019-03-03 15:23:03 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bones_in_world_transform"), "set_use_bones_in_world_transform", "is_using_bones_in_world_transform");
|
2017-03-05 15:44:50 +00:00
|
|
|
BIND_CONSTANT(NOTIFICATION_UPDATE_SKELETON);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Skeleton::Skeleton() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
rest_global_inverse_dirty = true;
|
|
|
|
dirty = false;
|
2018-08-07 01:35:09 +00:00
|
|
|
process_order_dirty = true;
|
2017-03-05 15:44:50 +00:00
|
|
|
skeleton = VisualServer::get_singleton()->skeleton_create();
|
2017-12-29 14:55:34 +00:00
|
|
|
set_notify_transform(true);
|
2019-03-03 15:23:03 +00:00
|
|
|
use_bones_in_world_transform = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Skeleton::~Skeleton() {
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualServer::get_singleton()->free(skeleton);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|