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-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #include "particles_editor_plugin.h"
31 #include "editor/plugins/spatial_editor_plugin.h"
32 #include "io/resource_loader.h"
33 #include "servers/visual/particle_system_sw.h"
34 
_node_removed(Node * p_node)35 void ParticlesEditor::_node_removed(Node *p_node) {
36 
37 	if (p_node == node) {
38 		node = NULL;
39 		hide();
40 	}
41 }
42 
_resource_seleted(const String & p_res)43 void ParticlesEditor::_resource_seleted(const String &p_res) {
44 
45 	//print_line("selected resource path: "+p_res);
46 }
47 
_node_selected(const NodePath & p_path)48 void ParticlesEditor::_node_selected(const NodePath &p_path) {
49 
50 	Node *sel = get_node(p_path);
51 	if (!sel)
52 		return;
53 
54 	VisualInstance *vi = sel->cast_to<VisualInstance>();
55 	if (!vi) {
56 
57 		err_dialog->set_text(TTR("Node does not contain geometry."));
58 		err_dialog->popup_centered_minsize();
59 		return;
60 	}
61 
62 	geometry = vi->get_faces(VisualInstance::FACES_SOLID);
63 
64 	if (geometry.size() == 0) {
65 
66 		err_dialog->set_text(TTR("Node does not contain geometry (faces)."));
67 		err_dialog->popup_centered_minsize();
68 		return;
69 	}
70 
71 	Transform geom_xform = node->get_global_transform().affine_inverse() * vi->get_global_transform();
72 
73 	int gc = geometry.size();
74 	DVector<Face3>::Write w = geometry.write();
75 
76 	for (int i = 0; i < gc; i++) {
77 		for (int j = 0; j < 3; j++) {
78 			w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
79 		}
80 	}
81 
82 	w = DVector<Face3>::Write();
83 
84 	emission_dialog->popup_centered(Size2(300, 130));
85 }
86 
87 /*
88 
89 void ParticlesEditor::_populate() {
90 
91 	if(!node)
92 		return;
93 
94 
95 	if (node->get_particles().is_null())
96 		return;
97 
98 	node->get_particles()->set_instance_count(populate_amount->get_text().to_int());
99 	node->populate_parent(populate_rotate_random->get_val(),populate_tilt_random->get_val(),populate_scale_random->get_text().to_double(),populate_scale->get_text().to_double());
100 
101 }
102 */
103 
_notification(int p_notification)104 void ParticlesEditor::_notification(int p_notification) {
105 
106 	if (p_notification == NOTIFICATION_ENTER_TREE) {
107 		options->set_icon(options->get_popup()->get_icon("Particles", "EditorIcons"));
108 	}
109 }
110 
_menu_option(int p_option)111 void ParticlesEditor::_menu_option(int p_option) {
112 
113 	switch (p_option) {
114 
115 		case MENU_OPTION_GENERATE_AABB: {
116 
117 			Transform globalizer = node->get_global_transform();
118 			ParticleSystemSW pssw;
119 			for (int i = 0; i < VS::PARTICLE_VAR_MAX; i++) {
120 
121 				pssw.particle_vars[i] = node->get_variable((Particles::Variable)i);
122 				pssw.particle_randomness[i] = node->get_randomness((Particles::Variable)i);
123 			}
124 
125 			pssw.emission_half_extents = node->get_emission_half_extents();
126 			pssw.emission_points = node->get_emission_points();
127 			pssw.emission_base_velocity = node->get_emission_base_velocity();
128 			pssw.amount = node->get_amount();
129 			pssw.gravity_normal = node->get_gravity_normal();
130 			pssw.emitting = true;
131 			pssw.height_from_velocity = node->has_height_from_velocity();
132 			pssw.color_phase_count = 1;
133 
134 			ParticleSystemProcessSW pp;
135 			float delta = 0.01;
136 			float lifetime = pssw.particle_vars[VS::PARTICLE_LIFETIME];
137 
138 			Transform localizer = globalizer.affine_inverse();
139 			AABB aabb;
140 			for (float t = 0; t < lifetime; t += delta) {
141 
142 				pp.process(&pssw, globalizer, delta);
143 				for (int i = 0; i < pp.particle_data.size(); i++) {
144 
145 					Vector3 p = localizer.xform(pp.particle_data[i].pos);
146 
147 					if (t == 0 && i == 0)
148 						aabb.pos = p;
149 					else
150 						aabb.expand_to(p);
151 				}
152 			}
153 
154 			aabb.grow_by(aabb.get_longest_axis_size() * 0.2);
155 
156 			node->set_visibility_aabb(aabb);
157 
158 		} break;
159 		case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH: {
160 
161 			emission_file_dialog->popup_centered_ratio();
162 
163 		} break;
164 
165 		case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
166 			/*
167 			Node *root = get_scene()->get_root_node();
168 			ERR_FAIL_COND(!root);
169 			EditorNode *en = root->cast_to<EditorNode>();
170 			ERR_FAIL_COND(!en);
171 			Node * node = en->get_edited_scene();
172 */
173 			emission_tree_dialog->popup_centered_ratio();
174 
175 		} break;
176 	}
177 }
178 
edit(Particles * p_particles)179 void ParticlesEditor::edit(Particles *p_particles) {
180 
181 	node = p_particles;
182 }
183 
_generate_emission_points()184 void ParticlesEditor::_generate_emission_points() {
185 
186 	/// hacer codigo aca
187 	DVector<Vector3> points;
188 
189 	if (emission_fill->get_selected() == 0) {
190 
191 		float area_accum = 0;
192 		Map<float, int> triangle_area_map;
193 		print_line("geometry size: " + itos(geometry.size()));
194 
195 		for (int i = 0; i < geometry.size(); i++) {
196 
197 			float area = geometry[i].get_area();
198 			if (area < CMP_EPSILON)
199 				continue;
200 			triangle_area_map[area_accum] = i;
201 			area_accum += area;
202 		}
203 
204 		if (!triangle_area_map.size() || area_accum == 0) {
205 
206 			err_dialog->set_text(TTR("Faces contain no area!"));
207 			err_dialog->popup_centered_minsize();
208 			return;
209 		}
210 
211 		int emissor_count = emission_amount->get_val();
212 
213 		for (int i = 0; i < emissor_count; i++) {
214 
215 			float areapos = Math::random(0, area_accum);
216 
217 			Map<float, int>::Element *E = triangle_area_map.find_closest(areapos);
218 			ERR_FAIL_COND(!E)
219 			int index = E->get();
220 			ERR_FAIL_INDEX(index, geometry.size());
221 
222 			// ok FINALLY get face
223 			Face3 face = geometry[index];
224 			//now compute some position inside the face...
225 
226 			Vector3 pos = face.get_random_point_inside();
227 
228 			points.push_back(pos);
229 		}
230 	} else {
231 
232 		int gcount = geometry.size();
233 
234 		if (gcount == 0) {
235 
236 			err_dialog->set_text(TTR("No faces!"));
237 			err_dialog->popup_centered_minsize();
238 			return;
239 		}
240 
241 		DVector<Face3>::Read r = geometry.read();
242 
243 		AABB aabb;
244 
245 		for (int i = 0; i < gcount; i++) {
246 
247 			for (int j = 0; j < 3; j++) {
248 
249 				if (i == 0 && j == 0)
250 					aabb.pos = r[i].vertex[j];
251 				else
252 					aabb.expand_to(r[i].vertex[j]);
253 			}
254 		}
255 
256 		int emissor_count = emission_amount->get_val();
257 
258 		for (int i = 0; i < emissor_count; i++) {
259 
260 			int attempts = 5;
261 
262 			for (int j = 0; j < attempts; j++) {
263 
264 				Vector3 dir;
265 				dir[Math::rand() % 3] = 1.0;
266 				Vector3 ofs = Vector3(1, 1, 1) - dir;
267 				ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size;
268 				ofs += aabb.pos;
269 
270 				Vector3 ofsv = ofs + aabb.size * dir;
271 
272 				//space it a little
273 				ofs -= dir;
274 				ofsv += dir;
275 
276 				float max = -1e7, min = 1e7;
277 
278 				for (int k = 0; k < gcount; k++) {
279 
280 					const Face3 &f3 = r[k];
281 
282 					Vector3 res;
283 					if (f3.intersects_segment(ofs, ofsv, &res)) {
284 
285 						res -= ofs;
286 						float d = dir.dot(res);
287 
288 						if (d < min)
289 							min = d;
290 						if (d > max)
291 							max = d;
292 					}
293 				}
294 
295 				if (max < min)
296 					continue; //lost attempt
297 
298 				float val = min + (max - min) * Math::randf();
299 
300 				Vector3 point = ofs + dir * val;
301 
302 				points.push_back(point);
303 				break;
304 			}
305 		}
306 	}
307 
308 	//print_line("point count: "+itos(points.size()));
309 	node->set_emission_points(points);
310 }
311 
_bind_methods()312 void ParticlesEditor::_bind_methods() {
313 
314 	ObjectTypeDB::bind_method("_menu_option", &ParticlesEditor::_menu_option);
315 	ObjectTypeDB::bind_method("_resource_seleted", &ParticlesEditor::_resource_seleted);
316 	ObjectTypeDB::bind_method("_node_selected", &ParticlesEditor::_node_selected);
317 	ObjectTypeDB::bind_method("_generate_emission_points", &ParticlesEditor::_generate_emission_points);
318 
319 	//ObjectTypeDB::bind_method("_populate",&ParticlesEditor::_populate);
320 }
321 
ParticlesEditor()322 ParticlesEditor::ParticlesEditor() {
323 
324 	particles_editor_hb = memnew(HBoxContainer);
325 	SpatialEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb);
326 	options = memnew(MenuButton);
327 	particles_editor_hb->add_child(options);
328 	particles_editor_hb->hide();
329 
330 	options->set_text("Particles");
331 	options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
332 	options->get_popup()->add_separator();
333 	options->get_popup()->add_item(TTR("Create Emitter From Mesh"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH);
334 	options->get_popup()->add_item(TTR("Create Emitter From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
335 	options->get_popup()->add_item(TTR("Clear Emitter"), MENU_OPTION_CLEAR_EMISSION_VOLUME);
336 
337 	options->get_popup()->connect("item_pressed", this, "_menu_option");
338 
339 	emission_dialog = memnew(ConfirmationDialog);
340 	emission_dialog->set_title(TTR("Create Emitter"));
341 	add_child(emission_dialog);
342 	Label *l = memnew(Label);
343 	l->set_pos(Point2(5, 5));
344 	l->set_text(TTR("Emission Positions:"));
345 	emission_dialog->add_child(l);
346 
347 	emission_amount = memnew(SpinBox);
348 	emission_amount->set_anchor(MARGIN_RIGHT, ANCHOR_END);
349 	emission_amount->set_begin(Point2(20, 23));
350 	emission_amount->set_end(Point2(5, 25));
351 	emission_amount->set_min(1);
352 	emission_amount->set_max(65536);
353 	emission_amount->set_val(512);
354 	emission_dialog->add_child(emission_amount);
355 	emission_dialog->get_ok()->set_text(TTR("Create"));
356 	emission_dialog->connect("confirmed", this, "_generate_emission_points");
357 
358 	l = memnew(Label);
359 	l->set_pos(Point2(5, 50));
360 	l->set_text(TTR("Emission Fill:"));
361 	emission_dialog->add_child(l);
362 
363 	emission_fill = memnew(OptionButton);
364 	emission_fill->set_anchor(MARGIN_RIGHT, ANCHOR_END);
365 	emission_fill->set_begin(Point2(20, 70));
366 	emission_fill->set_end(Point2(5, 75));
367 	emission_fill->add_item(TTR("Surface"));
368 	emission_fill->add_item(TTR("Volume"));
369 	emission_dialog->add_child(emission_fill);
370 
371 	err_dialog = memnew(ConfirmationDialog);
372 	//err_dialog->get_cancel()->hide();
373 	add_child(err_dialog);
374 
375 	emission_file_dialog = memnew(EditorFileDialog);
376 	add_child(emission_file_dialog);
377 	emission_file_dialog->connect("file_selected", this, "_resource_seleted");
378 	emission_tree_dialog = memnew(SceneTreeDialog);
379 	add_child(emission_tree_dialog);
380 	emission_tree_dialog->connect("selected", this, "_node_selected");
381 
382 	List<String> extensions;
383 	ResourceLoader::get_recognized_extensions_for_type("Mesh", &extensions);
384 
385 	emission_file_dialog->clear_filters();
386 	for (int i = 0; i < extensions.size(); i++) {
387 
388 		emission_file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
389 	}
390 
391 	emission_file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
392 
393 	//options->set_anchor(MARGIN_LEFT,Control::ANCHOR_END);
394 	//options->set_anchor(MARGIN_RIGHT,Control::ANCHOR_END);
395 }
396 
edit(Object * p_object)397 void ParticlesEditorPlugin::edit(Object *p_object) {
398 
399 	particles_editor->edit(p_object->cast_to<Particles>());
400 }
401 
handles(Object * p_object) const402 bool ParticlesEditorPlugin::handles(Object *p_object) const {
403 
404 	return p_object->is_type("Particles");
405 }
406 
make_visible(bool p_visible)407 void ParticlesEditorPlugin::make_visible(bool p_visible) {
408 
409 	if (p_visible) {
410 		particles_editor->show();
411 		particles_editor->particles_editor_hb->show();
412 	} else {
413 		particles_editor->particles_editor_hb->hide();
414 		particles_editor->hide();
415 		particles_editor->edit(NULL);
416 	}
417 }
418 
ParticlesEditorPlugin(EditorNode * p_node)419 ParticlesEditorPlugin::ParticlesEditorPlugin(EditorNode *p_node) {
420 
421 	editor = p_node;
422 	particles_editor = memnew(ParticlesEditor);
423 	editor->get_viewport()->add_child(particles_editor);
424 
425 	particles_editor->hide();
426 }
427 
~ParticlesEditorPlugin()428 ParticlesEditorPlugin::~ParticlesEditorPlugin() {
429 }
430