particles_editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "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. triangle_area_map[area_accum] = i;
  45. area_accum += area;
  46. }
  47. if (!triangle_area_map.size() || area_accum == 0) {
  48. err_dialog->set_text(TTR("Faces contain no area!"));
  49. err_dialog->popup_centered_minsize();
  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. err_dialog->set_text(TTR("No faces!"));
  73. err_dialog->popup_centered_minsize();
  74. return false;
  75. }
  76. PoolVector<Face3>::Read r = geometry.read();
  77. AABB aabb;
  78. for (int i = 0; i < gcount; i++) {
  79. for (int j = 0; j < 3; j++) {
  80. if (i == 0 && j == 0)
  81. aabb.position = r[i].vertex[j];
  82. else
  83. aabb.expand_to(r[i].vertex[j]);
  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. if (d > max)
  107. max = d;
  108. }
  109. }
  110. if (max < min)
  111. continue; //lost attempt
  112. float val = min + (max - min) * Math::randf();
  113. Vector3 point = ofs + dir * val;
  114. points.push_back(point);
  115. break;
  116. }
  117. }
  118. }
  119. return true;
  120. }
  121. void ParticlesEditorBase::_node_selected(const NodePath &p_path) {
  122. Node *sel = get_node(p_path);
  123. if (!sel)
  124. return;
  125. VisualInstance *vi = Object::cast_to<VisualInstance>(sel);
  126. if (!vi) {
  127. err_dialog->set_text(TTR("Node does not contain geometry."));
  128. err_dialog->popup_centered_minsize();
  129. return;
  130. }
  131. geometry = vi->get_faces(VisualInstance::FACES_SOLID);
  132. if (geometry.size() == 0) {
  133. err_dialog->set_text(TTR("Node does not contain geometry (faces)."));
  134. err_dialog->popup_centered_minsize();
  135. return;
  136. }
  137. Transform geom_xform = base_node->get_global_transform().affine_inverse() * vi->get_global_transform();
  138. int gc = geometry.size();
  139. PoolVector<Face3>::Write w = geometry.write();
  140. for (int i = 0; i < gc; i++) {
  141. for (int j = 0; j < 3; j++) {
  142. w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
  143. }
  144. }
  145. w = PoolVector<Face3>::Write();
  146. emission_dialog->popup_centered(Size2(300, 130));
  147. }
  148. void ParticlesEditorBase::_bind_methods() {
  149. ClassDB::bind_method("_node_selected", &ParticlesEditorBase::_node_selected);
  150. ClassDB::bind_method("_generate_emission_points", &ParticlesEditorBase::_generate_emission_points);
  151. }
  152. ParticlesEditorBase::ParticlesEditorBase() {
  153. emission_dialog = memnew(ConfirmationDialog);
  154. emission_dialog->set_title(TTR("Create Emitter"));
  155. add_child(emission_dialog);
  156. VBoxContainer *emd_vb = memnew(VBoxContainer);
  157. emission_dialog->add_child(emd_vb);
  158. emission_amount = memnew(SpinBox);
  159. emission_amount->set_min(1);
  160. emission_amount->set_max(100000);
  161. emission_amount->set_value(512);
  162. emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount);
  163. emission_fill = memnew(OptionButton);
  164. emission_fill->add_item(TTR("Surface Points"));
  165. emission_fill->add_item(TTR("Surface Points+Normal (Directed)"));
  166. emission_fill->add_item(TTR("Volume"));
  167. emd_vb->add_margin_child(TTR("Emission Source: "), emission_fill);
  168. emission_dialog->get_ok()->set_text(TTR("Create"));
  169. emission_dialog->connect("confirmed", this, "_generate_emission_points");
  170. err_dialog = memnew(ConfirmationDialog);
  171. add_child(err_dialog);
  172. emission_file_dialog = memnew(EditorFileDialog);
  173. add_child(emission_file_dialog);
  174. emission_file_dialog->connect("file_selected", this, "_resource_seleted");
  175. emission_tree_dialog = memnew(SceneTreeDialog);
  176. add_child(emission_tree_dialog);
  177. emission_tree_dialog->connect("selected", this, "_node_selected");
  178. List<String> extensions;
  179. ResourceLoader::get_recognized_extensions_for_type("Mesh", &extensions);
  180. emission_file_dialog->clear_filters();
  181. for (int i = 0; i < extensions.size(); i++) {
  182. emission_file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  183. }
  184. emission_file_dialog->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  185. }
  186. void ParticlesEditor::_node_removed(Node *p_node) {
  187. if (p_node == node) {
  188. node = NULL;
  189. hide();
  190. }
  191. }
  192. void ParticlesEditor::_notification(int p_notification) {
  193. if (p_notification == NOTIFICATION_ENTER_TREE) {
  194. options->set_icon(options->get_popup()->get_icon("Particles", "EditorIcons"));
  195. get_tree()->connect("node_removed", this, "_node_removed");
  196. }
  197. }
  198. void ParticlesEditor::_menu_option(int p_option) {
  199. switch (p_option) {
  200. case MENU_OPTION_GENERATE_AABB: {
  201. float gen_time = node->get_lifetime();
  202. if (gen_time < 1.0)
  203. generate_seconds->set_value(1.0);
  204. else
  205. generate_seconds->set_value(trunc(gen_time) + 1.0);
  206. generate_aabb->popup_centered_minsize();
  207. } break;
  208. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH: {
  209. Ref<ParticlesMaterial> material = node->get_process_material();
  210. if (material.is_null()) {
  211. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
  212. return;
  213. }
  214. emission_file_dialog->popup_centered_ratio();
  215. } break;
  216. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
  217. Ref<ParticlesMaterial> material = node->get_process_material();
  218. if (material.is_null()) {
  219. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
  220. return;
  221. }
  222. emission_tree_dialog->popup_centered_ratio();
  223. } break;
  224. case MENU_OPTION_CONVERT_TO_CPU_PARTICLES: {
  225. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  226. CPUParticles *cpu_particles = memnew(CPUParticles);
  227. cpu_particles->convert_from_particles(node);
  228. cpu_particles->set_name(node->get_name());
  229. cpu_particles->set_transform(node->get_transform());
  230. cpu_particles->set_visible(node->is_visible());
  231. cpu_particles->set_pause_mode(node->get_pause_mode());
  232. undo_redo->create_action("Replace Particles by CPUParticles");
  233. undo_redo->add_do_method(node, "replace_by", cpu_particles);
  234. undo_redo->add_undo_method(cpu_particles, "replace_by", node);
  235. undo_redo->add_do_reference(cpu_particles);
  236. undo_redo->add_undo_reference(node);
  237. undo_redo->commit_action();
  238. } break;
  239. }
  240. }
  241. void ParticlesEditor::_generate_aabb() {
  242. float time = generate_seconds->get_value();
  243. float running = 0.0;
  244. EditorProgress ep("gen_aabb", TTR("Generating AABB"), int(time));
  245. bool was_emitting = node->is_emitting();
  246. if (!was_emitting) {
  247. node->set_emitting(true);
  248. OS::get_singleton()->delay_usec(1000);
  249. }
  250. AABB rect;
  251. while (running < time) {
  252. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  253. ep.step("Generating...", int(running), true);
  254. OS::get_singleton()->delay_usec(1000);
  255. AABB capture = node->capture_aabb();
  256. if (rect == AABB())
  257. rect = capture;
  258. else
  259. rect.merge_with(capture);
  260. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  261. }
  262. if (!was_emitting) {
  263. node->set_emitting(false);
  264. }
  265. node->set_visibility_aabb(rect);
  266. }
  267. void ParticlesEditor::edit(Particles *p_particles) {
  268. base_node = p_particles;
  269. node = p_particles;
  270. }
  271. void ParticlesEditor::_generate_emission_points() {
  272. /// hacer codigo aca
  273. PoolVector<Vector3> points;
  274. PoolVector<Vector3> normals;
  275. if (!_generate(points, normals)) {
  276. return;
  277. }
  278. int point_count = points.size();
  279. int w = 2048;
  280. int h = (point_count / 2048) + 1;
  281. PoolVector<uint8_t> point_img;
  282. point_img.resize(w * h * 3 * sizeof(float));
  283. {
  284. PoolVector<uint8_t>::Write iw = point_img.write();
  285. zeromem(iw.ptr(), w * h * 3 * sizeof(float));
  286. PoolVector<Vector3>::Read r = points.read();
  287. float *wf = (float *)iw.ptr();
  288. for (int i = 0; i < point_count; i++) {
  289. wf[i * 3 + 0] = r[i].x;
  290. wf[i * 3 + 1] = r[i].y;
  291. wf[i * 3 + 2] = r[i].z;
  292. }
  293. }
  294. Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));
  295. Ref<ImageTexture> tex;
  296. tex.instance();
  297. tex->create_from_image(image, Texture::FLAG_FILTER);
  298. Ref<ParticlesMaterial> material = node->get_process_material();
  299. ERR_FAIL_COND(material.is_null());
  300. if (normals.size() > 0) {
  301. material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  302. material->set_emission_point_count(point_count);
  303. material->set_emission_point_texture(tex);
  304. PoolVector<uint8_t> point_img2;
  305. point_img2.resize(w * h * 3 * sizeof(float));
  306. {
  307. PoolVector<uint8_t>::Write iw = point_img2.write();
  308. zeromem(iw.ptr(), w * h * 3 * sizeof(float));
  309. PoolVector<Vector3>::Read r = normals.read();
  310. float *wf = (float *)iw.ptr();
  311. for (int i = 0; i < point_count; i++) {
  312. wf[i * 3 + 0] = r[i].x;
  313. wf[i * 3 + 1] = r[i].y;
  314. wf[i * 3 + 2] = r[i].z;
  315. }
  316. }
  317. Ref<Image> image2 = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img2));
  318. Ref<ImageTexture> tex2;
  319. tex2.instance();
  320. tex2->create_from_image(image2, Texture::FLAG_FILTER);
  321. material->set_emission_normal_texture(tex2);
  322. } else {
  323. material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_POINTS);
  324. material->set_emission_point_count(point_count);
  325. material->set_emission_point_texture(tex);
  326. }
  327. }
  328. void ParticlesEditor::_bind_methods() {
  329. ClassDB::bind_method("_menu_option", &ParticlesEditor::_menu_option);
  330. ClassDB::bind_method("_generate_aabb", &ParticlesEditor::_generate_aabb);
  331. ClassDB::bind_method("_node_removed", &ParticlesEditor::_node_removed);
  332. }
  333. ParticlesEditor::ParticlesEditor() {
  334. node = NULL;
  335. particles_editor_hb = memnew(HBoxContainer);
  336. SpatialEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb);
  337. options = memnew(MenuButton);
  338. particles_editor_hb->add_child(options);
  339. particles_editor_hb->hide();
  340. options->set_text(TTR("Particles"));
  341. options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
  342. options->get_popup()->add_separator();
  343. options->get_popup()->add_item(TTR("Create Emission Points From Mesh"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_MESH);
  344. options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
  345. options->get_popup()->add_separator();
  346. options->get_popup()->add_item(TTR("Convert to CPUParticles"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES);
  347. options->get_popup()->connect("id_pressed", this, "_menu_option");
  348. generate_aabb = memnew(ConfirmationDialog);
  349. generate_aabb->set_title(TTR("Generate Visibility AABB"));
  350. VBoxContainer *genvb = memnew(VBoxContainer);
  351. generate_aabb->add_child(genvb);
  352. generate_seconds = memnew(SpinBox);
  353. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  354. generate_seconds->set_min(0.1);
  355. generate_seconds->set_max(25);
  356. generate_seconds->set_value(2);
  357. add_child(generate_aabb);
  358. generate_aabb->connect("confirmed", this, "_generate_aabb");
  359. }
  360. void ParticlesEditorPlugin::edit(Object *p_object) {
  361. particles_editor->edit(Object::cast_to<Particles>(p_object));
  362. }
  363. bool ParticlesEditorPlugin::handles(Object *p_object) const {
  364. return p_object->is_class("Particles");
  365. }
  366. void ParticlesEditorPlugin::make_visible(bool p_visible) {
  367. if (p_visible) {
  368. particles_editor->show();
  369. particles_editor->particles_editor_hb->show();
  370. } else {
  371. particles_editor->particles_editor_hb->hide();
  372. particles_editor->hide();
  373. particles_editor->edit(NULL);
  374. }
  375. }
  376. ParticlesEditorPlugin::ParticlesEditorPlugin(EditorNode *p_node) {
  377. editor = p_node;
  378. particles_editor = memnew(ParticlesEditor);
  379. editor->get_viewport()->add_child(particles_editor);
  380. particles_editor->hide();
  381. }
  382. ParticlesEditorPlugin::~ParticlesEditorPlugin() {
  383. }