multimesh_editor_plugin.cpp 12 KB

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