Path: blob/master/editor/scene/2d/particles_2d_editor_plugin.cpp
21011 views
/**************************************************************************/1/* particles_2d_editor_plugin.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "particles_2d_editor_plugin.h"3132#include "core/io/image_loader.h"33#include "editor/editor_node.h"34#include "editor/editor_string_names.h"35#include "editor/editor_undo_redo_manager.h"36#include "editor/gui/editor_file_dialog.h"37#include "scene/2d/cpu_particles_2d.h"38#include "scene/2d/gpu_particles_2d.h"39#include "scene/gui/box_container.h"40#include "scene/gui/check_box.h"41#include "scene/gui/margin_container.h"42#include "scene/gui/option_button.h"43#include "scene/gui/popup_menu.h"44#include "scene/gui/spin_box.h"45#include "scene/resources/image_texture.h"46#include "scene/resources/particle_process_material.h"4748void GPUParticles2DEditorPlugin::_menu_callback(int p_idx) {49if (p_idx == MENU_GENERATE_VISIBILITY_RECT) {50if (need_show_lifetime_dialog(generate_seconds)) {51generate_visibility_rect->popup_centered();52} else {53_generate_visibility_rect();54}55} else {56Particles2DEditorPlugin::_menu_callback(p_idx);57}58}5960void GPUParticles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {61Particles2DEditorPlugin::_add_menu_options(p_menu);62p_menu->add_item(TTR("Generate Visibility Rect"), MENU_GENERATE_VISIBILITY_RECT);63}6465void Particles2DEditorPlugin::_browse_mask_texture_pressed() {66browsing_texture_type = TEXTURE_TYPE_MASK;67file_dialog->popup_file_dialog();68}6970void Particles2DEditorPlugin::_browse_direction_texture_pressed() {71browsing_texture_type = TEXTURE_TYPE_DIRECTION;72file_dialog->popup_centered();73}7475void Particles2DEditorPlugin::_file_selected(const String &p_file) {76switch (browsing_texture_type) {77case TEXTURE_TYPE_MASK: {78mask_img_path_line_edit->set_text(p_file);79break;80}81case TEXTURE_TYPE_DIRECTION: {82direction_img_path_line_edit->set_text(p_file);83break;84}85}8687_validate_textures();88}8990void Particles2DEditorPlugin::_process_emission_masks(PackedVector2Array &r_valid_positions, PackedVector2Array &r_valid_normals, PackedByteArray &r_valid_colors, Vector2i &r_image_size) {91Ref<Image> mask_img;92mask_img.instantiate();93Error err = ImageLoader::load_image(mask_img_path_line_edit->get_text(), mask_img);94ERR_FAIL_COND_MSG(err != OK, vformat("Error loading image '%s'.", mask_img_path_line_edit->get_text()));9596if (mask_img->is_compressed()) {97mask_img->decompress();98}99mask_img->convert(Image::FORMAT_RGBA8);100ERR_FAIL_COND(mask_img->get_format() != Image::FORMAT_RGBA8);101Size2i mask_img_size = mask_img->get_size();102ERR_FAIL_COND(mask_img_size.width == 0 || mask_img_size.height == 0);103104r_image_size = mask_img_size;105106r_valid_positions.resize(mask_img_size.width * mask_img_size.height);107108MaskMode emission_mode = static_cast<MaskMode>(emission_mask_mode->get_selected());109DirectionMode direction_mode = static_cast<DirectionMode>(emission_direction_mode->get_selected());110111if (direction_mode != DIRECTION_MODE_NONE) {112r_valid_normals.resize(mask_img_size.width * mask_img_size.height);113}114115bool capture_colors = emission_mask_colors->is_pressed();116117if (capture_colors) {118r_valid_colors.resize(mask_img_size.width * mask_img_size.height * 4);119}120121int valid_point_count = 0;122123{124Vector<uint8_t> mask_img_data = mask_img->get_data();125const uint8_t *mask_img_ptr = mask_img_data.ptr();126127for (int mask_img_x = 0; mask_img_x < mask_img_size.width; mask_img_x++) {128for (int mask_img_y = 0; mask_img_y < mask_img_size.height; mask_img_y++) {129uint8_t mask_alpha = mask_img_ptr[(mask_img_y * mask_img_size.width + mask_img_x) * 4 + 3];130131if (mask_alpha <= 128) {132continue;133}134135if (emission_mode == MASK_MODE_SOLID) {136r_valid_positions.write[valid_point_count++] = Point2(mask_img_x, mask_img_y);137} else {138bool pixel_is_on_border = false;139for (int x = mask_img_x - 1; x <= mask_img_x + 1; x++) {140for (int y = mask_img_y - 1; y <= mask_img_y + 1; y++) {141if (x < 0 || y < 0 || x >= mask_img_size.width || y >= mask_img_size.height || mask_img_ptr[(y * mask_img_size.width + x) * 4 + 3] <= 128) {142pixel_is_on_border = true;143break;144}145}146147if (pixel_is_on_border) {148break;149}150}151152if (!pixel_is_on_border) {153continue;154}155156r_valid_positions.write[valid_point_count] = Point2(mask_img_x, mask_img_y);157158if (direction_mode == DIRECTION_MODE_GENERATE) {159Vector2 normal;160for (int x = mask_img_x - 2; x <= mask_img_x + 2; x++) {161for (int y = mask_img_y - 2; y <= mask_img_y + 2; y++) {162if (x == mask_img_x && y == mask_img_y) {163continue;164}165166if (x < 0 || y < 0 || x >= mask_img_size.width || y >= mask_img_size.height || mask_img_ptr[(y * mask_img_size.width + x) * 4 + 3] <= 128) {167normal += Vector2(x - mask_img_x, y - mask_img_y).normalized();168}169}170}171172normal.normalize();173r_valid_normals.write[valid_point_count] = normal;174}175176valid_point_count++;177}178}179}180181if (capture_colors) {182for (int i = 0; i < valid_point_count; ++i) {183const Point2i point = r_valid_positions.get(i);184r_valid_colors.write[i * 4 + 0] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 0];185r_valid_colors.write[i * 4 + 1] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 1];186r_valid_colors.write[i * 4 + 2] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 2];187r_valid_colors.write[i * 4 + 3] = mask_img_ptr[(point.y * mask_img_size.width + point.x) * 4 + 3];188}189}190}191192if (direction_mode == DIRECTION_MODE_TEXTURE) {193Ref<Image> normal_img;194normal_img.instantiate();195err = ImageLoader::load_image(direction_img_path_line_edit->get_text(), normal_img);196ERR_FAIL_COND_MSG(err != OK, vformat("Error loading image '%s'.", direction_img_path_line_edit->get_text()));197198if (normal_img->is_compressed()) {199normal_img->decompress();200}201normal_img->convert(Image::FORMAT_RGB8);202ERR_FAIL_COND(normal_img->get_format() != Image::FORMAT_RGB8);203Size2i normal_img_size = normal_img->get_size();204ERR_FAIL_COND(normal_img_size.width == 0 || normal_img_size.height == 0);205ERR_FAIL_COND_MSG(normal_img_size != mask_img_size, "Mask and Normal texture must have the same size.");206207Vector<uint8_t> normal_img_data = normal_img->get_data();208const uint8_t *normal_img_ptr = normal_img_data.ptr();209210for (int i = 0; i < valid_point_count; ++i) {211const Point2i point = r_valid_positions.get(i);212const uint8_t normal_r = normal_img_ptr[(point.y * normal_img_size.width + point.x) * 3 + 0];213const uint8_t normal_g = normal_img_ptr[(point.y * normal_img_size.width + point.x) * 3 + 1];214215Vector2 normal;216normal.x = static_cast<float>(normal_r) / 255.0f - 0.5f;217normal.y = static_cast<float>(normal_g) / 255.0f - 0.5f;218219normal.normalize();220221r_valid_normals.write[i] = normal;222}223}224225r_valid_positions.resize(valid_point_count);226if (!r_valid_normals.is_empty()) {227r_valid_normals.resize(valid_point_count);228}229}230231Particles2DEditorPlugin::Particles2DEditorPlugin() {232file_dialog = memnew(EditorFileDialog);233234List<String> ext;235ImageLoader::get_recognized_extensions(&ext);236for (const String &E : ext) {237file_dialog->add_filter("*." + E, E.to_upper());238}239240file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);241file_dialog->connect("file_selected", callable_mp(this, &Particles2DEditorPlugin::_file_selected));242243emission_mask_dialog = memnew(ConfirmationDialog);244emission_mask_dialog->set_title(TTRC("Load Emission Mask"));245emission_mask_dialog->add_child(file_dialog);246emission_mask_dialog->get_ok_button()->set_disabled(true);247248VBoxContainer *emvb = memnew(VBoxContainer);249emission_mask_dialog->add_child(emvb);250251HBoxContainer *mask_img_hbox = memnew(HBoxContainer);252253mask_img_path_line_edit = memnew(LineEdit);254mask_img_hbox->add_child(mask_img_path_line_edit);255mask_img_path_line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);256mask_img_path_line_edit->set_editable(false);257mask_img_path_line_edit->connect(SceneStringName(text_changed), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));258259mask_browse_button = memnew(Button);260mask_img_hbox->add_child(mask_browse_button);261mask_browse_button->connect(SceneStringName(pressed), callable_mp(this, &Particles2DEditorPlugin::_browse_mask_texture_pressed));262emvb->add_margin_child(TTRC("Mask Texture"), mask_img_hbox);263264emission_mask_mode = memnew(OptionButton);265emission_mask_mode->add_item(TTRC("Solid Pixels"), MASK_MODE_SOLID);266emission_mask_mode->add_item(TTRC("Border Pixels"), MASK_MODE_BORDER);267emission_mask_mode->connect(SceneStringName(item_selected), callable_mp(this, &Particles2DEditorPlugin::_emission_mask_mode_item_changed));268emvb->add_margin_child(TTRC("Mask Mode"), emission_mask_mode);269270emission_direction_mode = memnew(OptionButton);271emission_direction_mode->add_item(TTRC("None"), DIRECTION_MODE_NONE);272emission_direction_mode->add_item(TTRC("Generate"), DIRECTION_MODE_GENERATE);273emission_direction_mode->add_item(TTRC("Texture"), DIRECTION_MODE_TEXTURE);274emission_direction_mode->connect(SceneStringName(item_selected), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));275emission_direction_mode->set_item_disabled(DIRECTION_MODE_GENERATE, true);276emvb->add_margin_child(TTRC("Direction Mode"), emission_direction_mode);277278direction_img_label = memnew(Label);279direction_img_label->set_text(TTRC("Direction Texture"));280direction_img_label->set_theme_type_variation("HeaderSmall");281emvb->add_child(direction_img_label);282direction_img_label->hide();283284direction_img_hbox = memnew(HBoxContainer);285direction_img_path_line_edit = memnew(LineEdit);286direction_img_hbox->add_child(direction_img_path_line_edit);287direction_img_path_line_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);288direction_img_path_line_edit->set_editable(false);289direction_img_path_line_edit->connect(SceneStringName(text_changed), callable_mp(this, &Particles2DEditorPlugin::_validate_textures).unbind(1));290291direction_browse_button = memnew(Button);292direction_img_hbox->add_child(direction_browse_button);293direction_browse_button->connect(SceneStringName(pressed), callable_mp(this, &Particles2DEditorPlugin::_browse_direction_texture_pressed));294emvb->add_child(direction_img_hbox);295direction_img_hbox->hide();296297VBoxContainer *optionsvb = memnew(VBoxContainer);298emvb->add_margin_child(TTRC("Options"), optionsvb);299300emission_mask_centered = memnew(CheckBox(TTRC("Centered")));301emission_mask_centered->set_pressed(true);302optionsvb->add_child(emission_mask_centered);303emission_mask_colors = memnew(CheckBox(TTRC("Copy Color from Mask Texture")));304optionsvb->add_child(emission_mask_colors);305306error_message = memnew(Label);307error_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);308error_message->set_h_size_flags(Control::SIZE_EXPAND_FILL);309error_message->add_theme_color_override(SceneStringName(font_color), EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));310emvb->add_child(error_message);311312EditorNode::get_singleton()->get_gui_base()->add_child(emission_mask_dialog);313314emission_mask_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Particles2DEditorPlugin::_generate_emission_mask));315emission_mask_dialog->connect(SceneStringName(theme_changed), callable_mp(this, &Particles2DEditorPlugin::_theme_changed));316}317318void Particles2DEditorPlugin::_set_show_gizmos(Node *p_node, bool p_show) {319GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_node);320if (gpu_particles) {321gpu_particles->set_show_gizmos(p_show);322}323CPUParticles2D *cpu_particles = Object::cast_to<CPUParticles2D>(p_node);324if (cpu_particles) {325cpu_particles->set_show_gizmos(p_show);326}327328// The `selection_changed` signal is deferred. A node could be deleted before the signal is emitted.329if (p_show) {330p_node->connect(SceneStringName(tree_exiting), callable_mp(this, &Particles2DEditorPlugin::_node_removed).bind(p_node));331} else {332p_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Particles2DEditorPlugin::_node_removed));333}334}335336void Particles2DEditorPlugin::_selection_changed() {337const List<Node *> ¤t_selection = EditorNode::get_singleton()->get_editor_selection()->get_top_selected_node_list();338if (selected_particles.is_empty() && current_selection.is_empty()) {339return;340}341342// Turn gizmos on for nodes that are newly selected.343HashSet<ObjectID> nodes_in_current_selection;344for (Node *node : current_selection) {345ObjectID nid = node->get_instance_id();346nodes_in_current_selection.insert(nid);347if (!selected_particles.has(nid)) {348_set_show_gizmos(node, true);349selected_particles.insert(nid);350}351}352353mask_img_path_line_edit->set_text("");354emission_mask_mode->select(MASK_MODE_SOLID);355emission_direction_mode->select(DIRECTION_MODE_NONE);356emission_mask_centered->set_pressed(true);357emission_mask_colors->set_pressed(false);358direction_img_path_line_edit->set_text("");359360// Turn gizmos off for nodes that are no longer selected.361LocalVector<ObjectID> to_erase;362for (const ObjectID &nid : selected_particles) {363if (!nodes_in_current_selection.has(nid)) {364Node *node = ObjectDB::get_instance<Node>(nid);365if (node) {366_set_show_gizmos(node, false);367}368to_erase.push_back(nid);369}370}371372for (const ObjectID &nid : to_erase) {373selected_particles.erase(nid);374}375}376377void Particles2DEditorPlugin::_node_removed(Node *p_node) {378if (p_node && selected_particles.erase(p_node->get_instance_id())) {379_set_show_gizmos(p_node, false);380}381}382383void GPUParticles2DEditorPlugin::_generate_visibility_rect() {384GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);385386double time = generate_seconds->get_value();387388float running = 0.0;389390EditorProgress ep("gen_vrect", TTR("Generating Visibility Rect (Waiting for Particle Simulation)"), int(time));391392bool was_emitting = particles->is_emitting();393if (!was_emitting) {394particles->set_emitting(true);395OS::get_singleton()->delay_usec(1000);396}397398Rect2 rect;399while (running < time) {400uint64_t ticks = OS::get_singleton()->get_ticks_usec();401ep.step(TTR("Generating..."), int(running), true);402OS::get_singleton()->delay_usec(1000);403404Rect2 capture = particles->capture_rect();405if (rect == Rect2()) {406rect = capture;407} else {408rect = rect.merge(capture);409}410411running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;412}413414if (!was_emitting) {415particles->set_emitting(false);416}417418EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();419undo_redo->create_action(TTR("Generate Visibility Rect"));420undo_redo->add_do_method(particles, "set_visibility_rect", rect);421undo_redo->add_undo_method(particles, "set_visibility_rect", particles->get_visibility_rect());422undo_redo->commit_action();423}424425void Particles2DEditorPlugin::_notification(int p_what) {426switch (p_what) {427case NOTIFICATION_ENTER_TREE: {428EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", callable_mp(this, &Particles2DEditorPlugin::_selection_changed));429} break;430}431}432433void Particles2DEditorPlugin::_theme_changed() {434mask_browse_button->set_button_icon(mask_browse_button->get_editor_theme_icon(SNAME("FileBrowse")));435direction_browse_button->set_button_icon(direction_browse_button->get_editor_theme_icon(SNAME("FileBrowse")));436}437438void Particles2DEditorPlugin::_menu_callback(int p_idx) {439if (p_idx == MENU_LOAD_EMISSION_MASK) {440GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);441if (particles && particles->get_process_material().is_null()) {442EditorNode::get_singleton()->show_warning(TTR("Loading emission mask requires ParticleProcessMaterial."));443return;444}445446emission_mask_dialog->popup_centered();447} else {448ParticlesEditorPlugin::_menu_callback(p_idx);449}450}451452void Particles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {453p_menu->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);454}455456void Particles2DEditorPlugin::_validate_textures() {457DirectionMode direction_mode = static_cast<DirectionMode>(emission_direction_mode->get_selected());458direction_img_label->set_visible(direction_mode == DIRECTION_MODE_TEXTURE);459direction_img_hbox->set_visible(direction_mode == DIRECTION_MODE_TEXTURE);460461error_message->hide();462emission_mask_dialog->get_ok_button()->set_disabled(true);463464if (mask_img_path_line_edit->get_text().is_empty()) {465emission_mask_dialog->reset_size();466return;467}468469Ref<Image> mask_img;470mask_img.instantiate();471Error err = ImageLoader::load_image(mask_img_path_line_edit->get_text(), mask_img);472if (err != OK) {473error_message->show();474error_message->set_text(TTRC("Failed to load mask texture."));475emission_mask_dialog->reset_size();476return;477}478479if (mask_img->is_compressed()) {480mask_img->decompress();481}482mask_img->convert(Image::FORMAT_RGBA8);483484if (mask_img->get_format() != Image::FORMAT_RGBA8) {485error_message->show();486error_message->set_text(TTRC("Failed to convert mask texture to RGBA8."));487emission_mask_dialog->reset_size();488return;489}490491Size2i mask_img_size = mask_img->get_size();492if (mask_img_size.width == 0 || mask_img_size.height == 0) {493error_message->show();494error_message->set_text(TTRC("Mask texture has an invalid size."));495emission_mask_dialog->reset_size();496return;497}498499if (direction_mode == DIRECTION_MODE_TEXTURE) {500if (direction_img_path_line_edit->get_text().is_empty()) {501return;502}503504Ref<Image> direction_img;505direction_img.instantiate();506err = ImageLoader::load_image(direction_img_path_line_edit->get_text(), direction_img);507508if (err != OK) {509error_message->show();510error_message->set_text(TTRC("Failed to load direction texture."));511emission_mask_dialog->reset_size();512return;513}514515if (direction_img->is_compressed()) {516direction_img->decompress();517}518direction_img->convert(Image::FORMAT_RGBA8);519520if (direction_img->get_format() != Image::FORMAT_RGBA8) {521error_message->show();522error_message->set_text(TTRC("Failed to convert direction texture to RGBA8."));523emission_mask_dialog->reset_size();524return;525}526527Size2i direction_img_size = direction_img->get_size();528529if (direction_img_size.width == 0 || direction_img_size.height == 0 || direction_img_size != mask_img_size) {530error_message->show();531error_message->set_text(TTRC("Direction texture has an invalid size. It must have the same size as the mask texture."));532emission_mask_dialog->reset_size();533return;534}535}536537emission_mask_dialog->get_ok_button()->set_disabled(false);538emission_mask_dialog->reset_size();539}540541void Particles2DEditorPlugin::_emission_mask_mode_item_changed(int p_idx) const {542emission_direction_mode->set_item_disabled(DIRECTION_MODE_GENERATE, p_idx == static_cast<int>(MASK_MODE_SOLID));543544if (emission_direction_mode->get_selected() == DIRECTION_MODE_GENERATE) {545emission_direction_mode->select(DIRECTION_MODE_NONE);546}547}548549Node *GPUParticles2DEditorPlugin::_convert_particles() {550GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);551552CPUParticles2D *cpu_particles = memnew(CPUParticles2D);553cpu_particles->convert_from_particles(particles);554cpu_particles->set_name(particles->get_name());555cpu_particles->set_transform(particles->get_transform());556cpu_particles->set_visible(particles->is_visible());557cpu_particles->set_process_mode(particles->get_process_mode());558cpu_particles->set_z_index(particles->get_z_index());559return cpu_particles;560}561562void GPUParticles2DEditorPlugin::_generate_emission_mask() {563GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);564Ref<ParticleProcessMaterial> pm = particles->get_process_material();565ERR_FAIL_COND(pm.is_null());566567PackedVector2Array emission_positions;568PackedVector2Array emission_normals;569PackedByteArray emission_colors;570Vector2i texture_size;571_process_emission_masks(emission_positions, emission_normals, emission_colors, texture_size);572573ERR_FAIL_COND_MSG(emission_positions.is_empty(), "No pixels with transparency > 128 in image...");574575EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();576undo_redo->create_action(TTR("Load Emission Mask"));577ParticleProcessMaterial *pmptr = pm.ptr();578579Vector<uint8_t> mask_texture_data;580581int valid_positions_count = emission_positions.size();582int w = 2048;583int h = (valid_positions_count / 2048) + 1;584585mask_texture_data.resize_initialized(w * h * 2 * sizeof(float));586587{588Vector2 offset;589if (emission_mask_centered->is_pressed()) {590offset = Vector2(-texture_size.width * 0.5, -texture_size.height * 0.5);591}592593uint8_t *tw = mask_texture_data.ptrw();594float *twf = reinterpret_cast<float *>(tw);595for (int i = 0; i < valid_positions_count; i++) {596twf[i * 2 + 0] = emission_positions[i].x + offset.x;597twf[i * 2 + 1] = emission_positions[i].y + offset.y;598}599}600601Ref<Image> img;602img.instantiate();603img->set_data(w, h, false, Image::FORMAT_RGF, mask_texture_data);604undo_redo->add_do_property(pmptr, "emission_point_texture", ImageTexture::create_from_image(img));605undo_redo->add_undo_property(pmptr, "emission_point_texture", pm->get_emission_point_texture());606undo_redo->add_do_property(pmptr, "emission_point_count", valid_positions_count);607undo_redo->add_undo_property(pmptr, "emission_point_count", pm->get_emission_point_count());608609if (emission_mask_colors->is_pressed()) {610Vector<uint8_t> color_texture_data;611color_texture_data.resize_initialized(w * h * 4);612613{614uint8_t *tw = color_texture_data.ptrw();615for (int i = 0; i < valid_positions_count * 4; i++) {616tw[i] = emission_colors[i];617}618}619620img.instantiate();621img->set_data(w, h, false, Image::FORMAT_RGBA8, color_texture_data);622undo_redo->add_do_property(pmptr, "emission_color_texture", ImageTexture::create_from_image(img));623undo_redo->add_undo_property(pmptr, "emission_color_texture", pm->get_emission_color_texture());624}625626if (emission_normals.size()) {627undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);628undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());629pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);630631Vector<uint8_t> normal_texture_data;632normal_texture_data.resize_initialized(w * h * 2 * sizeof(float));633634{635uint8_t *tw = normal_texture_data.ptrw();636float *twf = reinterpret_cast<float *>(tw);637for (int i = 0; i < valid_positions_count; i++) {638twf[i * 2 + 0] = emission_normals[i].x;639twf[i * 2 + 1] = emission_normals[i].y;640}641}642643img.instantiate();644img->set_data(w, h, false, Image::FORMAT_RGF, normal_texture_data);645undo_redo->add_do_property(pmptr, "emission_normal_texture", ImageTexture::create_from_image(img));646undo_redo->add_undo_property(pmptr, "emission_normal_texture", pm->get_emission_normal_texture());647} else {648undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);649undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());650}651undo_redo->commit_action();652}653654GPUParticles2DEditorPlugin::GPUParticles2DEditorPlugin() {655handled_type = TTRC("GPUParticles2D");656conversion_option_name = TTR("Convert to CPUParticles2D");657658generate_visibility_rect = memnew(ConfirmationDialog);659generate_visibility_rect->set_title(TTR("Generate Visibility Rect"));660661VBoxContainer *genvb = memnew(VBoxContainer);662generate_visibility_rect->add_child(genvb);663664generate_seconds = memnew(SpinBox);665generate_seconds->set_min(0.1);666generate_seconds->set_max(25);667generate_seconds->set_value(2);668genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);669670EditorNode::get_singleton()->get_gui_base()->add_child(generate_visibility_rect);671672generate_visibility_rect->connect(SceneStringName(confirmed), callable_mp(this, &GPUParticles2DEditorPlugin::_generate_visibility_rect));673}674675Node *CPUParticles2DEditorPlugin::_convert_particles() {676CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);677678GPUParticles2D *gpu_particles = memnew(GPUParticles2D);679gpu_particles->convert_from_particles(particles);680gpu_particles->set_name(particles->get_name());681gpu_particles->set_transform(particles->get_transform());682gpu_particles->set_visible(particles->is_visible());683gpu_particles->set_process_mode(particles->get_process_mode());684return gpu_particles;685}686687void CPUParticles2DEditorPlugin::_generate_emission_mask() {688CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);689690PackedVector2Array valid_positions;691PackedVector2Array valid_normals;692PackedByteArray valid_colors;693Vector2i image_size;694_process_emission_masks(valid_positions, valid_normals, valid_colors, image_size);695696ERR_FAIL_COND_MSG(valid_positions.is_empty(), "No pixels with transparency > 128 in image...");697698EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();699undo_redo->create_action(TTR("Load Emission Mask"));700701int valid_point_count = valid_positions.size();702if (emission_mask_colors->is_pressed()) {703PackedColorArray pca;704pca.resize(valid_point_count);705Color *pcaw = pca.ptrw();706for (int i = 0; i < valid_point_count; i += 1) {707Color color;708color.r = valid_colors[i * 4 + 0] / 255.0f;709color.g = valid_colors[i * 4 + 1] / 255.0f;710color.b = valid_colors[i * 4 + 2] / 255.0f;711color.a = valid_colors[i * 4 + 3] / 255.0f;712pcaw[i] = color;713}714undo_redo->add_do_property(particles, "emission_colors", pca);715undo_redo->add_undo_property(particles, "emission_colors", particles->get_emission_colors());716}717718if (!valid_normals.is_empty()) {719undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_DIRECTED_POINTS);720undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());721PackedVector2Array norms;722norms.resize(valid_normals.size());723Vector2 *normsw = norms.ptrw();724for (int i = 0; i < valid_normals.size(); i += 1) {725normsw[i] = valid_normals[i];726}727undo_redo->add_do_property(particles, "emission_normals", norms);728undo_redo->add_undo_property(particles, "emission_normals", particles->get_emission_normals());729} else {730undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_POINTS);731undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());732}733734{735Vector2 offset;736if (emission_mask_centered->is_pressed()) {737offset = Vector2(-image_size.width * 0.5, -image_size.height * 0.5);738}739740PackedVector2Array points;741points.resize(valid_positions.size());742Vector2 *pointsw = points.ptrw();743for (int i = 0; i < valid_positions.size(); i += 1) {744pointsw[i] = valid_positions[i] + offset;745}746undo_redo->add_do_property(particles, "emission_points", points);747undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_points());748}749undo_redo->commit_action();750}751752CPUParticles2DEditorPlugin::CPUParticles2DEditorPlugin() {753handled_type = TTRC("CPUParticles2D");754conversion_option_name = TTR("Convert to GPUParticles2D");755}756757758