Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/3d/particles_3d_editor_plugin.cpp
9903 views
1
/**************************************************************************/
2
/* particles_3d_editor_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_editor_plugin.h"
32
33
#include "editor/editor_node.h"
34
#include "editor/editor_undo_redo_manager.h"
35
#include "editor/scene/scene_tree_editor.h"
36
#include "scene/3d/cpu_particles_3d.h"
37
#include "scene/3d/gpu_particles_3d.h"
38
#include "scene/3d/mesh_instance_3d.h"
39
#include "scene/gui/box_container.h"
40
#include "scene/gui/option_button.h"
41
#include "scene/gui/spin_box.h"
42
#include "scene/resources/image_texture.h"
43
#include "scene/resources/particle_process_material.h"
44
45
void Particles3DEditorPlugin::_generate_aabb() {
46
double time = generate_seconds->get_value();
47
48
double running = 0.0;
49
50
EditorProgress ep("gen_aabb", TTR("Generating Visibility AABB (Waiting for Particle Simulation)"), int(time));
51
52
bool was_emitting = edited_node->get("emitting");
53
if (!was_emitting) {
54
edited_node->set("emitting", true);
55
OS::get_singleton()->delay_usec(1000);
56
}
57
58
AABB rect;
59
Callable capture_aabb = Callable(edited_node, "capture_aabb");
60
61
while (running < time) {
62
uint64_t ticks = OS::get_singleton()->get_ticks_usec();
63
ep.step(TTR("Generating..."), int(running), true);
64
OS::get_singleton()->delay_usec(1000);
65
66
AABB capture = capture_aabb.call();
67
if (rect == AABB()) {
68
rect = capture;
69
} else {
70
rect.merge_with(capture);
71
}
72
73
running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
74
}
75
76
if (!was_emitting) {
77
edited_node->set("emitting", false);
78
}
79
80
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
81
ur->create_action(TTR("Generate Visibility AABB"));
82
ur->add_do_property(edited_node, "visibility_aabb", rect);
83
ur->add_undo_property(edited_node, "visibility_aabb", edited_node->get("visibility_aabb"));
84
ur->commit_action();
85
}
86
87
void Particles3DEditorPlugin::_node_selected(const NodePath &p_path) {
88
Node *sel = get_node(p_path);
89
if (!sel) {
90
return;
91
}
92
93
if (!sel->is_class("Node3D")) {
94
EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't inherit from Node3D."), sel->get_name()));
95
return;
96
}
97
98
MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(sel);
99
if (!mi || mi->get_mesh().is_null()) {
100
EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain geometry."), sel->get_name()));
101
return;
102
}
103
104
geometry = mi->get_mesh()->get_faces();
105
if (geometry.is_empty()) {
106
EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain face geometry."), sel->get_name()));
107
return;
108
}
109
110
Transform3D geom_xform = edited_node->get("global_transform");
111
geom_xform = geom_xform.affine_inverse() * mi->get_global_transform();
112
int gc = geometry.size();
113
Face3 *w = geometry.ptrw();
114
115
for (int i = 0; i < gc; i++) {
116
for (int j = 0; j < 3; j++) {
117
w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
118
}
119
}
120
emission_dialog->popup_centered(Size2(300, 130));
121
}
122
123
void Particles3DEditorPlugin::_menu_callback(int p_idx) {
124
switch (p_idx) {
125
case MENU_OPTION_GENERATE_AABB: {
126
if (need_show_lifetime_dialog(generate_seconds)) {
127
generate_aabb->popup_centered();
128
} else {
129
_generate_aabb();
130
}
131
} break;
132
133
case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
134
if (_can_generate_points()) {
135
emission_tree_dialog->popup_scenetree_dialog();
136
}
137
} break;
138
139
default: {
140
ParticlesEditorPlugin::_menu_callback(p_idx);
141
}
142
}
143
}
144
145
void Particles3DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
146
p_menu->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
147
p_menu->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
148
}
149
150
bool Particles3DEditorPlugin::_generate(Vector<Vector3> &r_points, Vector<Vector3> &r_normals) {
151
bool use_normals = emission_fill->get_selected() == 1;
152
153
if (emission_fill->get_selected() < 2) {
154
float area_accum = 0;
155
RBMap<float, int> triangle_area_map;
156
157
for (int i = 0; i < geometry.size(); i++) {
158
float area = geometry[i].get_area();
159
if (area < CMP_EPSILON) {
160
continue;
161
}
162
triangle_area_map[area_accum] = i;
163
area_accum += area;
164
}
165
166
if (!triangle_area_map.size() || area_accum == 0) {
167
EditorNode::get_singleton()->show_warning(TTR("The geometry's faces don't contain any area."));
168
return false;
169
}
170
171
int emissor_count = emission_amount->get_value();
172
173
for (int i = 0; i < emissor_count; i++) {
174
float areapos = Math::random(0.0f, area_accum);
175
176
RBMap<float, int>::Iterator E = triangle_area_map.find_closest(areapos);
177
ERR_FAIL_COND_V(!E, false);
178
int index = E->value;
179
ERR_FAIL_INDEX_V(index, geometry.size(), false);
180
181
// ok FINALLY get face
182
Face3 face = geometry[index];
183
//now compute some position inside the face...
184
185
Vector3 pos = face.get_random_point_inside();
186
187
r_points.push_back(pos);
188
189
if (use_normals) {
190
Vector3 normal = face.get_plane().normal;
191
r_normals.push_back(normal);
192
}
193
}
194
} else {
195
int gcount = geometry.size();
196
197
if (gcount == 0) {
198
EditorNode::get_singleton()->show_warning(TTR("The geometry doesn't contain any faces."));
199
return false;
200
}
201
202
const Face3 *r = geometry.ptr();
203
204
AABB aabb;
205
206
for (int i = 0; i < gcount; i++) {
207
for (int j = 0; j < 3; j++) {
208
if (i == 0 && j == 0) {
209
aabb.position = r[i].vertex[j];
210
} else {
211
aabb.expand_to(r[i].vertex[j]);
212
}
213
}
214
}
215
216
int emissor_count = emission_amount->get_value();
217
218
for (int i = 0; i < emissor_count; i++) {
219
int attempts = 5;
220
221
for (int j = 0; j < attempts; j++) {
222
Vector3 dir;
223
dir[Math::rand() % 3] = 1.0;
224
Vector3 ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size + aabb.position;
225
226
Vector3 ofsv = ofs + aabb.size * dir;
227
228
//space it a little
229
ofs -= dir;
230
ofsv += dir;
231
232
float max = -1e7, min = 1e7;
233
234
for (int k = 0; k < gcount; k++) {
235
const Face3 &f3 = r[k];
236
237
Vector3 res;
238
if (f3.intersects_segment(ofs, ofsv, &res)) {
239
res -= ofs;
240
float d = dir.dot(res);
241
242
if (d < min) {
243
min = d;
244
}
245
if (d > max) {
246
max = d;
247
}
248
}
249
}
250
251
if (max < min) {
252
continue; //lost attempt
253
}
254
255
float val = min + (max - min) * Math::randf();
256
257
Vector3 point = ofs + dir * val;
258
259
r_points.push_back(point);
260
break;
261
}
262
}
263
}
264
return true;
265
}
266
267
Particles3DEditorPlugin::Particles3DEditorPlugin() {
268
generate_aabb = memnew(ConfirmationDialog);
269
generate_aabb->set_title(TTR("Generate Visibility AABB"));
270
271
VBoxContainer *genvb = memnew(VBoxContainer);
272
generate_aabb->add_child(genvb);
273
274
generate_seconds = memnew(SpinBox);
275
generate_seconds->set_accessibility_name(TTRC("Generation Time (sec)"));
276
generate_seconds->set_min(0.1);
277
generate_seconds->set_max(25);
278
generate_seconds->set_value(2);
279
genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
280
281
EditorNode::get_singleton()->get_gui_base()->add_child(generate_aabb);
282
283
generate_aabb->connect(SceneStringName(confirmed), callable_mp(this, &Particles3DEditorPlugin::_generate_aabb));
284
285
emission_tree_dialog = memnew(SceneTreeDialog);
286
Vector<StringName> valid_types;
287
valid_types.push_back("MeshInstance3D");
288
emission_tree_dialog->set_valid_types(valid_types);
289
EditorNode::get_singleton()->get_gui_base()->add_child(emission_tree_dialog);
290
emission_tree_dialog->connect("selected", callable_mp(this, &Particles3DEditorPlugin::_node_selected));
291
292
emission_dialog = memnew(ConfirmationDialog);
293
emission_dialog->set_title(TTR("Create Emitter"));
294
EditorNode::get_singleton()->get_gui_base()->add_child(emission_dialog);
295
296
VBoxContainer *emd_vb = memnew(VBoxContainer);
297
emission_dialog->add_child(emd_vb);
298
299
emission_amount = memnew(SpinBox);
300
emission_amount->set_accessibility_name(TTRC("Emission Points:"));
301
emission_amount->set_min(1);
302
emission_amount->set_max(100000);
303
emission_amount->set_value(512);
304
emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount);
305
306
emission_fill = memnew(OptionButton);
307
emission_fill->set_accessibility_name(TTRC("Emission Source:"));
308
emission_fill->add_item(TTR("Surface Points"));
309
emission_fill->add_item(TTR("Surface Points+Normal (Directed)"));
310
emission_fill->add_item(TTR("Volume"));
311
emd_vb->add_margin_child(TTR("Emission Source:"), emission_fill);
312
313
emission_dialog->set_ok_button_text(TTR("Create"));
314
emission_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Particles3DEditorPlugin::_generate_emission_points));
315
}
316
317
Node *GPUParticles3DEditorPlugin::_convert_particles() {
318
GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);
319
320
CPUParticles3D *cpu_particles = memnew(CPUParticles3D);
321
cpu_particles->convert_from_particles(particles);
322
cpu_particles->set_name(particles->get_name());
323
cpu_particles->set_transform(particles->get_transform());
324
cpu_particles->set_visible(particles->is_visible());
325
cpu_particles->set_process_mode(particles->get_process_mode());
326
return cpu_particles;
327
}
328
329
bool GPUParticles3DEditorPlugin::_can_generate_points() const {
330
GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);
331
Ref<ParticleProcessMaterial> mat = particles->get_process_material();
332
if (mat.is_null()) {
333
EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticleProcessMaterial' is required."));
334
return false;
335
}
336
return true;
337
}
338
339
void GPUParticles3DEditorPlugin::_generate_emission_points() {
340
GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);
341
342
/// hacer codigo aca
343
Vector<Vector3> points;
344
Vector<Vector3> normals;
345
346
if (!_generate(points, normals)) {
347
return;
348
}
349
350
int point_count = points.size();
351
352
int w = 2048;
353
int h = (point_count / 2048) + 1;
354
355
Vector<uint8_t> point_img;
356
point_img.resize(w * h * 3 * sizeof(float));
357
358
{
359
uint8_t *iw = point_img.ptrw();
360
memset(iw, 0, w * h * 3 * sizeof(float));
361
const Vector3 *r = points.ptr();
362
float *wf = reinterpret_cast<float *>(iw);
363
for (int i = 0; i < point_count; i++) {
364
wf[i * 3 + 0] = r[i].x;
365
wf[i * 3 + 1] = r[i].y;
366
wf[i * 3 + 2] = r[i].z;
367
}
368
}
369
370
Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));
371
Ref<ImageTexture> tex = ImageTexture::create_from_image(image);
372
373
Ref<ParticleProcessMaterial> mat = particles->get_process_material();
374
ERR_FAIL_COND(mat.is_null());
375
376
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
377
undo_redo->create_action(TTR("Create Emission Points"));
378
ParticleProcessMaterial *matptr = mat.ptr();
379
380
if (!normals.is_empty()) {
381
undo_redo->add_do_property(matptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
382
undo_redo->add_undo_property(matptr, "emission_shape", matptr->get_emission_shape());
383
384
Vector<uint8_t> point_img2;
385
point_img2.resize(w * h * 3 * sizeof(float));
386
387
{
388
uint8_t *iw = point_img2.ptrw();
389
memset(iw, 0, w * h * 3 * sizeof(float));
390
const Vector3 *r = normals.ptr();
391
float *wf = reinterpret_cast<float *>(iw);
392
for (int i = 0; i < point_count; i++) {
393
wf[i * 3 + 0] = r[i].x;
394
wf[i * 3 + 1] = r[i].y;
395
wf[i * 3 + 2] = r[i].z;
396
}
397
}
398
399
Ref<Image> image2 = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img2));
400
undo_redo->add_do_property(matptr, "emission_normal_texture", ImageTexture::create_from_image(image2));
401
undo_redo->add_undo_property(matptr, "emission_normal_texture", matptr->get_emission_normal_texture());
402
} else {
403
undo_redo->add_do_property(matptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
404
undo_redo->add_undo_property(matptr, "emission_shape", matptr->get_emission_shape());
405
}
406
undo_redo->add_do_property(matptr, "emission_point_count", point_count);
407
undo_redo->add_undo_property(matptr, "emission_point_count", matptr->get_emission_point_count());
408
undo_redo->add_do_property(matptr, "emission_point_texture", tex);
409
undo_redo->add_undo_property(matptr, "emission_point_texture", matptr->get_emission_point_texture());
410
undo_redo->commit_action();
411
}
412
413
GPUParticles3DEditorPlugin::GPUParticles3DEditorPlugin() {
414
handled_type = TTRC("GPUParticles3D");
415
conversion_option_name = TTR("Convert to CPUParticles3D");
416
}
417
418
Node *CPUParticles3DEditorPlugin::_convert_particles() {
419
CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(edited_node);
420
421
GPUParticles3D *gpu_particles = memnew(GPUParticles3D);
422
gpu_particles->convert_from_particles(particles);
423
gpu_particles->set_name(particles->get_name());
424
gpu_particles->set_transform(particles->get_transform());
425
gpu_particles->set_visible(particles->is_visible());
426
gpu_particles->set_process_mode(particles->get_process_mode());
427
return gpu_particles;
428
}
429
430
void CPUParticles3DEditorPlugin::_generate_emission_points() {
431
CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(edited_node);
432
433
/// hacer codigo aca
434
Vector<Vector3> points;
435
Vector<Vector3> normals;
436
437
if (!_generate(points, normals)) {
438
return;
439
}
440
441
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
442
undo_redo->create_action(TTR("Create Emission Points"));
443
444
if (normals.is_empty()) {
445
undo_redo->add_do_property(particles, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
446
undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
447
} else {
448
undo_redo->add_do_property(particles, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
449
undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
450
undo_redo->add_do_property(particles, "emission_normals", normals);
451
undo_redo->add_undo_property(particles, "emission_normals", particles->get_emission_normals());
452
}
453
undo_redo->add_do_property(particles, "emission_points", points);
454
undo_redo->add_undo_property(particles, "emission_points", particles->get_emission_points());
455
undo_redo->commit_action();
456
}
457
458
CPUParticles3DEditorPlugin::CPUParticles3DEditorPlugin() {
459
handled_type = TTRC("CPUParticles3D");
460
conversion_option_name = TTR("Convert to GPUParticles3D");
461
}
462
463