2
0

multimesh_editor_plugin.cpp 12 KB

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