particles_editor_plugin.cpp 16 KB

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