particles_editor_plugin.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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(Vector<Vector3> &points, Vector<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. const Face3 *r = geometry.ptr();
  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. Face3 *w = geometry.ptrw();
  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. emission_dialog->popup_centered(Size2(300, 130));
  146. }
  147. void ParticlesEditorBase::_bind_methods() {
  148. }
  149. ParticlesEditorBase::ParticlesEditorBase() {
  150. emission_dialog = memnew(ConfirmationDialog);
  151. emission_dialog->set_title(TTR("Create Emitter"));
  152. add_child(emission_dialog);
  153. VBoxContainer *emd_vb = memnew(VBoxContainer);
  154. emission_dialog->add_child(emd_vb);
  155. emission_amount = memnew(SpinBox);
  156. emission_amount->set_min(1);
  157. emission_amount->set_max(100000);
  158. emission_amount->set_value(512);
  159. emd_vb->add_margin_child(TTR("Emission Points:"), emission_amount);
  160. emission_fill = memnew(OptionButton);
  161. emission_fill->add_item(TTR("Surface Points"));
  162. emission_fill->add_item(TTR("Surface Points+Normal (Directed)"));
  163. emission_fill->add_item(TTR("Volume"));
  164. emd_vb->add_margin_child(TTR("Emission Source: "), emission_fill);
  165. emission_dialog->get_ok()->set_text(TTR("Create"));
  166. emission_dialog->connect("confirmed", callable_mp(this, &ParticlesEditorBase::_generate_emission_points));
  167. emission_tree_dialog = memnew(SceneTreeDialog);
  168. add_child(emission_tree_dialog);
  169. emission_tree_dialog->connect("selected", callable_mp(this, &ParticlesEditorBase::_node_selected));
  170. }
  171. void ParticlesEditor::_node_removed(Node *p_node) {
  172. if (p_node == node) {
  173. node = NULL;
  174. hide();
  175. }
  176. }
  177. void ParticlesEditor::_notification(int p_notification) {
  178. if (p_notification == NOTIFICATION_ENTER_TREE) {
  179. options->set_icon(options->get_popup()->get_icon("Particles", "EditorIcons"));
  180. get_tree()->connect("node_removed", callable_mp(this, &ParticlesEditor::_node_removed));
  181. }
  182. }
  183. void ParticlesEditor::_menu_option(int p_option) {
  184. switch (p_option) {
  185. case MENU_OPTION_GENERATE_AABB: {
  186. float gen_time = node->get_lifetime();
  187. if (gen_time < 1.0)
  188. generate_seconds->set_value(1.0);
  189. else
  190. generate_seconds->set_value(trunc(gen_time) + 1.0);
  191. generate_aabb->popup_centered_minsize();
  192. } break;
  193. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
  194. Ref<ParticlesMaterial> material = node->get_process_material();
  195. if (material.is_null()) {
  196. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticlesMaterial' is required."));
  197. return;
  198. }
  199. emission_tree_dialog->popup_centered_ratio();
  200. } break;
  201. case MENU_OPTION_CONVERT_TO_CPU_PARTICLES: {
  202. CPUParticles *cpu_particles = memnew(CPUParticles);
  203. cpu_particles->convert_from_particles(node);
  204. cpu_particles->set_name(node->get_name());
  205. cpu_particles->set_transform(node->get_transform());
  206. cpu_particles->set_visible(node->is_visible());
  207. cpu_particles->set_pause_mode(node->get_pause_mode());
  208. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  209. ur->create_action(TTR("Convert to CPUParticles"));
  210. ur->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", node, cpu_particles, true, false);
  211. ur->add_do_reference(cpu_particles);
  212. ur->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock(), "replace_node", cpu_particles, node, false, false);
  213. ur->add_undo_reference(node);
  214. ur->commit_action();
  215. } break;
  216. case MENU_OPTION_RESTART: {
  217. node->restart();
  218. } break;
  219. }
  220. }
  221. void ParticlesEditor::_generate_aabb() {
  222. float time = generate_seconds->get_value();
  223. float running = 0.0;
  224. EditorProgress ep("gen_aabb", TTR("Generating AABB"), int(time));
  225. bool was_emitting = node->is_emitting();
  226. if (!was_emitting) {
  227. node->set_emitting(true);
  228. OS::get_singleton()->delay_usec(1000);
  229. }
  230. AABB rect;
  231. while (running < time) {
  232. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  233. ep.step("Generating...", int(running), true);
  234. OS::get_singleton()->delay_usec(1000);
  235. AABB capture = node->capture_aabb();
  236. if (rect == AABB())
  237. rect = capture;
  238. else
  239. rect.merge_with(capture);
  240. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  241. }
  242. if (!was_emitting) {
  243. node->set_emitting(false);
  244. }
  245. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  246. ur->create_action(TTR("Generate Visibility AABB"));
  247. ur->add_do_method(node, "set_visibility_aabb", rect);
  248. ur->add_undo_method(node, "set_visibility_aabb", node->get_visibility_aabb());
  249. ur->commit_action();
  250. }
  251. void ParticlesEditor::edit(Particles *p_particles) {
  252. base_node = p_particles;
  253. node = p_particles;
  254. }
  255. void ParticlesEditor::_generate_emission_points() {
  256. /// hacer codigo aca
  257. Vector<Vector3> points;
  258. Vector<Vector3> normals;
  259. if (!_generate(points, normals)) {
  260. return;
  261. }
  262. int point_count = points.size();
  263. int w = 2048;
  264. int h = (point_count / 2048) + 1;
  265. Vector<uint8_t> point_img;
  266. point_img.resize(w * h * 3 * sizeof(float));
  267. {
  268. uint8_t *iw = point_img.ptrw();
  269. zeromem(iw, w * h * 3 * sizeof(float));
  270. const Vector3 *r = points.ptr();
  271. float *wf = (float *)iw;
  272. for (int i = 0; i < point_count; i++) {
  273. wf[i * 3 + 0] = r[i].x;
  274. wf[i * 3 + 1] = r[i].y;
  275. wf[i * 3 + 2] = r[i].z;
  276. }
  277. }
  278. Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));
  279. Ref<ImageTexture> tex;
  280. tex.instance();
  281. Ref<ParticlesMaterial> material = node->get_process_material();
  282. ERR_FAIL_COND(material.is_null());
  283. if (normals.size() > 0) {
  284. material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  285. material->set_emission_point_count(point_count);
  286. material->set_emission_point_texture(tex);
  287. Vector<uint8_t> point_img2;
  288. point_img2.resize(w * h * 3 * sizeof(float));
  289. {
  290. uint8_t *iw = point_img2.ptrw();
  291. zeromem(iw, w * h * 3 * sizeof(float));
  292. const Vector3 *r = normals.ptr();
  293. float *wf = (float *)iw;
  294. for (int i = 0; i < point_count; i++) {
  295. wf[i * 3 + 0] = r[i].x;
  296. wf[i * 3 + 1] = r[i].y;
  297. wf[i * 3 + 2] = r[i].z;
  298. }
  299. }
  300. Ref<Image> image2 = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img2));
  301. Ref<ImageTexture> tex2;
  302. tex2.instance();
  303. material->set_emission_normal_texture(tex2);
  304. } else {
  305. material->set_emission_shape(ParticlesMaterial::EMISSION_SHAPE_POINTS);
  306. material->set_emission_point_count(point_count);
  307. material->set_emission_point_texture(tex);
  308. }
  309. }
  310. void ParticlesEditor::_bind_methods() {
  311. }
  312. ParticlesEditor::ParticlesEditor() {
  313. node = NULL;
  314. particles_editor_hb = memnew(HBoxContainer);
  315. SpatialEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb);
  316. options = memnew(MenuButton);
  317. options->set_switch_on_hover(true);
  318. particles_editor_hb->add_child(options);
  319. particles_editor_hb->hide();
  320. options->set_text(TTR("Particles"));
  321. options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
  322. options->get_popup()->add_separator();
  323. options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
  324. options->get_popup()->add_separator();
  325. options->get_popup()->add_item(TTR("Convert to CPUParticles"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES);
  326. options->get_popup()->add_separator();
  327. options->get_popup()->add_item(TTR("Restart"), MENU_OPTION_RESTART);
  328. options->get_popup()->connect("id_pressed", callable_mp(this, &ParticlesEditor::_menu_option));
  329. generate_aabb = memnew(ConfirmationDialog);
  330. generate_aabb->set_title(TTR("Generate Visibility AABB"));
  331. VBoxContainer *genvb = memnew(VBoxContainer);
  332. generate_aabb->add_child(genvb);
  333. generate_seconds = memnew(SpinBox);
  334. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  335. generate_seconds->set_min(0.1);
  336. generate_seconds->set_max(25);
  337. generate_seconds->set_value(2);
  338. add_child(generate_aabb);
  339. generate_aabb->connect("confirmed", callable_mp(this, &ParticlesEditor::_generate_aabb));
  340. }
  341. void ParticlesEditorPlugin::edit(Object *p_object) {
  342. particles_editor->edit(Object::cast_to<Particles>(p_object));
  343. }
  344. bool ParticlesEditorPlugin::handles(Object *p_object) const {
  345. return p_object->is_class("Particles");
  346. }
  347. void ParticlesEditorPlugin::make_visible(bool p_visible) {
  348. if (p_visible) {
  349. particles_editor->show();
  350. particles_editor->particles_editor_hb->show();
  351. } else {
  352. particles_editor->particles_editor_hb->hide();
  353. particles_editor->hide();
  354. particles_editor->edit(NULL);
  355. }
  356. }
  357. ParticlesEditorPlugin::ParticlesEditorPlugin(EditorNode *p_node) {
  358. editor = p_node;
  359. particles_editor = memnew(ParticlesEditor);
  360. editor->get_viewport()->add_child(particles_editor);
  361. particles_editor->hide();
  362. }
  363. ParticlesEditorPlugin::~ParticlesEditorPlugin() {
  364. }