1 /*************************************************************************/
2 /* particles_editor_plugin.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 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
31 #include "particles_editor_plugin.h"
32
33 #include "core/io/resource_loader.h"
34 #include "editor/plugins/spatial_editor_plugin.h"
35 #include "scene/3d/cpu_particles.h"
36 #include "scene/resources/particles_material.h"
37
_generate(PoolVector<Vector3> & points,PoolVector<Vector3> & normals)38 bool ParticlesEditorBase::_generate(PoolVector<Vector3> &points, PoolVector<Vector3> &normals) {
39
40 bool use_normals = emission_fill->get_selected() == 1;
41
42 if (emission_fill->get_selected() < 2) {
43
44 float area_accum = 0;
45 Map<float, int> triangle_area_map;
46
47 for (int i = 0; i < geometry.size(); i++) {
48
49 float area = geometry[i].get_area();
50 if (area < CMP_EPSILON)
51 continue;
52 triangle_area_map[area_accum] = i;
53 area_accum += area;
54 }
55
56 if (!triangle_area_map.size() || area_accum == 0) {
57
58 EditorNode::get_singleton()->show_warning(TTR("The geometry's faces don't contain any area."));
59 return false;
60 }
61
62 int emissor_count = emission_amount->get_value();
63
64 for (int i = 0; i < emissor_count; i++) {
65
66 float areapos = Math::random(0.0f, area_accum);
67
68 Map<float, int>::Element *E = triangle_area_map.find_closest(areapos);
69 ERR_FAIL_COND_V(!E, false);
70 int index = E->get();
71 ERR_FAIL_INDEX_V(index, geometry.size(), false);
72
73 // ok FINALLY get face
74 Face3 face = geometry[index];
75 //now compute some position inside the face...
76
77 Vector3 pos = face.get_random_point_inside();
78
79 points.push_back(pos);
80
81 if (use_normals) {
82 Vector3 normal = face.get_plane().normal;
83 normals.push_back(normal);
84 }
85 }
86 } else {
87
88 int gcount = geometry.size();
89
90 if (gcount == 0) {
91
92 EditorNode::get_singleton()->show_warning(TTR("The geometry doesn't contain any faces."));
93 return false;
94 }
95
96 PoolVector<Face3>::Read r = geometry.read();
97
98 AABB aabb;
99
100 for (int i = 0; i < gcount; i++) {
101
102 for (int j = 0; j < 3; j++) {
103
104 if (i == 0 && j == 0)
105 aabb.position = r[i].vertex[j];
106 else
107 aabb.expand_to(r[i].vertex[j]);
108 }
109 }
110
111 int emissor_count = emission_amount->get_value();
112
113 for (int i = 0; i < emissor_count; i++) {
114
115 int attempts = 5;
116
117 for (int j = 0; j < attempts; j++) {
118
119 Vector3 dir;
120 dir[Math::rand() % 3] = 1.0;
121 Vector3 ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size + aabb.position;
122
123 Vector3 ofsv = ofs + aabb.size * dir;
124
125 //space it a little
126 ofs -= dir;
127 ofsv += dir;
128
129 float max = -1e7, min = 1e7;
130
131 for (int k = 0; k < gcount; k++) {
132
133 const Face3 &f3 = r[k];
134
135 Vector3 res;
136 if (f3.intersects_segment(ofs, ofsv, &res)) {
137
138 res -= ofs;
139 float d = dir.dot(res);
140
141 if (d < min)
142 min = d;
143 if (d > max)
144 max = d;
145 }
146 }
147
148 if (max < min)
149 continue; //lost attempt
150
151 float val = min + (max - min) * Math::randf();
152
153 Vector3 point = ofs + dir * val;
154
155 points.push_back(point);
156 break;
157 }
158 }
159 }
160
161 return true;
162 }
163
_node_selected(const NodePath & p_path)164 void ParticlesEditorBase::_node_selected(const NodePath &p_path) {
165
166 Node *sel = get_node(p_path);
167 if (!sel)
168 return;
169
170 if (!sel->is_class("Spatial")) {
171
172 EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't inherit from Spatial."), sel->get_name()));
173 return;
174 }
175
176 VisualInstance *vi = Object::cast_to<VisualInstance>(sel);
177 if (!vi) {
178
179 EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain geometry."), sel->get_name()));
180 return;
181 }
182
183 geometry = vi->get_faces(VisualInstance::FACES_SOLID);
184
185 if (geometry.size() == 0) {
186
187 EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain face geometry."), sel->get_name()));
188 return;
189 }
190
191 Transform geom_xform = base_node->get_global_transform().affine_inverse() * vi->get_global_transform();
192
193 int gc = geometry.size();
194 PoolVector<Face3>::Write w = geometry.write();
195
196 for (int i = 0; i < gc; i++) {
197 for (int j = 0; j < 3; j++) {
198 w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
199 }
200 }
201
202 w.release();
203
204 emission_dialog->popup_centered(Size2(300, 130));
205 }
206
_bind_methods()207 void ParticlesEditorBase::_bind_methods() {
208
209 ClassDB::bind_method("_node_selected", &ParticlesEditorBase::_node_selected);
210 ClassDB::bind_method("_generate_emission_points", &ParticlesEditorBase::_generate_emission_points);
211 }
212
ParticlesEditorBase()213 ParticlesEditorBase::ParticlesEditorBase() {
214
215 emission_dialog = memnew(ConfirmationDialog);
216 emission_dialog->set_title(TTR("Create Emitter"));
217 add_child(emission_dialog);
218 VBoxContainer *emd_vb = memnew(VBoxContainer);
219 emission_dialog->add_child(emd_vb);
220
221 emission_amount = memnew(SpinBox);
222 emission_amount->set_min(1);
223 emission_amount->set_max(100000);
224 emission_amount->set_value(512);
225 emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount);
226
227 emission_fill = memnew(OptionButton);
228 emission_fill->add_item(TTR("Surface Points"));
229 emission_fill->add_item(TTR("Surface Points+Normal (Directed)"));
230 emission_fill->add_item(TTR("Volume"));
231 emd_vb->add_margin_child(TTR("Emission Source: "), emission_fill);
232
233 emission_dialog->get_ok()->set_text(TTR("Create"));
234 emission_dialog->connect("confirmed", this, "_generate_emission_points");
235
236 emission_file_dialog = memnew(EditorFileDialog);
237 add_child(emission_file_dialog);
238 emission_file_dialog->connect("file_selected", this, "_resource_seleted");
239 emission_tree_dialog = memnew(SceneTreeDialog);
240 add_child(emission_tree_dialog);
241 emission_tree_dialog->connect("selected", this, "_node_selected");
242
243 List<String> extensions;
244 ResourceLoader::get_recognized_extensions_for_type("Mesh", &extensions);
245
246 emission_file_dialog->clear_filters();
247 for (int i = 0; i < extensions.size(); i++) {
248
249 emission_file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
250 }
251
252 emission_file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
253 }
254
_node_removed(Node * p_node)255 void ParticlesEditor::_node_removed(Node *p_node) {
256
257 if (p_node == node) {
258 node = NULL;
259 hide();
260 }
261 }
262
_notification(int p_notification)263 void ParticlesEditor::_notification(int p_notification) {
264
265 if (p_notification == NOTIFICATION_ENTER_TREE) {
266 options->set_icon(options->get_popup()->get_icon("Particles", "EditorIcons"));
267 get_tree()->connect("node_removed", this, "_node_removed");
268 }
269 }
270
_menu_option(int p_option)271 void ParticlesEditor::_menu_option(int p_option) {
272
273 switch (p_option) {
274
275 case MENU_OPTION_GENERATE_AABB: {
276 float gen_time = node->get_lifetime();
277
278 if (gen_time < 1.0)
279 generate_seconds->set_value(1.0);
280 else
281 generate_seconds->set_value(trunc(gen_time) + 1.0);
282 generate_aabb->popup_centered_minsize();
283 } break;
284 case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH: {
285
286 Ref<ParticlesMaterial> material = node->get_process_material();
287 if (material.is_null()) {
288 EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
289 return;
290 }
291 emission_file_dialog->popup_centered_ratio();
292
293 } break;
294
295 case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
296 Ref<ParticlesMaterial> material = node->get_process_material();
297 if (material.is_null()) {
298 EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
299 return;
300 }
301
302 emission_tree_dialog->popup_centered_ratio();
303
304 } break;
305 case MENU_OPTION_CONVERT_TO_CPU_PARTICLES: {
306
307 CPUParticles *cpu_particles = memnew(CPUParticles);
308 cpu_particles->convert_from_particles(node);
309 cpu_particles->set_name(node->get_name());
310 cpu_particles->set_transform(node->get_transform());
311 cpu_particles->set_visible(node->is_visible());
312 cpu_particles->set_pause_mode(node->get_pause_mode());
313
314 UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
315 ur->create_action(TTR("Convert to CPUParticles"));
316 ur->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", node, cpu_particles, true, false);
317 ur->add_do_reference(cpu_particles);
318 ur->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", cpu_particles, node, false, false);
319 ur->add_undo_reference(node);
320 ur->commit_action();
321
322 } break;
323 case MENU_OPTION_RESTART: {
324
325 node->restart();
326
327 } break;
328 }
329 }
330
_generate_aabb()331 void ParticlesEditor::_generate_aabb() {
332
333 float time = generate_seconds->get_value();
334
335 float running = 0.0;
336
337 EditorProgress ep("gen_aabb", TTR("Generating AABB"), int(time));
338
339 bool was_emitting = node->is_emitting();
340 if (!was_emitting) {
341 node->set_emitting(true);
342 OS::get_singleton()->delay_usec(1000);
343 }
344
345 AABB rect;
346
347 while (running < time) {
348
349 uint64_t ticks = OS::get_singleton()->get_ticks_usec();
350 ep.step("Generating...", int(running), true);
351 OS::get_singleton()->delay_usec(1000);
352
353 AABB capture = node->capture_aabb();
354 if (rect == AABB())
355 rect = capture;
356 else
357 rect.merge_with(capture);
358
359 running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
360 }
361
362 if (!was_emitting) {
363 node->set_emitting(false);
364 }
365
366 UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
367 ur->create_action(TTR("Generate Visibility AABB"));
368 ur->add_do_method(node, "set_visibility_aabb", rect);
369 ur->add_undo_method(node, "set_visibility_aabb", node->get_visibility_aabb());
370 ur->commit_action();
371 }
372
edit(Particles * p_particles)373 void ParticlesEditor::edit(Particles *p_particles) {
374
375 base_node = p_particles;
376 node = p_particles;
377 }
378
_generate_emission_points()379 void ParticlesEditor::_generate_emission_points() {
380
381 /// hacer codigo aca
382 PoolVector<Vector3> points;
383 PoolVector<Vector3> normals;
384
385 if (!_generate(points, normals)) {
386 return;
387 }
388
389 int point_count = points.size();
390
391 int w = 2048;
392 int h = (point_count / 2048) + 1;
393
394 PoolVector<uint8_t> point_img;
395 point_img.resize(w * h * 3 * sizeof(float));
396
397 {
398 PoolVector<uint8_t>::Write iw = point_img.write();
399 zeromem(iw.ptr(), w * h * 3 * sizeof(float));
400 PoolVector<Vector3>::Read r = points.read();
401 float *wf = (float *)iw.ptr();
402 for (int i = 0; i < point_count; i++) {
403 wf[i * 3 + 0] = r[i].x;
404 wf[i * 3 + 1] = r[i].y;
405 wf[i * 3 + 2] = r[i].z;
406 }
407 }
408
409 Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));
410
411 Ref<ImageTexture> tex;
412 tex.instance();
413 tex->create_from_image(image, Texture::FLAG_FILTER);
414
415 Ref<ParticlesMaterial> material = node->get_process_material();
416 ERR_FAIL_COND(material.is_null());
417
418 if (normals.size() > 0) {
419
420 material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
421 material->set_emission_point_count(point_count);
422 material->set_emission_point_texture(tex);
423
424 PoolVector<uint8_t> point_img2;
425 point_img2.resize(w * h * 3 * sizeof(float));
426
427 {
428 PoolVector<uint8_t>::Write iw = point_img2.write();
429 zeromem(iw.ptr(), w * h * 3 * sizeof(float));
430 PoolVector<Vector3>::Read r = normals.read();
431 float *wf = (float *)iw.ptr();
432 for (int i = 0; i < point_count; i++) {
433 wf[i * 3 + 0] = r[i].x;
434 wf[i * 3 + 1] = r[i].y;
435 wf[i * 3 + 2] = r[i].z;
436 }
437 }
438
439 Ref<Image> image2 = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img2));
440
441 Ref<ImageTexture> tex2;
442 tex2.instance();
443 tex2->create_from_image(image2, Texture::FLAG_FILTER);
444
445 material->set_emission_normal_texture(tex2);
446 } else {
447
448 material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_POINTS);
449 material->set_emission_point_count(point_count);
450 material->set_emission_point_texture(tex);
451 }
452 }
453
_bind_methods()454 void ParticlesEditor::_bind_methods() {
455
456 ClassDB::bind_method("_menu_option", &ParticlesEditor::_menu_option);
457 ClassDB::bind_method("_generate_aabb", &ParticlesEditor::_generate_aabb);
458 ClassDB::bind_method("_node_removed", &ParticlesEditor::_node_removed);
459 }
460
ParticlesEditor()461 ParticlesEditor::ParticlesEditor() {
462
463 node = NULL;
464 particles_editor_hb = memnew(HBoxContainer);
465 SpatialEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb);
466 options = memnew(MenuButton);
467 options->set_switch_on_hover(true);
468 particles_editor_hb->add_child(options);
469 particles_editor_hb->hide();
470
471 options->set_text(TTR("Particles"));
472 options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
473 options->get_popup()->add_separator();
474 options->get_popup()->add_item(TTR("Create Emission Points From Mesh"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH);
475 options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
476 options->get_popup()->add_separator();
477 options->get_popup()->add_item(TTR("Convert to CPUParticles"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES);
478 options->get_popup()->add_separator();
479 options->get_popup()->add_item(TTR("Restart"), MENU_OPTION_RESTART);
480
481 options->get_popup()->connect("id_pressed", this, "_menu_option");
482
483 generate_aabb = memnew(ConfirmationDialog);
484 generate_aabb->set_title(TTR("Generate Visibility AABB"));
485 VBoxContainer *genvb = memnew(VBoxContainer);
486 generate_aabb->add_child(genvb);
487 generate_seconds = memnew(SpinBox);
488 genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
489 generate_seconds->set_min(0.1);
490 generate_seconds->set_max(25);
491 generate_seconds->set_value(2);
492
493 add_child(generate_aabb);
494
495 generate_aabb->connect("confirmed", this, "_generate_aabb");
496 }
497
edit(Object * p_object)498 void ParticlesEditorPlugin::edit(Object *p_object) {
499
500 particles_editor->edit(Object::cast_to<Particles>(p_object));
501 }
502
handles(Object * p_object) const503 bool ParticlesEditorPlugin::handles(Object *p_object) const {
504
505 return p_object->is_class("Particles");
506 }
507
make_visible(bool p_visible)508 void ParticlesEditorPlugin::make_visible(bool p_visible) {
509
510 if (p_visible) {
511 particles_editor->show();
512 particles_editor->particles_editor_hb->show();
513 } else {
514 particles_editor->particles_editor_hb->hide();
515 particles_editor->hide();
516 particles_editor->edit(NULL);
517 }
518 }
519
ParticlesEditorPlugin(EditorNode * p_node)520 ParticlesEditorPlugin::ParticlesEditorPlugin(EditorNode *p_node) {
521
522 editor = p_node;
523 particles_editor = memnew(ParticlesEditor);
524 editor->get_viewport()->add_child(particles_editor);
525
526 particles_editor->hide();
527 }
528
~ParticlesEditorPlugin()529 ParticlesEditorPlugin::~ParticlesEditorPlugin() {
530 }
531