gpu_particles_3d_editor_plugin.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. Vector<StringName> valid_types;
  181. valid_types.push_back("MeshInstance3D");
  182. emission_tree_dialog->set_valid_types(valid_types);
  183. add_child(emission_tree_dialog);
  184. emission_tree_dialog->connect("selected", callable_mp(this, &GPUParticles3DEditorBase::_node_selected));
  185. }
  186. void GPUParticles3DEditor::_node_removed(Node *p_node) {
  187. if (p_node == node) {
  188. node = nullptr;
  189. hide();
  190. }
  191. }
  192. void GPUParticles3DEditor::_notification(int p_notification) {
  193. switch (p_notification) {
  194. case NOTIFICATION_ENTER_TREE: {
  195. options->set_icon(options->get_popup()->get_editor_theme_icon(SNAME("GPUParticles3D")));
  196. get_tree()->connect("node_removed", callable_mp(this, &GPUParticles3DEditor::_node_removed));
  197. } break;
  198. }
  199. }
  200. void GPUParticles3DEditor::_menu_option(int p_option) {
  201. switch (p_option) {
  202. case MENU_OPTION_GENERATE_AABB: {
  203. // Add one second to the default generation lifetime, since the progress is updated every second.
  204. generate_seconds->set_value(MAX(1.0, trunc(node->get_lifetime()) + 1.0));
  205. if (generate_seconds->get_value() >= 11.0 + CMP_EPSILON) {
  206. // Only pop up the time dialog if the particle's lifetime is long enough to warrant shortening it.
  207. generate_aabb->popup_centered();
  208. } else {
  209. // Generate the visibility AABB immediately.
  210. _generate_aabb();
  211. }
  212. } break;
  213. case MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE: {
  214. Ref<ParticleProcessMaterial> mat = node->get_process_material();
  215. if (mat.is_null()) {
  216. EditorNode::get_singleton()->show_warning(TTR("A processor material of type 'ParticleProcessMaterial' is required."));
  217. return;
  218. }
  219. emission_tree_dialog->popup_scenetree_dialog();
  220. } break;
  221. case MENU_OPTION_CONVERT_TO_CPU_PARTICLES: {
  222. CPUParticles3D *cpu_particles = memnew(CPUParticles3D);
  223. cpu_particles->convert_from_particles(node);
  224. cpu_particles->set_name(node->get_name());
  225. cpu_particles->set_transform(node->get_transform());
  226. cpu_particles->set_visible(node->is_visible());
  227. cpu_particles->set_process_mode(node->get_process_mode());
  228. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  229. ur->create_action(TTR("Convert to CPUParticles3D"), UndoRedo::MERGE_DISABLE, node);
  230. SceneTreeDock::get_singleton()->replace_node(node, cpu_particles);
  231. ur->commit_action(false);
  232. } break;
  233. case MENU_OPTION_RESTART: {
  234. node->restart();
  235. } break;
  236. }
  237. }
  238. void GPUParticles3DEditor::_generate_aabb() {
  239. double time = generate_seconds->get_value();
  240. double running = 0.0;
  241. EditorProgress ep("gen_aabb", TTR("Generating Visibility AABB (Waiting for Particle Simulation)"), int(time));
  242. bool was_emitting = node->is_emitting();
  243. if (!was_emitting) {
  244. node->set_emitting(true);
  245. OS::get_singleton()->delay_usec(1000);
  246. }
  247. AABB rect;
  248. while (running < time) {
  249. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  250. ep.step("Generating...", int(running), true);
  251. OS::get_singleton()->delay_usec(1000);
  252. AABB capture = node->capture_aabb();
  253. if (rect == AABB()) {
  254. rect = capture;
  255. } else {
  256. rect.merge_with(capture);
  257. }
  258. running += (OS::get_singleton()->get_ticks_usec() - ticks) / 1000000.0;
  259. }
  260. if (!was_emitting) {
  261. node->set_emitting(false);
  262. }
  263. EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
  264. ur->create_action(TTR("Generate Visibility AABB"));
  265. ur->add_do_method(node, "set_visibility_aabb", rect);
  266. ur->add_undo_method(node, "set_visibility_aabb", node->get_visibility_aabb());
  267. ur->commit_action();
  268. }
  269. void GPUParticles3DEditor::edit(GPUParticles3D *p_particles) {
  270. base_node = p_particles;
  271. node = p_particles;
  272. }
  273. void GPUParticles3DEditor::_generate_emission_points() {
  274. /// hacer codigo aca
  275. Vector<Vector3> points;
  276. Vector<Vector3> normals;
  277. if (!_generate(points, normals)) {
  278. return;
  279. }
  280. int point_count = points.size();
  281. int w = 2048;
  282. int h = (point_count / 2048) + 1;
  283. Vector<uint8_t> point_img;
  284. point_img.resize(w * h * 3 * sizeof(float));
  285. {
  286. uint8_t *iw = point_img.ptrw();
  287. memset(iw, 0, w * h * 3 * sizeof(float));
  288. const Vector3 *r = points.ptr();
  289. float *wf = reinterpret_cast<float *>(iw);
  290. for (int i = 0; i < point_count; i++) {
  291. wf[i * 3 + 0] = r[i].x;
  292. wf[i * 3 + 1] = r[i].y;
  293. wf[i * 3 + 2] = r[i].z;
  294. }
  295. }
  296. Ref<Image> image = memnew(Image(w, h, false, Image::FORMAT_RGBF, point_img));
  297. Ref<ImageTexture> tex = ImageTexture::create_from_image(image);
  298. Ref<ParticleProcessMaterial> mat = node->get_process_material();
  299. ERR_FAIL_COND(mat.is_null());
  300. if (normals.size() > 0) {
  301. mat->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_DIRECTED_POINTS);
  302. mat->set_emission_point_count(point_count);
  303. mat->set_emission_point_texture(tex);
  304. Vector<uint8_t> point_img2;
  305. point_img2.resize(w * h * 3 * sizeof(float));
  306. {
  307. uint8_t *iw = point_img2.ptrw();
  308. memset(iw, 0, w * h * 3 * sizeof(float));
  309. const Vector3 *r = normals.ptr();
  310. float *wf = reinterpret_cast<float *>(iw);
  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. mat->set_emission_normal_texture(ImageTexture::create_from_image(image2));
  319. } else {
  320. mat->set_emission_shape(ParticleProcessMaterial::EMISSION_SHAPE_POINTS);
  321. mat->set_emission_point_count(point_count);
  322. mat->set_emission_point_texture(tex);
  323. }
  324. }
  325. void GPUParticles3DEditor::_bind_methods() {
  326. }
  327. GPUParticles3DEditor::GPUParticles3DEditor() {
  328. node = nullptr;
  329. particles_editor_hb = memnew(HBoxContainer);
  330. Node3DEditor::get_singleton()->add_control_to_menu_panel(particles_editor_hb);
  331. options = memnew(MenuButton);
  332. options->set_switch_on_hover(true);
  333. particles_editor_hb->add_child(options);
  334. particles_editor_hb->hide();
  335. options->set_text(TTR("GPUParticles3D"));
  336. options->get_popup()->add_item(TTR("Restart"), MENU_OPTION_RESTART);
  337. options->get_popup()->add_item(TTR("Generate AABB"), MENU_OPTION_GENERATE_AABB);
  338. options->get_popup()->add_item(TTR("Create Emission Points From Node"), MENU_OPTION_CREATE_EMISSION_VOLUME_FROM_NODE);
  339. options->get_popup()->add_item(TTR("Convert to CPUParticles3D"), MENU_OPTION_CONVERT_TO_CPU_PARTICLES);
  340. options->get_popup()->connect("id_pressed", callable_mp(this, &GPUParticles3DEditor::_menu_option));
  341. generate_aabb = memnew(ConfirmationDialog);
  342. generate_aabb->set_title(TTR("Generate Visibility AABB"));
  343. VBoxContainer *genvb = memnew(VBoxContainer);
  344. generate_aabb->add_child(genvb);
  345. generate_seconds = memnew(SpinBox);
  346. genvb->add_margin_child(TTR("Generation Time (sec):"), generate_seconds);
  347. generate_seconds->set_min(0.1);
  348. generate_seconds->set_max(25);
  349. generate_seconds->set_value(2);
  350. add_child(generate_aabb);
  351. generate_aabb->connect("confirmed", callable_mp(this, &GPUParticles3DEditor::_generate_aabb));
  352. }
  353. void GPUParticles3DEditorPlugin::edit(Object *p_object) {
  354. particles_editor->edit(Object::cast_to<GPUParticles3D>(p_object));
  355. }
  356. bool GPUParticles3DEditorPlugin::handles(Object *p_object) const {
  357. return p_object->is_class("GPUParticles3D");
  358. }
  359. void GPUParticles3DEditorPlugin::make_visible(bool p_visible) {
  360. if (p_visible) {
  361. particles_editor->show();
  362. particles_editor->particles_editor_hb->show();
  363. } else {
  364. particles_editor->particles_editor_hb->hide();
  365. particles_editor->hide();
  366. particles_editor->edit(nullptr);
  367. }
  368. }
  369. GPUParticles3DEditorPlugin::GPUParticles3DEditorPlugin() {
  370. particles_editor = memnew(GPUParticles3DEditor);
  371. EditorNode::get_singleton()->get_main_screen_control()->add_child(particles_editor);
  372. particles_editor->hide();
  373. }
  374. GPUParticles3DEditorPlugin::~GPUParticles3DEditorPlugin() {
  375. }