multimesh_editor_plugin.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 = PoolVector<Face3>::Write();
  111. PoolVector<Face3> faces = geometry;
  112. ERR_EXPLAIN(TTR("Parent has no solid faces to populate."));
  113. int facecount = faces.size();
  114. ERR_FAIL_COND(!facecount);
  115. PoolVector<Face3>::Read r = faces.read();
  116. float area_accum = 0;
  117. Map<float, int> triangle_area_map;
  118. for (int i = 0; i < facecount; i++) {
  119. float area = r[i].get_area();
  120. if (area < CMP_EPSILON)
  121. continue;
  122. triangle_area_map[area_accum] = i;
  123. area_accum += area;
  124. }
  125. ERR_EXPLAIN(TTR("Couldn't map area."));
  126. ERR_FAIL_COND(triangle_area_map.size() == 0);
  127. ERR_EXPLAIN(TTR("Couldn't map area."));
  128. ERR_FAIL_COND(area_accum == 0);
  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_color_format(MultiMesh::COLOR_NONE);
  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. Transform 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. Map<float, int>::Element *E = triangle_area_map.find_closest(areapos);
  150. ERR_FAIL_COND(!E)
  151. int index = E->get();
  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. Transform 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_axis(1), -Math::random(-_rotate_random, _rotate_random) * Math_PI);
  164. post_xform.rotate(xform.basis.get_axis(2), -Math::random(-_tilt_random, _tilt_random) * Math_PI);
  165. post_xform.rotate(xform.basis.get_axis(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. void MultiMeshEditor::_menu_option(int p_option) {
  181. switch (p_option) {
  182. case MENU_OPTION_POPULATE: {
  183. if (_last_pp_node != node) {
  184. surface_source->set_text("..");
  185. mesh_source->set_text("..");
  186. populate_axis->select(1);
  187. populate_rotate_random->set_value(0);
  188. populate_tilt_random->set_value(0);
  189. populate_scale_random->set_value(0);
  190. populate_scale->set_value(1);
  191. populate_amount->set_value(128);
  192. _last_pp_node = node;
  193. }
  194. populate_dialog->popup_centered(Size2(250, 380));
  195. } break;
  196. }
  197. }
  198. void MultiMeshEditor::edit(MultiMeshInstance *p_multimesh) {
  199. node = p_multimesh;
  200. }
  201. void MultiMeshEditor::_browse(bool p_source) {
  202. browsing_source = p_source;
  203. std->get_scene_tree()->set_marked(node, false);
  204. std->popup_centered_ratio();
  205. if (p_source)
  206. std->set_title(TTR("Select a Source Mesh:"));
  207. else
  208. std->set_title(TTR("Select a Target Surface:"));
  209. }
  210. void MultiMeshEditor::_bind_methods() {
  211. ClassDB::bind_method("_menu_option", &MultiMeshEditor::_menu_option);
  212. ClassDB::bind_method("_populate", &MultiMeshEditor::_populate);
  213. ClassDB::bind_method("_browsed", &MultiMeshEditor::_browsed);
  214. ClassDB::bind_method("_browse", &MultiMeshEditor::_browse);
  215. }
  216. MultiMeshEditor::MultiMeshEditor() {
  217. options = memnew(MenuButton);
  218. SpatialEditor::get_singleton()->add_control_to_menu_panel(options);
  219. options->set_text("MultiMesh");
  220. options->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("MultiMeshInstance", "EditorIcons"));
  221. options->get_popup()->add_item(TTR("Populate Surface"));
  222. options->get_popup()->connect("id_pressed", this, "_menu_option");
  223. populate_dialog = memnew(ConfirmationDialog);
  224. populate_dialog->set_title(TTR("Populate MultiMesh"));
  225. add_child(populate_dialog);
  226. VBoxContainer *vbc = memnew(VBoxContainer);
  227. populate_dialog->add_child(vbc);
  228. //populate_dialog->set_child_rect(vbc);
  229. HBoxContainer *hbc = memnew(HBoxContainer);
  230. surface_source = memnew(LineEdit);
  231. hbc->add_child(surface_source);
  232. surface_source->set_h_size_flags(SIZE_EXPAND_FILL);
  233. Button *b = memnew(Button);
  234. hbc->add_child(b);
  235. b->set_text("..");
  236. b->connect("pressed", this, "_browse", make_binds(false));
  237. vbc->add_margin_child(TTR("Target Surface:"), hbc);
  238. hbc = memnew(HBoxContainer);
  239. mesh_source = memnew(LineEdit);
  240. hbc->add_child(mesh_source);
  241. mesh_source->set_h_size_flags(SIZE_EXPAND_FILL);
  242. b = memnew(Button);
  243. hbc->add_child(b);
  244. b->set_text("..");
  245. vbc->add_margin_child(TTR("Source Mesh:"), hbc);
  246. b->connect("pressed", this, "_browse", make_binds(true));
  247. populate_axis = memnew(OptionButton);
  248. populate_axis->add_item(TTR("X-Axis"));
  249. populate_axis->add_item(TTR("Y-Axis"));
  250. populate_axis->add_item(TTR("Z-Axis"));
  251. populate_axis->select(2);
  252. vbc->add_margin_child(TTR("Mesh Up Axis:"), populate_axis);
  253. populate_rotate_random = memnew(HSlider);
  254. populate_rotate_random->set_max(1);
  255. populate_rotate_random->set_step(0.01);
  256. vbc->add_margin_child(TTR("Random Rotation:"), populate_rotate_random);
  257. populate_tilt_random = memnew(HSlider);
  258. populate_tilt_random->set_max(1);
  259. populate_tilt_random->set_step(0.01);
  260. vbc->add_margin_child(TTR("Random Tilt:"), populate_tilt_random);
  261. populate_scale_random = memnew(SpinBox);
  262. populate_scale_random->set_min(0);
  263. populate_scale_random->set_max(1);
  264. populate_scale_random->set_value(0);
  265. populate_scale_random->set_step(0.01);
  266. vbc->add_margin_child(TTR("Random Scale:"), populate_scale_random);
  267. populate_scale = memnew(SpinBox);
  268. populate_scale->set_min(0.001);
  269. populate_scale->set_max(4096);
  270. populate_scale->set_value(1);
  271. populate_scale->set_step(0.01);
  272. vbc->add_margin_child(TTR("Scale:"), populate_scale);
  273. populate_amount = memnew(SpinBox);
  274. populate_amount->set_anchor(MARGIN_RIGHT, ANCHOR_END);
  275. populate_amount->set_begin(Point2(20, 232));
  276. populate_amount->set_end(Point2(-5, 237));
  277. populate_amount->set_min(1);
  278. populate_amount->set_max(65536);
  279. populate_amount->set_value(128);
  280. vbc->add_margin_child(TTR("Amount:"), populate_amount);
  281. populate_dialog->get_ok()->set_text(TTR("Populate"));
  282. populate_dialog->get_ok()->connect("pressed", this, "_populate");
  283. std = memnew(SceneTreeDialog);
  284. populate_dialog->add_child(std);
  285. std->connect("selected", this, "_browsed");
  286. _last_pp_node = NULL;
  287. err_dialog = memnew(AcceptDialog);
  288. add_child(err_dialog);
  289. }
  290. void MultiMeshEditorPlugin::edit(Object *p_object) {
  291. multimesh_editor->edit(Object::cast_to<MultiMeshInstance>(p_object));
  292. }
  293. bool MultiMeshEditorPlugin::handles(Object *p_object) const {
  294. return p_object->is_class("MultiMeshInstance");
  295. }
  296. void MultiMeshEditorPlugin::make_visible(bool p_visible) {
  297. if (p_visible) {
  298. multimesh_editor->options->show();
  299. } else {
  300. multimesh_editor->options->hide();
  301. multimesh_editor->edit(NULL);
  302. }
  303. }
  304. MultiMeshEditorPlugin::MultiMeshEditorPlugin(EditorNode *p_node) {
  305. editor = p_node;
  306. multimesh_editor = memnew(MultiMeshEditor);
  307. editor->get_viewport()->add_child(multimesh_editor);
  308. multimesh_editor->options->hide();
  309. }
  310. MultiMeshEditorPlugin::~MultiMeshEditorPlugin() {
  311. }