multimesh_editor_plugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 = ms_node->cast_to<MeshInstance>();
  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 = ss_node->cast_to<MeshInstance>();
  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. DVector<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. DVector<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 = DVector<Face3>::Write();
  111. #if 0
  112. node->get_multimesh()->set_instance_count(populate_amount->get_val());
  113. node->populate_parent(populate_rotate_random->get_val(),populate_tilt_random->get_val(),populate_scale_random->get_val(),populate_scale->get_val());
  114. ERR_EXPLAIN("Parent is not of type VisualInstance.");
  115. ERR_FAIL_COND(!get_parent() || !get_parent()->is_type("VisualInstance"));
  116. ERR_EXPLAIN("Multimesh not present.");
  117. ERR_FAIL_COND(multimesh.is_null());
  118. VisualInstance *vi = get_parent()->cast_to<VisualInstance>();
  119. ERR_EXPLAIN("Parent is not of type VisualInstance, can't be populated.");
  120. ERR_FAIL_COND(!vi);
  121. #endif
  122. DVector<Face3> faces = geometry;
  123. ERR_EXPLAIN(TTR("Parent has no solid faces to populate."));
  124. int facecount = faces.size();
  125. ERR_FAIL_COND(!facecount);
  126. DVector<Face3>::Read r = faces.read();
  127. float area_accum = 0;
  128. Map<float, int> triangle_area_map;
  129. for (int i = 0; i < facecount; i++) {
  130. float area = r[i].get_area();
  131. if (area < CMP_EPSILON)
  132. continue;
  133. triangle_area_map[area_accum] = i;
  134. area_accum += area;
  135. }
  136. ERR_EXPLAIN(TTR("Couldn't map area."));
  137. ERR_FAIL_COND(triangle_area_map.size() == 0);
  138. ERR_EXPLAIN(TTR("Couldn't map area."));
  139. ERR_FAIL_COND(area_accum == 0);
  140. Ref<MultiMesh> multimesh = memnew(MultiMesh);
  141. multimesh->set_mesh(mesh);
  142. int instance_count = populate_amount->get_val();
  143. multimesh->set_instance_count(instance_count);
  144. float _tilt_random = populate_tilt_random->get_val();
  145. float _rotate_random = populate_rotate_random->get_val();
  146. float _scale_random = populate_scale_random->get_val();
  147. float _scale = populate_scale->get_val();
  148. int axis = populate_axis->get_selected();
  149. Transform axis_xform;
  150. if (axis == Vector3::AXIS_Z) {
  151. axis_xform.rotate(Vector3(1, 0, 0), Math_PI * 0.5);
  152. }
  153. if (axis == Vector3::AXIS_X) {
  154. axis_xform.rotate(Vector3(0, 0, 1), Math_PI * 0.5);
  155. }
  156. for (int i = 0; i < instance_count; i++) {
  157. float areapos = Math::random(0, area_accum);
  158. Map<float, int>::Element *E = triangle_area_map.find_closest(areapos);
  159. ERR_FAIL_COND(!E)
  160. int index = E->get();
  161. ERR_FAIL_INDEX(index, facecount);
  162. // ok FINALLY get face
  163. Face3 face = r[index];
  164. //now compute some position inside the face...
  165. Vector3 pos = face.get_random_point_inside();
  166. Vector3 normal = face.get_plane().normal;
  167. Vector3 op_axis = (face.vertex[0] - face.vertex[1]).normalized();
  168. Transform xform;
  169. xform.set_look_at(pos, pos + op_axis, normal);
  170. xform = xform * axis_xform;
  171. Matrix3 post_xform;
  172. post_xform.rotate(xform.basis.get_axis(0), Math::random(-_tilt_random, _tilt_random) * Math_PI);
  173. post_xform.rotate(xform.basis.get_axis(2), Math::random(-_tilt_random, _tilt_random) * Math_PI);
  174. post_xform.rotate(xform.basis.get_axis(1), Math::random(-_rotate_random, _rotate_random) * Math_PI);
  175. xform.basis = post_xform * xform.basis;
  176. //xform.basis.orthonormalize();
  177. xform.basis.scale(Vector3(1, 1, 1) * (_scale + Math::random(-_scale_random, _scale_random)));
  178. multimesh->set_instance_transform(i, xform);
  179. multimesh->set_instance_color(i, Color(1, 1, 1, 1));
  180. }
  181. multimesh->generate_aabb();
  182. node->set_multimesh(multimesh);
  183. }
  184. void MultiMeshEditor::_browsed(const NodePath &p_path) {
  185. NodePath path = node->get_path_to(get_node(p_path));
  186. if (browsing_source)
  187. mesh_source->set_text(path);
  188. else
  189. surface_source->set_text(path);
  190. }
  191. void MultiMeshEditor::_menu_option(int p_option) {
  192. switch (p_option) {
  193. case MENU_OPTION_POPULATE: {
  194. if (_last_pp_node != node) {
  195. surface_source->set_text("..");
  196. mesh_source->set_text("..");
  197. populate_axis->select(1);
  198. populate_rotate_random->set_val(0);
  199. populate_tilt_random->set_val(0);
  200. populate_scale_random->set_val(0);
  201. populate_scale->set_val(1);
  202. populate_amount->set_val(128);
  203. _last_pp_node = node;
  204. }
  205. populate_dialog->popup_centered(Size2(250, 380));
  206. } break;
  207. }
  208. }
  209. void MultiMeshEditor::edit(MultiMeshInstance *p_multimesh) {
  210. node = p_multimesh;
  211. }
  212. void MultiMeshEditor::_browse(bool p_source) {
  213. browsing_source = p_source;
  214. std->get_scene_tree()->set_marked(node, false);
  215. std->popup_centered_ratio();
  216. if (p_source)
  217. std->set_title(TTR("Select a Source Mesh:"));
  218. else
  219. std->set_title(TTR("Select a Target Surface:"));
  220. }
  221. void MultiMeshEditor::_bind_methods() {
  222. ObjectTypeDB::bind_method("_menu_option", &MultiMeshEditor::_menu_option);
  223. ObjectTypeDB::bind_method("_populate", &MultiMeshEditor::_populate);
  224. ObjectTypeDB::bind_method("_browsed", &MultiMeshEditor::_browsed);
  225. ObjectTypeDB::bind_method("_browse", &MultiMeshEditor::_browse);
  226. }
  227. MultiMeshEditor::MultiMeshEditor() {
  228. options = memnew(MenuButton);
  229. SpatialEditor::get_singleton()->add_control_to_menu_panel(options);
  230. options->set_text("MultiMesh");
  231. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("MultiMeshInstance", "EditorIcons"));
  232. options->get_popup()->add_item(TTR("Populate Surface"));
  233. options->get_popup()->connect("item_pressed", this, "_menu_option");
  234. populate_dialog = memnew(ConfirmationDialog);
  235. populate_dialog->set_title(TTR("Populate MultiMesh"));
  236. add_child(populate_dialog);
  237. VBoxContainer *vbc = memnew(VBoxContainer);
  238. populate_dialog->add_child(vbc);
  239. populate_dialog->set_child_rect(vbc);
  240. HBoxContainer *hbc = memnew(HBoxContainer);
  241. surface_source = memnew(LineEdit);
  242. hbc->add_child(surface_source);
  243. surface_source->set_h_size_flags(SIZE_EXPAND_FILL);
  244. Button *b = memnew(Button);
  245. hbc->add_child(b);
  246. b->set_text("..");
  247. b->connect("pressed", this, "_browse", make_binds(false));
  248. vbc->add_margin_child(TTR("Target Surface:"), hbc);
  249. hbc = memnew(HBoxContainer);
  250. mesh_source = memnew(LineEdit);
  251. hbc->add_child(mesh_source);
  252. mesh_source->set_h_size_flags(SIZE_EXPAND_FILL);
  253. b = memnew(Button);
  254. hbc->add_child(b);
  255. b->set_text("..");
  256. vbc->add_margin_child(TTR("Source Mesh:"), hbc);
  257. b->connect("pressed", this, "_browse", make_binds(true));
  258. populate_axis = memnew(OptionButton);
  259. populate_axis->add_item(TTR("X-Axis"));
  260. populate_axis->add_item(TTR("Y-Axis"));
  261. populate_axis->add_item(TTR("Z-Axis"));
  262. populate_axis->select(2);
  263. vbc->add_margin_child(TTR("Mesh Up Axis:"), populate_axis);
  264. populate_rotate_random = memnew(HSlider);
  265. populate_rotate_random->set_max(1);
  266. populate_rotate_random->set_step(0.01);
  267. vbc->add_margin_child(TTR("Random Rotation:"), populate_rotate_random);
  268. populate_tilt_random = memnew(HSlider);
  269. populate_tilt_random->set_max(1);
  270. populate_tilt_random->set_step(0.01);
  271. vbc->add_margin_child(TTR("Random Tilt:"), populate_tilt_random);
  272. populate_scale_random = memnew(SpinBox);
  273. populate_scale_random->set_min(0);
  274. populate_scale_random->set_max(1);
  275. populate_scale_random->set_val(0);
  276. vbc->add_margin_child(TTR("Random Scale:"), populate_scale_random);
  277. populate_scale = memnew(SpinBox);
  278. populate_scale->set_min(0.001);
  279. populate_scale->set_max(4096);
  280. populate_scale->set_val(1);
  281. vbc->add_margin_child(TTR("Scale:"), populate_scale);
  282. populate_amount = memnew(SpinBox);
  283. populate_amount->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  284. populate_amount->set_begin(Point2(20, 232));
  285. populate_amount->set_end(Point2(5, 237));
  286. populate_amount->set_min(1);
  287. populate_amount->set_max(65536);
  288. populate_amount->set_val(128);
  289. vbc->add_margin_child(TTR("Amount:"), populate_amount);
  290. populate_dialog->get_ok()->set_text(TTR("Populate"));
  291. populate_dialog->get_ok()->connect("pressed", this, "_populate");
  292. std = memnew(SceneTreeDialog);
  293. populate_dialog->add_child(std);
  294. std->connect("selected", this, "_browsed");
  295. _last_pp_node = NULL;
  296. err_dialog = memnew(AcceptDialog);
  297. add_child(err_dialog);
  298. }
  299. void MultiMeshEditorPlugin::edit(Object *p_object) {
  300. multimesh_editor->edit(p_object->cast_to<MultiMeshInstance>());
  301. }
  302. bool MultiMeshEditorPlugin::handles(Object *p_object) const {
  303. return p_object->is_type("MultiMeshInstance");
  304. }
  305. void MultiMeshEditorPlugin::make_visible(bool p_visible) {
  306. if (p_visible) {
  307. multimesh_editor->options->show();
  308. } else {
  309. multimesh_editor->options->hide();
  310. multimesh_editor->edit(NULL);
  311. }
  312. }
  313. MultiMeshEditorPlugin::MultiMeshEditorPlugin(EditorNode *p_node) {
  314. editor = p_node;
  315. multimesh_editor = memnew(MultiMeshEditor);
  316. editor->get_viewport()->add_child(multimesh_editor);
  317. multimesh_editor->options->hide();
  318. }
  319. MultiMeshEditorPlugin::~MultiMeshEditorPlugin() {
  320. }