2
0

particles_2d_editor_plugin.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /**************************************************************************/
  2. /* particles_2d_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "particles_2d_editor_plugin.h"
  31. #include "core/io/image_loader.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/gui/editor_file_dialog.h"
  35. #include "scene/2d/cpu_particles_2d.h"
  36. #include "scene/2d/gpu_particles_2d.h"
  37. #include "scene/gui/box_container.h"
  38. #include "scene/gui/check_box.h"
  39. #include "scene/gui/option_button.h"
  40. #include "scene/gui/popup_menu.h"
  41. #include "scene/gui/spin_box.h"
  42. #include "scene/resources/image_texture.h"
  43. #include "scene/resources/particle_process_material.h"
  44. void GPUParticles2DEditorPlugin::_menu_callback(int p_idx) {
  45. if (p_idx == MENU_GENERATE_VISIBILITY_RECT) {
  46. if (need_show_lifetime_dialog(generate_seconds)) {
  47. generate_visibility_rect->popup_centered();
  48. } else {
  49. _generate_visibility_rect();
  50. }
  51. } else {
  52. Particles2DEditorPlugin::_menu_callback(p_idx);
  53. }
  54. }
  55. void GPUParticles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
  56. Particles2DEditorPlugin::_add_menu_options(p_menu);
  57. p_menu->add_item(TTR("Generate Visibility Rect"), MENU_GENERATE_VISIBILITY_RECT);
  58. }
  59. void Particles2DEditorPlugin::_file_selected(const String &p_file) {
  60. source_emission_file = p_file;
  61. emission_mask->popup_centered();
  62. }
  63. void Particles2DEditorPlugin::_get_base_emission_mask(PackedVector2Array &r_valid_positions, PackedVector2Array &r_valid_normals, PackedByteArray &r_valid_colors, Vector2i &r_image_size) {
  64. Ref<Image> img;
  65. img.instantiate();
  66. Error err = ImageLoader::load_image(source_emission_file, img);
  67. ERR_FAIL_COND_MSG(err != OK, "Error loading image '" + source_emission_file + "'.");
  68. if (img->is_compressed()) {
  69. img->decompress();
  70. }
  71. img->convert(Image::FORMAT_RGBA8);
  72. ERR_FAIL_COND(img->get_format() != Image::FORMAT_RGBA8);
  73. Size2i s = img->get_size();
  74. ERR_FAIL_COND(s.width == 0 || s.height == 0);
  75. r_image_size = s;
  76. r_valid_positions.resize(s.width * s.height);
  77. EmissionMode emode = (EmissionMode)emission_mask_mode->get_selected();
  78. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  79. r_valid_normals.resize(s.width * s.height);
  80. }
  81. bool capture_colors = emission_colors->is_pressed();
  82. if (capture_colors) {
  83. r_valid_colors.resize(s.width * s.height * 4);
  84. }
  85. int vpc = 0;
  86. {
  87. Vector<uint8_t> img_data = img->get_data();
  88. const uint8_t *r = img_data.ptr();
  89. for (int i = 0; i < s.width; i++) {
  90. for (int j = 0; j < s.height; j++) {
  91. uint8_t a = r[(j * s.width + i) * 4 + 3];
  92. if (a > 128) {
  93. if (emode == EMISSION_MODE_SOLID) {
  94. if (capture_colors) {
  95. r_valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  96. r_valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  97. r_valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  98. r_valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  99. }
  100. r_valid_positions.write[vpc++] = Point2(i, j);
  101. } else {
  102. bool on_border = false;
  103. for (int x = i - 1; x <= i + 1; x++) {
  104. for (int y = j - 1; y <= j + 1; y++) {
  105. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  106. on_border = true;
  107. break;
  108. }
  109. }
  110. if (on_border) {
  111. break;
  112. }
  113. }
  114. if (on_border) {
  115. r_valid_positions.write[vpc] = Point2(i, j);
  116. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  117. Vector2 normal;
  118. for (int x = i - 2; x <= i + 2; x++) {
  119. for (int y = j - 2; y <= j + 2; y++) {
  120. if (x == i && y == j) {
  121. continue;
  122. }
  123. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  124. normal += Vector2(x - i, y - j).normalized();
  125. }
  126. }
  127. }
  128. normal.normalize();
  129. r_valid_normals.write[vpc] = normal;
  130. }
  131. if (capture_colors) {
  132. r_valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  133. r_valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  134. r_valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  135. r_valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  136. }
  137. vpc++;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. r_valid_positions.resize(vpc);
  145. if (!r_valid_normals.is_empty()) {
  146. r_valid_normals.resize(vpc);
  147. }
  148. }
  149. Particles2DEditorPlugin::Particles2DEditorPlugin() {
  150. file = memnew(EditorFileDialog);
  151. List<String> ext;
  152. ImageLoader::get_recognized_extensions(&ext);
  153. for (const String &E : ext) {
  154. file->add_filter("*." + E, E.to_upper());
  155. }
  156. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  157. EditorNode::get_singleton()->get_gui_base()->add_child(file);
  158. file->connect("file_selected", callable_mp(this, &Particles2DEditorPlugin::_file_selected));
  159. emission_mask = memnew(ConfirmationDialog);
  160. emission_mask->set_title(TTR("Load Emission Mask"));
  161. VBoxContainer *emvb = memnew(VBoxContainer);
  162. emission_mask->add_child(emvb);
  163. emission_mask_mode = memnew(OptionButton);
  164. emission_mask_mode->add_item(TTR("Solid Pixels"), EMISSION_MODE_SOLID);
  165. emission_mask_mode->add_item(TTR("Border Pixels"), EMISSION_MODE_BORDER);
  166. emission_mask_mode->add_item(TTR("Directed Border Pixels"), EMISSION_MODE_BORDER_DIRECTED);
  167. emvb->add_margin_child(TTR("Emission Mask"), emission_mask_mode);
  168. VBoxContainer *optionsvb = memnew(VBoxContainer);
  169. emvb->add_margin_child(TTR("Options"), optionsvb);
  170. emission_mask_centered = memnew(CheckBox(TTR("Centered")));
  171. optionsvb->add_child(emission_mask_centered);
  172. emission_colors = memnew(CheckBox(TTR("Capture Colors from Pixel")));
  173. optionsvb->add_child(emission_colors);
  174. EditorNode::get_singleton()->get_gui_base()->add_child(emission_mask);
  175. emission_mask->connect(SceneStringName(confirmed), callable_mp(this, &Particles2DEditorPlugin::_generate_emission_mask));
  176. }
  177. void Particles2DEditorPlugin::_set_show_gizmos(Node *p_node, bool p_show) {
  178. GPUParticles2D *gpu_particles = Object::cast_to<GPUParticles2D>(p_node);
  179. if (gpu_particles) {
  180. gpu_particles->set_show_gizmos(p_show);
  181. }
  182. CPUParticles2D *cpu_particles = Object::cast_to<CPUParticles2D>(p_node);
  183. if (cpu_particles) {
  184. cpu_particles->set_show_gizmos(p_show);
  185. }
  186. // The `selection_changed` signal is deferred. A node could be deleted before the signal is emitted.
  187. if (p_show) {
  188. p_node->connect(SceneStringName(tree_exiting), callable_mp(this, &Particles2DEditorPlugin::_node_removed).bind(p_node));
  189. } else {
  190. p_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Particles2DEditorPlugin::_node_removed));
  191. }
  192. }
  193. void Particles2DEditorPlugin::_selection_changed() {
  194. List<Node *> current_selection = EditorNode::get_singleton()->get_editor_selection()->get_top_selected_node_list();
  195. if (selected_particles.is_empty() && current_selection.is_empty()) {
  196. return;
  197. }
  198. // Turn gizmos off for nodes that are no longer selected.
  199. for (List<Node *>::Element *E = selected_particles.front(); E;) {
  200. Node *node = E->get();
  201. List<Node *>::Element *N = E->next();
  202. if (current_selection.find(node) == nullptr) {
  203. _set_show_gizmos(node, false);
  204. selected_particles.erase(E);
  205. }
  206. E = N;
  207. }
  208. // Turn gizmos on for nodes that are newly selected.
  209. for (Node *node : current_selection) {
  210. if (selected_particles.find(node) == nullptr) {
  211. _set_show_gizmos(node, true);
  212. selected_particles.push_back(node);
  213. }
  214. }
  215. }
  216. void Particles2DEditorPlugin::_node_removed(Node *p_node) {
  217. List<Node *>::Element *E = selected_particles.find(p_node);
  218. if (E) {
  219. _set_show_gizmos(E->get(), false);
  220. selected_particles.erase(E);
  221. }
  222. }
  223. void GPUParticles2DEditorPlugin::_generate_visibility_rect() {
  224. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  225. double time = generate_seconds->get_value();
  226. float running = 0.0;
  227. EditorProgress ep("gen_vrect", TTR("Generating Visibility Rect (Waiting for Particle Simulation)"), int(time));
  228. bool was_emitting = particles->is_emitting();
  229. if (!was_emitting) {
  230. particles->set_emitting(true);
  231. OS::get_singleton()->delay_usec(1000);
  232. }
  233. Rect2 rect;
  234. while (running < time) {
  235. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  236. ep.step(TTR("Generating..."), int(running), true);
  237. OS::get_singleton()->delay_usec(1000);
  238. Rect2 capture = particles->capture_rect();
  239. if (rect == Rect2()) {
  240. rect = capture;
  241. } else {
  242. rect = rect.merge(capture);
  243. }
  244. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  245. }
  246. if (!was_emitting) {
  247. particles->set_emitting(false);
  248. }
  249. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  250. undo_redo->create_action(TTR("Generate Visibility Rect"));
  251. undo_redo->add_do_method(particles, "set_visibility_rect", rect);
  252. undo_redo->add_undo_method(particles, "set_visibility_rect", particles->get_visibility_rect());
  253. undo_redo->commit_action();
  254. }
  255. void Particles2DEditorPlugin::_notification(int p_what) {
  256. switch (p_what) {
  257. case NOTIFICATION_ENTER_TREE: {
  258. EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", callable_mp(this, &Particles2DEditorPlugin::_selection_changed));
  259. } break;
  260. }
  261. }
  262. void Particles2DEditorPlugin::_menu_callback(int p_idx) {
  263. if (p_idx == MENU_LOAD_EMISSION_MASK) {
  264. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  265. if (particles && particles->get_process_material().is_null()) {
  266. EditorNode::get_singleton()->show_warning(TTR("Loading emission mask requires ParticleProcessMaterial."));
  267. return;
  268. }
  269. file->popup_file_dialog();
  270. } else {
  271. ParticlesEditorPlugin::_menu_callback(p_idx);
  272. }
  273. }
  274. void Particles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
  275. p_menu->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
  276. }
  277. Node *GPUParticles2DEditorPlugin::_convert_particles() {
  278. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  279. CPUParticles2D *cpu_particles = memnew(CPUParticles2D);
  280. cpu_particles->convert_from_particles(particles);
  281. cpu_particles->set_name(particles->get_name());
  282. cpu_particles->set_transform(particles->get_transform());
  283. cpu_particles->set_visible(particles->is_visible());
  284. cpu_particles->set_process_mode(particles->get_process_mode());
  285. cpu_particles->set_z_index(particles->get_z_index());
  286. return cpu_particles;
  287. }
  288. void GPUParticles2DEditorPlugin::_generate_emission_mask() {
  289. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  290. Ref<ParticleProcessMaterial> pm = particles->get_process_material();
  291. ERR_FAIL_COND(pm.is_null());
  292. PackedVector2Array valid_positions;
  293. PackedVector2Array valid_normals;
  294. PackedByteArray valid_colors;
  295. Vector2i image_size;
  296. _get_base_emission_mask(valid_positions, valid_normals, valid_colors, image_size);
  297. ERR_FAIL_COND_MSG(valid_positions.is_empty(), "No pixels with transparency > 128 in image...");
  298. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  299. undo_redo->create_action(TTR("Load Emission Mask"));
  300. ParticleProcessMaterial *pmptr = pm.ptr();
  301. Vector<uint8_t> texdata;
  302. int vpc = valid_positions.size();
  303. int w = 2048;
  304. int h = (vpc / 2048) + 1;
  305. texdata.resize(w * h * 2 * sizeof(float));
  306. {
  307. Vector2 offset;
  308. if (emission_mask_centered->is_pressed()) {
  309. offset = Vector2(-image_size.width * 0.5, -image_size.height * 0.5);
  310. }
  311. uint8_t *tw = texdata.ptrw();
  312. float *twf = reinterpret_cast<float *>(tw);
  313. for (int i = 0; i < vpc; i++) {
  314. twf[i * 2 + 0] = valid_positions[i].x + offset.x;
  315. twf[i * 2 + 1] = valid_positions[i].y + offset.y;
  316. }
  317. }
  318. Ref<Image> img;
  319. img.instantiate();
  320. img->set_data(w, h, false, Image::FORMAT_RGF, texdata);
  321. undo_redo->add_do_property(pmptr, "emission_point_texture", ImageTexture::create_from_image(img));
  322. undo_redo->add_undo_property(pmptr, "emission_point_texture", pm->get_emission_point_texture());
  323. undo_redo->add_do_property(pmptr, "emission_point_count", vpc);
  324. undo_redo->add_undo_property(pmptr, "emission_point_count", pm->get_emission_point_count());
  325. if (emission_colors->is_pressed()) {
  326. Vector<uint8_t> colordata;
  327. colordata.resize(w * h * 4); //use RG texture
  328. {
  329. uint8_t *tw = colordata.ptrw();
  330. for (int i = 0; i < vpc * 4; i++) {
  331. tw[i] = valid_colors[i];
  332. }
  333. }
  334. img.instantiate();
  335. img->set_data(w, h, false, Image::FORMAT_RGBA8, colordata);
  336. undo_redo->add_do_property(pmptr, "emission_color_texture", ImageTexture::create_from_image(img));
  337. undo_redo->add_undo_property(pmptr, "emission_color_texture", pm->get_emission_color_texture());
  338. }
  339. if (!valid_normals.is_empty()) {
  340. undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  341. undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());
  342. pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  343. Vector<uint8_t> normdata;
  344. normdata.resize(w * h * 2 * sizeof(float)); //use RG texture
  345. {
  346. uint8_t *tw = normdata.ptrw();
  347. float *twf = reinterpret_cast<float *>(tw);
  348. for (int i = 0; i < vpc; i++) {
  349. twf[i * 2 + 0] = valid_normals[i].x;
  350. twf[i * 2 + 1] = valid_normals[i].y;
  351. }
  352. }
  353. img.instantiate();
  354. img->set_data(w, h, false, Image::FORMAT_RGF, normdata);
  355. undo_redo->add_do_property(pmptr, "emission_normal_texture", ImageTexture::create_from_image(img));
  356. undo_redo->add_undo_property(pmptr, "emission_normal_texture", pm->get_emission_normal_texture());
  357. } else {
  358. undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
  359. undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());
  360. }
  361. undo_redo->commit_action();
  362. }
  363. GPUParticles2DEditorPlugin::GPUParticles2DEditorPlugin() {
  364. handled_type = TTRC("GPUParticles2D");
  365. conversion_option_name = TTR("Convert to CPUParticles2D");
  366. generate_visibility_rect = memnew(ConfirmationDialog);
  367. generate_visibility_rect->set_title(TTR("Generate Visibility Rect"));
  368. VBoxContainer *genvb = memnew(VBoxContainer);
  369. generate_visibility_rect->add_child(genvb);
  370. generate_seconds = memnew(SpinBox);
  371. generate_seconds->set_min(0.1);
  372. generate_seconds->set_max(25);
  373. generate_seconds->set_value(2);
  374. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  375. EditorNode::get_singleton()->get_gui_base()->add_child(generate_visibility_rect);
  376. generate_visibility_rect->connect(SceneStringName(confirmed), callable_mp(this, &GPUParticles2DEditorPlugin::_generate_visibility_rect));
  377. }
  378. Node *CPUParticles2DEditorPlugin::_convert_particles() {
  379. CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);
  380. GPUParticles2D *gpu_particles = memnew(GPUParticles2D);
  381. gpu_particles->convert_from_particles(particles);
  382. gpu_particles->set_name(particles->get_name());
  383. gpu_particles->set_transform(particles->get_transform());
  384. gpu_particles->set_visible(particles->is_visible());
  385. gpu_particles->set_process_mode(particles->get_process_mode());
  386. return gpu_particles;
  387. }
  388. void CPUParticles2DEditorPlugin::_generate_emission_mask() {
  389. CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);
  390. PackedVector2Array valid_positions;
  391. PackedVector2Array valid_normals;
  392. PackedByteArray valid_colors;
  393. Vector2i image_size;
  394. _get_base_emission_mask(valid_positions, valid_normals, valid_colors, image_size);
  395. ERR_FAIL_COND_MSG(valid_positions.is_empty(), "No pixels with transparency > 128 in image...");
  396. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  397. undo_redo->create_action(TTR("Load Emission Mask"));
  398. int vpc = valid_positions.size();
  399. if (emission_colors->is_pressed()) {
  400. PackedColorArray pca;
  401. pca.resize(vpc);
  402. Color *pcaw = pca.ptrw();
  403. for (int i = 0; i < vpc; i += 1) {
  404. Color color;
  405. color.r = valid_colors[i * 4 + 0] / 255.0f;
  406. color.g = valid_colors[i * 4 + 1] / 255.0f;
  407. color.b = valid_colors[i * 4 + 2] / 255.0f;
  408. color.a = valid_colors[i * 4 + 3] / 255.0f;
  409. pcaw[i] = color;
  410. }
  411. undo_redo->add_do_property(particles, "emission_colors", pca);
  412. undo_redo->add_undo_property(particles, "emission_colors", particles->get_emission_colors());
  413. }
  414. if (!valid_normals.is_empty()) {
  415. undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_DIRECTED_POINTS);
  416. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
  417. PackedVector2Array norms;
  418. norms.resize(valid_normals.size());
  419. Vector2 *normsw = norms.ptrw();
  420. for (int i = 0; i < valid_normals.size(); i += 1) {
  421. normsw[i] = valid_normals[i];
  422. }
  423. undo_redo->add_do_property(particles, "emission_normals", norms);
  424. undo_redo->add_undo_property(particles, "emission_normals", particles->get_emission_normals());
  425. } else {
  426. undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_POINTS);
  427. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
  428. }
  429. {
  430. Vector2 offset;
  431. if (emission_mask_centered->is_pressed()) {
  432. offset = Vector2(-image_size.width * 0.5, -image_size.height * 0.5);
  433. }
  434. PackedVector2Array points;
  435. points.resize(valid_positions.size());
  436. Vector2 *pointsw = points.ptrw();
  437. for (int i = 0; i < valid_positions.size(); i += 1) {
  438. pointsw[i] = valid_positions[i] + offset;
  439. }
  440. undo_redo->add_do_property(particles, "emission_points", points);
  441. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_points());
  442. }
  443. undo_redo->commit_action();
  444. }
  445. CPUParticles2DEditorPlugin::CPUParticles2DEditorPlugin() {
  446. handled_type = TTRC("CPUParticles2D");
  447. conversion_option_name = TTR("Convert to GPUParticles2D");
  448. }