root_motion_view.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************/
  2. /* root_motion_view.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include "root_motion_view.h"
  31. #include "scene/animation/animation_tree.h"
  32. #include "scene/resources/material.h"
  33. void RootMotionView::set_animation_path(const NodePath &p_path) {
  34. path = p_path;
  35. first = true;
  36. }
  37. NodePath RootMotionView::get_animation_path() const {
  38. return path;
  39. }
  40. void RootMotionView::set_color(const Color &p_color) {
  41. color = p_color;
  42. first = true;
  43. }
  44. Color RootMotionView::get_color() const {
  45. return color;
  46. }
  47. void RootMotionView::set_cell_size(float p_size) {
  48. cell_size = p_size;
  49. first = true;
  50. }
  51. float RootMotionView::get_cell_size() const {
  52. return cell_size;
  53. }
  54. void RootMotionView::set_radius(float p_radius) {
  55. radius = p_radius;
  56. first = true;
  57. }
  58. float RootMotionView::get_radius() const {
  59. return radius;
  60. }
  61. void RootMotionView::set_zero_y(bool p_zero_y) {
  62. zero_y = p_zero_y;
  63. }
  64. bool RootMotionView::get_zero_y() const {
  65. return zero_y;
  66. }
  67. void RootMotionView::_notification(int p_what) {
  68. if (p_what == NOTIFICATION_ENTER_TREE) {
  69. VS::get_singleton()->immediate_set_material(immediate, SpatialMaterial::get_material_rid_for_2d(false, true, false, false, false));
  70. first = true;
  71. }
  72. if (p_what == NOTIFICATION_INTERNAL_PROCESS || p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  73. Transform transform;
  74. if (has_node(path)) {
  75. Node *node = get_node(path);
  76. AnimationTree *tree = Object::cast_to<AnimationTree>(node);
  77. if (tree && tree->is_active() && tree->get_root_motion_track() != NodePath()) {
  78. if (is_processing_internal() && tree->get_process_mode() == AnimationTree::ANIMATION_PROCESS_PHYSICS) {
  79. set_process_internal(false);
  80. set_physics_process_internal(true);
  81. }
  82. if (is_physics_processing_internal() && tree->get_process_mode() == AnimationTree::ANIMATION_PROCESS_IDLE) {
  83. set_process_internal(true);
  84. set_physics_process_internal(false);
  85. }
  86. transform = tree->get_root_motion_transform();
  87. }
  88. }
  89. if (!first && transform == Transform()) {
  90. return;
  91. }
  92. first = false;
  93. transform.orthonormalize(); //don't want scale, too imprecise
  94. transform.affine_invert();
  95. accumulated = transform * accumulated;
  96. accumulated.origin.x = Math::fposmod(accumulated.origin.x, cell_size);
  97. if (zero_y) {
  98. accumulated.origin.y = 0;
  99. }
  100. accumulated.origin.z = Math::fposmod(accumulated.origin.z, cell_size);
  101. VS::get_singleton()->immediate_clear(immediate);
  102. int cells_in_radius = int((radius / cell_size) + 1.0);
  103. VS::get_singleton()->immediate_begin(immediate, VS::PRIMITIVE_LINES);
  104. for (int i = -cells_in_radius; i < cells_in_radius; i++) {
  105. for (int j = -cells_in_radius; j < cells_in_radius; j++) {
  106. Vector3 from(i * cell_size, 0, j * cell_size);
  107. Vector3 from_i((i + 1) * cell_size, 0, j * cell_size);
  108. Vector3 from_j(i * cell_size, 0, (j + 1) * cell_size);
  109. from = accumulated.xform(from);
  110. from_i = accumulated.xform(from_i);
  111. from_j = accumulated.xform(from_j);
  112. Color c = color, c_i = color, c_j = color;
  113. c.a *= MAX(0, 1.0 - from.length() / radius);
  114. c_i.a *= MAX(0, 1.0 - from_i.length() / radius);
  115. c_j.a *= MAX(0, 1.0 - from_j.length() / radius);
  116. VS::get_singleton()->immediate_color(immediate, c);
  117. VS::get_singleton()->immediate_vertex(immediate, from);
  118. VS::get_singleton()->immediate_color(immediate, c_i);
  119. VS::get_singleton()->immediate_vertex(immediate, from_i);
  120. VS::get_singleton()->immediate_color(immediate, c);
  121. VS::get_singleton()->immediate_vertex(immediate, from);
  122. VS::get_singleton()->immediate_color(immediate, c_j);
  123. VS::get_singleton()->immediate_vertex(immediate, from_j);
  124. }
  125. }
  126. VS::get_singleton()->immediate_end(immediate);
  127. }
  128. }
  129. AABB RootMotionView::get_aabb() const {
  130. return AABB(Vector3(-radius, 0, -radius), Vector3(radius * 2, 0.001, radius * 2));
  131. }
  132. PoolVector<Face3> RootMotionView::get_faces(uint32_t p_usage_flags) const {
  133. return PoolVector<Face3>();
  134. }
  135. void RootMotionView::_bind_methods() {
  136. ClassDB::bind_method(D_METHOD("set_animation_path", "path"), &RootMotionView::set_animation_path);
  137. ClassDB::bind_method(D_METHOD("get_animation_path"), &RootMotionView::get_animation_path);
  138. ClassDB::bind_method(D_METHOD("set_color", "color"), &RootMotionView::set_color);
  139. ClassDB::bind_method(D_METHOD("get_color"), &RootMotionView::get_color);
  140. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &RootMotionView::set_cell_size);
  141. ClassDB::bind_method(D_METHOD("get_cell_size"), &RootMotionView::get_cell_size);
  142. ClassDB::bind_method(D_METHOD("set_radius", "size"), &RootMotionView::set_radius);
  143. ClassDB::bind_method(D_METHOD("get_radius"), &RootMotionView::get_radius);
  144. ClassDB::bind_method(D_METHOD("set_zero_y", "enable"), &RootMotionView::set_zero_y);
  145. ClassDB::bind_method(D_METHOD("get_zero_y"), &RootMotionView::get_zero_y);
  146. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "animation_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationTree"), "set_animation_path", "get_animation_path");
  147. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
  148. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell_size", PROPERTY_HINT_RANGE, "0.1,16,0.01,or_greater"), "set_cell_size", "get_cell_size");
  149. ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_RANGE, "0.1,16,0.01,or_greater"), "set_radius", "get_radius");
  150. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "zero_y"), "set_zero_y", "get_zero_y");
  151. }
  152. RootMotionView::RootMotionView() {
  153. zero_y = true;
  154. radius = 10;
  155. cell_size = 1;
  156. set_process_internal(true);
  157. immediate = VisualServer::get_singleton()->immediate_create();
  158. set_base(immediate);
  159. color = Color(0.5, 0.5, 1.0);
  160. }
  161. RootMotionView::~RootMotionView() {
  162. set_base(RID());
  163. VisualServer::get_singleton()->free(immediate);
  164. }