particles_editor_plugin.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /**************************************************************************/
  2. /* particles_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_editor_plugin.h"
  31. #include "canvas_item_editor_plugin.h"
  32. #include "core/io/image_loader.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/gui/editor_file_dialog.h"
  37. #include "editor/scene_tree_dock.h"
  38. #include "scene/2d/cpu_particles_2d.h"
  39. #include "scene/2d/gpu_particles_2d.h"
  40. #include "scene/3d/cpu_particles_3d.h"
  41. #include "scene/3d/gpu_particles_3d.h"
  42. #include "scene/3d/mesh_instance_3d.h"
  43. #include "scene/gui/box_container.h"
  44. #include "scene/gui/menu_button.h"
  45. #include "scene/gui/spin_box.h"
  46. #include "scene/resources/image_texture.h"
  47. #include "scene/resources/particle_process_material.h"
  48. void ParticlesEditorPlugin::_notification(int p_what) {
  49. switch (p_what) {
  50. case NOTIFICATION_ENTER_TREE: {
  51. if (handled_type.ends_with("2D")) {
  52. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  53. } else if (handled_type.ends_with("3D")) {
  54. add_control_to_container(CONTAINER_SPATIAL_EDITOR_MENU, toolbar);
  55. } else {
  56. DEV_ASSERT(false);
  57. }
  58. menu->set_button_icon(menu->get_editor_theme_icon(handled_type));
  59. menu->set_text(handled_type);
  60. PopupMenu *popup = menu->get_popup();
  61. popup->add_shortcut(ED_SHORTCUT("particles/restart_emission", TTRC("Restart Emission"), KeyModifierMask::CTRL | Key::R), MENU_RESTART);
  62. _add_menu_options(popup);
  63. popup->add_item(conversion_option_name, MENU_OPTION_CONVERT);
  64. } break;
  65. }
  66. }
  67. bool ParticlesEditorPlugin::need_show_lifetime_dialog(SpinBox *p_seconds) {
  68. // Add one second to the default generation lifetime, since the progress is updated every second.
  69. p_seconds->set_value(MAX(1.0, trunc(edited_node->get("lifetime").operator double()) + 1.0));
  70. if (p_seconds->get_value() >= 11.0 + CMP_EPSILON) {
  71. // Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.
  72. return true;
  73. } else {
  74. // Generate the visibility rect/AABB immediately.
  75. return false;
  76. }
  77. }
  78. void ParticlesEditorPlugin::_menu_callback(int p_idx) {
  79. switch (p_idx) {
  80. case MENU_OPTION_CONVERT: {
  81. Node *converted_node = _convert_particles();
  82. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  83. ur->create_action(conversion_option_name, UndoRedo::MERGE_DISABLE, edited_node);
  84. SceneTreeDock::get_singleton()->replace_node(edited_node, converted_node);
  85. ur->commit_action(false);
  86. } break;
  87. case MENU_RESTART: {
  88. edited_node->call("restart");
  89. }
  90. }
  91. }
  92. void ParticlesEditorPlugin::edit(Object *p_object) {
  93. edited_node = Object::cast_to<Node>(p_object);
  94. }
  95. bool ParticlesEditorPlugin::handles(Object *p_object) const {
  96. return p_object->is_class(handled_type);
  97. }
  98. void ParticlesEditorPlugin::make_visible(bool p_visible) {
  99. toolbar->set_visible(p_visible);
  100. }
  101. ParticlesEditorPlugin::ParticlesEditorPlugin() {
  102. toolbar = memnew(HBoxContainer);
  103. toolbar->hide();
  104. menu = memnew(MenuButton);
  105. menu->set_switch_on_hover(true);
  106. toolbar->add_child(menu);
  107. menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ParticlesEditorPlugin::_menu_callback));
  108. }
  109. // 2D /////////////////////////////////////////////
  110. void GPUParticles2DEditorPlugin::_menu_callback(int p_idx) {
  111. if (p_idx == MENU_GENERATE_VISIBILITY_RECT) {
  112. if (need_show_lifetime_dialog(generate_seconds)) {
  113. generate_visibility_rect->popup_centered();
  114. } else {
  115. _generate_visibility_rect();
  116. }
  117. } else {
  118. Particles2DEditorPlugin::_menu_callback(p_idx);
  119. }
  120. }
  121. void GPUParticles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
  122. Particles2DEditorPlugin::_add_menu_options(p_menu);
  123. p_menu->add_item(TTR("Generate Visibility Rect"), MENU_GENERATE_VISIBILITY_RECT);
  124. }
  125. void Particles2DEditorPlugin::_file_selected(const String &p_file) {
  126. source_emission_file = p_file;
  127. emission_mask->popup_centered();
  128. }
  129. void Particles2DEditorPlugin::_get_base_emission_mask(PackedVector2Array &r_valid_positions, PackedVector2Array &r_valid_normals, PackedByteArray &r_valid_colors, Vector2i &r_image_size) {
  130. Ref<Image> img;
  131. img.instantiate();
  132. Error err = ImageLoader::load_image(source_emission_file, img);
  133. ERR_FAIL_COND_MSG(err != OK, "Error loading image '" + source_emission_file + "'.");
  134. if (img->is_compressed()) {
  135. img->decompress();
  136. }
  137. img->convert(Image::FORMAT_RGBA8);
  138. ERR_FAIL_COND(img->get_format() != Image::FORMAT_RGBA8);
  139. Size2i s = img->get_size();
  140. ERR_FAIL_COND(s.width == 0 || s.height == 0);
  141. r_image_size = s;
  142. r_valid_positions.resize(s.width * s.height);
  143. EmissionMode emode = (EmissionMode)emission_mask_mode->get_selected();
  144. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  145. r_valid_normals.resize(s.width * s.height);
  146. }
  147. bool capture_colors = emission_colors->is_pressed();
  148. if (capture_colors) {
  149. r_valid_colors.resize(s.width * s.height * 4);
  150. }
  151. int vpc = 0;
  152. {
  153. Vector<uint8_t> img_data = img->get_data();
  154. const uint8_t *r = img_data.ptr();
  155. for (int i = 0; i < s.width; i++) {
  156. for (int j = 0; j < s.height; j++) {
  157. uint8_t a = r[(j * s.width + i) * 4 + 3];
  158. if (a > 128) {
  159. if (emode == EMISSION_MODE_SOLID) {
  160. if (capture_colors) {
  161. r_valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  162. r_valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  163. r_valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  164. r_valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  165. }
  166. r_valid_positions.write[vpc++] = Point2(i, j);
  167. } else {
  168. bool on_border = false;
  169. for (int x = i - 1; x <= i + 1; x++) {
  170. for (int y = j - 1; y <= j + 1; y++) {
  171. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  172. on_border = true;
  173. break;
  174. }
  175. }
  176. if (on_border) {
  177. break;
  178. }
  179. }
  180. if (on_border) {
  181. r_valid_positions.write[vpc] = Point2(i, j);
  182. if (emode == EMISSION_MODE_BORDER_DIRECTED) {
  183. Vector2 normal;
  184. for (int x = i - 2; x <= i + 2; x++) {
  185. for (int y = j - 2; y <= j + 2; y++) {
  186. if (x == i && y == j) {
  187. continue;
  188. }
  189. if (x < 0 || y < 0 || x >= s.width || y >= s.height || r[(y * s.width + x) * 4 + 3] <= 128) {
  190. normal += Vector2(x - i, y - j).normalized();
  191. }
  192. }
  193. }
  194. normal.normalize();
  195. r_valid_normals.write[vpc] = normal;
  196. }
  197. if (capture_colors) {
  198. r_valid_colors.write[vpc * 4 + 0] = r[(j * s.width + i) * 4 + 0];
  199. r_valid_colors.write[vpc * 4 + 1] = r[(j * s.width + i) * 4 + 1];
  200. r_valid_colors.write[vpc * 4 + 2] = r[(j * s.width + i) * 4 + 2];
  201. r_valid_colors.write[vpc * 4 + 3] = r[(j * s.width + i) * 4 + 3];
  202. }
  203. vpc++;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. r_valid_positions.resize(vpc);
  211. if (!r_valid_normals.is_empty()) {
  212. r_valid_normals.resize(vpc);
  213. }
  214. }
  215. Particles2DEditorPlugin::Particles2DEditorPlugin() {
  216. file = memnew(EditorFileDialog);
  217. List<String> ext;
  218. ImageLoader::get_recognized_extensions(&ext);
  219. for (const String &E : ext) {
  220. file->add_filter("*." + E, E.to_upper());
  221. }
  222. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  223. EditorNode::get_singleton()->get_gui_base()->add_child(file);
  224. file->connect("file_selected", callable_mp(this, &Particles2DEditorPlugin::_file_selected));
  225. emission_mask = memnew(ConfirmationDialog);
  226. emission_mask->set_title(TTR("Load Emission Mask"));
  227. VBoxContainer *emvb = memnew(VBoxContainer);
  228. emission_mask->add_child(emvb);
  229. emission_mask_mode = memnew(OptionButton);
  230. emission_mask_mode->add_item(TTR("Solid Pixels"), EMISSION_MODE_SOLID);
  231. emission_mask_mode->add_item(TTR("Border Pixels"), EMISSION_MODE_BORDER);
  232. emission_mask_mode->add_item(TTR("Directed Border Pixels"), EMISSION_MODE_BORDER_DIRECTED);
  233. emvb->add_margin_child(TTR("Emission Mask"), emission_mask_mode);
  234. VBoxContainer *optionsvb = memnew(VBoxContainer);
  235. emvb->add_margin_child(TTR("Options"), optionsvb);
  236. emission_mask_centered = memnew(CheckBox(TTR("Centered")));
  237. optionsvb->add_child(emission_mask_centered);
  238. emission_colors = memnew(CheckBox(TTR("Capture Colors from Pixel")));
  239. optionsvb->add_child(emission_colors);
  240. EditorNode::get_singleton()->get_gui_base()->add_child(emission_mask);
  241. emission_mask->connect(SceneStringName(confirmed), callable_mp(this, &Particles2DEditorPlugin::_generate_emission_mask));
  242. }
  243. void GPUParticles2DEditorPlugin::_selection_changed() {
  244. List<Node *> selected_nodes = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list();
  245. if (selected_particles.is_empty() && selected_nodes.is_empty()) {
  246. return;
  247. }
  248. for (GPUParticles2D *particles : selected_particles) {
  249. particles->set_show_visibility_rect(false);
  250. }
  251. selected_particles.clear();
  252. for (Node *node : selected_nodes) {
  253. GPUParticles2D *selected_particle = Object::cast_to<GPUParticles2D>(node);
  254. if (selected_particle) {
  255. selected_particle->set_show_visibility_rect(true);
  256. selected_particles.push_back(selected_particle);
  257. }
  258. }
  259. }
  260. void GPUParticles2DEditorPlugin::_generate_visibility_rect() {
  261. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  262. double time = generate_seconds->get_value();
  263. float running = 0.0;
  264. EditorProgress ep("gen_vrect", TTR("Generating Visibility Rect (Waiting for Particle Simulation)"), int(time));
  265. bool was_emitting = particles->is_emitting();
  266. if (!was_emitting) {
  267. particles->set_emitting(true);
  268. OS::get_singleton()->delay_usec(1000);
  269. }
  270. Rect2 rect;
  271. while (running < time) {
  272. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  273. ep.step(TTR("Generating..."), int(running), true);
  274. OS::get_singleton()->delay_usec(1000);
  275. Rect2 capture = particles->capture_rect();
  276. if (rect == Rect2()) {
  277. rect = capture;
  278. } else {
  279. rect = rect.merge(capture);
  280. }
  281. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  282. }
  283. if (!was_emitting) {
  284. particles->set_emitting(false);
  285. }
  286. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  287. undo_redo->create_action(TTR("Generate Visibility Rect"));
  288. undo_redo->add_do_method(particles, "set_visibility_rect", rect);
  289. undo_redo->add_undo_method(particles, "set_visibility_rect", particles->get_visibility_rect());
  290. undo_redo->commit_action();
  291. }
  292. void GPUParticles2DEditorPlugin::_notification(int p_what) {
  293. switch (p_what) {
  294. case NOTIFICATION_ENTER_TREE: {
  295. EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", callable_mp(this, &GPUParticles2DEditorPlugin::_selection_changed));
  296. } break;
  297. }
  298. }
  299. void Particles2DEditorPlugin::_menu_callback(int p_idx) {
  300. if (p_idx == MENU_LOAD_EMISSION_MASK) {
  301. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  302. if (particles && particles->get_process_material().is_null()) {
  303. EditorNode::get_singleton()->show_warning(TTR("Loading emission mask requires ParticleProcessMaterial."));
  304. return;
  305. }
  306. file->popup_file_dialog();
  307. } else {
  308. ParticlesEditorPlugin::_menu_callback(p_idx);
  309. }
  310. }
  311. void Particles2DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
  312. p_menu->add_item(TTR("Load Emission Mask"), MENU_LOAD_EMISSION_MASK);
  313. }
  314. Node *GPUParticles2DEditorPlugin::_convert_particles() {
  315. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  316. CPUParticles2D *cpu_particles = memnew(CPUParticles2D);
  317. cpu_particles->convert_from_particles(particles);
  318. cpu_particles->set_name(particles->get_name());
  319. cpu_particles->set_transform(particles->get_transform());
  320. cpu_particles->set_visible(particles->is_visible());
  321. cpu_particles->set_process_mode(particles->get_process_mode());
  322. cpu_particles->set_z_index(particles->get_z_index());
  323. return cpu_particles;
  324. }
  325. void GPUParticles2DEditorPlugin::_generate_emission_mask() {
  326. GPUParticles2D *particles = Object::cast_to<GPUParticles2D>(edited_node);
  327. Ref<ParticleProcessMaterial> pm = particles->get_process_material();
  328. ERR_FAIL_COND(pm.is_null());
  329. PackedVector2Array valid_positions;
  330. PackedVector2Array valid_normals;
  331. PackedByteArray valid_colors;
  332. Vector2i image_size;
  333. _get_base_emission_mask(valid_positions, valid_normals, valid_colors, image_size);
  334. ERR_FAIL_COND_MSG(valid_positions.is_empty(), "No pixels with transparency > 128 in image...");
  335. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  336. undo_redo->create_action(TTR("Load Emission Mask"));
  337. ParticleProcessMaterial *pmptr = pm.ptr();
  338. Vector<uint8_t> texdata;
  339. int vpc = valid_positions.size();
  340. int w = 2048;
  341. int h = (vpc / 2048) + 1;
  342. texdata.resize(w * h * 2 * sizeof(float));
  343. {
  344. Vector2 offset;
  345. if (emission_mask_centered->is_pressed()) {
  346. offset = Vector2(-image_size.width * 0.5, -image_size.height * 0.5);
  347. }
  348. uint8_t *tw = texdata.ptrw();
  349. float *twf = reinterpret_cast<float *>(tw);
  350. for (int i = 0; i < vpc; i++) {
  351. twf[i * 2 + 0] = valid_positions[i].x + offset.x;
  352. twf[i * 2 + 1] = valid_positions[i].y + offset.y;
  353. }
  354. }
  355. Ref<Image> img;
  356. img.instantiate();
  357. img->set_data(w, h, false, Image::FORMAT_RGF, texdata);
  358. undo_redo->add_do_property(pmptr, "emission_point_texture", ImageTexture::create_from_image(img));
  359. undo_redo->add_undo_property(pmptr, "emission_point_texture", pm->get_emission_point_texture());
  360. undo_redo->add_do_property(pmptr, "emission_point_count", vpc);
  361. undo_redo->add_undo_property(pmptr, "emission_point_count", pm->get_emission_point_count());
  362. if (emission_colors->is_pressed()) {
  363. Vector<uint8_t> colordata;
  364. colordata.resize(w * h * 4); //use RG texture
  365. {
  366. uint8_t *tw = colordata.ptrw();
  367. for (int i = 0; i < vpc * 4; i++) {
  368. tw[i] = valid_colors[i];
  369. }
  370. }
  371. img.instantiate();
  372. img->set_data(w, h, false, Image::FORMAT_RGBA8, colordata);
  373. undo_redo->add_do_property(pmptr, "emission_color_texture", ImageTexture::create_from_image(img));
  374. undo_redo->add_undo_property(pmptr, "emission_color_texture", pm->get_emission_color_texture());
  375. }
  376. if (valid_normals.size()) {
  377. undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  378. undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());
  379. pm->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  380. Vector<uint8_t> normdata;
  381. normdata.resize(w * h * 2 * sizeof(float)); //use RG texture
  382. {
  383. uint8_t *tw = normdata.ptrw();
  384. float *twf = reinterpret_cast<float *>(tw);
  385. for (int i = 0; i < vpc; i++) {
  386. twf[i * 2 + 0] = valid_normals[i].x;
  387. twf[i * 2 + 1] = valid_normals[i].y;
  388. }
  389. }
  390. img.instantiate();
  391. img->set_data(w, h, false, Image::FORMAT_RGF, normdata);
  392. undo_redo->add_do_property(pmptr, "emission_normal_texture", ImageTexture::create_from_image(img));
  393. undo_redo->add_undo_property(pmptr, "emission_normal_texture", pm->get_emission_normal_texture());
  394. } else {
  395. undo_redo->add_do_property(pmptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
  396. undo_redo->add_undo_property(pmptr, "emission_shape", pm->get_emission_shape());
  397. }
  398. undo_redo->commit_action();
  399. }
  400. GPUParticles2DEditorPlugin::GPUParticles2DEditorPlugin() {
  401. handled_type = TTRC("GPUParticles2D");
  402. conversion_option_name = TTR("Convert to CPUParticles2D");
  403. generate_visibility_rect = memnew(ConfirmationDialog);
  404. generate_visibility_rect->set_title(TTR("Generate Visibility Rect"));
  405. VBoxContainer *genvb = memnew(VBoxContainer);
  406. generate_visibility_rect->add_child(genvb);
  407. generate_seconds = memnew(SpinBox);
  408. generate_seconds->set_min(0.1);
  409. generate_seconds->set_max(25);
  410. generate_seconds->set_value(2);
  411. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  412. EditorNode::get_singleton()->get_gui_base()->add_child(generate_visibility_rect);
  413. generate_visibility_rect->connect(SceneStringName(confirmed), callable_mp(this, &GPUParticles2DEditorPlugin::_generate_visibility_rect));
  414. }
  415. Node *CPUParticles2DEditorPlugin::_convert_particles() {
  416. CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);
  417. GPUParticles2D *gpu_particles = memnew(GPUParticles2D);
  418. gpu_particles->convert_from_particles(particles);
  419. gpu_particles->set_name(particles->get_name());
  420. gpu_particles->set_transform(particles->get_transform());
  421. gpu_particles->set_visible(particles->is_visible());
  422. gpu_particles->set_process_mode(particles->get_process_mode());
  423. return gpu_particles;
  424. }
  425. void CPUParticles2DEditorPlugin::_generate_emission_mask() {
  426. CPUParticles2D *particles = Object::cast_to<CPUParticles2D>(edited_node);
  427. PackedVector2Array valid_positions;
  428. PackedVector2Array valid_normals;
  429. PackedByteArray valid_colors;
  430. Vector2i image_size;
  431. _get_base_emission_mask(valid_positions, valid_normals, valid_colors, image_size);
  432. ERR_FAIL_COND_MSG(valid_positions.is_empty(), "No pixels with transparency > 128 in image...");
  433. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  434. undo_redo->create_action(TTR("Load Emission Mask"));
  435. int vpc = valid_positions.size();
  436. if (emission_colors->is_pressed()) {
  437. PackedColorArray pca;
  438. pca.resize(vpc);
  439. Color *pcaw = pca.ptrw();
  440. for (int i = 0; i < vpc; i += 1) {
  441. Color color;
  442. color.r = valid_colors[i * 4 + 0] / 255.0f;
  443. color.g = valid_colors[i * 4 + 1] / 255.0f;
  444. color.b = valid_colors[i * 4 + 2] / 255.0f;
  445. color.a = valid_colors[i * 4 + 3] / 255.0f;
  446. pcaw[i] = color;
  447. }
  448. undo_redo->add_do_property(particles, "emission_colors", pca);
  449. undo_redo->add_undo_property(particles, "emission_colors", particles->get_emission_colors());
  450. }
  451. if (valid_normals.size()) {
  452. undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_DIRECTED_POINTS);
  453. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
  454. PackedVector2Array norms;
  455. norms.resize(valid_normals.size());
  456. Vector2 *normsw = norms.ptrw();
  457. for (int i = 0; i < valid_normals.size(); i += 1) {
  458. normsw[i] = valid_normals[i];
  459. }
  460. undo_redo->add_do_property(particles, "emission_normals", norms);
  461. undo_redo->add_undo_property(particles, "emission_normals", particles->get_emission_normals());
  462. } else {
  463. undo_redo->add_do_property(particles, "emission_shape", CPUParticles2D::EMISSION_SHAPE_POINTS);
  464. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
  465. }
  466. {
  467. Vector2 offset;
  468. if (emission_mask_centered->is_pressed()) {
  469. offset = Vector2(-image_size.width * 0.5, -image_size.height * 0.5);
  470. }
  471. PackedVector2Array points;
  472. points.resize(valid_positions.size());
  473. Vector2 *pointsw = points.ptrw();
  474. for (int i = 0; i < valid_positions.size(); i += 1) {
  475. pointsw[i] = valid_positions[i] + offset;
  476. }
  477. undo_redo->add_do_property(particles, "emission_points", points);
  478. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_points());
  479. }
  480. undo_redo->commit_action();
  481. }
  482. CPUParticles2DEditorPlugin::CPUParticles2DEditorPlugin() {
  483. handled_type = TTRC("CPUParticles2D");
  484. conversion_option_name = TTR("Convert to GPUParticles2D");
  485. }
  486. // 3D /////////////////////////////////////////////
  487. void Particles3DEditorPlugin::_generate_aabb() {
  488. double time = generate_seconds->get_value();
  489. double running = 0.0;
  490. EditorProgress ep("gen_aabb", TTR("Generating Visibility AABB (Waiting for Particle Simulation)"), int(time));
  491. bool was_emitting = edited_node->get("emitting");
  492. if (!was_emitting) {
  493. edited_node->set("emitting", true);
  494. OS::get_singleton()->delay_usec(1000);
  495. }
  496. AABB rect;
  497. Callable capture_aabb = Callable(edited_node, "capture_aabb");
  498. while (running < time) {
  499. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  500. ep.step(TTR("Generating..."), int(running), true);
  501. OS::get_singleton()->delay_usec(1000);
  502. AABB capture = capture_aabb.call();
  503. if (rect == AABB()) {
  504. rect = capture;
  505. } else {
  506. rect.merge_with(capture);
  507. }
  508. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  509. }
  510. if (!was_emitting) {
  511. edited_node->set("emitting", false);
  512. }
  513. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  514. ur->create_action(TTR("Generate Visibility AABB"));
  515. ur->add_do_property(edited_node, "visibility_aabb", rect);
  516. ur->add_undo_property(edited_node, "visibility_aabb", edited_node->get("visibility_aabb"));
  517. ur->commit_action();
  518. }
  519. void Particles3DEditorPlugin::_node_selected(const NodePath &p_path) {
  520. Node *sel = get_node(p_path);
  521. if (!sel) {
  522. return;
  523. }
  524. if (!sel->is_class("Node3D")) {
  525. EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't inherit from Node3D."), sel->get_name()));
  526. return;
  527. }
  528. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(sel);
  529. if (!mi || mi->get_mesh().is_null()) {
  530. EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain geometry."), sel->get_name()));
  531. return;
  532. }
  533. geometry = mi->get_mesh()->get_faces();
  534. if (geometry.size() == 0) {
  535. EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain face geometry."), sel->get_name()));
  536. return;
  537. }
  538. Transform3D geom_xform = edited_node->get("global_transform");
  539. geom_xform = geom_xform.affine_inverse() * mi->get_global_transform();
  540. int gc = geometry.size();
  541. Face3 *w = geometry.ptrw();
  542. for (int i = 0; i < gc; i++) {
  543. for (int j = 0; j < 3; j++) {
  544. w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
  545. }
  546. }
  547. emission_dialog->popup_centered(Size2(300, 130));
  548. }
  549. void Particles3DEditorPlugin::_menu_callback(int p_idx) {
  550. switch (p_idx) {
  551. case MENU_OPTION_GENERATE_AABB: {
  552. if (need_show_lifetime_dialog(generate_seconds)) {
  553. generate_aabb->popup_centered();
  554. } else {
  555. _generate_aabb();
  556. }
  557. } break;
  558. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
  559. if (_can_generate_points()) {
  560. emission_tree_dialog->popup_scenetree_dialog();
  561. }
  562. } break;
  563. default: {
  564. ParticlesEditorPlugin::_menu_callback(p_idx);
  565. }
  566. }
  567. }
  568. void Particles3DEditorPlugin::_add_menu_options(PopupMenu *p_menu) {
  569. p_menu->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
  570. p_menu->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
  571. }
  572. bool Particles3DEditorPlugin::_generate(Vector<Vector3> &r_points, Vector<Vector3> &r_normals) {
  573. bool use_normals = emission_fill->get_selected() == 1;
  574. if (emission_fill->get_selected() < 2) {
  575. float area_accum = 0;
  576. RBMap<float, int> triangle_area_map;
  577. for (int i = 0; i < geometry.size(); i++) {
  578. float area = geometry[i].get_area();
  579. if (area < CMP_EPSILON) {
  580. continue;
  581. }
  582. triangle_area_map[area_accum] = i;
  583. area_accum += area;
  584. }
  585. if (!triangle_area_map.size() || area_accum == 0) {
  586. EditorNode::get_singleton()->show_warning(TTR("The geometry's faces don't contain any area."));
  587. return false;
  588. }
  589. int emissor_count = emission_amount->get_value();
  590. for (int i = 0; i < emissor_count; i++) {
  591. float areapos = Math::random(0.0f, area_accum);
  592. RBMap<float, int>::Iterator E = triangle_area_map.find_closest(areapos);
  593. ERR_FAIL_COND_V(!E, false);
  594. int index = E->value;
  595. ERR_FAIL_INDEX_V(index, geometry.size(), false);
  596. // ok FINALLY get face
  597. Face3 face = geometry[index];
  598. //now compute some position inside the face...
  599. Vector3 pos = face.get_random_point_inside();
  600. r_points.push_back(pos);
  601. if (use_normals) {
  602. Vector3 normal = face.get_plane().normal;
  603. r_normals.push_back(normal);
  604. }
  605. }
  606. } else {
  607. int gcount = geometry.size();
  608. if (gcount == 0) {
  609. EditorNode::get_singleton()->show_warning(TTR("The geometry doesn't contain any faces."));
  610. return false;
  611. }
  612. const Face3 *r = geometry.ptr();
  613. AABB aabb;
  614. for (int i = 0; i < gcount; i++) {
  615. for (int j = 0; j < 3; j++) {
  616. if (i == 0 && j == 0) {
  617. aabb.position = r[i].vertex[j];
  618. } else {
  619. aabb.expand_to(r[i].vertex[j]);
  620. }
  621. }
  622. }
  623. int emissor_count = emission_amount->get_value();
  624. for (int i = 0; i < emissor_count; i++) {
  625. int attempts = 5;
  626. for (int j = 0; j < attempts; j++) {
  627. Vector3 dir;
  628. dir[Math::rand() % 3] = 1.0;
  629. Vector3 ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size + aabb.position;
  630. Vector3 ofsv = ofs + aabb.size * dir;
  631. //space it a little
  632. ofs -= dir;
  633. ofsv += dir;
  634. float max = -1e7, min = 1e7;
  635. for (int k = 0; k < gcount; k++) {
  636. const Face3 &f3 = r[k];
  637. Vector3 res;
  638. if (f3.intersects_segment(ofs, ofsv, &res)) {
  639. res -= ofs;
  640. float d = dir.dot(res);
  641. if (d < min) {
  642. min = d;
  643. }
  644. if (d > max) {
  645. max = d;
  646. }
  647. }
  648. }
  649. if (max < min) {
  650. continue; //lost attempt
  651. }
  652. float val = min + (max - min) * Math::randf();
  653. Vector3 point = ofs + dir * val;
  654. r_points.push_back(point);
  655. break;
  656. }
  657. }
  658. }
  659. return true;
  660. }
  661. Particles3DEditorPlugin::Particles3DEditorPlugin() {
  662. generate_aabb = memnew(ConfirmationDialog);
  663. generate_aabb->set_title(TTR("Generate Visibility AABB"));
  664. VBoxContainer *genvb = memnew(VBoxContainer);
  665. generate_aabb->add_child(genvb);
  666. generate_seconds = memnew(SpinBox);
  667. generate_seconds->set_min(0.1);
  668. generate_seconds->set_max(25);
  669. generate_seconds->set_value(2);
  670. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  671. EditorNode::get_singleton()->get_gui_base()->add_child(generate_aabb);
  672. generate_aabb->connect(SceneStringName(confirmed), callable_mp(this, &Particles3DEditorPlugin::_generate_aabb));
  673. emission_tree_dialog = memnew(SceneTreeDialog);
  674. Vector<StringName> valid_types;
  675. valid_types.push_back("MeshInstance3D");
  676. emission_tree_dialog->set_valid_types(valid_types);
  677. EditorNode::get_singleton()->get_gui_base()->add_child(emission_tree_dialog);
  678. emission_tree_dialog->connect("selected", callable_mp(this, &Particles3DEditorPlugin::_node_selected));
  679. emission_dialog = memnew(ConfirmationDialog);
  680. emission_dialog->set_title(TTR("Create Emitter"));
  681. EditorNode::get_singleton()->get_gui_base()->add_child(emission_dialog);
  682. VBoxContainer *emd_vb = memnew(VBoxContainer);
  683. emission_dialog->add_child(emd_vb);
  684. emission_amount = memnew(SpinBox);
  685. emission_amount->set_min(1);
  686. emission_amount->set_max(100000);
  687. emission_amount->set_value(512);
  688. emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount);
  689. emission_fill = memnew(OptionButton);
  690. emission_fill->add_item(TTR("Surface Points"));
  691. emission_fill->add_item(TTR("Surface Points+Normal (Directed)"));
  692. emission_fill->add_item(TTR("Volume"));
  693. emd_vb->add_margin_child(TTR("Emission Source:"), emission_fill);
  694. emission_dialog->set_ok_button_text(TTR("Create"));
  695. emission_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Particles3DEditorPlugin::_generate_emission_points));
  696. }
  697. Node *GPUParticles3DEditorPlugin::_convert_particles() {
  698. GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);
  699. CPUParticles3D *cpu_particles = memnew(CPUParticles3D);
  700. cpu_particles->convert_from_particles(particles);
  701. cpu_particles->set_name(particles->get_name());
  702. cpu_particles->set_transform(particles->get_transform());
  703. cpu_particles->set_visible(particles->is_visible());
  704. cpu_particles->set_process_mode(particles->get_process_mode());
  705. return cpu_particles;
  706. }
  707. bool GPUParticles3DEditorPlugin::_can_generate_points() const {
  708. GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);
  709. Ref<ParticleProcessMaterial> mat = particles->get_process_material();
  710. if (mat.is_null()) {
  711. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticleProcessMaterial' is required."));
  712. return false;
  713. }
  714. return true;
  715. }
  716. void GPUParticles3DEditorPlugin::_generate_emission_points() {
  717. GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(edited_node);
  718. /// hacer codigo aca
  719. Vector<Vector3> points;
  720. Vector<Vector3> normals;
  721. if (!_generate(points, normals)) {
  722. return;
  723. }
  724. int point_count = points.size();
  725. int w = 2048;
  726. int h = (point_count / 2048) + 1;
  727. Vector<uint8_t> point_img;
  728. point_img.resize(w * h * 3 * sizeof(float));
  729. {
  730. uint8_t *iw = point_img.ptrw();
  731. memset(iw, 0, w * h * 3 * sizeof(float));
  732. const Vector3 *r = points.ptr();
  733. float *wf = reinterpret_cast<float *>(iw);
  734. for (int i = 0; i < point_count; i++) {
  735. wf[i * 3 + 0] = r[i].x;
  736. wf[i * 3 + 1] = r[i].y;
  737. wf[i * 3 + 2] = r[i].z;
  738. }
  739. }
  740. Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));
  741. Ref<ImageTexture> tex = ImageTexture::create_from_image(image);
  742. Ref<ParticleProcessMaterial> mat = particles->get_process_material();
  743. ERR_FAIL_COND(mat.is_null());
  744. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  745. undo_redo->create_action(TTR("Create Emission Points"));
  746. ParticleProcessMaterial *matptr = mat.ptr();
  747. if (normals.size() > 0) {
  748. undo_redo->add_do_property(matptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  749. undo_redo->add_undo_property(matptr, "emission_shape", matptr->get_emission_shape());
  750. Vector<uint8_t> point_img2;
  751. point_img2.resize(w * h * 3 * sizeof(float));
  752. {
  753. uint8_t *iw = point_img2.ptrw();
  754. memset(iw, 0, w * h * 3 * sizeof(float));
  755. const Vector3 *r = normals.ptr();
  756. float *wf = reinterpret_cast<float *>(iw);
  757. for (int i = 0; i < point_count; i++) {
  758. wf[i * 3 + 0] = r[i].x;
  759. wf[i * 3 + 1] = r[i].y;
  760. wf[i * 3 + 2] = r[i].z;
  761. }
  762. }
  763. Ref<Image> image2 = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img2));
  764. undo_redo->add_do_property(matptr, "emission_normal_texture", image2);
  765. undo_redo->add_undo_property(matptr, "emission_normal_texture", matptr->get_emission_normal_texture());
  766. } else {
  767. undo_redo->add_do_property(matptr, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
  768. undo_redo->add_undo_property(matptr, "emission_shape", matptr->get_emission_shape());
  769. }
  770. undo_redo->add_do_property(matptr, "emission_point_count", point_count);
  771. undo_redo->add_undo_property(matptr, "emission_point_count", matptr->get_emission_point_count());
  772. undo_redo->add_do_property(matptr, "emission_point_texture", tex);
  773. undo_redo->add_undo_property(matptr, "emission_point_texture", matptr->get_emission_point_texture());
  774. undo_redo->commit_action();
  775. }
  776. GPUParticles3DEditorPlugin::GPUParticles3DEditorPlugin() {
  777. handled_type = TTRC("GPUParticles3D");
  778. conversion_option_name = TTR("Convert to CPUParticles3D");
  779. }
  780. Node *CPUParticles3DEditorPlugin::_convert_particles() {
  781. CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(edited_node);
  782. GPUParticles3D *gpu_particles = memnew(GPUParticles3D);
  783. gpu_particles->convert_from_particles(particles);
  784. gpu_particles->set_name(particles->get_name());
  785. gpu_particles->set_transform(particles->get_transform());
  786. gpu_particles->set_visible(particles->is_visible());
  787. gpu_particles->set_process_mode(particles->get_process_mode());
  788. return gpu_particles;
  789. }
  790. void CPUParticles3DEditorPlugin::_generate_emission_points() {
  791. CPUParticles3D *particles = Object::cast_to<CPUParticles3D>(edited_node);
  792. /// hacer codigo aca
  793. Vector<Vector3> points;
  794. Vector<Vector3> normals;
  795. if (!_generate(points, normals)) {
  796. return;
  797. }
  798. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  799. undo_redo->create_action(TTR("Create Emission Points"));
  800. if (normals.is_empty()) {
  801. undo_redo->add_do_property(particles, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
  802. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
  803. } else {
  804. undo_redo->add_do_property(particles, "emission_shape", ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  805. undo_redo->add_undo_property(particles, "emission_shape", particles->get_emission_shape());
  806. undo_redo->add_do_property(particles, "emission_normals", normals);
  807. undo_redo->add_undo_property(particles, "emission_normals", particles->get_emission_normals());
  808. }
  809. undo_redo->add_do_property(particles, "emission_points", points);
  810. undo_redo->add_undo_property(particles, "emission_points", particles->get_emission_points());
  811. undo_redo->commit_action();
  812. }
  813. CPUParticles3DEditorPlugin::CPUParticles3DEditorPlugin() {
  814. handled_type = TTRC("CPUParticles3D");
  815. conversion_option_name = TTR("Convert to GPUParticles3D");
  816. }