| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  gpu_particles_2d_editor_plugin.cpp                                    */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | #include "gpu_particles_2d_editor_plugin.h"
 | 
					
						
							| 
									
										
										
										
											2017-01-16 08:04:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | #include "canvas_item_editor_plugin.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-11 18:13:45 +02:00
										 |  |  | #include "core/io/image_loader.h"
 | 
					
						
							| 
									
										
										
										
											2022-02-12 02:46:22 +01:00
										 |  |  | #include "editor/editor_node.h"
 | 
					
						
							| 
									
										
										
										
											2022-03-25 18:06:46 +01:00
										 |  |  | #include "editor/editor_undo_redo_manager.h"
 | 
					
						
							| 
									
										
										
										
											2023-04-07 18:59:49 +02:00
										 |  |  | #include "editor/gui/editor_file_dialog.h"
 | 
					
						
							| 
									
										
										
										
											2022-02-12 02:46:22 +01:00
										 |  |  | #include "editor/scene_tree_dock.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-27 13:05:57 +02:00
										 |  |  | #include "scene/2d/cpu_particles_2d.h"
 | 
					
						
							| 
									
										
										
										
											2022-11-19 12:45:49 +01:00
										 |  |  | #include "scene/gui/menu_button.h"
 | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | #include "scene/gui/separator.h"
 | 
					
						
							| 
									
										
										
										
											2022-08-18 11:09:22 +02:00
										 |  |  | #include "scene/resources/particle_process_material.h"
 | 
					
						
							| 
									
										
										
										
											2017-08-24 22:58:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::edit(Object *p_object) { | 
					
						
							|  |  |  | 	particles = Object::cast_to<GPUParticles2D>(p_object); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | bool GPUParticles2DEditorPlugin::handles(Object *p_object) const { | 
					
						
							| 
									
										
										
										
											2020-03-26 18:49:16 -03:00
										 |  |  | 	return p_object->is_class("GPUParticles2D"); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::make_visible(bool p_visible) { | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	if (p_visible) { | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 		toolbar->show(); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 		toolbar->hide(); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::_file_selected(const String &p_file) { | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	source_emission_file = p_file; | 
					
						
							| 
									
										
										
										
											2020-03-06 14:00:16 -03:00
										 |  |  | 	emission_mask->popup_centered(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 10:58:52 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::_selection_changed() { | 
					
						
							| 
									
										
										
										
											2022-01-27 10:36:51 +01:00
										 |  |  | 	List<Node *> selected_nodes = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list(); | 
					
						
							| 
									
										
										
										
											2021-11-17 10:58:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (selected_particles.is_empty() && selected_nodes.is_empty()) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (GPUParticles2D *SP : selected_particles) { | 
					
						
							|  |  |  | 		SP->set_show_visibility_rect(false); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	selected_particles.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (Node *P : selected_nodes) { | 
					
						
							|  |  |  | 		GPUParticles2D *selected_particle = Object::cast_to<GPUParticles2D>(P); | 
					
						
							|  |  |  | 		if (selected_particle != nullptr) { | 
					
						
							|  |  |  | 			selected_particle->set_show_visibility_rect(true); | 
					
						
							|  |  |  | 			selected_particles.push_back(selected_particle); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::_menu_callback(int p_idx) { | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	switch (p_idx) { | 
					
						
							|  |  |  | 		case MENU_GENERATE_VISIBILITY_RECT: { | 
					
						
							| 
									
										
										
										
											2021-07-05 15:05:39 +02:00
										 |  |  | 			// Add one second to the default generation lifetime, since the progress is updated every second.
 | 
					
						
							|  |  |  | 			generate_seconds->set_value(MAX(1.0, trunc(particles->get_lifetime()) + 1.0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (generate_seconds->get_value() >= 11.0 + CMP_EPSILON) { | 
					
						
							|  |  |  | 				// Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.
 | 
					
						
							|  |  |  | 				generate_visibility_rect->popup_centered(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2021-07-05 15:05:39 +02:00
										 |  |  | 				// Generate the visibility rect immediately.
 | 
					
						
							|  |  |  | 				_generate_visibility_rect(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 		} break; | 
					
						
							|  |  |  | 		case MENU_LOAD_EMISSION_MASK: { | 
					
						
							| 
									
										
										
										
											2020-07-11 18:45:19 +02:00
										 |  |  | 			file->popup_file_dialog(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							|  |  |  | 		case MENU_CLEAR_EMISSION_MASK: { | 
					
						
							| 
									
										
										
										
											2020-03-06 14:00:16 -03:00
										 |  |  | 			emission_mask->popup_centered(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2018-09-27 13:05:57 +02:00
										 |  |  | 		case MENU_OPTION_CONVERT_TO_CPU_PARTICLES: { | 
					
						
							|  |  |  | 			CPUParticles2D *cpu_particles = memnew(CPUParticles2D); | 
					
						
							|  |  |  | 			cpu_particles->convert_from_particles(particles); | 
					
						
							|  |  |  | 			cpu_particles->set_name(particles->get_name()); | 
					
						
							|  |  |  | 			cpu_particles->set_transform(particles->get_transform()); | 
					
						
							|  |  |  | 			cpu_particles->set_visible(particles->is_visible()); | 
					
						
							| 
									
										
										
										
											2021-02-18 15:52:29 -03:00
										 |  |  | 			cpu_particles->set_process_mode(particles->get_process_mode()); | 
					
						
							| 
									
										
										
										
											2019-02-12 12:30:56 -05:00
										 |  |  | 			cpu_particles->set_z_index(particles->get_z_index()); | 
					
						
							| 
									
										
										
										
											2018-09-27 13:05:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-23 23:53:16 +01:00
										 |  |  | 			EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton(); | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | 			ur->create_action(TTR("Convert to CPUParticles2D")); | 
					
						
							| 
									
										
										
										
											2021-11-17 21:08:55 +01:00
										 |  |  | 			ur->add_do_method(SceneTreeDock::get_singleton(), "replace_node", particles, cpu_particles, true, false); | 
					
						
							| 
									
										
										
										
											2019-06-24 12:08:28 +02:00
										 |  |  | 			ur->add_do_reference(cpu_particles); | 
					
						
							| 
									
										
										
										
											2021-11-17 21:08:55 +01:00
										 |  |  | 			ur->add_undo_method(SceneTreeDock::get_singleton(), "replace_node", cpu_particles, particles, false, false); | 
					
						
							| 
									
										
										
										
											2019-06-24 12:08:28 +02:00
										 |  |  | 			ur->add_undo_reference(particles); | 
					
						
							| 
									
										
										
										
											2019-05-30 06:51:36 -04:00
										 |  |  | 			ur->commit_action(); | 
					
						
							| 
									
										
										
										
											2018-09-27 13:05:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2019-06-21 22:33:11 -07:00
										 |  |  | 		case MENU_RESTART: { | 
					
						
							|  |  |  | 			particles->restart(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::_generate_visibility_rect() { | 
					
						
							| 
									
										
										
										
											2021-02-01 21:16:37 -05:00
										 |  |  | 	double time = generate_seconds->get_value(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	float running = 0.0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-05 15:05:39 +02:00
										 |  |  | 	EditorProgress ep("gen_vrect", TTR("Generating Visibility Rect (Waiting for Particle Simulation)"), int(time)); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 11:24:49 +02:00
										 |  |  | 	bool was_emitting = particles->is_emitting(); | 
					
						
							|  |  |  | 	if (!was_emitting) { | 
					
						
							|  |  |  | 		particles->set_emitting(true); | 
					
						
							|  |  |  | 		OS::get_singleton()->delay_usec(1000); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	Rect2 rect; | 
					
						
							|  |  |  | 	while (running < time) { | 
					
						
							|  |  |  | 		uint64_t ticks = OS::get_singleton()->get_ticks_usec(); | 
					
						
							| 
									
										
										
										
											2018-04-22 19:36:01 +02:00
										 |  |  | 		ep.step("Generating...", int(running), true); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 		OS::get_singleton()->delay_usec(1000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Rect2 capture = particles->capture_rect(); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		if (rect == Rect2()) { | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 			rect = capture; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 			rect = rect.merge(capture); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 11:24:49 +02:00
										 |  |  | 	if (!was_emitting) { | 
					
						
							|  |  |  | 		particles->set_emitting(false); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-23 23:53:16 +01:00
										 |  |  | 	EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); | 
					
						
							| 
									
										
										
										
											2019-03-04 12:49:32 +00:00
										 |  |  | 	undo_redo->create_action(TTR("Generate Visibility Rect")); | 
					
						
							|  |  |  | 	undo_redo->add_do_method(particles, "set_visibility_rect", rect); | 
					
						
							|  |  |  | 	undo_redo->add_undo_method(particles, "set_visibility_rect", particles->get_visibility_rect()); | 
					
						
							|  |  |  | 	undo_redo->commit_action(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::_generate_emission_mask() { | 
					
						
							| 
									
										
										
										
											2022-08-18 11:09:22 +02:00
										 |  |  | 	Ref<ParticleProcessMaterial> pm = particles->get_process_material(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	if (!pm.is_valid()) { | 
					
						
							| 
									
										
										
										
											2022-08-18 11:09:22 +02:00
										 |  |  | 		EditorNode::get_singleton()->show_warning(TTR("Can only set point into a ParticleProcessMaterial process material")); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-17 07:36:47 -03:00
										 |  |  | 	Ref<Image> img; | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 	img.instantiate(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	Error err = ImageLoader::load_image(source_emission_file, img); | 
					
						
							| 
									
										
										
										
											2019-09-25 10:28:50 +02:00
										 |  |  | 	ERR_FAIL_COND_MSG(err != OK, "Error loading image '" + source_emission_file + "'."); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	if (img->is_compressed()) { | 
					
						
							|  |  |  | 		img->decompress(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	img->convert(Image::FORMAT_RGBA8); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(img->get_format() != Image::FORMAT_RGBA8); | 
					
						
							| 
									
										
										
										
											2021-09-23 20:43:43 +05:45
										 |  |  | 	Size2i s = img->get_size(); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	ERR_FAIL_COND(s.width == 0 || s.height == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	Vector<Point2> valid_positions; | 
					
						
							|  |  |  | 	Vector<Point2> valid_normals; | 
					
						
							|  |  |  | 	Vector<uint8_t> valid_colors; | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	valid_positions.resize(s.width * s.height); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	EmissionMode emode = (EmissionMode)emission_mask_mode->get_selected(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (emode == EMISSION_MODE_BORDER_DIRECTED) { | 
					
						
							|  |  |  | 		valid_normals.resize(s.width * s.height); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool capture_colors = emission_colors->is_pressed(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (capture_colors) { | 
					
						
							|  |  |  | 		valid_colors.resize(s.width * s.height * 4); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	int vpc = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2022-09-29 12:53:28 +03:00
										 |  |  | 		Vector<uint8_t> img_data = img->get_data(); | 
					
						
							|  |  |  | 		const uint8_t *r = img_data.ptr(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for (int i = 0; i < s.width; i++) { | 
					
						
							|  |  |  | 			for (int j = 0; j < s.height; j++) { | 
					
						
							|  |  |  | 				uint8_t a = r[(j * s.width + i) * 4 + 3]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (a > 128) { | 
					
						
							|  |  |  | 					if (emode == EMISSION_MODE_SOLID) { | 
					
						
							|  |  |  | 						if (capture_colors) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 							valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0]; | 
					
						
							|  |  |  | 							valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1]; | 
					
						
							|  |  |  | 							valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2]; | 
					
						
							|  |  |  | 							valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3]; | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 						valid_positions.write[vpc++] = Point2(i, j); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						bool on_border = false; | 
					
						
							|  |  |  | 						for (int x = i - 1; x <= i + 1; x++) { | 
					
						
							|  |  |  | 							for (int y = j - 1; y <= j + 1; y++) { | 
					
						
							|  |  |  | 								if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) { | 
					
						
							|  |  |  | 									on_border = true; | 
					
						
							|  |  |  | 									break; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 							if (on_border) { | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 								break; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						if (on_border) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 							valid_positions.write[vpc] = Point2(i, j); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							if (emode == EMISSION_MODE_BORDER_DIRECTED) { | 
					
						
							|  |  |  | 								Vector2 normal; | 
					
						
							|  |  |  | 								for (int x = i - 2; x <= i + 2; x++) { | 
					
						
							|  |  |  | 									for (int y = j - 2; y <= j + 2; y++) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 										if (x == i && y == j) { | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 											continue; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 										} | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 										if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) { | 
					
						
							|  |  |  | 											normal += Vector2(x - i, y - j).normalized(); | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								normal.normalize(); | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 								valid_normals.write[vpc] = normal; | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							if (capture_colors) { | 
					
						
							| 
									
										
										
										
											2018-07-25 03:11:03 +02:00
										 |  |  | 								valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0]; | 
					
						
							|  |  |  | 								valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1]; | 
					
						
							|  |  |  | 								valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2]; | 
					
						
							|  |  |  | 								valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3]; | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							vpc++; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	valid_positions.resize(vpc); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	if (valid_normals.size()) { | 
					
						
							|  |  |  | 		valid_normals.resize(vpc); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-14 20:57:49 -06:00
										 |  |  | 	ERR_FAIL_COND_MSG(valid_positions.size() == 0, "No pixels with transparency > 128 in image..."); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	Vector<uint8_t> texdata; | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	int w = 2048; | 
					
						
							|  |  |  | 	int h = (vpc / 2048) + 1; | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	texdata.resize(w * h * 2 * sizeof(float)); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 		uint8_t *tw = texdata.ptrw(); | 
					
						
							| 
									
										
										
										
											2022-04-07 13:23:40 +03:00
										 |  |  | 		float *twf = reinterpret_cast<float *>(tw); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 		for (int i = 0; i < vpc; i++) { | 
					
						
							|  |  |  | 			twf[i * 2 + 0] = valid_positions[i].x; | 
					
						
							|  |  |  | 			twf[i * 2 + 1] = valid_positions[i].y; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 	img.instantiate(); | 
					
						
							| 
									
										
										
										
											2022-07-22 20:06:19 +02:00
										 |  |  | 	img->set_data(w, h, false, Image::FORMAT_RGF, texdata); | 
					
						
							| 
									
										
										
										
											2022-05-04 01:49:20 +02:00
										 |  |  | 	pm->set_emission_point_texture(ImageTexture::create_from_image(img)); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	pm->set_emission_point_count(vpc); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	if (capture_colors) { | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 		Vector<uint8_t> colordata; | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 		colordata.resize(w * h * 4); //use RG texture
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 			uint8_t *tw = colordata.ptrw(); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 			for (int i = 0; i < vpc * 4; i++) { | 
					
						
							|  |  |  | 				tw[i] = valid_colors[i]; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 		img.instantiate(); | 
					
						
							| 
									
										
										
										
											2022-07-22 20:06:19 +02:00
										 |  |  | 		img->set_data(w, h, false, Image::FORMAT_RGBA8, colordata); | 
					
						
							| 
									
										
										
										
											2022-05-04 01:49:20 +02:00
										 |  |  | 		pm->set_emission_color_texture(ImageTexture::create_from_image(img)); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (valid_normals.size()) { | 
					
						
							| 
									
										
										
										
											2022-08-18 11:09:22 +02:00
										 |  |  | 		pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 		Vector<uint8_t> normdata; | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 		normdata.resize(w * h * 2 * sizeof(float)); //use RG texture
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 			uint8_t *tw = normdata.ptrw(); | 
					
						
							| 
									
										
										
										
											2022-04-07 13:23:40 +03:00
										 |  |  | 			float *twf = reinterpret_cast<float *>(tw); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 			for (int i = 0; i < vpc; i++) { | 
					
						
							|  |  |  | 				twf[i * 2 + 0] = valid_normals[i].x; | 
					
						
							|  |  |  | 				twf[i * 2 + 1] = valid_normals[i].y; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 		img.instantiate(); | 
					
						
							| 
									
										
										
										
											2022-07-22 20:06:19 +02:00
										 |  |  | 		img->set_data(w, h, false, Image::FORMAT_RGF, normdata); | 
					
						
							| 
									
										
										
										
											2022-05-04 01:49:20 +02:00
										 |  |  | 		pm->set_emission_normal_texture(ImageTexture::create_from_image(img)); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2022-08-18 11:09:22 +02:00
										 |  |  | 		pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_POINTS); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::_notification(int p_what) { | 
					
						
							| 
									
										
										
										
											2022-02-15 21:44:22 -05:00
										 |  |  | 	switch (p_what) { | 
					
						
							|  |  |  | 		case NOTIFICATION_ENTER_TREE: { | 
					
						
							|  |  |  | 			menu->get_popup()->connect("id_pressed", callable_mp(this, &GPUParticles2DEditorPlugin::_menu_callback)); | 
					
						
							|  |  |  | 			menu->set_icon(menu->get_theme_icon(SNAME("GPUParticles2D"), SNAME("EditorIcons"))); | 
					
						
							|  |  |  | 			file->connect("file_selected", callable_mp(this, &GPUParticles2DEditorPlugin::_file_selected)); | 
					
						
							|  |  |  | 			EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", callable_mp(this, &GPUParticles2DEditorPlugin::_selection_changed)); | 
					
						
							|  |  |  | 		} break; | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | void GPUParticles2DEditorPlugin::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 10:36:51 +01:00
										 |  |  | GPUParticles2DEditorPlugin::GPUParticles2DEditorPlugin() { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	particles = nullptr; | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	toolbar = memnew(HBoxContainer); | 
					
						
							|  |  |  | 	add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar); | 
					
						
							|  |  |  | 	toolbar->hide(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	toolbar->add_child(memnew(VSeparator)); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	menu = memnew(MenuButton); | 
					
						
							| 
									
										
										
										
											2020-04-12 21:45:01 -04:00
										 |  |  | 	menu->get_popup()->add_item(TTR("Restart"), MENU_RESTART); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	menu->get_popup()->add_item(TTR("Generate Visibility Rect"), MENU_GENERATE_VISIBILITY_RECT); | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	menu->get_popup()->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	//	menu->get_popup()->add_item(TTR("Clear Emission Mask"), MENU_CLEAR_EMISSION_MASK);
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | 	menu->get_popup()->add_item(TTR("Convert to CPUParticles2D"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES); | 
					
						
							|  |  |  | 	menu->set_text(TTR("GPUParticles2D")); | 
					
						
							| 
									
										
										
										
											2019-04-25 10:27:33 -03:00
										 |  |  | 	menu->set_switch_on_hover(true); | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 	toolbar->add_child(menu); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 	file = memnew(EditorFileDialog); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	List<String> ext; | 
					
						
							|  |  |  | 	ImageLoader::get_recognized_extensions(&ext); | 
					
						
							| 
									
										
										
										
											2021-07-24 15:46:25 +02:00
										 |  |  | 	for (const String &E : ext) { | 
					
						
							| 
									
										
										
										
											2022-07-04 16:26:26 -05:00
										 |  |  | 		file->add_filter("*." + E, E.to_upper()); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-06 14:00:16 -03:00
										 |  |  | 	file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE); | 
					
						
							| 
									
										
										
										
											2016-03-14 16:05:20 -03:00
										 |  |  | 	toolbar->add_child(file); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | 	epoints = memnew(SpinBox); | 
					
						
							|  |  |  | 	epoints->set_min(1); | 
					
						
							|  |  |  | 	epoints->set_max(8192); | 
					
						
							|  |  |  | 	epoints->set_step(1); | 
					
						
							| 
									
										
										
										
											2017-01-04 01:16:14 -03:00
										 |  |  | 	epoints->set_value(512); | 
					
						
							| 
									
										
										
										
											2016-05-03 22:25:37 -03:00
										 |  |  | 	file->get_vbox()->add_margin_child(TTR("Generated Point Count:"), epoints); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 11:47:29 +02:00
										 |  |  | 	generate_visibility_rect = memnew(ConfirmationDialog); | 
					
						
							|  |  |  | 	generate_visibility_rect->set_title(TTR("Generate Visibility Rect")); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	VBoxContainer *genvb = memnew(VBoxContainer); | 
					
						
							| 
									
										
										
										
											2018-08-17 11:47:29 +02:00
										 |  |  | 	generate_visibility_rect->add_child(genvb); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	generate_seconds = memnew(SpinBox); | 
					
						
							|  |  |  | 	genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds); | 
					
						
							|  |  |  | 	generate_seconds->set_min(0.1); | 
					
						
							|  |  |  | 	generate_seconds->set_max(25); | 
					
						
							|  |  |  | 	generate_seconds->set_value(2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-17 11:47:29 +02:00
										 |  |  | 	toolbar->add_child(generate_visibility_rect); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | 	generate_visibility_rect->connect("confirmed", callable_mp(this, &GPUParticles2DEditorPlugin::_generate_visibility_rect)); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	emission_mask = memnew(ConfirmationDialog); | 
					
						
							| 
									
										
										
										
											2019-03-19 17:28:57 +01:00
										 |  |  | 	emission_mask->set_title(TTR("Load Emission Mask")); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	VBoxContainer *emvb = memnew(VBoxContainer); | 
					
						
							|  |  |  | 	emission_mask->add_child(emvb); | 
					
						
							|  |  |  | 	emission_mask_mode = memnew(OptionButton); | 
					
						
							|  |  |  | 	emvb->add_margin_child(TTR("Emission Mask"), emission_mask_mode); | 
					
						
							| 
									
										
										
										
											2020-01-04 13:33:52 +08:00
										 |  |  | 	emission_mask_mode->add_item(TTR("Solid Pixels"), EMISSION_MODE_SOLID); | 
					
						
							|  |  |  | 	emission_mask_mode->add_item(TTR("Border Pixels"), EMISSION_MODE_BORDER); | 
					
						
							|  |  |  | 	emission_mask_mode->add_item(TTR("Directed Border Pixels"), EMISSION_MODE_BORDER_DIRECTED); | 
					
						
							| 
									
										
										
										
											2017-06-21 16:25:45 -03:00
										 |  |  | 	emission_colors = memnew(CheckBox); | 
					
						
							|  |  |  | 	emission_colors->set_text(TTR("Capture from Pixel")); | 
					
						
							|  |  |  | 	emvb->add_margin_child(TTR("Emission Colors"), emission_colors); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	toolbar->add_child(emission_mask); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | 	emission_mask->connect("confirmed", callable_mp(this, &GPUParticles2DEditorPlugin::_generate_emission_mask)); | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 08:44:44 +01:00
										 |  |  | GPUParticles2DEditorPlugin::~GPUParticles2DEditorPlugin() { | 
					
						
							| 
									
										
										
										
											2014-11-05 21:20:42 -03:00
										 |  |  | } |