gpu_particles_3d_editor_plugin.cpp 15 KB

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