Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************/
2 /* particles_3d_emission_shape_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 "particles_3d_emission_shape_gizmo_plugin.h"
32
33 #include "editor/editor_node.h"
34 #include "editor/editor_string_names.h"
35 #include "editor/editor_undo_redo_manager.h"
36 #include "editor/scene/3d/node_3d_editor_plugin.h"
37 #include "editor/settings/editor_settings.h"
38 #include "scene/3d/cpu_particles_3d.h"
39 #include "scene/3d/gpu_particles_3d.h"
40 #include "scene/resources/3d/primitive_meshes.h"
41 #include "scene/resources/particle_process_material.h"
42
43 Particles3DEmissionShapeGizmoPlugin::Particles3DEmissionShapeGizmoPlugin() {
44 helper.instantiate();
45
46 Color gizmo_color = EDITOR_DEF_RST("editors/3d_gizmos/gizmo_colors/particles_emission_shape", Color(0.5, 0.7, 1));
47 create_material("particles_emission_shape_material", gizmo_color);
48
49 create_handle_material("handles");
50 }
51
52 bool Particles3DEmissionShapeGizmoPlugin::has_gizmo(Node3D *p_spatial) {
53 return Object::cast_to<GPUParticles3D>(p_spatial) || Object::cast_to<CPUParticles3D>(p_spatial) != nullptr;
54 }
55
56 String Particles3DEmissionShapeGizmoPlugin::get_gizmo_name() const {
57 return "Particles3DEmissionShape";
58 }
59
60 int Particles3DEmissionShapeGizmoPlugin::get_priority() const {
61 return -1;
62 }
63
64 bool Particles3DEmissionShapeGizmoPlugin::is_selectable_when_hidden() const {
65 return true;
66 }
67
68 String Particles3DEmissionShapeGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
69 return "";
70 }
71
72 Variant Particles3DEmissionShapeGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
73 return Variant();
74 }
75
76 void Particles3DEmissionShapeGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
77 }
78
79 void Particles3DEmissionShapeGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
80 }
81
82 void Particles3DEmissionShapeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
83 p_gizmo->clear();
84
85 if (Object::cast_to<GPUParticles3D>(p_gizmo->get_node_3d())) {
86 const GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_node_3d());
87
88 const Ref<ParticleProcessMaterial> mat = particles->get_process_material();
89 if (mat.is_valid()) {
90 const ParticleProcessMaterial::EmissionShape shape = mat->get_emission_shape();
91
92 const Ref<Material> material = get_material("particles_emission_shape_material", p_gizmo);
93 const Ref<Material> handles_material = get_material("handles");
94
95 if (shape == ParticleProcessMaterial::EMISSION_SHAPE_SPHERE || shape == ParticleProcessMaterial::EMISSION_SHAPE_SPHERE_SURFACE) {
96 const Vector3 offset = mat->get_emission_shape_offset();
97 const Vector3 scale = mat->get_emission_shape_scale();
98
99 const float r = mat->get_emission_sphere_radius();
100 Vector<Vector3> points;
101 for (int i = 0; i <= 120; i++) {
102 const float ra = Math::deg_to_rad((float)(i * 3));
103 const float rb = Math::deg_to_rad((float)((i + 1) * 3));
104 const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
105 const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
106
107 points.push_back(Vector3(a.x * scale.x + offset.x, offset.y, a.y * scale.z + offset.z));
108 points.push_back(Vector3(b.x * scale.x + offset.x, offset.y, b.y * scale.z + offset.z));
109 points.push_back(Vector3(offset.x, a.x * scale.y + offset.y, a.y * scale.z + offset.z));
110 points.push_back(Vector3(offset.x, b.x * scale.y + offset.y, b.y * scale.z + offset.z));
111 points.push_back(Vector3(a.x * scale.x + offset.x, a.y * scale.y + offset.y, offset.z));
112 points.push_back(Vector3(b.x * scale.x + offset.x, b.y * scale.y + offset.y, offset.z));
113 }
114
115 if (p_gizmo->is_selected()) {
116 p_gizmo->add_lines(points, material);
117 }
118 } else if (shape == ParticleProcessMaterial::EMISSION_SHAPE_BOX) {
119 const Vector3 offset = mat->get_emission_shape_offset();
120 const Vector3 scale = mat->get_emission_shape_scale();
121
122 const Vector3 box_extents = mat->get_emission_box_extents();
123 Ref<BoxMesh> box;
124 box.instantiate();
125 const AABB box_aabb = box->get_aabb();
126 Vector<Vector3> lines;
127
128 for (int i = 0; i < 12; i++) {
129 Vector3 a;
130 Vector3 b;
131 box_aabb.get_edge(i, a, b);
132 // Multiplication by 2 due to the extents being only half of the box size.
133 lines.push_back(a * 2.0 * scale * box_extents + offset);
134 lines.push_back(b * 2.0 * scale * box_extents + offset);
135 }
136
137 if (p_gizmo->is_selected()) {
138 p_gizmo->add_lines(lines, material);
139 }
140 } else if (shape == ParticleProcessMaterial::EMISSION_SHAPE_RING) {
141 const Vector3 offset = mat->get_emission_shape_offset();
142 const Vector3 scale = mat->get_emission_shape_scale();
143
144 const float ring_height = mat->get_emission_ring_height();
145 const float half_ring_height = ring_height / 2;
146 const float ring_radius = mat->get_emission_ring_radius();
147 const float ring_inner_radius = mat->get_emission_ring_inner_radius();
148 const Vector3 ring_axis = mat->get_emission_ring_axis();
149 const float ring_cone_angle = mat->get_emission_ring_cone_angle();
150 const float ring_radius_top = MAX(ring_radius - Math::tan(Math::deg_to_rad(90.0 - ring_cone_angle)) * ring_height, 0.0);
151 const float ring_inner_radius_top = (ring_inner_radius / ring_radius) * ring_radius_top;
152
153 Vector<Vector3> points;
154
155 Basis basis;
156 basis.rows[1] = ring_axis.normalized();
157 basis.rows[0] = Vector3(basis[1][1], -basis[1][2], -basis[1][0]).normalized();
158 basis.rows[0] = (basis[0] - basis[0].dot(basis[1]) * basis[1]).normalized();
159 basis[2] = basis[0].cross(basis[1]).normalized();
160 basis.invert();
161
162 for (int i = 0; i <= 120; i++) {
163 const float ra = Math::deg_to_rad((float)(i * 3));
164 const float ra_sin = Math::sin(ra);
165 const float ra_cos = Math::cos(ra);
166 const float rb = Math::deg_to_rad((float)((i + 1) * 3));
167 const float rb_sin = Math::sin(rb);
168 const float rb_cos = Math::cos(rb);
169 const Point2 a = Vector2(ra_sin, ra_cos) * ring_radius;
170 const Point2 b = Vector2(rb_sin, rb_cos) * ring_radius;
171 const Point2 a2 = Vector2(ra_sin, ra_cos) * ring_radius_top;
172 const Point2 b2 = Vector2(rb_sin, rb_cos) * ring_radius_top;
173 const Point2 inner_a = Vector2(ra_sin, ra_cos) * ring_inner_radius;
174 const Point2 inner_b = Vector2(rb_sin, rb_cos) * ring_inner_radius;
175 const Point2 inner_a2 = Vector2(ra_sin, ra_cos) * ring_inner_radius_top;
176 const Point2 inner_b2 = Vector2(rb_sin, rb_cos) * ring_inner_radius_top;
177
178 // Outer top ring cap.
179 points.push_back(basis.xform(Vector3(a2.x, half_ring_height, a2.y)) * scale + offset);
180 points.push_back(basis.xform(Vector3(b2.x, half_ring_height, b2.y)) * scale + offset);
181
182 // Outer bottom ring cap.
183 points.push_back(basis.xform(Vector3(a.x, -half_ring_height, a.y)) * scale + offset);
184 points.push_back(basis.xform(Vector3(b.x, -half_ring_height, b.y)) * scale + offset);
185
186 // Inner top ring cap.
187 points.push_back(basis.xform(Vector3(inner_a2.x, half_ring_height, inner_a2.y)) * scale + offset);
188 points.push_back(basis.xform(Vector3(inner_b2.x, half_ring_height, inner_b2.y)) * scale + offset);
189
190 // Inner bottom ring cap.
191 points.push_back(basis.xform(Vector3(inner_a.x, -half_ring_height, inner_a.y)) * scale + offset);
192 points.push_back(basis.xform(Vector3(inner_b.x, -half_ring_height, inner_b.y)) * scale + offset);
193 }
194
195 for (int i = 0; i <= 120; i = i + 30) {
196 const float ra = Math::deg_to_rad((float)(i * 3));
197 const float ra_sin = Math::sin(ra);
198 const float ra_cos = Math::cos(ra);
199 const Point2 a = Vector2(ra_sin, ra_cos) * ring_radius;
200 const Point2 a2 = Vector2(ra_sin, ra_cos) * ring_radius_top;
201 const Point2 inner_a = Vector2(ra_sin, ra_cos) * ring_inner_radius;
202 const Point2 inner_a2 = Vector2(ra_sin, ra_cos) * ring_inner_radius_top;
203
204 // Outer 90 degrees vertical lines.
205 points.push_back(basis.xform(Vector3(a2.x, half_ring_height, a2.y)) * scale + offset);
206 points.push_back(basis.xform(Vector3(a.x, -half_ring_height, a.y)) * scale + offset);
207
208 // Inner 90 degrees vertical lines.
209 points.push_back(basis.xform(Vector3(inner_a2.x, half_ring_height, inner_a2.y)) * scale + offset);
210 points.push_back(basis.xform(Vector3(inner_a.x, -half_ring_height, inner_a.y)) * scale + offset);
211 }
212
213 if (p_gizmo->is_selected()) {
214 p_gizmo->add_lines(points, material);
215 }
216 }
217 }
218 } else if (Object::cast_to<CPUParticles3D>(p_gizmo->get_node_3d())) {
219 const CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(p_gizmo->get_node_3d());
220 const CPUParticles3D::EmissionShape shape = particles->get_emission_shape();
221
222 const Ref<Material> material = get_material("particles_emission_shape_material", p_gizmo);
223 const Ref<Material> handles_material = get_material("handles");
224
225 if (shape == CPUParticles3D::EMISSION_SHAPE_SPHERE || shape == CPUParticles3D::EMISSION_SHAPE_SPHERE_SURFACE) {
226 const float r = particles->get_emission_sphere_radius();
227 Vector<Vector3> points;
228 for (int i = 0; i <= 120; i++) {
229 const float ra = Math::deg_to_rad((float)(i * 3));
230 const float rb = Math::deg_to_rad((float)((i + 1) * 3));
231 const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
232 const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
233
234 points.push_back(Vector3(a.x, 0.0, a.y));
235 points.push_back(Vector3(b.x, 0.0, b.y));
236 points.push_back(Vector3(0.0, a.x, a.y));
237 points.push_back(Vector3(0.0, b.x, b.y));
238 points.push_back(Vector3(a.x, a.y, 0.0));
239 points.push_back(Vector3(b.x, b.y, 0.0));
240 }
241
242 if (p_gizmo->is_selected()) {
243 p_gizmo->add_lines(points, material);
244 }
245 } else if (shape == CPUParticles3D::EMISSION_SHAPE_BOX) {
246 const Vector3 box_extents = particles->get_emission_box_extents();
247 Ref<BoxMesh> box;
248 box.instantiate();
249 const AABB box_aabb = box->get_aabb();
250 Vector<Vector3> lines;
251
252 for (int i = 0; i < 12; i++) {
253 Vector3 a;
254 Vector3 b;
255 box_aabb.get_edge(i, a, b);
256 // Multiplication by 2 due to the extents being only half of the box size.
257 lines.push_back(a * 2.0 * box_extents);
258 lines.push_back(b * 2.0 * box_extents);
259 }
260
261 if (p_gizmo->is_selected()) {
262 p_gizmo->add_lines(lines, material);
263 }
264 } else if (shape == CPUParticles3D::EMISSION_SHAPE_RING) {
265 const float ring_height = particles->get_emission_ring_height();
266 const float half_ring_height = ring_height / 2;
267 const float ring_radius = particles->get_emission_ring_radius();
268 const float ring_inner_radius = particles->get_emission_ring_inner_radius();
269 const Vector3 ring_axis = particles->get_emission_ring_axis();
270 const float ring_cone_angle = particles->get_emission_ring_cone_angle();
271 const float ring_radius_top = MAX(ring_radius - Math::tan(Math::deg_to_rad(90.0 - ring_cone_angle)) * ring_height, 0.0);
272 const float ring_inner_radius_top = (ring_inner_radius / ring_radius) * ring_radius_top;
273
274 Vector<Vector3> points;
275
276 Basis basis;
277 basis.rows[1] = ring_axis.normalized();
278 basis.rows[0] = Vector3(basis[1][1], -basis[1][2], -basis[1][0]).normalized();
279 basis.rows[0] = (basis[0] - basis[0].dot(basis[1]) * basis[1]).normalized();
280 basis[2] = basis[0].cross(basis[1]).normalized();
281 basis.invert();
282
283 for (int i = 0; i <= 120; i++) {
284 const float ra = Math::deg_to_rad((float)(i * 3));
285 const float ra_sin = Math::sin(ra);
286 const float ra_cos = Math::cos(ra);
287 const float rb = Math::deg_to_rad((float)((i + 1) * 3));
288 const float rb_sin = Math::sin(rb);
289 const float rb_cos = Math::cos(rb);
290 const Point2 a = Vector2(ra_sin, ra_cos) * ring_radius;
291 const Point2 b = Vector2(rb_sin, rb_cos) * ring_radius;
292 const Point2 a2 = Vector2(ra_sin, ra_cos) * ring_radius_top;
293 const Point2 b2 = Vector2(rb_sin, rb_cos) * ring_radius_top;
294 const Point2 inner_a = Vector2(ra_sin, ra_cos) * ring_inner_radius;
295 const Point2 inner_b = Vector2(rb_sin, rb_cos) * ring_inner_radius;
296 const Point2 inner_a2 = Vector2(ra_sin, ra_cos) * ring_inner_radius_top;
297 const Point2 inner_b2 = Vector2(rb_sin, rb_cos) * ring_inner_radius_top;
298
299 // Outer top ring cap.
300 points.push_back(basis.xform(Vector3(a2.x, half_ring_height, a2.y)));
301 points.push_back(basis.xform(Vector3(b2.x, half_ring_height, b2.y)));
302
303 // Outer bottom ring cap.
304 points.push_back(basis.xform(Vector3(a.x, -half_ring_height, a.y)));
305 points.push_back(basis.xform(Vector3(b.x, -half_ring_height, b.y)));
306
307 // Inner top ring cap.
308 points.push_back(basis.xform(Vector3(inner_a2.x, half_ring_height, inner_a2.y)));
309 points.push_back(basis.xform(Vector3(inner_b2.x, half_ring_height, inner_b2.y)));
310
311 // Inner bottom ring cap.
312 points.push_back(basis.xform(Vector3(inner_a.x, -half_ring_height, inner_a.y)));
313 points.push_back(basis.xform(Vector3(inner_b.x, -half_ring_height, inner_b.y)));
314 }
315
316 for (int i = 0; i <= 120; i = i + 30) {
317 const float ra = Math::deg_to_rad((float)(i * 3));
318 const float ra_sin = Math::sin(ra);
319 const float ra_cos = Math::cos(ra);
320 const Point2 a = Vector2(ra_sin, ra_cos) * ring_radius;
321 const Point2 a2 = Vector2(ra_sin, ra_cos) * ring_radius_top;
322 const Point2 inner_a = Vector2(ra_sin, ra_cos) * ring_inner_radius;
323 const Point2 inner_a2 = Vector2(ra_sin, ra_cos) * ring_inner_radius_top;
324
325 // Outer 90 degrees vertical lines.
326 points.push_back(basis.xform(Vector3(a2.x, half_ring_height, a2.y)));
327 points.push_back(basis.xform(Vector3(a.x, -half_ring_height, a.y)));
328
329 // Inner 90 degrees vertical lines.
330 points.push_back(basis.xform(Vector3(inner_a2.x, half_ring_height, inner_a2.y)));
331 points.push_back(basis.xform(Vector3(inner_a.x, -half_ring_height, inner_a.y)));
332 }
333
334 if (p_gizmo->is_selected()) {
335 p_gizmo->add_lines(points, material);
336 }
337 }
338 }
339 }