mesh_library.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**************************************************************************/
  2. /* mesh_library.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 "mesh_library.h"
  31. #include "box_shape_3d.h"
  32. bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
  33. String prop_name = p_name;
  34. if (prop_name.begins_with("item/")) {
  35. int idx = prop_name.get_slicec('/', 1).to_int();
  36. String what = prop_name.get_slicec('/', 2);
  37. if (!item_map.has(idx)) {
  38. create_item(idx);
  39. }
  40. if (what == "name") {
  41. set_item_name(idx, p_value);
  42. } else if (what == "mesh") {
  43. set_item_mesh(idx, p_value);
  44. } else if (what == "mesh_transform") {
  45. set_item_mesh_transform(idx, p_value);
  46. } else if (what == "mesh_cast_shadow") {
  47. switch ((int)p_value) {
  48. case 0: {
  49. set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_OFF);
  50. } break;
  51. case 1: {
  52. set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);
  53. } break;
  54. case 2: {
  55. set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_DOUBLE_SIDED);
  56. } break;
  57. case 3: {
  58. set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_SHADOWS_ONLY);
  59. } break;
  60. default: {
  61. set_item_mesh_cast_shadow(idx, RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON);
  62. } break;
  63. }
  64. } else if (what == "shape") {
  65. Vector<ShapeData> shapes;
  66. ShapeData sd;
  67. sd.shape = p_value;
  68. shapes.push_back(sd);
  69. set_item_shapes(idx, shapes);
  70. } else if (what == "shapes") {
  71. _set_item_shapes(idx, p_value);
  72. } else if (what == "preview") {
  73. set_item_preview(idx, p_value);
  74. } else if (what == "navigation_mesh") {
  75. set_item_navigation_mesh(idx, p_value);
  76. } else if (what == "navigation_mesh_transform") {
  77. set_item_navigation_mesh_transform(idx, p_value);
  78. #ifndef DISABLE_DEPRECATED
  79. } else if (what == "navmesh") { // Renamed in 4.0 beta 9.
  80. set_item_navigation_mesh(idx, p_value);
  81. } else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
  82. set_item_navigation_mesh_transform(idx, p_value);
  83. #endif // DISABLE_DEPRECATED
  84. } else if (what == "navigation_layers") {
  85. set_item_navigation_layers(idx, p_value);
  86. } else {
  87. return false;
  88. }
  89. return true;
  90. }
  91. return false;
  92. }
  93. bool MeshLibrary::_get(const StringName &p_name, Variant &r_ret) const {
  94. String prop_name = p_name;
  95. int idx = prop_name.get_slicec('/', 1).to_int();
  96. ERR_FAIL_COND_V(!item_map.has(idx), false);
  97. String what = prop_name.get_slicec('/', 2);
  98. if (what == "name") {
  99. r_ret = get_item_name(idx);
  100. } else if (what == "mesh") {
  101. r_ret = get_item_mesh(idx);
  102. } else if (what == "mesh_transform") {
  103. r_ret = get_item_mesh_transform(idx);
  104. } else if (what == "mesh_cast_shadow") {
  105. r_ret = (int)get_item_mesh_cast_shadow(idx);
  106. } else if (what == "shapes") {
  107. r_ret = _get_item_shapes(idx);
  108. } else if (what == "navigation_mesh") {
  109. r_ret = get_item_navigation_mesh(idx);
  110. } else if (what == "navigation_mesh_transform") {
  111. r_ret = get_item_navigation_mesh_transform(idx);
  112. #ifndef DISABLE_DEPRECATED
  113. } else if (what == "navmesh") { // Renamed in 4.0 beta 9.
  114. r_ret = get_item_navigation_mesh(idx);
  115. } else if (what == "navmesh_transform") { // Renamed in 4.0 beta 9.
  116. r_ret = get_item_navigation_mesh_transform(idx);
  117. #endif // DISABLE_DEPRECATED
  118. } else if (what == "navigation_layers") {
  119. r_ret = get_item_navigation_layers(idx);
  120. } else if (what == "preview") {
  121. r_ret = get_item_preview(idx);
  122. } else {
  123. return false;
  124. }
  125. return true;
  126. }
  127. void MeshLibrary::_get_property_list(List<PropertyInfo> *p_list) const {
  128. for (const KeyValue<int, Item> &E : item_map) {
  129. String prop_name = vformat("%s/%d/", PNAME("item"), E.key);
  130. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + PNAME("name")));
  131. p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("mesh"), PROPERTY_HINT_RESOURCE_TYPE, "Mesh"));
  132. p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
  133. p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("mesh_cast_shadow"), PROPERTY_HINT_ENUM, "Off,On,Double-Sided,Shadows Only"));
  134. p_list->push_back(PropertyInfo(Variant::ARRAY, prop_name + PNAME("shapes")));
  135. p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("navigation_mesh"), PROPERTY_HINT_RESOURCE_TYPE, "NavigationMesh"));
  136. p_list->push_back(PropertyInfo(Variant::TRANSFORM3D, prop_name + PNAME("navigation_mesh_transform"), PROPERTY_HINT_NONE, "suffix:m"));
  137. p_list->push_back(PropertyInfo(Variant::INT, prop_name + PNAME("navigation_layers"), PROPERTY_HINT_LAYERS_3D_NAVIGATION));
  138. p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + PNAME("preview"), PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT));
  139. }
  140. }
  141. void MeshLibrary::create_item(int p_item) {
  142. ERR_FAIL_COND(p_item < 0);
  143. ERR_FAIL_COND(item_map.has(p_item));
  144. item_map[p_item] = Item();
  145. emit_changed();
  146. notify_property_list_changed();
  147. }
  148. void MeshLibrary::set_item_name(int p_item, const String &p_name) {
  149. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  150. item_map[p_item].name = p_name;
  151. emit_changed();
  152. }
  153. void MeshLibrary::set_item_mesh(int p_item, const Ref<Mesh> &p_mesh) {
  154. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  155. item_map[p_item].mesh = p_mesh;
  156. emit_changed();
  157. }
  158. void MeshLibrary::set_item_mesh_transform(int p_item, const Transform3D &p_transform) {
  159. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  160. item_map[p_item].mesh_transform = p_transform;
  161. emit_changed();
  162. }
  163. void MeshLibrary::set_item_mesh_cast_shadow(int p_item, RS::ShadowCastingSetting p_shadow_casting_setting) {
  164. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  165. item_map[p_item].mesh_cast_shadow = p_shadow_casting_setting;
  166. emit_changed();
  167. }
  168. void MeshLibrary::set_item_shapes(int p_item, const Vector<ShapeData> &p_shapes) {
  169. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  170. item_map[p_item].shapes = p_shapes;
  171. emit_changed();
  172. notify_property_list_changed();
  173. }
  174. void MeshLibrary::set_item_navigation_mesh(int p_item, const Ref<NavigationMesh> &p_navigation_mesh) {
  175. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  176. item_map[p_item].navigation_mesh = p_navigation_mesh;
  177. emit_changed();
  178. }
  179. void MeshLibrary::set_item_navigation_mesh_transform(int p_item, const Transform3D &p_transform) {
  180. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  181. item_map[p_item].navigation_mesh_transform = p_transform;
  182. emit_changed();
  183. }
  184. void MeshLibrary::set_item_navigation_layers(int p_item, uint32_t p_navigation_layers) {
  185. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  186. item_map[p_item].navigation_layers = p_navigation_layers;
  187. emit_changed();
  188. }
  189. void MeshLibrary::set_item_preview(int p_item, const Ref<Texture2D> &p_preview) {
  190. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  191. item_map[p_item].preview = p_preview;
  192. emit_changed();
  193. }
  194. String MeshLibrary::get_item_name(int p_item) const {
  195. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), "", "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  196. return item_map[p_item].name;
  197. }
  198. Ref<Mesh> MeshLibrary::get_item_mesh(int p_item) const {
  199. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Mesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  200. return item_map[p_item].mesh;
  201. }
  202. Transform3D MeshLibrary::get_item_mesh_transform(int p_item) const {
  203. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  204. return item_map[p_item].mesh_transform;
  205. }
  206. RS::ShadowCastingSetting MeshLibrary::get_item_mesh_cast_shadow(int p_item) const {
  207. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), RS::ShadowCastingSetting::SHADOW_CASTING_SETTING_ON, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  208. return item_map[p_item].mesh_cast_shadow;
  209. }
  210. Vector<MeshLibrary::ShapeData> MeshLibrary::get_item_shapes(int p_item) const {
  211. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Vector<ShapeData>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  212. return item_map[p_item].shapes;
  213. }
  214. Ref<NavigationMesh> MeshLibrary::get_item_navigation_mesh(int p_item) const {
  215. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<NavigationMesh>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  216. return item_map[p_item].navigation_mesh;
  217. }
  218. Transform3D MeshLibrary::get_item_navigation_mesh_transform(int p_item) const {
  219. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Transform3D(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  220. return item_map[p_item].navigation_mesh_transform;
  221. }
  222. uint32_t MeshLibrary::get_item_navigation_layers(int p_item) const {
  223. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), 0, "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  224. return item_map[p_item].navigation_layers;
  225. }
  226. Ref<Texture2D> MeshLibrary::get_item_preview(int p_item) const {
  227. ERR_FAIL_COND_V_MSG(!item_map.has(p_item), Ref<Texture2D>(), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  228. return item_map[p_item].preview;
  229. }
  230. bool MeshLibrary::has_item(int p_item) const {
  231. return item_map.has(p_item);
  232. }
  233. void MeshLibrary::remove_item(int p_item) {
  234. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  235. item_map.erase(p_item);
  236. notify_property_list_changed();
  237. emit_changed();
  238. }
  239. void MeshLibrary::clear() {
  240. item_map.clear();
  241. notify_property_list_changed();
  242. emit_changed();
  243. }
  244. Vector<int> MeshLibrary::get_item_list() const {
  245. Vector<int> ret;
  246. ret.resize(item_map.size());
  247. int idx = 0;
  248. for (const KeyValue<int, Item> &E : item_map) {
  249. ret.write[idx++] = E.key;
  250. }
  251. return ret;
  252. }
  253. int MeshLibrary::find_item_by_name(const String &p_name) const {
  254. for (const KeyValue<int, Item> &E : item_map) {
  255. if (E.value.name == p_name) {
  256. return E.key;
  257. }
  258. }
  259. return -1;
  260. }
  261. int MeshLibrary::get_last_unused_item_id() const {
  262. if (!item_map.size()) {
  263. return 0;
  264. } else {
  265. return item_map.back()->key() + 1;
  266. }
  267. }
  268. void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
  269. Array arr_shapes = p_shapes;
  270. int size = p_shapes.size();
  271. if (size & 1) {
  272. ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
  273. int prev_size = item_map[p_item].shapes.size() * 2;
  274. if (prev_size < size) {
  275. // Check if last element is a shape.
  276. Ref<Shape3D> shape = arr_shapes[size - 1];
  277. if (shape.is_null()) {
  278. Ref<BoxShape3D> box_shape;
  279. box_shape.instantiate();
  280. arr_shapes[size - 1] = box_shape;
  281. }
  282. // Make sure the added element is a Transform3D.
  283. arr_shapes.push_back(Transform3D());
  284. size++;
  285. } else {
  286. size--;
  287. arr_shapes.resize(size);
  288. }
  289. }
  290. Vector<ShapeData> shapes;
  291. for (int i = 0; i < size; i += 2) {
  292. ShapeData sd;
  293. sd.shape = arr_shapes[i + 0];
  294. sd.local_transform = arr_shapes[i + 1];
  295. if (sd.shape.is_valid()) {
  296. shapes.push_back(sd);
  297. }
  298. }
  299. set_item_shapes(p_item, shapes);
  300. }
  301. Array MeshLibrary::_get_item_shapes(int p_item) const {
  302. Vector<ShapeData> shapes = get_item_shapes(p_item);
  303. Array ret;
  304. for (int i = 0; i < shapes.size(); i++) {
  305. ret.push_back(shapes[i].shape);
  306. ret.push_back(shapes[i].local_transform);
  307. }
  308. return ret;
  309. }
  310. void MeshLibrary::reset_state() {
  311. clear();
  312. }
  313. void MeshLibrary::_bind_methods() {
  314. ClassDB::bind_method(D_METHOD("create_item", "id"), &MeshLibrary::create_item);
  315. ClassDB::bind_method(D_METHOD("set_item_name", "id", "name"), &MeshLibrary::set_item_name);
  316. ClassDB::bind_method(D_METHOD("set_item_mesh", "id", "mesh"), &MeshLibrary::set_item_mesh);
  317. ClassDB::bind_method(D_METHOD("set_item_mesh_transform", "id", "mesh_transform"), &MeshLibrary::set_item_mesh_transform);
  318. ClassDB::bind_method(D_METHOD("set_item_mesh_cast_shadow", "id", "shadow_casting_setting"), &MeshLibrary::set_item_mesh_cast_shadow);
  319. ClassDB::bind_method(D_METHOD("set_item_navigation_mesh", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh);
  320. ClassDB::bind_method(D_METHOD("set_item_navigation_mesh_transform", "id", "navigation_mesh"), &MeshLibrary::set_item_navigation_mesh_transform);
  321. ClassDB::bind_method(D_METHOD("set_item_navigation_layers", "id", "navigation_layers"), &MeshLibrary::set_item_navigation_layers);
  322. ClassDB::bind_method(D_METHOD("set_item_shapes", "id", "shapes"), &MeshLibrary::_set_item_shapes);
  323. ClassDB::bind_method(D_METHOD("set_item_preview", "id", "texture"), &MeshLibrary::set_item_preview);
  324. ClassDB::bind_method(D_METHOD("get_item_name", "id"), &MeshLibrary::get_item_name);
  325. ClassDB::bind_method(D_METHOD("get_item_mesh", "id"), &MeshLibrary::get_item_mesh);
  326. ClassDB::bind_method(D_METHOD("get_item_mesh_transform", "id"), &MeshLibrary::get_item_mesh_transform);
  327. ClassDB::bind_method(D_METHOD("get_item_mesh_cast_shadow", "id"), &MeshLibrary::get_item_mesh_cast_shadow);
  328. ClassDB::bind_method(D_METHOD("get_item_navigation_mesh", "id"), &MeshLibrary::get_item_navigation_mesh);
  329. ClassDB::bind_method(D_METHOD("get_item_navigation_mesh_transform", "id"), &MeshLibrary::get_item_navigation_mesh_transform);
  330. ClassDB::bind_method(D_METHOD("get_item_navigation_layers", "id"), &MeshLibrary::get_item_navigation_layers);
  331. ClassDB::bind_method(D_METHOD("get_item_shapes", "id"), &MeshLibrary::_get_item_shapes);
  332. ClassDB::bind_method(D_METHOD("get_item_preview", "id"), &MeshLibrary::get_item_preview);
  333. ClassDB::bind_method(D_METHOD("remove_item", "id"), &MeshLibrary::remove_item);
  334. ClassDB::bind_method(D_METHOD("find_item_by_name", "name"), &MeshLibrary::find_item_by_name);
  335. ClassDB::bind_method(D_METHOD("clear"), &MeshLibrary::clear);
  336. ClassDB::bind_method(D_METHOD("get_item_list"), &MeshLibrary::get_item_list);
  337. ClassDB::bind_method(D_METHOD("get_last_unused_item_id"), &MeshLibrary::get_last_unused_item_id);
  338. }
  339. MeshLibrary::MeshLibrary() {
  340. }
  341. MeshLibrary::~MeshLibrary() {
  342. }