| 1 | /**************************************************************************/ |
| 2 | /* animation_blend_space_2d.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "animation_blend_space_2d.h" |
| 32 | |
| 33 | #include "animation_blend_tree.h" |
| 34 | #include "core/math/geometry_2d.h" |
| 35 | |
| 36 | void AnimationNodeBlendSpace2D::get_parameter_list(List<PropertyInfo> *r_list) const { |
| 37 | r_list->push_back(PropertyInfo(Variant::VECTOR2, blend_position)); |
| 38 | r_list->push_back(PropertyInfo(Variant::INT, closest, PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NONE)); |
| 39 | r_list->push_back(PropertyInfo(Variant::FLOAT, length_internal, PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NONE)); |
| 40 | } |
| 41 | |
| 42 | Variant AnimationNodeBlendSpace2D::get_parameter_default_value(const StringName &p_parameter) const { |
| 43 | if (p_parameter == closest) { |
| 44 | return -1; |
| 45 | } else if (p_parameter == length_internal) { |
| 46 | return 0; |
| 47 | } else { |
| 48 | return Vector2(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void AnimationNodeBlendSpace2D::get_child_nodes(List<ChildNode> *r_child_nodes) { |
| 53 | for (int i = 0; i < blend_points_used; i++) { |
| 54 | ChildNode cn; |
| 55 | cn.name = itos(i); |
| 56 | cn.node = blend_points[i].node; |
| 57 | r_child_nodes->push_back(cn); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void AnimationNodeBlendSpace2D::add_blend_point(const Ref<AnimationRootNode> &p_node, const Vector2 &p_position, int p_at_index) { |
| 62 | ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS); |
| 63 | ERR_FAIL_COND(p_node.is_null()); |
| 64 | ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used); |
| 65 | |
| 66 | if (p_at_index == -1 || p_at_index == blend_points_used) { |
| 67 | p_at_index = blend_points_used; |
| 68 | } else { |
| 69 | for (int i = blend_points_used - 1; i > p_at_index; i--) { |
| 70 | blend_points[i] = blend_points[i - 1]; |
| 71 | } |
| 72 | for (int i = 0; i < triangles.size(); i++) { |
| 73 | for (int j = 0; j < 3; j++) { |
| 74 | if (triangles[i].points[j] >= p_at_index) { |
| 75 | triangles.write[i].points[j]++; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | blend_points[p_at_index].node = p_node; |
| 81 | blend_points[p_at_index].position = p_position; |
| 82 | |
| 83 | blend_points[p_at_index].node->connect("tree_changed" , callable_mp(this, &AnimationNodeBlendSpace2D::_tree_changed), CONNECT_REFERENCE_COUNTED); |
| 84 | blend_points[p_at_index].node->connect("animation_node_renamed" , callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED); |
| 85 | blend_points[p_at_index].node->connect("animation_node_removed" , callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_removed), CONNECT_REFERENCE_COUNTED); |
| 86 | blend_points_used++; |
| 87 | |
| 88 | _queue_auto_triangles(); |
| 89 | |
| 90 | emit_signal(SNAME("tree_changed" )); |
| 91 | } |
| 92 | |
| 93 | void AnimationNodeBlendSpace2D::set_blend_point_position(int p_point, const Vector2 &p_position) { |
| 94 | ERR_FAIL_INDEX(p_point, blend_points_used); |
| 95 | blend_points[p_point].position = p_position; |
| 96 | _queue_auto_triangles(); |
| 97 | } |
| 98 | |
| 99 | void AnimationNodeBlendSpace2D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) { |
| 100 | ERR_FAIL_INDEX(p_point, blend_points_used); |
| 101 | ERR_FAIL_COND(p_node.is_null()); |
| 102 | |
| 103 | if (blend_points[p_point].node.is_valid()) { |
| 104 | blend_points[p_point].node->disconnect("tree_changed" , callable_mp(this, &AnimationNodeBlendSpace2D::_tree_changed)); |
| 105 | blend_points[p_point].node->disconnect("animation_node_renamed" , callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_renamed)); |
| 106 | blend_points[p_point].node->disconnect("animation_node_removed" , callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_removed)); |
| 107 | } |
| 108 | blend_points[p_point].node = p_node; |
| 109 | blend_points[p_point].node->connect("tree_changed" , callable_mp(this, &AnimationNodeBlendSpace2D::_tree_changed), CONNECT_REFERENCE_COUNTED); |
| 110 | blend_points[p_point].node->connect("animation_node_renamed" , callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED); |
| 111 | blend_points[p_point].node->connect("animation_node_removed" , callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_removed), CONNECT_REFERENCE_COUNTED); |
| 112 | |
| 113 | emit_signal(SNAME("tree_changed" )); |
| 114 | } |
| 115 | |
| 116 | Vector2 AnimationNodeBlendSpace2D::get_blend_point_position(int p_point) const { |
| 117 | ERR_FAIL_INDEX_V(p_point, blend_points_used, Vector2()); |
| 118 | return blend_points[p_point].position; |
| 119 | } |
| 120 | |
| 121 | Ref<AnimationRootNode> AnimationNodeBlendSpace2D::get_blend_point_node(int p_point) const { |
| 122 | ERR_FAIL_INDEX_V(p_point, blend_points_used, Ref<AnimationRootNode>()); |
| 123 | return blend_points[p_point].node; |
| 124 | } |
| 125 | |
| 126 | void AnimationNodeBlendSpace2D::remove_blend_point(int p_point) { |
| 127 | ERR_FAIL_INDEX(p_point, blend_points_used); |
| 128 | |
| 129 | ERR_FAIL_COND(blend_points[p_point].node.is_null()); |
| 130 | blend_points[p_point].node->disconnect("tree_changed" , callable_mp(this, &AnimationNodeBlendSpace2D::_tree_changed)); |
| 131 | blend_points[p_point].node->disconnect("animation_node_renamed" , callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_renamed)); |
| 132 | blend_points[p_point].node->disconnect("animation_node_removed" , callable_mp(this, &AnimationNodeBlendSpace2D::_animation_node_removed)); |
| 133 | |
| 134 | for (int i = 0; i < triangles.size(); i++) { |
| 135 | bool erase = false; |
| 136 | for (int j = 0; j < 3; j++) { |
| 137 | if (triangles[i].points[j] == p_point) { |
| 138 | erase = true; |
| 139 | break; |
| 140 | } else if (triangles[i].points[j] > p_point) { |
| 141 | triangles.write[i].points[j]--; |
| 142 | } |
| 143 | } |
| 144 | if (erase) { |
| 145 | triangles.remove_at(i); |
| 146 | |
| 147 | i--; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | for (int i = p_point; i < blend_points_used - 1; i++) { |
| 152 | blend_points[i] = blend_points[i + 1]; |
| 153 | } |
| 154 | blend_points_used--; |
| 155 | |
| 156 | emit_signal(SNAME("animation_node_removed" ), get_instance_id(), itos(p_point)); |
| 157 | emit_signal(SNAME("tree_changed" )); |
| 158 | } |
| 159 | |
| 160 | int AnimationNodeBlendSpace2D::get_blend_point_count() const { |
| 161 | return blend_points_used; |
| 162 | } |
| 163 | |
| 164 | bool AnimationNodeBlendSpace2D::has_triangle(int p_x, int p_y, int p_z) const { |
| 165 | ERR_FAIL_INDEX_V(p_x, blend_points_used, false); |
| 166 | ERR_FAIL_INDEX_V(p_y, blend_points_used, false); |
| 167 | ERR_FAIL_INDEX_V(p_z, blend_points_used, false); |
| 168 | |
| 169 | BlendTriangle t; |
| 170 | t.points[0] = p_x; |
| 171 | t.points[1] = p_y; |
| 172 | t.points[2] = p_z; |
| 173 | |
| 174 | SortArray<int> sort; |
| 175 | sort.sort(t.points, 3); |
| 176 | |
| 177 | for (int i = 0; i < triangles.size(); i++) { |
| 178 | bool all_equal = true; |
| 179 | for (int j = 0; j < 3; j++) { |
| 180 | if (triangles[i].points[j] != t.points[j]) { |
| 181 | all_equal = false; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | if (all_equal) { |
| 186 | return true; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | void AnimationNodeBlendSpace2D::add_triangle(int p_x, int p_y, int p_z, int p_at_index) { |
| 194 | ERR_FAIL_INDEX(p_x, blend_points_used); |
| 195 | ERR_FAIL_INDEX(p_y, blend_points_used); |
| 196 | ERR_FAIL_INDEX(p_z, blend_points_used); |
| 197 | |
| 198 | _update_triangles(); |
| 199 | |
| 200 | BlendTriangle t; |
| 201 | t.points[0] = p_x; |
| 202 | t.points[1] = p_y; |
| 203 | t.points[2] = p_z; |
| 204 | |
| 205 | SortArray<int> sort; |
| 206 | sort.sort(t.points, 3); |
| 207 | |
| 208 | for (int i = 0; i < triangles.size(); i++) { |
| 209 | bool all_equal = true; |
| 210 | for (int j = 0; j < 3; j++) { |
| 211 | if (triangles[i].points[j] != t.points[j]) { |
| 212 | all_equal = false; |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | ERR_FAIL_COND(all_equal); |
| 217 | } |
| 218 | |
| 219 | if (p_at_index == -1 || p_at_index == triangles.size()) { |
| 220 | triangles.push_back(t); |
| 221 | } else { |
| 222 | triangles.insert(p_at_index, t); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | int AnimationNodeBlendSpace2D::get_triangle_point(int p_triangle, int p_point) { |
| 227 | _update_triangles(); |
| 228 | |
| 229 | ERR_FAIL_INDEX_V(p_point, 3, -1); |
| 230 | ERR_FAIL_INDEX_V(p_triangle, triangles.size(), -1); |
| 231 | return triangles[p_triangle].points[p_point]; |
| 232 | } |
| 233 | |
| 234 | void AnimationNodeBlendSpace2D::remove_triangle(int p_triangle) { |
| 235 | ERR_FAIL_INDEX(p_triangle, triangles.size()); |
| 236 | |
| 237 | triangles.remove_at(p_triangle); |
| 238 | } |
| 239 | |
| 240 | int AnimationNodeBlendSpace2D::get_triangle_count() const { |
| 241 | return triangles.size(); |
| 242 | } |
| 243 | |
| 244 | void AnimationNodeBlendSpace2D::set_min_space(const Vector2 &p_min) { |
| 245 | min_space = p_min; |
| 246 | if (min_space.x >= max_space.x) { |
| 247 | min_space.x = max_space.x - 1; |
| 248 | } |
| 249 | if (min_space.y >= max_space.y) { |
| 250 | min_space.y = max_space.y - 1; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | Vector2 AnimationNodeBlendSpace2D::get_min_space() const { |
| 255 | return min_space; |
| 256 | } |
| 257 | |
| 258 | void AnimationNodeBlendSpace2D::set_max_space(const Vector2 &p_max) { |
| 259 | max_space = p_max; |
| 260 | if (max_space.x <= min_space.x) { |
| 261 | max_space.x = min_space.x + 1; |
| 262 | } |
| 263 | if (max_space.y <= min_space.y) { |
| 264 | max_space.y = min_space.y + 1; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | Vector2 AnimationNodeBlendSpace2D::get_max_space() const { |
| 269 | return max_space; |
| 270 | } |
| 271 | |
| 272 | void AnimationNodeBlendSpace2D::set_snap(const Vector2 &p_snap) { |
| 273 | snap = p_snap; |
| 274 | } |
| 275 | |
| 276 | Vector2 AnimationNodeBlendSpace2D::get_snap() const { |
| 277 | return snap; |
| 278 | } |
| 279 | |
| 280 | void AnimationNodeBlendSpace2D::set_x_label(const String &p_label) { |
| 281 | x_label = p_label; |
| 282 | } |
| 283 | |
| 284 | String AnimationNodeBlendSpace2D::get_x_label() const { |
| 285 | return x_label; |
| 286 | } |
| 287 | |
| 288 | void AnimationNodeBlendSpace2D::set_y_label(const String &p_label) { |
| 289 | y_label = p_label; |
| 290 | } |
| 291 | |
| 292 | String AnimationNodeBlendSpace2D::get_y_label() const { |
| 293 | return y_label; |
| 294 | } |
| 295 | |
| 296 | void AnimationNodeBlendSpace2D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) { |
| 297 | if (p_index == blend_points_used) { |
| 298 | add_blend_point(p_node, Vector2()); |
| 299 | } else { |
| 300 | set_blend_point_node(p_index, p_node); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | void AnimationNodeBlendSpace2D::_set_triangles(const Vector<int> &p_triangles) { |
| 305 | if (auto_triangles) { |
| 306 | return; |
| 307 | } |
| 308 | ERR_FAIL_COND(p_triangles.size() % 3 != 0); |
| 309 | for (int i = 0; i < p_triangles.size(); i += 3) { |
| 310 | add_triangle(p_triangles[i + 0], p_triangles[i + 1], p_triangles[i + 2]); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | Vector<int> AnimationNodeBlendSpace2D::_get_triangles() const { |
| 315 | Vector<int> t; |
| 316 | if (auto_triangles && trianges_dirty) { |
| 317 | return t; |
| 318 | } |
| 319 | |
| 320 | t.resize(triangles.size() * 3); |
| 321 | for (int i = 0; i < triangles.size(); i++) { |
| 322 | t.write[i * 3 + 0] = triangles[i].points[0]; |
| 323 | t.write[i * 3 + 1] = triangles[i].points[1]; |
| 324 | t.write[i * 3 + 2] = triangles[i].points[2]; |
| 325 | } |
| 326 | return t; |
| 327 | } |
| 328 | |
| 329 | void AnimationNodeBlendSpace2D::_queue_auto_triangles() { |
| 330 | if (!auto_triangles || trianges_dirty) { |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | trianges_dirty = true; |
| 335 | call_deferred(SNAME("_update_triangles" )); |
| 336 | } |
| 337 | |
| 338 | void AnimationNodeBlendSpace2D::_update_triangles() { |
| 339 | if (!auto_triangles || !trianges_dirty) { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | trianges_dirty = false; |
| 344 | triangles.clear(); |
| 345 | if (blend_points_used < 3) { |
| 346 | emit_signal(SNAME("triangles_updated" )); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | Vector<Vector2> points; |
| 351 | points.resize(blend_points_used); |
| 352 | for (int i = 0; i < blend_points_used; i++) { |
| 353 | points.write[i] = blend_points[i].position; |
| 354 | } |
| 355 | |
| 356 | Vector<Delaunay2D::Triangle> tr = Delaunay2D::triangulate(points); |
| 357 | |
| 358 | for (int i = 0; i < tr.size(); i++) { |
| 359 | add_triangle(tr[i].points[0], tr[i].points[1], tr[i].points[2]); |
| 360 | } |
| 361 | emit_signal(SNAME("triangles_updated" )); |
| 362 | } |
| 363 | |
| 364 | Vector2 AnimationNodeBlendSpace2D::get_closest_point(const Vector2 &p_point) { |
| 365 | _update_triangles(); |
| 366 | |
| 367 | if (triangles.size() == 0) { |
| 368 | return Vector2(); |
| 369 | } |
| 370 | |
| 371 | Vector2 best_point; |
| 372 | bool first = true; |
| 373 | |
| 374 | for (int i = 0; i < triangles.size(); i++) { |
| 375 | Vector2 points[3]; |
| 376 | for (int j = 0; j < 3; j++) { |
| 377 | points[j] = get_blend_point_position(get_triangle_point(i, j)); |
| 378 | } |
| 379 | |
| 380 | if (Geometry2D::is_point_in_triangle(p_point, points[0], points[1], points[2])) { |
| 381 | return p_point; |
| 382 | } |
| 383 | |
| 384 | for (int j = 0; j < 3; j++) { |
| 385 | Vector2 s[2] = { |
| 386 | points[j], |
| 387 | points[(j + 1) % 3] |
| 388 | }; |
| 389 | Vector2 closest_point = Geometry2D::get_closest_point_to_segment(p_point, s); |
| 390 | if (first || closest_point.distance_to(p_point) < best_point.distance_to(p_point)) { |
| 391 | best_point = closest_point; |
| 392 | first = false; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return best_point; |
| 398 | } |
| 399 | |
| 400 | void AnimationNodeBlendSpace2D::_blend_triangle(const Vector2 &p_pos, const Vector2 *p_points, float *r_weights) { |
| 401 | if (p_pos.is_equal_approx(p_points[0])) { |
| 402 | r_weights[0] = 1; |
| 403 | r_weights[1] = 0; |
| 404 | r_weights[2] = 0; |
| 405 | return; |
| 406 | } |
| 407 | if (p_pos.is_equal_approx(p_points[1])) { |
| 408 | r_weights[0] = 0; |
| 409 | r_weights[1] = 1; |
| 410 | r_weights[2] = 0; |
| 411 | return; |
| 412 | } |
| 413 | if (p_pos.is_equal_approx(p_points[2])) { |
| 414 | r_weights[0] = 0; |
| 415 | r_weights[1] = 0; |
| 416 | r_weights[2] = 1; |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | Vector2 v0 = p_points[1] - p_points[0]; |
| 421 | Vector2 v1 = p_points[2] - p_points[0]; |
| 422 | Vector2 v2 = p_pos - p_points[0]; |
| 423 | |
| 424 | float d00 = v0.dot(v0); |
| 425 | float d01 = v0.dot(v1); |
| 426 | float d11 = v1.dot(v1); |
| 427 | float d20 = v2.dot(v0); |
| 428 | float d21 = v2.dot(v1); |
| 429 | float denom = (d00 * d11 - d01 * d01); |
| 430 | if (denom == 0) { |
| 431 | r_weights[0] = 1; |
| 432 | r_weights[1] = 0; |
| 433 | r_weights[2] = 0; |
| 434 | return; |
| 435 | } |
| 436 | float v = (d11 * d20 - d01 * d21) / denom; |
| 437 | float w = (d00 * d21 - d01 * d20) / denom; |
| 438 | float u = 1.0f - v - w; |
| 439 | |
| 440 | r_weights[0] = u; |
| 441 | r_weights[1] = v; |
| 442 | r_weights[2] = w; |
| 443 | } |
| 444 | |
| 445 | double AnimationNodeBlendSpace2D::_process(double p_time, bool p_seek, bool p_is_external_seeking, bool p_test_only) { |
| 446 | _update_triangles(); |
| 447 | |
| 448 | Vector2 blend_pos = get_parameter(blend_position); |
| 449 | int cur_closest = get_parameter(closest); |
| 450 | double cur_length_internal = get_parameter(length_internal); |
| 451 | double mind = 0.0; //time of min distance point |
| 452 | |
| 453 | if (blend_mode == BLEND_MODE_INTERPOLATED) { |
| 454 | if (triangles.size() == 0) { |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | Vector2 best_point; |
| 459 | bool first = true; |
| 460 | int blend_triangle = -1; |
| 461 | float blend_weights[3] = { 0, 0, 0 }; |
| 462 | |
| 463 | for (int i = 0; i < triangles.size(); i++) { |
| 464 | Vector2 points[3]; |
| 465 | for (int j = 0; j < 3; j++) { |
| 466 | points[j] = get_blend_point_position(get_triangle_point(i, j)); |
| 467 | } |
| 468 | |
| 469 | if (Geometry2D::is_point_in_triangle(blend_pos, points[0], points[1], points[2])) { |
| 470 | blend_triangle = i; |
| 471 | _blend_triangle(blend_pos, points, blend_weights); |
| 472 | break; |
| 473 | } |
| 474 | |
| 475 | for (int j = 0; j < 3; j++) { |
| 476 | Vector2 s[2] = { |
| 477 | points[j], |
| 478 | points[(j + 1) % 3] |
| 479 | }; |
| 480 | Vector2 closest2 = Geometry2D::get_closest_point_to_segment(blend_pos, s); |
| 481 | if (first || closest2.distance_to(blend_pos) < best_point.distance_to(blend_pos)) { |
| 482 | best_point = closest2; |
| 483 | blend_triangle = i; |
| 484 | first = false; |
| 485 | float d = s[0].distance_to(s[1]); |
| 486 | if (d == 0.0) { |
| 487 | blend_weights[j] = 1.0; |
| 488 | blend_weights[(j + 1) % 3] = 0.0; |
| 489 | blend_weights[(j + 2) % 3] = 0.0; |
| 490 | } else { |
| 491 | float c = s[0].distance_to(closest2) / d; |
| 492 | |
| 493 | blend_weights[j] = 1.0 - c; |
| 494 | blend_weights[(j + 1) % 3] = c; |
| 495 | blend_weights[(j + 2) % 3] = 0.0; |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | ERR_FAIL_COND_V(blend_triangle == -1, 0); //should never reach here |
| 502 | |
| 503 | int triangle_points[3]; |
| 504 | for (int j = 0; j < 3; j++) { |
| 505 | triangle_points[j] = get_triangle_point(blend_triangle, j); |
| 506 | } |
| 507 | |
| 508 | first = true; |
| 509 | |
| 510 | for (int i = 0; i < blend_points_used; i++) { |
| 511 | bool found = false; |
| 512 | for (int j = 0; j < 3; j++) { |
| 513 | if (i == triangle_points[j]) { |
| 514 | //blend with the given weight |
| 515 | double t = blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, p_is_external_seeking, blend_weights[j], FILTER_IGNORE, true, p_test_only); |
| 516 | if (first || t < mind) { |
| 517 | mind = t; |
| 518 | first = false; |
| 519 | } |
| 520 | found = true; |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if (sync && !found) { |
| 526 | blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, p_is_external_seeking, 0, FILTER_IGNORE, true, p_test_only); |
| 527 | } |
| 528 | } |
| 529 | } else { |
| 530 | int new_closest = -1; |
| 531 | float new_closest_dist = 1e20; |
| 532 | |
| 533 | for (int i = 0; i < blend_points_used; i++) { |
| 534 | float d = blend_points[i].position.distance_squared_to(blend_pos); |
| 535 | if (d < new_closest_dist) { |
| 536 | new_closest = i; |
| 537 | new_closest_dist = d; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | if (new_closest != cur_closest && new_closest != -1) { |
| 542 | double from = 0.0; |
| 543 | if (blend_mode == BLEND_MODE_DISCRETE_CARRY && cur_closest != -1) { |
| 544 | //for ping-pong loop |
| 545 | Ref<AnimationNodeAnimation> na_c = static_cast<Ref<AnimationNodeAnimation>>(blend_points[cur_closest].node); |
| 546 | Ref<AnimationNodeAnimation> na_n = static_cast<Ref<AnimationNodeAnimation>>(blend_points[new_closest].node); |
| 547 | if (!na_c.is_null() && !na_n.is_null()) { |
| 548 | na_n->set_backward(na_c->is_backward()); |
| 549 | } |
| 550 | //see how much animation remains |
| 551 | from = cur_length_internal - blend_node(blend_points[cur_closest].name, blend_points[cur_closest].node, p_time, false, p_is_external_seeking, 0.0, FILTER_IGNORE, true, p_test_only); |
| 552 | } |
| 553 | |
| 554 | mind = blend_node(blend_points[new_closest].name, blend_points[new_closest].node, from, true, p_is_external_seeking, 1.0, FILTER_IGNORE, true, p_test_only); |
| 555 | cur_length_internal = from + mind; |
| 556 | |
| 557 | cur_closest = new_closest; |
| 558 | |
| 559 | } else { |
| 560 | mind = blend_node(blend_points[cur_closest].name, blend_points[cur_closest].node, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, true, p_test_only); |
| 561 | } |
| 562 | |
| 563 | if (sync) { |
| 564 | for (int i = 0; i < blend_points_used; i++) { |
| 565 | if (i != cur_closest) { |
| 566 | blend_node(blend_points[i].name, blend_points[i].node, p_time, p_seek, p_is_external_seeking, 0, FILTER_IGNORE, true, p_test_only); |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | set_parameter(this->closest, cur_closest); |
| 573 | set_parameter(this->length_internal, cur_length_internal); |
| 574 | return mind; |
| 575 | } |
| 576 | |
| 577 | String AnimationNodeBlendSpace2D::get_caption() const { |
| 578 | return "BlendSpace2D" ; |
| 579 | } |
| 580 | |
| 581 | void AnimationNodeBlendSpace2D::_validate_property(PropertyInfo &p_property) const { |
| 582 | if (auto_triangles && p_property.name == "triangles" ) { |
| 583 | p_property.usage = PROPERTY_USAGE_NONE; |
| 584 | } |
| 585 | if (p_property.name.begins_with("blend_point_" )) { |
| 586 | String left = p_property.name.get_slicec('/', 0); |
| 587 | int idx = left.get_slicec('_', 2).to_int(); |
| 588 | if (idx >= blend_points_used) { |
| 589 | p_property.usage = PROPERTY_USAGE_NONE; |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void AnimationNodeBlendSpace2D::set_auto_triangles(bool p_enable) { |
| 595 | if (auto_triangles == p_enable) { |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | auto_triangles = p_enable; |
| 600 | _queue_auto_triangles(); |
| 601 | } |
| 602 | |
| 603 | bool AnimationNodeBlendSpace2D::get_auto_triangles() const { |
| 604 | return auto_triangles; |
| 605 | } |
| 606 | |
| 607 | Ref<AnimationNode> AnimationNodeBlendSpace2D::get_child_by_name(const StringName &p_name) const { |
| 608 | return get_blend_point_node(p_name.operator String().to_int()); |
| 609 | } |
| 610 | |
| 611 | void AnimationNodeBlendSpace2D::set_blend_mode(BlendMode p_blend_mode) { |
| 612 | blend_mode = p_blend_mode; |
| 613 | } |
| 614 | |
| 615 | AnimationNodeBlendSpace2D::BlendMode AnimationNodeBlendSpace2D::get_blend_mode() const { |
| 616 | return blend_mode; |
| 617 | } |
| 618 | |
| 619 | void AnimationNodeBlendSpace2D::set_use_sync(bool p_sync) { |
| 620 | sync = p_sync; |
| 621 | } |
| 622 | |
| 623 | bool AnimationNodeBlendSpace2D::is_using_sync() const { |
| 624 | return sync; |
| 625 | } |
| 626 | |
| 627 | void AnimationNodeBlendSpace2D::_tree_changed() { |
| 628 | AnimationRootNode::_tree_changed(); |
| 629 | } |
| 630 | |
| 631 | void AnimationNodeBlendSpace2D::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) { |
| 632 | AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name); |
| 633 | } |
| 634 | |
| 635 | void AnimationNodeBlendSpace2D::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) { |
| 636 | AnimationRootNode::_animation_node_removed(p_oid, p_node); |
| 637 | } |
| 638 | |
| 639 | void AnimationNodeBlendSpace2D::_bind_methods() { |
| 640 | ClassDB::bind_method(D_METHOD("add_blend_point" , "node" , "pos" , "at_index" ), &AnimationNodeBlendSpace2D::add_blend_point, DEFVAL(-1)); |
| 641 | ClassDB::bind_method(D_METHOD("set_blend_point_position" , "point" , "pos" ), &AnimationNodeBlendSpace2D::set_blend_point_position); |
| 642 | ClassDB::bind_method(D_METHOD("get_blend_point_position" , "point" ), &AnimationNodeBlendSpace2D::get_blend_point_position); |
| 643 | ClassDB::bind_method(D_METHOD("set_blend_point_node" , "point" , "node" ), &AnimationNodeBlendSpace2D::set_blend_point_node); |
| 644 | ClassDB::bind_method(D_METHOD("get_blend_point_node" , "point" ), &AnimationNodeBlendSpace2D::get_blend_point_node); |
| 645 | ClassDB::bind_method(D_METHOD("remove_blend_point" , "point" ), &AnimationNodeBlendSpace2D::remove_blend_point); |
| 646 | ClassDB::bind_method(D_METHOD("get_blend_point_count" ), &AnimationNodeBlendSpace2D::get_blend_point_count); |
| 647 | |
| 648 | ClassDB::bind_method(D_METHOD("add_triangle" , "x" , "y" , "z" , "at_index" ), &AnimationNodeBlendSpace2D::add_triangle, DEFVAL(-1)); |
| 649 | ClassDB::bind_method(D_METHOD("get_triangle_point" , "triangle" , "point" ), &AnimationNodeBlendSpace2D::get_triangle_point); |
| 650 | ClassDB::bind_method(D_METHOD("remove_triangle" , "triangle" ), &AnimationNodeBlendSpace2D::remove_triangle); |
| 651 | ClassDB::bind_method(D_METHOD("get_triangle_count" ), &AnimationNodeBlendSpace2D::get_triangle_count); |
| 652 | |
| 653 | ClassDB::bind_method(D_METHOD("set_min_space" , "min_space" ), &AnimationNodeBlendSpace2D::set_min_space); |
| 654 | ClassDB::bind_method(D_METHOD("get_min_space" ), &AnimationNodeBlendSpace2D::get_min_space); |
| 655 | |
| 656 | ClassDB::bind_method(D_METHOD("set_max_space" , "max_space" ), &AnimationNodeBlendSpace2D::set_max_space); |
| 657 | ClassDB::bind_method(D_METHOD("get_max_space" ), &AnimationNodeBlendSpace2D::get_max_space); |
| 658 | |
| 659 | ClassDB::bind_method(D_METHOD("set_snap" , "snap" ), &AnimationNodeBlendSpace2D::set_snap); |
| 660 | ClassDB::bind_method(D_METHOD("get_snap" ), &AnimationNodeBlendSpace2D::get_snap); |
| 661 | |
| 662 | ClassDB::bind_method(D_METHOD("set_x_label" , "text" ), &AnimationNodeBlendSpace2D::set_x_label); |
| 663 | ClassDB::bind_method(D_METHOD("get_x_label" ), &AnimationNodeBlendSpace2D::get_x_label); |
| 664 | |
| 665 | ClassDB::bind_method(D_METHOD("set_y_label" , "text" ), &AnimationNodeBlendSpace2D::set_y_label); |
| 666 | ClassDB::bind_method(D_METHOD("get_y_label" ), &AnimationNodeBlendSpace2D::get_y_label); |
| 667 | |
| 668 | ClassDB::bind_method(D_METHOD("_add_blend_point" , "index" , "node" ), &AnimationNodeBlendSpace2D::_add_blend_point); |
| 669 | |
| 670 | ClassDB::bind_method(D_METHOD("_set_triangles" , "triangles" ), &AnimationNodeBlendSpace2D::_set_triangles); |
| 671 | ClassDB::bind_method(D_METHOD("_get_triangles" ), &AnimationNodeBlendSpace2D::_get_triangles); |
| 672 | |
| 673 | ClassDB::bind_method(D_METHOD("set_auto_triangles" , "enable" ), &AnimationNodeBlendSpace2D::set_auto_triangles); |
| 674 | ClassDB::bind_method(D_METHOD("get_auto_triangles" ), &AnimationNodeBlendSpace2D::get_auto_triangles); |
| 675 | |
| 676 | ClassDB::bind_method(D_METHOD("set_blend_mode" , "mode" ), &AnimationNodeBlendSpace2D::set_blend_mode); |
| 677 | ClassDB::bind_method(D_METHOD("get_blend_mode" ), &AnimationNodeBlendSpace2D::get_blend_mode); |
| 678 | |
| 679 | ClassDB::bind_method(D_METHOD("set_use_sync" , "enable" ), &AnimationNodeBlendSpace2D::set_use_sync); |
| 680 | ClassDB::bind_method(D_METHOD("is_using_sync" ), &AnimationNodeBlendSpace2D::is_using_sync); |
| 681 | |
| 682 | ClassDB::bind_method(D_METHOD("_update_triangles" ), &AnimationNodeBlendSpace2D::_update_triangles); |
| 683 | |
| 684 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_triangles" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "set_auto_triangles" , "get_auto_triangles" ); |
| 685 | |
| 686 | for (int i = 0; i < MAX_BLEND_POINTS; i++) { |
| 687 | ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node" , PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode" , PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point" , "get_blend_point_node" , i); |
| 688 | ADD_PROPERTYI(PropertyInfo(Variant::VECTOR2, "blend_point_" + itos(i) + "/pos" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position" , "get_blend_point_position" , i); |
| 689 | } |
| 690 | |
| 691 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "triangles" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_triangles" , "_get_triangles" ); |
| 692 | |
| 693 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "min_space" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "set_min_space" , "get_min_space" ); |
| 694 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "max_space" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "set_max_space" , "get_max_space" ); |
| 695 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "snap" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "set_snap" , "get_snap" ); |
| 696 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "x_label" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "set_x_label" , "get_x_label" ); |
| 697 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "y_label" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "set_y_label" , "get_y_label" ); |
| 698 | ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode" , PROPERTY_HINT_ENUM, "Interpolated,Discrete,Carry" , PROPERTY_USAGE_NO_EDITOR), "set_blend_mode" , "get_blend_mode" ); |
| 699 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NO_EDITOR), "set_use_sync" , "is_using_sync" ); |
| 700 | |
| 701 | ADD_SIGNAL(MethodInfo("triangles_updated" )); |
| 702 | BIND_ENUM_CONSTANT(BLEND_MODE_INTERPOLATED); |
| 703 | BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE); |
| 704 | BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE_CARRY); |
| 705 | } |
| 706 | |
| 707 | AnimationNodeBlendSpace2D::AnimationNodeBlendSpace2D() { |
| 708 | for (int i = 0; i < MAX_BLEND_POINTS; i++) { |
| 709 | blend_points[i].name = itos(i); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | AnimationNodeBlendSpace2D::~AnimationNodeBlendSpace2D() { |
| 714 | } |
| 715 | |