particles_editor_plugin.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 "core/io/resource_loader.h"
  32. #include "editor/plugins/spatial_editor_plugin.h"
  33. #include "scene/3d/cpu_particles.h"
  34. #include "scene/resources/particles_material.h"
  35. bool ParticlesEditorBase::_generate(PoolVector<Vector3> &points, PoolVector<Vector3> &normals) {
  36. bool use_normals = emission_fill->get_selected() == 1;
  37. if (emission_fill->get_selected() < 2) {
  38. float area_accum = 0;
  39. Map<float, int> triangle_area_map;
  40. for (int i = 0; i < geometry.size(); i++) {
  41. float area = geometry[i].get_area();
  42. if (area < CMP_EPSILON) {
  43. continue;
  44. }
  45. triangle_area_map[area_accum] = i;
  46. area_accum += area;
  47. }
  48. if (!triangle_area_map.size() || area_accum == 0) {
  49. EditorNode::get_singleton()->show_warning(TTR("The geometry's faces don't contain any area."));
  50. return false;
  51. }
  52. int emissor_count = emission_amount->get_value();
  53. for (int i = 0; i < emissor_count; i++) {
  54. float areapos = Math::random(0.0f, area_accum);
  55. Map<float, int>::Element *E = triangle_area_map.find_closest(areapos);
  56. ERR_FAIL_COND_V(!E, false);
  57. int index = E->get();
  58. ERR_FAIL_INDEX_V(index, geometry.size(), false);
  59. // ok FINALLY get face
  60. Face3 face = geometry[index];
  61. //now compute some position inside the face...
  62. Vector3 pos = face.get_random_point_inside();
  63. points.push_back(pos);
  64. if (use_normals) {
  65. Vector3 normal = face.get_plane().normal;
  66. normals.push_back(normal);
  67. }
  68. }
  69. } else {
  70. int gcount = geometry.size();
  71. if (gcount == 0) {
  72. EditorNode::get_singleton()->show_warning(TTR("The geometry doesn't contain any faces."));
  73. return false;
  74. }
  75. PoolVector<Face3>::Read r = geometry.read();
  76. AABB aabb;
  77. for (int i = 0; i < gcount; i++) {
  78. for (int j = 0; j < 3; j++) {
  79. if (i == 0 && j == 0) {
  80. aabb.position = r[i].vertex[j];
  81. } else {
  82. aabb.expand_to(r[i].vertex[j]);
  83. }
  84. }
  85. }
  86. int emissor_count = emission_amount->get_value();
  87. for (int i = 0; i < emissor_count; i++) {
  88. int attempts = 5;
  89. for (int j = 0; j < attempts; j++) {
  90. Vector3 dir;
  91. dir[Math::rand() % 3] = 1.0;
  92. Vector3 ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size + aabb.position;
  93. Vector3 ofsv = ofs + aabb.size * dir;
  94. //space it a little
  95. ofs -= dir;
  96. ofsv += dir;
  97. float max = -1e7, min = 1e7;
  98. for (int k = 0; k < gcount; k++) {
  99. const Face3 &f3 = r[k];
  100. Vector3 res;
  101. if (f3.intersects_segment(ofs, ofsv, &res)) {
  102. res -= ofs;
  103. float d = dir.dot(res);
  104. if (d < min) {
  105. min = d;
  106. }
  107. if (d > max) {
  108. max = d;
  109. }
  110. }
  111. }
  112. if (max < min) {
  113. continue; //lost attempt
  114. }
  115. float val = min + (max - min) * Math::randf();
  116. Vector3 point = ofs + dir * val;
  117. points.push_back(point);
  118. break;
  119. }
  120. }
  121. }
  122. return true;
  123. }
  124. void ParticlesEditorBase::_node_selected(const NodePath &p_path) {
  125. Node *sel = get_node(p_path);
  126. if (!sel) {
  127. return;
  128. }
  129. if (!sel->is_class("Spatial")) {
  130. EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't inherit from Spatial."), sel->get_name()));
  131. return;
  132. }
  133. VisualInstance *vi = Object::cast_to<VisualInstance>(sel);
  134. if (!vi) {
  135. EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain geometry."), sel->get_name()));
  136. return;
  137. }
  138. geometry = vi->get_faces(VisualInstance::FACES_SOLID);
  139. if (geometry.size() == 0) {
  140. EditorNode::get_singleton()->show_warning(vformat(TTR("\"%s\" doesn't contain face geometry."), sel->get_name()));
  141. return;
  142. }
  143. Transform geom_xform = base_node->get_global_transform().affine_inverse() * vi->get_global_transform();
  144. int gc = geometry.size();
  145. PoolVector<Face3>::Write w = geometry.write();
  146. for (int i = 0; i < gc; i++) {
  147. for (int j = 0; j < 3; j++) {
  148. w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
  149. }
  150. }
  151. w.release();
  152. emission_dialog->popup_centered(Size2(300, 130));
  153. }
  154. void ParticlesEditorBase::_bind_methods() {
  155. ClassDB::bind_method("_node_selected", &ParticlesEditorBase::_node_selected);
  156. ClassDB::bind_method("_generate_emission_points", &ParticlesEditorBase::_generate_emission_points);
  157. }
  158. ParticlesEditorBase::ParticlesEditorBase() {
  159. emission_dialog = memnew(ConfirmationDialog);
  160. emission_dialog->set_title(TTR("Create Emitter"));
  161. add_child(emission_dialog);
  162. VBoxContainer *emd_vb = memnew(VBoxContainer);
  163. emission_dialog->add_child(emd_vb);
  164. emission_amount = memnew(SpinBox);
  165. emission_amount->set_min(1);
  166. emission_amount->set_max(100000);
  167. emission_amount->set_value(512);
  168. emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount);
  169. emission_fill = memnew(OptionButton);
  170. emission_fill->add_item(TTR("Surface Points"));
  171. emission_fill->add_item(TTR("Surface Points+Normal (Directed)"));
  172. emission_fill->add_item(TTR("Volume"));
  173. emd_vb->add_margin_child(TTR("Emission Source:"), emission_fill);
  174. emission_dialog->get_ok()->set_text(TTR("Create"));
  175. emission_dialog->connect("confirmed", this, "_generate_emission_points");
  176. emission_file_dialog = memnew(EditorFileDialog);
  177. add_child(emission_file_dialog);
  178. emission_file_dialog->connect("file_selected", this, "_resource_seleted");
  179. emission_tree_dialog = memnew(SceneTreeDialog);
  180. add_child(emission_tree_dialog);
  181. emission_tree_dialog->connect("selected", this, "_node_selected");
  182. List<String> extensions;
  183. ResourceLoader::get_recognized_extensions_for_type("Mesh", &extensions);
  184. emission_file_dialog->clear_filters();
  185. for (int i = 0; i < extensions.size(); i++) {
  186. emission_file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  187. }
  188. emission_file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  189. }
  190. void ParticlesEditor::_node_removed(Node *p_node) {
  191. if (p_node == node) {
  192. node = nullptr;
  193. hide();
  194. }
  195. }
  196. void ParticlesEditor::_notification(int p_notification) {
  197. if (p_notification == NOTIFICATION_ENTER_TREE) {
  198. options->set_icon(options->get_popup()->get_icon("Particles", "EditorIcons"));
  199. get_tree()->connect("node_removed", this, "_node_removed");
  200. }
  201. }
  202. void ParticlesEditor::_menu_option(int p_option) {
  203. switch (p_option) {
  204. case MENU_OPTION_GENERATE_AABB: {
  205. // Add one second to the default generation lifetime, since the progress is updated every second.
  206. generate_seconds->set_value(MAX(1.0, trunc(node->get_lifetime()) + 1.0));
  207. if (generate_seconds->get_value() >= 11.0 + CMP_EPSILON) {
  208. // Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.
  209. generate_aabb->popup_centered_minsize();
  210. } else {
  211. // Generate the visibility AABB immediately.
  212. _generate_aabb();
  213. }
  214. } break;
  215. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH: {
  216. Ref<ParticlesMaterial> material = node->get_process_material();
  217. if (material.is_null()) {
  218. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
  219. return;
  220. }
  221. emission_file_dialog->popup_centered_ratio();
  222. } break;
  223. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
  224. Ref<ParticlesMaterial> material = node->get_process_material();
  225. if (material.is_null()) {
  226. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
  227. return;
  228. }
  229. emission_tree_dialog->popup_centered_ratio();
  230. } break;
  231. case MENU_OPTION_CONVERT_TO_CPU_PARTICLES: {
  232. CPUParticles *cpu_particles = memnew(CPUParticles);
  233. cpu_particles->convert_from_particles(node);
  234. cpu_particles->set_name(node->get_name());
  235. cpu_particles->set_transform(node->get_transform());
  236. cpu_particles->set_visible(node->is_visible());
  237. cpu_particles->set_pause_mode(node->get_pause_mode());
  238. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  239. ur->create_action(TTR("Convert to CPUParticles"));
  240. ur->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", node, cpu_particles, true, false);
  241. ur->add_do_reference(cpu_particles);
  242. ur->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", cpu_particles, node, false, false);
  243. ur->add_undo_reference(node);
  244. ur->commit_action();
  245. } break;
  246. case MENU_OPTION_RESTART: {
  247. node->restart();
  248. } break;
  249. }
  250. }
  251. void ParticlesEditor::_generate_aabb() {
  252. float time = generate_seconds->get_value();
  253. float running = 0.0;
  254. EditorProgress ep("gen_aabb", TTR("Generating Visibility AABB (Waiting for Particle Simulation)"), int(time));
  255. bool was_emitting = node->is_emitting();
  256. if (!was_emitting) {
  257. node->set_emitting(true);
  258. OS::get_singleton()->delay_usec(1000);
  259. }
  260. AABB rect;
  261. while (running < time) {
  262. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  263. ep.step("Generating...", int(running), true);
  264. OS::get_singleton()->delay_usec(1000);
  265. AABB capture = node->capture_aabb();
  266. if (rect == AABB()) {
  267. rect = capture;
  268. } else {
  269. rect.merge_with(capture);
  270. }
  271. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  272. }
  273. if (!was_emitting) {
  274. node->set_emitting(false);
  275. }
  276. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  277. ur->create_action(TTR("Generate Visibility AABB"));
  278. ur->add_do_method(node, "set_visibility_aabb", rect);
  279. ur->add_undo_method(node, "set_visibility_aabb", node->get_visibility_aabb());
  280. ur->commit_action();
  281. }
  282. void ParticlesEditor::edit(Particles *p_particles) {
  283. base_node = p_particles;
  284. node = p_particles;
  285. }
  286. void ParticlesEditor::_generate_emission_points() {
  287. /// hacer codigo aca
  288. PoolVector<Vector3> points;
  289. PoolVector<Vector3> normals;
  290. if (!_generate(points, normals)) {
  291. return;
  292. }
  293. int point_count = points.size();
  294. int w = 2048;
  295. int h = (point_count / 2048) + 1;
  296. PoolVector<uint8_t> point_img;
  297. point_img.resize(w * h * 3 * sizeof(float));
  298. {
  299. PoolVector<uint8_t>::Write iw = point_img.write();
  300. memset(iw.ptr(), 0, w * h * 3 * sizeof(float));
  301. PoolVector<Vector3>::Read r = points.read();
  302. float *wf = (float *)iw.ptr();
  303. for (int i = 0; i < point_count; i++) {
  304. wf[i * 3 + 0] = r[i].x;
  305. wf[i * 3 + 1] = r[i].y;
  306. wf[i * 3 + 2] = r[i].z;
  307. }
  308. }
  309. Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));
  310. Ref<ImageTexture> tex;
  311. tex.instance();
  312. tex->create_from_image(image, Texture::FLAG_FILTER);
  313. Ref<ParticlesMaterial> material = node->get_process_material();
  314. ERR_FAIL_COND(material.is_null());
  315. if (normals.size() > 0) {
  316. material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  317. material->set_emission_point_count(point_count);
  318. material->set_emission_point_texture(tex);
  319. PoolVector<uint8_t> point_img2;
  320. point_img2.resize(w * h * 3 * sizeof(float));
  321. {
  322. PoolVector<uint8_t>::Write iw = point_img2.write();
  323. memset(iw.ptr(), 0, w * h * 3 * sizeof(float));
  324. PoolVector<Vector3>::Read r = normals.read();
  325. float *wf = (float *)iw.ptr();
  326. for (int i = 0; i < point_count; i++) {
  327. wf[i * 3 + 0] = r[i].x;
  328. wf[i * 3 + 1] = r[i].y;
  329. wf[i * 3 + 2] = r[i].z;
  330. }
  331. }
  332. Ref<Image> image2 = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img2));
  333. Ref<ImageTexture> tex2;
  334. tex2.instance();
  335. tex2->create_from_image(image2, Texture::FLAG_FILTER);
  336. material->set_emission_normal_texture(tex2);
  337. } else {
  338. material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_POINTS);
  339. material->set_emission_point_count(point_count);
  340. material->set_emission_point_texture(tex);
  341. }
  342. }
  343. void ParticlesEditor::_bind_methods() {
  344. ClassDB::bind_method("_menu_option", &ParticlesEditor::_menu_option);
  345. ClassDB::bind_method("_generate_aabb", &ParticlesEditor::_generate_aabb);
  346. ClassDB::bind_method("_node_removed", &ParticlesEditor::_node_removed);
  347. }
  348. ParticlesEditor::ParticlesEditor() {
  349. node = nullptr;
  350. particles_editor_hb = memnew(HBoxContainer);
  351. SpatialEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb);
  352. options = memnew(MenuButton);
  353. options->set_switch_on_hover(true);
  354. particles_editor_hb->add_child(options);
  355. particles_editor_hb->hide();
  356. options->set_text(TTR("Particles"));
  357. options->get_popup()->add_item(TTR("Generate Visibility AABB"), MENU_OPTION_GENERATE_AABB);
  358. options->get_popup()->add_separator();
  359. options->get_popup()->add_item(TTR("Create Emission Points From Mesh"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH);
  360. options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
  361. options->get_popup()->add_separator();
  362. options->get_popup()->add_item(TTR("Convert to CPUParticles"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES);
  363. options->get_popup()->add_separator();
  364. options->get_popup()->add_item(TTR("Restart"), MENU_OPTION_RESTART);
  365. options->get_popup()->connect("id_pressed", this, "_menu_option");
  366. generate_aabb = memnew(ConfirmationDialog);
  367. generate_aabb->set_title(TTR("Generate Visibility AABB"));
  368. VBoxContainer *genvb = memnew(VBoxContainer);
  369. generate_aabb->add_child(genvb);
  370. generate_seconds = memnew(SpinBox);
  371. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  372. generate_seconds->set_min(0.1);
  373. generate_seconds->set_max(25);
  374. generate_seconds->set_value(2);
  375. add_child(generate_aabb);
  376. generate_aabb->connect("confirmed", this, "_generate_aabb");
  377. }
  378. void ParticlesEditorPlugin::edit(Object *p_object) {
  379. particles_editor->edit(Object::cast_to<Particles>(p_object));
  380. }
  381. bool ParticlesEditorPlugin::handles(Object *p_object) const {
  382. return p_object->is_class("Particles");
  383. }
  384. void ParticlesEditorPlugin::make_visible(bool p_visible) {
  385. if (p_visible) {
  386. particles_editor->show();
  387. particles_editor->particles_editor_hb->show();
  388. } else {
  389. particles_editor->particles_editor_hb->hide();
  390. particles_editor->hide();
  391. particles_editor->edit(nullptr);
  392. }
  393. }
  394. ParticlesEditorPlugin::ParticlesEditorPlugin(EditorNode *p_node) {
  395. editor = p_node;
  396. particles_editor = memnew(ParticlesEditor);
  397. editor->get_viewport()->add_child(particles_editor);
  398. particles_editor->hide();
  399. }
  400. ParticlesEditorPlugin::~ParticlesEditorPlugin() {
  401. }