gpu_particles_3d_editor_plugin.cpp 15 KB

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