Debian Codegraph @

editor/scene/3d/gizmos/gpu_particles_collision_3d_gizmo_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* gpu_particles_collision_3d_gizmo_plugin.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 "gpu_particles_collision_3d_gizmo_plugin.h"
32
33 #include "editor/editor_undo_redo_manager.h"
34 #include "editor/scene/3d/gizmos/gizmo_3d_helper.h"
35 #include "editor/scene/3d/node_3d_editor_plugin.h"
36 #include "editor/settings/editor_settings.h"
37 #include "scene/3d/gpu_particles_collision_3d.h"
38
39 GPUParticlesCollision3DGizmoPlugin::GPUParticlesCollision3DGizmoPlugin() {
40 helper.instantiate();
41
42 Color gizmo_color_attractor = EDITOR_GET("editors/3d_gizmos/gizmo_colors/particle_attractor");
43 create_material("shape_material_attractor", gizmo_color_attractor);
44 gizmo_color_attractor.a = 0.15;
45 create_material("shape_material_attractor_internal", gizmo_color_attractor);
46
47 Color gizmo_color_collision = EDITOR_GET("editors/3d_gizmos/gizmo_colors/particle_collision");
48 create_material("shape_material_collision", gizmo_color_collision);
49 gizmo_color_collision.a = 0.15;
50 create_material("shape_material_collision_internal", gizmo_color_collision);
51
52 create_handle_material("handles");
53 }
54
55 bool GPUParticlesCollision3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
56 return (Object::cast_to<GPUParticlesCollision3D>(p_spatial) != nullptr) || (Object::cast_to<GPUParticlesAttractor3D>(p_spatial) != nullptr);
57 }
58
59 String GPUParticlesCollision3DGizmoPlugin::get_gizmo_name() const {
60 return "GPUParticlesCollision3D";
61 }
62
63 int GPUParticlesCollision3DGizmoPlugin::get_priority() const {
64 return -1;
65 }
66
67 String GPUParticlesCollision3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
68 const Node3D *cs = p_gizmo->get_node_3d();
69
70 if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) {
71 return "Radius";
72 }
73
74 if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) {
75 return helper->box_get_handle_name(p_id);
76 }
77
78 return "";
79 }
80
81 Variant GPUParticlesCollision3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
82 const Node3D *cs = p_gizmo->get_node_3d();
83
84 if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) {
85 return p_gizmo->get_node_3d()->call("get_radius");
86 }
87
88 if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) {
89 return Vector3(p_gizmo->get_node_3d()->call("get_size"));
90 }
91
92 return Variant();
93 }
94
95 void GPUParticlesCollision3DGizmoPlugin::begin_handle_action(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) {
96 helper->initialize_handle_action(get_handle_value(p_gizmo, p_id, p_secondary), p_gizmo->get_node_3d()->get_global_transform());
97 }
98
99 void GPUParticlesCollision3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
100 Node3D *sn = p_gizmo->get_node_3d();
101
102 Vector3 sg[2];
103 helper->get_segment(p_camera, p_point, sg);
104
105 if (Object::cast_to<GPUParticlesCollisionSphere3D>(sn) || Object::cast_to<GPUParticlesAttractorSphere3D>(sn)) {
106 Vector3 ra, rb;
107 Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
108 float d = ra.x;
109 if (Node3DEditor::get_singleton()->is_snap_enabled()) {
110 d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
111 }
112
113 if (d < 0.001) {
114 d = 0.001;
115 }
116
117 sn->call("set_radius", d);
118 }
119
120 if (Object::cast_to<GPUParticlesCollisionBox3D>(sn) || Object::cast_to<GPUParticlesAttractorBox3D>(sn) || Object::cast_to<GPUParticlesAttractorVectorField3D>(sn) || Object::cast_to<GPUParticlesCollisionSDF3D>(sn) || Object::cast_to<GPUParticlesCollisionHeightField3D>(sn)) {
121 Vector3 size = sn->call("get_size");
122 Vector3 position;
123 helper->box_set_handle(sg, p_id, size, position);
124 sn->call("set_size", size);
125 sn->set_global_position(position);
126 }
127 }
128
129 void GPUParticlesCollision3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
130 Node3D *sn = p_gizmo->get_node_3d();
131
132 if (Object::cast_to<GPUParticlesCollisionSphere3D>(sn) || Object::cast_to<GPUParticlesAttractorSphere3D>(sn)) {
133 if (p_cancel) {
134 sn->call("set_radius", p_restore);
135 return;
136 }
137
138 EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
139 ur->create_action(TTR("Change Radius"));
140 ur->add_do_method(sn, "set_radius", sn->call("get_radius"));
141 ur->add_undo_method(sn, "set_radius", p_restore);
142 ur->commit_action();
143 }
144
145 if (Object::cast_to<GPUParticlesCollisionBox3D>(sn) || Object::cast_to<GPUParticlesAttractorBox3D>(sn) || Object::cast_to<GPUParticlesAttractorVectorField3D>(sn) || Object::cast_to<GPUParticlesCollisionSDF3D>(sn) || Object::cast_to<GPUParticlesCollisionHeightField3D>(sn)) {
146 helper->box_commit_handle("Change Box Shape Size", p_cancel, sn);
147 }
148 }
149
150 void GPUParticlesCollision3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
151 Node3D *cs = p_gizmo->get_node_3d();
152
153 p_gizmo->clear();
154
155 Ref<Material> material;
156 Ref<Material> material_internal;
157 if (Object::cast_to<GPUParticlesAttractor3D>(cs)) {
158 material = get_material("shape_material_attractor", p_gizmo);
159 material_internal = get_material("shape_material_attractor_internal", p_gizmo);
160 } else {
161 material = get_material("shape_material_collision", p_gizmo);
162 material_internal = get_material("shape_material_collision_internal", p_gizmo);
163 }
164
165 const Ref<Material> handles_material = get_material("handles");
166
167 if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) {
168 float radius = cs->call("get_radius");
169
170 #define PUSH_QUARTER(m_from_x, m_from_y, m_to_x, m_to_y, m_y) \
171 points_ptrw[index++] = Vector3(m_from_x, m_y, m_from_y); \
172 points_ptrw[index++] = Vector3(m_to_x, m_y, m_to_y); \
173 points_ptrw[index++] = Vector3(m_from_x, m_y, -m_from_y); \
174 points_ptrw[index++] = Vector3(m_to_x, m_y, -m_to_y); \
175 points_ptrw[index++] = Vector3(-m_from_x, m_y, m_from_y); \
176 points_ptrw[index++] = Vector3(-m_to_x, m_y, m_to_y); \
177 points_ptrw[index++] = Vector3(-m_from_x, m_y, -m_from_y); \
178 points_ptrw[index++] = Vector3(-m_to_x, m_y, -m_to_y);
179
180 #define PUSH_QUARTER_XY(m_from_x, m_from_y, m_to_x, m_to_y) \
181 points_ptrw[index++] = Vector3(m_from_x, -m_from_y, 0); \
182 points_ptrw[index++] = Vector3(m_to_x, -m_to_y, 0); \
183 points_ptrw[index++] = Vector3(m_from_x, m_from_y, 0); \
184 points_ptrw[index++] = Vector3(m_to_x, m_to_y, 0); \
185 points_ptrw[index++] = Vector3(-m_from_x, -m_from_y, 0); \
186 points_ptrw[index++] = Vector3(-m_to_x, -m_to_y, 0); \
187 points_ptrw[index++] = Vector3(-m_from_x, m_from_y, 0); \
188 points_ptrw[index++] = Vector3(-m_to_x, m_to_y, 0);
189
190 #define PUSH_QUARTER_YZ(m_from_x, m_from_y, m_to_x, m_to_y) \
191 points_ptrw[index++] = Vector3(0, -m_from_y, m_from_x); \
192 points_ptrw[index++] = Vector3(0, -m_to_y, m_to_x); \
193 points_ptrw[index++] = Vector3(0, m_from_y, m_from_x); \
194 points_ptrw[index++] = Vector3(0, m_to_y, m_to_x); \
195 points_ptrw[index++] = Vector3(0, -m_from_y, -m_from_x); \
196 points_ptrw[index++] = Vector3(0, -m_to_y, -m_to_x); \
197 points_ptrw[index++] = Vector3(0, m_from_y, -m_from_x); \
198 points_ptrw[index++] = Vector3(0, m_to_y, -m_to_x);
199
200 // Number of points in an octant. So there will be 8 * points_in_octant points in total.
201 // This corresponds to the smoothness of the circle.
202 const uint32_t points_in_octant = 16;
203 const real_t octant_angle = Math::PI / 4;
204 const real_t inc = (Math::PI / (4 * points_in_octant));
205 const real_t radius_squared = radius * radius;
206 real_t r = 0;
207
208 Vector<Vector3> points;
209 points.resize(3 * 8 * points_in_octant * 2);
210 Vector3 *points_ptrw = points.ptrw();
211
212 uint32_t index = 0;
213 float previous_x = radius;
214 float previous_y = 0.f;
215
216 for (uint32_t i = 0; i < points_in_octant; ++i) {
217 r += inc;
218 real_t x = Math::cos((i == points_in_octant - 1) ? octant_angle : r) * radius;
219 real_t y = Math::sqrt(radius_squared - (x * x));
220
221 PUSH_QUARTER(previous_x, previous_y, x, y, 0);
222 PUSH_QUARTER(previous_y, previous_x, y, x, 0);
223
224 PUSH_QUARTER_XY(previous_x, previous_y, x, y);
225 PUSH_QUARTER_XY(previous_y, previous_x, y, x);
226
227 PUSH_QUARTER_YZ(previous_x, previous_y, x, y);
228 PUSH_QUARTER_YZ(previous_y, previous_x, y, x);
229
230 previous_x = x;
231 previous_y = y;
232 }
233
234 p_gizmo->add_lines(points, material);
235 p_gizmo->add_collision_segments(points);
236 Vector<Vector3> handles;
237 handles.push_back(Vector3(r, 0, 0));
238 p_gizmo->add_handles(handles, handles_material);
239
240 #undef PUSH_QUARTER
241 #undef PUSH_QUARTER_XY
242 #undef PUSH_QUARTER_YZ
243 }
244
245 if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) {
246 Vector<Vector3> lines;
247 AABB aabb;
248 aabb.size = cs->call("get_size").operator Vector3();
249 aabb.position = aabb.size / -2;
250
251 for (int i = 0; i < 12; i++) {
252 Vector3 a, b;
253 aabb.get_edge(i, a, b);
254 lines.push_back(a);
255 lines.push_back(b);
256 }
257
258 Vector<Vector3> handles = helper->box_get_handles(aabb.size);
259
260 p_gizmo->add_lines(lines, material);
261 p_gizmo->add_collision_segments(lines);
262 p_gizmo->add_handles(handles, handles_material);
263
264 GPUParticlesCollisionSDF3D *col_sdf = Object::cast_to<GPUParticlesCollisionSDF3D>(cs);
265 if (col_sdf) {
266 static const int subdivs[GPUParticlesCollisionSDF3D::RESOLUTION_MAX] = { 16, 32, 64, 128, 256, 512 };
267 int subdiv = subdivs[col_sdf->get_resolution()];
268 float cell_size = aabb.get_longest_axis_size() / subdiv;
269
270 lines.clear();
271
272 for (int i = 1; i < subdiv; i++) {
273 for (int j = 0; j < 3; j++) {
274 if (cell_size * i > aabb.size[j]) {
275 continue;
276 }
277
278 int j_n1 = (j + 1) % 3;
279 int j_n2 = (j + 2) % 3;
280
281 for (int k = 0; k < 4; k++) {
282 Vector3 from = aabb.position, to = aabb.position;
283 from[j] += cell_size * i;
284 to[j] += cell_size * i;
285
286 if (k & 1) {
287 to[j_n1] += aabb.size[j_n1];
288 } else {
289 to[j_n2] += aabb.size[j_n2];
290 }
291
292 if (k & 2) {
293 from[j_n1] += aabb.size[j_n1];
294 from[j_n2] += aabb.size[j_n2];
295 }
296
297 lines.push_back(from);
298 lines.push_back(to);
299 }
300 }
301 }
302
303 p_gizmo->add_lines(lines, material_internal);
304 }
305 }
306 }