particles_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*************************************************************************/
  2. /* particles_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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 "editor/plugins/spatial_editor_plugin.h"
  32. #include "io/resource_loader.h"
  33. void ParticlesEditor::_node_removed(Node *p_node) {
  34. if (p_node == node) {
  35. node = NULL;
  36. hide();
  37. }
  38. }
  39. void ParticlesEditor::_resource_seleted(const String &p_res) {
  40. //print_line("selected resource path: "+p_res);
  41. }
  42. void ParticlesEditor::_node_selected(const NodePath &p_path) {
  43. Node *sel = get_node(p_path);
  44. if (!sel)
  45. return;
  46. VisualInstance *vi = Object::cast_to<VisualInstance>(sel);
  47. if (!vi) {
  48. err_dialog->set_text(TTR("Node does not contain geometry."));
  49. err_dialog->popup_centered_minsize();
  50. return;
  51. }
  52. geometry = vi->get_faces(VisualInstance::FACES_SOLID);
  53. if (geometry.size() == 0) {
  54. err_dialog->set_text(TTR("Node does not contain geometry (faces)."));
  55. err_dialog->popup_centered_minsize();
  56. return;
  57. }
  58. Transform geom_xform = node->get_global_transform().affine_inverse() * vi->get_global_transform();
  59. int gc = geometry.size();
  60. PoolVector<Face3>::Write w = geometry.write();
  61. for (int i = 0; i < gc; i++) {
  62. for (int j = 0; j < 3; j++) {
  63. w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
  64. }
  65. }
  66. w = PoolVector<Face3>::Write();
  67. emission_dialog->popup_centered(Size2(300, 130));
  68. }
  69. /*
  70. void ParticlesEditor::_populate() {
  71. if(!node)
  72. return;
  73. if (node->get_particles().is_null())
  74. return;
  75. node->get_particles()->set_instance_count(populate_amount->get_text().to_int());
  76. node->populate_parent(populate_rotate_random->get_val(),populate_tilt_random->get_val(),populate_scale_random->get_text().to_double(),populate_scale->get_text().to_double());
  77. }
  78. */
  79. void ParticlesEditor::_notification(int p_notification) {
  80. if (p_notification == NOTIFICATION_ENTER_TREE) {
  81. options->set_icon(options->get_popup()->get_icon("Particles", "EditorIcons"));
  82. }
  83. }
  84. void ParticlesEditor::_menu_option(int p_option) {
  85. switch (p_option) {
  86. case MENU_OPTION_GENERATE_AABB: {
  87. generate_aabb->popup_centered_minsize();
  88. } break;
  89. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH: {
  90. Ref<ParticlesMaterial> material = node->get_process_material();
  91. if (material.is_null()) {
  92. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
  93. return;
  94. }
  95. emission_file_dialog->popup_centered_ratio();
  96. } break;
  97. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
  98. Ref<ParticlesMaterial> material = node->get_process_material();
  99. if (material.is_null()) {
  100. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
  101. return;
  102. }
  103. /*
  104. Node *root = get_scene()->get_root_node();
  105. ERR_FAIL_COND(!root);
  106. EditorNode *en = Object::cast_to<EditorNode>(root);
  107. ERR_FAIL_COND(!en);
  108. Node * node = en->get_edited_scene();
  109. */
  110. emission_tree_dialog->popup_centered_ratio();
  111. } break;
  112. }
  113. }
  114. void ParticlesEditor::_generate_aabb() {
  115. float time = generate_seconds->get_value();
  116. float running = 0.0;
  117. EditorProgress ep("gen_aabb", TTR("Generating AABB"), int(time));
  118. Rect3 rect;
  119. while (running < time) {
  120. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  121. ep.step("Generating..", int(running), true);
  122. OS::get_singleton()->delay_usec(1000);
  123. Rect3 capture = node->capture_aabb();
  124. if (rect == Rect3())
  125. rect = capture;
  126. else
  127. rect.merge_with(capture);
  128. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  129. }
  130. node->set_visibility_aabb(rect);
  131. }
  132. void ParticlesEditor::edit(Particles *p_particles) {
  133. node = p_particles;
  134. }
  135. void ParticlesEditor::_generate_emission_points() {
  136. /// hacer codigo aca
  137. PoolVector<float> points;
  138. bool use_normals = emission_fill->get_selected() == 1;
  139. PoolVector<float> normals;
  140. if (emission_fill->get_selected() < 2) {
  141. float area_accum = 0;
  142. Map<float, int> triangle_area_map;
  143. print_line("geometry size: " + itos(geometry.size()));
  144. for (int i = 0; i < geometry.size(); i++) {
  145. float area = geometry[i].get_area();
  146. if (area < CMP_EPSILON)
  147. continue;
  148. triangle_area_map[area_accum] = i;
  149. area_accum += area;
  150. }
  151. if (!triangle_area_map.size() || area_accum == 0) {
  152. err_dialog->set_text(TTR("Faces contain no area!"));
  153. err_dialog->popup_centered_minsize();
  154. return;
  155. }
  156. int emissor_count = emission_amount->get_value();
  157. for (int i = 0; i < emissor_count; i++) {
  158. float areapos = Math::random(0.0f, area_accum);
  159. Map<float, int>::Element *E = triangle_area_map.find_closest(areapos);
  160. ERR_FAIL_COND(!E)
  161. int index = E->get();
  162. ERR_FAIL_INDEX(index, geometry.size());
  163. // ok FINALLY get face
  164. Face3 face = geometry[index];
  165. //now compute some position inside the face...
  166. Vector3 pos = face.get_random_point_inside();
  167. points.push_back(pos.x);
  168. points.push_back(pos.y);
  169. points.push_back(pos.z);
  170. if (use_normals) {
  171. Vector3 normal = face.get_plane().normal;
  172. normals.push_back(normal.x);
  173. normals.push_back(normal.y);
  174. normals.push_back(normal.z);
  175. }
  176. }
  177. } else {
  178. int gcount = geometry.size();
  179. if (gcount == 0) {
  180. err_dialog->set_text(TTR("No faces!"));
  181. err_dialog->popup_centered_minsize();
  182. return;
  183. }
  184. PoolVector<Face3>::Read r = geometry.read();
  185. Rect3 aabb;
  186. for (int i = 0; i < gcount; i++) {
  187. for (int j = 0; j < 3; j++) {
  188. if (i == 0 && j == 0)
  189. aabb.position = r[i].vertex[j];
  190. else
  191. aabb.expand_to(r[i].vertex[j]);
  192. }
  193. }
  194. int emissor_count = emission_amount->get_value();
  195. for (int i = 0; i < emissor_count; i++) {
  196. int attempts = 5;
  197. for (int j = 0; j < attempts; j++) {
  198. Vector3 dir;
  199. dir[Math::rand() % 3] = 1.0;
  200. Vector3 ofs = (Vector3(1, 1, 1) - dir) * Vector3(Math::randf(), Math::randf(), Math::randf()) * aabb.size + aabb.position;
  201. Vector3 ofsv = ofs + aabb.size * dir;
  202. //space it a little
  203. ofs -= dir;
  204. ofsv += dir;
  205. float max = -1e7, min = 1e7;
  206. for (int k = 0; k < gcount; k++) {
  207. const Face3 &f3 = r[k];
  208. Vector3 res;
  209. if (f3.intersects_segment(ofs, ofsv, &res)) {
  210. res -= ofs;
  211. float d = dir.dot(res);
  212. if (d < min)
  213. min = d;
  214. if (d > max)
  215. max = d;
  216. }
  217. }
  218. if (max < min)
  219. continue; //lost attempt
  220. float val = min + (max - min) * Math::randf();
  221. Vector3 point = ofs + dir * val;
  222. points.push_back(point.x);
  223. points.push_back(point.y);
  224. points.push_back(point.z);
  225. break;
  226. }
  227. }
  228. }
  229. int point_count = points.size() / 3;
  230. int w = 2048;
  231. int h = (point_count / 2048) + 1;
  232. PoolVector<uint8_t> point_img;
  233. point_img.resize(w * h * 3 * sizeof(float));
  234. {
  235. PoolVector<uint8_t>::Write iw = point_img.write();
  236. zeromem(iw.ptr(), w * h * 3 * sizeof(float));
  237. PoolVector<float>::Read r = points.read();
  238. copymem(iw.ptr(), r.ptr(), point_count * sizeof(float) * 3);
  239. }
  240. Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));
  241. Ref<ImageTexture> tex;
  242. tex.instance();
  243. tex->create_from_image(image, Texture::FLAG_FILTER);
  244. Ref<ParticlesMaterial> material = node->get_process_material();
  245. ERR_FAIL_COND(material.is_null());
  246. if (use_normals) {
  247. material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  248. material->set_emission_point_count(point_count);
  249. material->set_emission_point_texture(tex);
  250. PoolVector<uint8_t> point_img2;
  251. point_img2.resize(w * h * 3 * sizeof(float));
  252. {
  253. PoolVector<uint8_t>::Write iw = point_img2.write();
  254. zeromem(iw.ptr(), w * h * 3 * sizeof(float));
  255. PoolVector<float>::Read r = normals.read();
  256. copymem(iw.ptr(), r.ptr(), point_count * sizeof(float) * 3);
  257. }
  258. Ref<Image> image2 = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img2));
  259. Ref<ImageTexture> tex2;
  260. tex2.instance();
  261. tex2->create_from_image(image2, Texture::FLAG_FILTER);
  262. material->set_emission_normal_texture(tex2);
  263. } else {
  264. material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_POINTS);
  265. material->set_emission_point_count(point_count);
  266. material->set_emission_point_texture(tex);
  267. }
  268. //print_line("point count: "+itos(points.size()));
  269. //node->set_emission_points(points);
  270. }
  271. void ParticlesEditor::_bind_methods() {
  272. ClassDB::bind_method("_menu_option", &ParticlesEditor::_menu_option);
  273. ClassDB::bind_method("_resource_seleted", &ParticlesEditor::_resource_seleted);
  274. ClassDB::bind_method("_node_selected", &ParticlesEditor::_node_selected);
  275. ClassDB::bind_method("_generate_emission_points", &ParticlesEditor::_generate_emission_points);
  276. ClassDB::bind_method("_generate_aabb", &ParticlesEditor::_generate_aabb);
  277. //ClassDB::bind_method("_populate",&ParticlesEditor::_populate);
  278. }
  279. ParticlesEditor::ParticlesEditor() {
  280. particles_editor_hb = memnew(HBoxContainer);
  281. SpatialEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb);
  282. options = memnew(MenuButton);
  283. particles_editor_hb->add_child(options);
  284. particles_editor_hb->hide();
  285. options->set_text(TTR("Particles"));
  286. options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
  287. options->get_popup()->add_separator();
  288. options->get_popup()->add_item(TTR("Create Emission Points From Mesh"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH);
  289. options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
  290. // options->get_popup()->add_item(TTR("Clear Emitter"), MENU_OPTION_CLEAR_EMISSION_VOLUME);
  291. options->get_popup()->connect("id_pressed", this, "_menu_option");
  292. emission_dialog = memnew(ConfirmationDialog);
  293. emission_dialog->set_title(TTR("Create Emitter"));
  294. add_child(emission_dialog);
  295. VBoxContainer *emd_vb = memnew(VBoxContainer);
  296. emission_dialog->add_child(emd_vb);
  297. emission_amount = memnew(SpinBox);
  298. emission_amount->set_min(1);
  299. emission_amount->set_max(100000);
  300. emission_amount->set_value(512);
  301. emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount);
  302. emission_fill = memnew(OptionButton);
  303. emission_fill->add_item(TTR("Surface Points"));
  304. emission_fill->add_item(TTR("Surface Points+Normal (Directed)"));
  305. emission_fill->add_item(TTR("Volume"));
  306. emd_vb->add_margin_child(TTR("Emission Source: "), emission_fill);
  307. emission_dialog->get_ok()->set_text(TTR("Create"));
  308. emission_dialog->connect("confirmed", this, "_generate_emission_points");
  309. err_dialog = memnew(ConfirmationDialog);
  310. //err_dialog->get_cancel()->hide();
  311. add_child(err_dialog);
  312. emission_file_dialog = memnew(EditorFileDialog);
  313. add_child(emission_file_dialog);
  314. emission_file_dialog->connect("file_selected", this, "_resource_seleted");
  315. emission_tree_dialog = memnew(SceneTreeDialog);
  316. add_child(emission_tree_dialog);
  317. emission_tree_dialog->connect("selected", this, "_node_selected");
  318. List<String> extensions;
  319. ResourceLoader::get_recognized_extensions_for_type("Mesh", &extensions);
  320. emission_file_dialog->clear_filters();
  321. for (int i = 0; i < extensions.size(); i++) {
  322. emission_file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  323. }
  324. emission_file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  325. generate_aabb = memnew(ConfirmationDialog);
  326. generate_aabb->set_title(TTR("Generate Visibility AABB"));
  327. VBoxContainer *genvb = memnew(VBoxContainer);
  328. generate_aabb->add_child(genvb);
  329. generate_seconds = memnew(SpinBox);
  330. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  331. generate_seconds->set_min(0.1);
  332. generate_seconds->set_max(25);
  333. generate_seconds->set_value(2);
  334. add_child(generate_aabb);
  335. generate_aabb->connect("confirmed", this, "_generate_aabb");
  336. //options->set_anchor(MARGIN_LEFT,Control::ANCHOR_END);
  337. //options->set_anchor(MARGIN_RIGHT,Control::ANCHOR_END);
  338. }
  339. void ParticlesEditorPlugin::edit(Object *p_object) {
  340. particles_editor->edit(Object::cast_to<Particles>(p_object));
  341. }
  342. bool ParticlesEditorPlugin::handles(Object *p_object) const {
  343. return p_object->is_class("Particles");
  344. }
  345. void ParticlesEditorPlugin::make_visible(bool p_visible) {
  346. if (p_visible) {
  347. particles_editor->show();
  348. particles_editor->particles_editor_hb->show();
  349. } else {
  350. particles_editor->particles_editor_hb->hide();
  351. particles_editor->hide();
  352. particles_editor->edit(NULL);
  353. }
  354. }
  355. ParticlesEditorPlugin::ParticlesEditorPlugin(EditorNode *p_node) {
  356. editor = p_node;
  357. particles_editor = memnew(ParticlesEditor);
  358. editor->get_viewport()->add_child(particles_editor);
  359. particles_editor->hide();
  360. }
  361. ParticlesEditorPlugin::~ParticlesEditorPlugin() {
  362. }