2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* animation_cache.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
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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 "animation_cache.h"
|
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
void AnimationCache::_node_exit_tree(Node *p_node) {
|
2014-02-10 01:10:30 +00:00
|
|
|
//it is one shot, so it disconnects upon arrival
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!connected_nodes.has(p_node));
|
|
|
|
|
|
|
|
connected_nodes.erase(p_node);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < path_cache.size(); i++) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (path_cache[i].node != p_node) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
path_cache.write[i].valid = false; //invalidate path cache
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::_animation_changed() {
|
|
|
|
_clear_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::_clear_cache() {
|
2017-03-05 15:44:50 +00:00
|
|
|
while (connected_nodes.size()) {
|
2020-02-21 17:28:45 +00:00
|
|
|
connected_nodes.front()->get()->disconnect("tree_exiting", callable_mp(this, &AnimationCache::_node_exit_tree));
|
2014-02-10 01:10:30 +00:00
|
|
|
connected_nodes.erase(connected_nodes.front());
|
|
|
|
}
|
2017-01-14 17:03:38 +00:00
|
|
|
path_cache.clear();
|
2017-03-05 15:44:50 +00:00
|
|
|
cache_valid = false;
|
|
|
|
cache_dirty = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::_update_cache() {
|
2017-03-05 15:44:50 +00:00
|
|
|
cache_valid = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!root);
|
2014-11-06 00:20:42 +00:00
|
|
|
ERR_FAIL_COND(!root->is_inside_tree());
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(animation.is_null());
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < animation->get_track_count(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
NodePath np = animation->track_get_path(i);
|
|
|
|
|
|
|
|
Node *node = root->get_node(np);
|
|
|
|
if (!node) {
|
|
|
|
path_cache.push_back(Path());
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_CONTINUE_MSG(!node, "Invalid track path in animation '" + np + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Path path;
|
|
|
|
|
|
|
|
Ref<Resource> res;
|
|
|
|
|
2021-06-04 00:34:41 +00:00
|
|
|
if (animation->track_get_type(i) == Animation::TYPE_TRANSFORM3D) {
|
2021-03-17 23:35:42 +00:00
|
|
|
#ifndef _3D_DISABLED
|
2017-05-30 20:20:15 +00:00
|
|
|
if (np.get_subname_count() > 1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
path_cache.push_back(Path());
|
2021-06-04 00:34:41 +00:00
|
|
|
ERR_CONTINUE_MSG(animation->track_get_type(i) == Animation::TYPE_TRANSFORM3D, "Transform tracks can't have a subpath '" + np + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
Node3D *sp = Object::cast_to<Node3D>(node);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-30 20:20:15 +00:00
|
|
|
if (!sp) {
|
2014-02-10 01:10:30 +00:00
|
|
|
path_cache.push_back(Path());
|
2020-03-26 21:49:16 +00:00
|
|
|
ERR_CONTINUE_MSG(!sp, "Transform track not of type Node3D '" + np + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 20:20:15 +00:00
|
|
|
if (np.get_subname_count() == 1) {
|
|
|
|
StringName property = np.get_subname(0);
|
2014-02-10 01:10:30 +00:00
|
|
|
String ps = property;
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
|
2017-05-30 20:20:15 +00:00
|
|
|
if (!sk) {
|
|
|
|
path_cache.push_back(Path());
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_CONTINUE_MSG(!sk, "Property defined in Transform track, but not a Skeleton! '" + np + "'.");
|
2017-05-30 20:20:15 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-30 20:20:15 +00:00
|
|
|
int idx = sk->find_bone(ps);
|
|
|
|
if (idx == -1) {
|
2014-02-10 01:10:30 +00:00
|
|
|
path_cache.push_back(Path());
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_CONTINUE_MSG(idx == -1, "Property defined in Transform track, but not a Skeleton Bone! '" + np + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 20:20:15 +00:00
|
|
|
path.bone_idx = idx;
|
|
|
|
path.skeleton = sk;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-03-17 23:35:42 +00:00
|
|
|
path.node_3d = sp;
|
|
|
|
#endif // _3D_DISABLED
|
2017-05-30 20:20:15 +00:00
|
|
|
} else {
|
|
|
|
if (np.get_subname_count() > 0) {
|
2019-02-12 20:10:08 +00:00
|
|
|
RES res2;
|
2017-05-30 20:20:15 +00:00
|
|
|
Vector<StringName> leftover_subpath;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-30 20:20:15 +00:00
|
|
|
// We don't want to cache the last resource unless it is a method call
|
|
|
|
bool is_method = animation->track_get_type(i) == Animation::TYPE_METHOD;
|
2019-02-12 20:10:08 +00:00
|
|
|
root->get_node_and_resource(np, res2, leftover_subpath, is_method);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-02-12 20:10:08 +00:00
|
|
|
if (res2.is_valid()) {
|
|
|
|
path.resource = res2;
|
2017-05-30 20:20:15 +00:00
|
|
|
} else {
|
|
|
|
path.node = node;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2019-02-12 20:10:08 +00:00
|
|
|
path.object = res2.is_valid() ? res2.ptr() : (Object *)node;
|
2017-05-30 20:20:15 +00:00
|
|
|
path.subpath = leftover_subpath;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-05-30 20:20:15 +00:00
|
|
|
} else {
|
|
|
|
path.node = node;
|
|
|
|
path.object = node;
|
|
|
|
path.subpath = np.get_subnames();
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
|
2017-05-30 20:20:15 +00:00
|
|
|
if (np.get_subname_count() == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
path_cache.push_back(Path());
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_CONTINUE_MSG(np.get_subname_count() == 0, "Value Track lacks property: " + np + ".");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (animation->track_get_type(i) == Animation::TYPE_METHOD) {
|
2017-05-30 20:20:15 +00:00
|
|
|
if (path.subpath.size() != 0) { // Trying to call a method of a non-resource
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2019-08-08 20:11:48 +00:00
|
|
|
ERR_CONTINUE_MSG(path.subpath.size() != 0, "Method Track has property: " + np + ".");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
path.valid = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
path_cache.push_back(path);
|
|
|
|
|
|
|
|
if (!connected_nodes.has(path.node)) {
|
|
|
|
connected_nodes.insert(path.node);
|
2020-02-21 17:28:45 +00:00
|
|
|
path.node->connect("tree_exiting", callable_mp(this, &AnimationCache::_node_exit_tree), Node::make_binds(path.node), CONNECT_ONESHOT);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
cache_dirty = false;
|
|
|
|
cache_valid = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 05:08:21 +00:00
|
|
|
void AnimationCache::set_track_transform(int p_idx, const Transform3D &p_transform) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (cache_dirty) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_cache();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!cache_valid);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, path_cache.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
Path &p = path_cache.write[p_idx];
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!p.valid) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-03-17 23:35:42 +00:00
|
|
|
#ifndef _3D_DISABLED
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!p.node);
|
2021-03-17 23:35:42 +00:00
|
|
|
ERR_FAIL_COND(!p.node_3d);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p.skeleton) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p.skeleton->set_bone_pose(p.bone_idx, p_transform);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2021-03-17 23:35:42 +00:00
|
|
|
p.node_3d->set_transform(p_transform);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2021-03-17 23:35:42 +00:00
|
|
|
#endif // _3D_DISABLED
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AnimationCache::set_track_value(int p_idx, const Variant &p_value) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (cache_dirty) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_cache();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!cache_valid);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, path_cache.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
Path &p = path_cache.write[p_idx];
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!p.valid) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!p.object);
|
2017-05-30 20:20:15 +00:00
|
|
|
p.object->set_indexed(p.subpath, p_value);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 19:27:19 +00:00
|
|
|
void AnimationCache::call_track(int p_idx, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (cache_dirty) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_cache();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!cache_valid);
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, path_cache.size());
|
2018-07-25 01:11:03 +00:00
|
|
|
Path &p = path_cache.write[p_idx];
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!p.valid) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!p.object);
|
2017-03-05 15:44:50 +00:00
|
|
|
p.object->call(p_method, p_args, p_argcount, r_error);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::set_all(float p_time, float p_delta) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (cache_dirty) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_cache();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!cache_valid);
|
|
|
|
|
|
|
|
int tc = animation->get_track_count();
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < tc; i++) {
|
|
|
|
switch (animation->track_get_type(i)) {
|
2021-06-04 00:34:41 +00:00
|
|
|
case Animation::TYPE_TRANSFORM3D: {
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 loc, scale;
|
2021-01-20 07:02:02 +00:00
|
|
|
Quaternion rot;
|
2017-03-05 15:44:50 +00:00
|
|
|
animation->transform_track_interpolate(i, p_time, &loc, &rot, &scale);
|
2020-10-17 05:08:21 +00:00
|
|
|
Transform3D tr(Basis(rot), loc);
|
2014-02-10 01:10:30 +00:00
|
|
|
tr.basis.scale(scale);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
set_track_transform(i, tr);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case Animation::TYPE_VALUE: {
|
2017-03-05 15:44:50 +00:00
|
|
|
if (animation->value_track_get_update_mode(i) == Animation::UPDATE_CONTINUOUS || (animation->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE && p_delta == 0)) {
|
|
|
|
Variant v = animation->value_track_interpolate(i, p_time);
|
|
|
|
set_track_value(i, v);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
List<int> indices;
|
2017-03-05 15:44:50 +00:00
|
|
|
animation->value_track_get_key_indices(i, p_time, p_delta, &indices);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
for (int &E : indices) {
|
|
|
|
Variant v = animation->track_get_key_value(i, E);
|
2017-03-05 15:44:50 +00:00
|
|
|
set_track_value(i, v);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case Animation::TYPE_METHOD: {
|
|
|
|
List<int> indices;
|
2017-03-05 15:44:50 +00:00
|
|
|
animation->method_track_get_key_indices(i, p_time, p_delta, &indices);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
for (int &E : indices) {
|
|
|
|
Vector<Variant> args = animation->method_track_get_params(i, E);
|
|
|
|
StringName name = animation->method_track_get_name(i, E);
|
2020-02-19 19:27:19 +00:00
|
|
|
Callable::CallError err;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!args.size()) {
|
2020-04-01 23:20:12 +00:00
|
|
|
call_track(i, name, nullptr, 0, err);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2018-07-25 01:11:03 +00:00
|
|
|
Vector<const Variant *> argptrs;
|
2014-02-10 01:10:30 +00:00
|
|
|
argptrs.resize(args.size());
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < args.size(); j++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
argptrs.write[j] = &args.write[j];
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
call_track(i, name, (const Variant **)&argptrs[0], args.size(), err);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
2019-04-09 15:08:36 +00:00
|
|
|
default: {
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AnimationCache::set_animation(const Ref<Animation> &p_animation) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_clear_cache();
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (animation.is_valid()) {
|
2020-02-21 17:28:45 +00:00
|
|
|
animation->disconnect("changed", callable_mp(this, &AnimationCache::_animation_changed));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
animation = p_animation;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (animation.is_valid()) {
|
2020-02-21 17:28:45 +00:00
|
|
|
animation->connect("changed", callable_mp(this, &AnimationCache::_animation_changed));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::_bind_methods() {
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void AnimationCache::set_root(Node *p_root) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_clear_cache();
|
2017-03-05 15:44:50 +00:00
|
|
|
root = p_root;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimationCache::AnimationCache() {
|
|
|
|
}
|