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