multimesh_editor_plugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /**************************************************************************/
  2. /* multimesh_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 "multimesh_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/gui/scene_tree_editor.h"
  34. #include "editor/plugins/node_3d_editor_plugin.h"
  35. #include "scene/3d/mesh_instance_3d.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/menu_button.h"
  38. #include "scene/gui/option_button.h"
  39. void MultiMeshEditor::_node_removed(Node *p_node) {
  40. if (p_node == node) {
  41. node = nullptr;
  42. hide();
  43. }
  44. }
  45. void MultiMeshEditor::_populate() {
  46. if (!node) {
  47. return;
  48. }
  49. Ref<Mesh> mesh;
  50. if (mesh_source->get_text().is_empty()) {
  51. Ref<MultiMesh> multimesh;
  52. multimesh = node->get_multimesh();
  53. if (multimesh.is_null()) {
  54. err_dialog->set_text(TTR("No mesh source specified (and no MultiMesh set in node)."));
  55. err_dialog->popup_centered();
  56. return;
  57. }
  58. if (multimesh->get_mesh().is_null()) {
  59. err_dialog->set_text(TTR("No mesh source specified (and MultiMesh contains no Mesh)."));
  60. err_dialog->popup_centered();
  61. return;
  62. }
  63. mesh = multimesh->get_mesh();
  64. } else {
  65. Node *ms_node = node->get_node(mesh_source->get_text());
  66. if (!ms_node) {
  67. err_dialog->set_text(TTR("Mesh source is invalid (invalid path)."));
  68. err_dialog->popup_centered();
  69. return;
  70. }
  71. MeshInstance3D *ms_instance = Object::cast_to<MeshInstance3D>(ms_node);
  72. if (!ms_instance) {
  73. err_dialog->set_text(TTR("Mesh source is invalid (not a MeshInstance3D)."));
  74. err_dialog->popup_centered();
  75. return;
  76. }
  77. mesh = ms_instance->get_mesh();
  78. if (mesh.is_null()) {
  79. err_dialog->set_text(TTR("Mesh source is invalid (contains no Mesh resource)."));
  80. err_dialog->popup_centered();
  81. return;
  82. }
  83. }
  84. if (surface_source->get_text().is_empty()) {
  85. err_dialog->set_text(TTR("No surface source specified."));
  86. err_dialog->popup_centered();
  87. return;
  88. }
  89. Node *ss_node = node->get_node(surface_source->get_text());
  90. if (!ss_node) {
  91. err_dialog->set_text(TTR("Surface source is invalid (invalid path)."));
  92. err_dialog->popup_centered();
  93. return;
  94. }
  95. MeshInstance3D *ss_instance = Object::cast_to<MeshInstance3D>(ss_node);
  96. if (!ss_instance || !ss_instance->get_mesh().is_valid()) {
  97. err_dialog->set_text(TTR("Surface source is invalid (no geometry)."));
  98. err_dialog->popup_centered();
  99. return;
  100. }
  101. Transform3D geom_xform = node->get_global_transform().affine_inverse() * ss_instance->get_global_transform();
  102. Vector<Face3> geometry = ss_instance->get_mesh()->get_faces();
  103. if (geometry.size() == 0) {
  104. err_dialog->set_text(TTR("Surface source is invalid (no faces)."));
  105. err_dialog->popup_centered();
  106. return;
  107. }
  108. //make all faces local
  109. int gc = geometry.size();
  110. Face3 *w = geometry.ptrw();
  111. for (int i = 0; i < gc; i++) {
  112. for (int j = 0; j < 3; j++) {
  113. w[i].vertex[j] = geom_xform.xform(w[i].vertex[j]);
  114. }
  115. }
  116. Vector<Face3> faces = geometry;
  117. int facecount = faces.size();
  118. ERR_FAIL_COND_MSG(!facecount, "Parent has no solid faces to populate.");
  119. const Face3 *r = faces.ptr();
  120. float area_accum = 0;
  121. RBMap<float, int> triangle_area_map;
  122. for (int i = 0; i < facecount; i++) {
  123. float area = r[i].get_area();
  124. if (area < CMP_EPSILON) {
  125. continue;
  126. }
  127. triangle_area_map[area_accum] = i;
  128. area_accum += area;
  129. }
  130. ERR_FAIL_COND_MSG(triangle_area_map.is_empty(), "Couldn't map area.");
  131. ERR_FAIL_COND_MSG(area_accum == 0, "Couldn't map area.");
  132. Ref<MultiMesh> multimesh = memnew(MultiMesh);
  133. multimesh->set_mesh(mesh);
  134. int instance_count = populate_amount->get_value();
  135. multimesh->set_transform_format(MultiMesh::TRANSFORM_3D);
  136. multimesh->set_use_colors(false);
  137. multimesh->set_instance_count(instance_count);
  138. float _tilt_random = populate_tilt_random->get_value();
  139. float _rotate_random = populate_rotate_random->get_value();
  140. float _scale_random = populate_scale_random->get_value();
  141. float _scale = populate_scale->get_value();
  142. int axis = populate_axis->get_selected();
  143. Transform3D axis_xform;
  144. if (axis == Vector3::AXIS_Z) {
  145. axis_xform.rotate(Vector3(1, 0, 0), -Math_PI * 0.5);
  146. }
  147. if (axis == Vector3::AXIS_X) {
  148. axis_xform.rotate(Vector3(0, 0, 1), -Math_PI * 0.5);
  149. }
  150. for (int i = 0; i < instance_count; i++) {
  151. float areapos = Math::random(0.0f, area_accum);
  152. RBMap<float, int>::Iterator E = triangle_area_map.find_closest(areapos);
  153. ERR_FAIL_COND(!E);
  154. int index = E->value;
  155. ERR_FAIL_INDEX(index, facecount);
  156. // ok FINALLY get face
  157. Face3 face = r[index];
  158. //now compute some position inside the face...
  159. Vector3 pos = face.get_random_point_inside();
  160. Vector3 normal = face.get_plane().normal;
  161. Vector3 op_axis = (face.vertex[0] - face.vertex[1]).normalized();
  162. Transform3D xform;
  163. xform.set_look_at(pos, pos + op_axis, normal);
  164. xform = xform * axis_xform;
  165. Basis post_xform;
  166. post_xform.rotate(xform.basis.get_column(1), -Math::random(-_rotate_random, _rotate_random) * Math_PI);
  167. post_xform.rotate(xform.basis.get_column(2), -Math::random(-_tilt_random, _tilt_random) * Math_PI);
  168. post_xform.rotate(xform.basis.get_column(0), -Math::random(-_tilt_random, _tilt_random) * Math_PI);
  169. xform.basis = post_xform * xform.basis;
  170. //xform.basis.orthonormalize();
  171. xform.basis.scale(Vector3(1, 1, 1) * (_scale + Math::random(-_scale_random, _scale_random)));
  172. multimesh->set_instance_transform(i, xform);
  173. }
  174. node->set_multimesh(multimesh);
  175. }
  176. void MultiMeshEditor::_browsed(const NodePath &p_path) {
  177. NodePath path = node->get_path_to(get_node(p_path));
  178. if (browsing_source) {
  179. mesh_source->set_text(path);
  180. } else {
  181. surface_source->set_text(path);
  182. }
  183. }
  184. void MultiMeshEditor::_menu_option(int p_option) {
  185. switch (p_option) {
  186. case MENU_OPTION_POPULATE: {
  187. if (_last_pp_node != node) {
  188. surface_source->set_text("..");
  189. mesh_source->set_text("..");
  190. populate_axis->select(1);
  191. populate_rotate_random->set_value(0);
  192. populate_tilt_random->set_value(0);
  193. populate_scale_random->set_value(0);
  194. populate_scale->set_value(1);
  195. populate_amount->set_value(128);
  196. _last_pp_node = node;
  197. }
  198. populate_dialog->popup_centered(Size2(250, 380));
  199. } break;
  200. }
  201. }
  202. void MultiMeshEditor::edit(MultiMeshInstance3D *p_multimesh) {
  203. node = p_multimesh;
  204. }
  205. void MultiMeshEditor::_browse(bool p_source) {
  206. browsing_source = p_source;
  207. Node *browsed_node = nullptr;
  208. if (p_source) {
  209. browsed_node = node->get_node_or_null(mesh_source->get_text());
  210. std->set_title(TTR("Select a Source Mesh:"));
  211. } else {
  212. browsed_node = node->get_node_or_null(surface_source->get_text());
  213. std->set_title(TTR("Select a Target Surface:"));
  214. }
  215. std->popup_scenetree_dialog(browsed_node);
  216. }
  217. void MultiMeshEditor::_bind_methods() {
  218. }
  219. MultiMeshEditor::MultiMeshEditor() {
  220. options = memnew(MenuButton);
  221. options->set_switch_on_hover(true);
  222. Node3DEditor::get_singleton()->add_control_to_menu_panel(options);
  223. options->set_text("MultiMesh");
  224. options->set_icon(EditorNode::get_singleton()->get_editor_theme()->get_icon(SNAME("MultiMeshInstance3D"), EditorStringName(EditorIcons)));
  225. options->get_popup()->add_item(TTR("Populate Surface"));
  226. options->get_popup()->connect("id_pressed", callable_mp(this, &MultiMeshEditor::_menu_option));
  227. populate_dialog = memnew(ConfirmationDialog);
  228. populate_dialog->set_title(TTR("Populate MultiMesh"));
  229. add_child(populate_dialog);
  230. VBoxContainer *vbc = memnew(VBoxContainer);
  231. populate_dialog->add_child(vbc);
  232. //populate_dialog->set_child_rect(vbc);
  233. HBoxContainer *hbc = memnew(HBoxContainer);
  234. surface_source = memnew(LineEdit);
  235. hbc->add_child(surface_source);
  236. surface_source->set_h_size_flags(SIZE_EXPAND_FILL);
  237. Button *b = memnew(Button);
  238. hbc->add_child(b);
  239. b->set_text("..");
  240. b->connect(SceneStringName(pressed), callable_mp(this, &MultiMeshEditor::_browse).bind(false));
  241. vbc->add_margin_child(TTR("Target Surface:"), hbc);
  242. hbc = memnew(HBoxContainer);
  243. mesh_source = memnew(LineEdit);
  244. hbc->add_child(mesh_source);
  245. mesh_source->set_h_size_flags(SIZE_EXPAND_FILL);
  246. b = memnew(Button);
  247. hbc->add_child(b);
  248. b->set_text("..");
  249. vbc->add_margin_child(TTR("Source Mesh:"), hbc);
  250. b->connect(SceneStringName(pressed), callable_mp(this, &MultiMeshEditor::_browse).bind(true));
  251. populate_axis = memnew(OptionButton);
  252. populate_axis->add_item(TTR("X-Axis"));
  253. populate_axis->add_item(TTR("Y-Axis"));
  254. populate_axis->add_item(TTR("Z-Axis"));
  255. populate_axis->select(2);
  256. vbc->add_margin_child(TTR("Mesh Up Axis:"), populate_axis);
  257. populate_rotate_random = memnew(HSlider);
  258. populate_rotate_random->set_max(1);
  259. populate_rotate_random->set_step(0.01);
  260. vbc->add_margin_child(TTR("Random Rotation:"), populate_rotate_random);
  261. populate_tilt_random = memnew(HSlider);
  262. populate_tilt_random->set_max(1);
  263. populate_tilt_random->set_step(0.01);
  264. vbc->add_margin_child(TTR("Random Tilt:"), populate_tilt_random);
  265. populate_scale_random = memnew(SpinBox);
  266. populate_scale_random->set_min(0);
  267. populate_scale_random->set_max(1);
  268. populate_scale_random->set_value(0);
  269. populate_scale_random->set_step(0.01);
  270. vbc->add_margin_child(TTR("Random Scale:"), populate_scale_random);
  271. populate_scale = memnew(SpinBox);
  272. populate_scale->set_min(0.001);
  273. populate_scale->set_max(4096);
  274. populate_scale->set_value(1);
  275. populate_scale->set_step(0.01);
  276. vbc->add_margin_child(TTR("Scale:"), populate_scale);
  277. populate_amount = memnew(SpinBox);
  278. populate_amount->set_anchor(SIDE_RIGHT, ANCHOR_END);
  279. populate_amount->set_begin(Point2(20, 232));
  280. populate_amount->set_end(Point2(-5, 237));
  281. populate_amount->set_min(1);
  282. populate_amount->set_max(65536);
  283. populate_amount->set_value(128);
  284. vbc->add_margin_child(TTR("Amount:"), populate_amount);
  285. populate_dialog->set_ok_button_text(TTR("Populate"));
  286. populate_dialog->get_ok_button()->connect(SceneStringName(pressed), callable_mp(this, &MultiMeshEditor::_populate));
  287. std = memnew(SceneTreeDialog);
  288. Vector<StringName> valid_types;
  289. valid_types.push_back("MeshInstance3D");
  290. std->set_valid_types(valid_types);
  291. populate_dialog->add_child(std);
  292. std->connect("selected", callable_mp(this, &MultiMeshEditor::_browsed));
  293. _last_pp_node = nullptr;
  294. err_dialog = memnew(AcceptDialog);
  295. add_child(err_dialog);
  296. }
  297. void MultiMeshEditorPlugin::edit(Object *p_object) {
  298. multimesh_editor->edit(Object::cast_to<MultiMeshInstance3D>(p_object));
  299. }
  300. bool MultiMeshEditorPlugin::handles(Object *p_object) const {
  301. return p_object->is_class("MultiMeshInstance3D");
  302. }
  303. void MultiMeshEditorPlugin::make_visible(bool p_visible) {
  304. if (p_visible) {
  305. multimesh_editor->options->show();
  306. } else {
  307. multimesh_editor->options->hide();
  308. multimesh_editor->edit(nullptr);
  309. }
  310. }
  311. MultiMeshEditorPlugin::MultiMeshEditorPlugin() {
  312. multimesh_editor = memnew(MultiMeshEditor);
  313. EditorNode::get_singleton()->get_main_screen_control()->add_child(multimesh_editor);
  314. multimesh_editor->options->hide();
  315. }
  316. MultiMeshEditorPlugin::~MultiMeshEditorPlugin() {
  317. }