| 1 | /**************************************************************************/ |
| 2 | /* root_motion_view.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 | #ifndef _3D_DISABLED |
| 32 | |
| 33 | #include "root_motion_view.h" |
| 34 | |
| 35 | #include "scene/animation/animation_mixer.h" |
| 36 | #include "scene/resources/material.h" |
| 37 | |
| 38 | void RootMotionView::set_animation_mixer(const NodePath &p_path) { |
| 39 | path = p_path; |
| 40 | first = true; |
| 41 | } |
| 42 | |
| 43 | NodePath RootMotionView::get_animation_mixer() const { |
| 44 | return path; |
| 45 | } |
| 46 | |
| 47 | void RootMotionView::set_color(const Color &p_color) { |
| 48 | color = p_color; |
| 49 | first = true; |
| 50 | } |
| 51 | |
| 52 | Color RootMotionView::get_color() const { |
| 53 | return color; |
| 54 | } |
| 55 | |
| 56 | void RootMotionView::set_cell_size(float p_size) { |
| 57 | cell_size = p_size; |
| 58 | first = true; |
| 59 | } |
| 60 | |
| 61 | float RootMotionView::get_cell_size() const { |
| 62 | return cell_size; |
| 63 | } |
| 64 | |
| 65 | void RootMotionView::set_radius(float p_radius) { |
| 66 | radius = p_radius; |
| 67 | first = true; |
| 68 | } |
| 69 | |
| 70 | float RootMotionView::get_radius() const { |
| 71 | return radius; |
| 72 | } |
| 73 | |
| 74 | void RootMotionView::set_zero_y(bool p_zero_y) { |
| 75 | zero_y = p_zero_y; |
| 76 | } |
| 77 | |
| 78 | bool RootMotionView::get_zero_y() const { |
| 79 | return zero_y; |
| 80 | } |
| 81 | |
| 82 | void RootMotionView::_notification(int p_what) { |
| 83 | switch (p_what) { |
| 84 | case NOTIFICATION_ENTER_TREE: { |
| 85 | immediate_material = StandardMaterial3D::get_material_for_2d(false, BaseMaterial3D::TRANSPARENCY_ALPHA, false); |
| 86 | |
| 87 | first = true; |
| 88 | } break; |
| 89 | |
| 90 | case NOTIFICATION_INTERNAL_PROCESS: |
| 91 | case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { |
| 92 | Transform3D transform; |
| 93 | Basis diff; |
| 94 | |
| 95 | if (has_node(path)) { |
| 96 | Node *node = get_node(path); |
| 97 | AnimationMixer *mixer = Object::cast_to<AnimationMixer>(node); |
| 98 | if (mixer && mixer->is_active() && mixer->get_root_motion_track() != NodePath()) { |
| 99 | if (is_processing_internal() && mixer->get_callback_mode_process() == AnimationMixer::ANIMATION_CALLBACK_MODE_PROCESS_PHYSICS) { |
| 100 | set_process_internal(false); |
| 101 | set_physics_process_internal(true); |
| 102 | } |
| 103 | |
| 104 | if (is_physics_processing_internal() && mixer->get_callback_mode_process() == AnimationMixer::ANIMATION_CALLBACK_MODE_PROCESS_IDLE) { |
| 105 | set_process_internal(true); |
| 106 | set_physics_process_internal(false); |
| 107 | } |
| 108 | |
| 109 | transform.origin = mixer->get_root_motion_position(); |
| 110 | transform.basis = mixer->get_root_motion_rotation(); // Scale is meaningless. |
| 111 | diff = mixer->is_root_motion_local() ? Quaternion() : mixer->get_root_motion_rotation_accumulator(); |
| 112 | } |
| 113 | } |
| 114 | if (!first && transform == Transform3D()) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | first = false; |
| 119 | |
| 120 | accumulated.basis *= transform.basis; |
| 121 | transform.origin = (diff.inverse() * accumulated.basis).xform(transform.origin); |
| 122 | accumulated.origin += transform.origin; |
| 123 | |
| 124 | accumulated.origin.x = Math::fposmod(accumulated.origin.x, cell_size); |
| 125 | if (zero_y) { |
| 126 | accumulated.origin.y = 0; |
| 127 | } |
| 128 | accumulated.origin.z = Math::fposmod(accumulated.origin.z, cell_size); |
| 129 | |
| 130 | immediate->clear_surfaces(); |
| 131 | |
| 132 | int cells_in_radius = int((radius / cell_size) + 1.0); |
| 133 | |
| 134 | immediate->surface_begin(Mesh::PRIMITIVE_LINES, immediate_material); |
| 135 | |
| 136 | for (int i = -cells_in_radius; i < cells_in_radius; i++) { |
| 137 | for (int j = -cells_in_radius; j < cells_in_radius; j++) { |
| 138 | Vector3 from(i * cell_size, 0, j * cell_size); |
| 139 | Vector3 from_i((i + 1) * cell_size, 0, j * cell_size); |
| 140 | Vector3 from_j(i * cell_size, 0, (j + 1) * cell_size); |
| 141 | from = accumulated.xform_inv(from); |
| 142 | from_i = accumulated.xform_inv(from_i); |
| 143 | from_j = accumulated.xform_inv(from_j); |
| 144 | |
| 145 | Color c = color, c_i = color, c_j = color; |
| 146 | c.a *= MAX(0, 1.0 - from.length() / radius); |
| 147 | c_i.a *= MAX(0, 1.0 - from_i.length() / radius); |
| 148 | c_j.a *= MAX(0, 1.0 - from_j.length() / radius); |
| 149 | |
| 150 | immediate->surface_set_color(c); |
| 151 | immediate->surface_add_vertex(from); |
| 152 | |
| 153 | immediate->surface_set_color(c_i); |
| 154 | immediate->surface_add_vertex(from_i); |
| 155 | |
| 156 | immediate->surface_set_color(c); |
| 157 | immediate->surface_add_vertex(from); |
| 158 | |
| 159 | immediate->surface_set_color(c_j); |
| 160 | immediate->surface_add_vertex(from_j); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | immediate->surface_end(); |
| 165 | } break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | AABB RootMotionView::get_aabb() const { |
| 170 | return AABB(Vector3(-radius, 0, -radius), Vector3(radius * 2, 0.001, radius * 2)); |
| 171 | } |
| 172 | |
| 173 | void RootMotionView::_bind_methods() { |
| 174 | ClassDB::bind_method(D_METHOD("set_animation_path", "path"), &RootMotionView::set_animation_mixer); |
| 175 | ClassDB::bind_method(D_METHOD("get_animation_path"), &RootMotionView::get_animation_mixer); |
| 176 | |
| 177 | ClassDB::bind_method(D_METHOD("set_color", "color"), &RootMotionView::set_color); |
| 178 | ClassDB::bind_method(D_METHOD("get_color"), &RootMotionView::get_color); |
| 179 | |
| 180 | ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &RootMotionView::set_cell_size); |
| 181 | ClassDB::bind_method(D_METHOD("get_cell_size"), &RootMotionView::get_cell_size); |
| 182 | |
| 183 | ClassDB::bind_method(D_METHOD("set_radius", "size"), &RootMotionView::set_radius); |
| 184 | ClassDB::bind_method(D_METHOD("get_radius"), &RootMotionView::get_radius); |
| 185 | |
| 186 | ClassDB::bind_method(D_METHOD("set_zero_y", "enable"), &RootMotionView::set_zero_y); |
| 187 | ClassDB::bind_method(D_METHOD("get_zero_y"), &RootMotionView::get_zero_y); |
| 188 | |
| 189 | ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "animation_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationMixer"), "set_animation_path", "get_animation_path"); |
| 190 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); |
| 191 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_size", PROPERTY_HINT_RANGE, "0.1,16,0.01,or_greater,suffix:m"), "set_cell_size", "get_cell_size"); |
| 192 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.1,16,0.01,or_greater,suffix:m"), "set_radius", "get_radius"); |
| 193 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "zero_y"), "set_zero_y", "get_zero_y"); |
| 194 | } |
| 195 | |
| 196 | RootMotionView::RootMotionView() { |
| 197 | if (Engine::get_singleton()->is_editor_hint()) { |
| 198 | set_process_internal(true); |
| 199 | } |
| 200 | immediate.instantiate(); |
| 201 | set_base(immediate->get_rid()); |
| 202 | } |
| 203 | |
| 204 | RootMotionView::~RootMotionView() { |
| 205 | set_base(RID()); |
| 206 | } |
| 207 | |
| 208 | #endif // _3D_DISABLED |