grid_map.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*************************************************************************/
  2. /* grid_map.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 "grid_map.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/object/message_queue.h"
  33. #include "scene/3d/light_3d.h"
  34. #include "scene/resources/mesh_library.h"
  35. #include "scene/resources/physics_material.h"
  36. #include "scene/resources/primitive_meshes.h"
  37. #include "scene/resources/surface_tool.h"
  38. #include "scene/scene_string_names.h"
  39. #include "servers/navigation_server_3d.h"
  40. #include "servers/rendering_server.h"
  41. bool GridMap::_set(const StringName &p_name, const Variant &p_value) {
  42. String name = p_name;
  43. if (name == "data") {
  44. Dictionary d = p_value;
  45. if (d.has("cells")) {
  46. Vector<int> cells = d["cells"];
  47. int amount = cells.size();
  48. const int *r = cells.ptr();
  49. ERR_FAIL_COND_V(amount % 3, false); // not even
  50. cell_map.clear();
  51. for (int i = 0; i < amount / 3; i++) {
  52. IndexKey ik;
  53. ik.key = decode_uint64((const uint8_t *)&r[i * 3]);
  54. Cell cell;
  55. cell.cell = decode_uint32((const uint8_t *)&r[i * 3 + 2]);
  56. cell_map[ik] = cell;
  57. }
  58. }
  59. _recreate_octant_data();
  60. } else if (name == "baked_meshes") {
  61. clear_baked_meshes();
  62. Array meshes = p_value;
  63. for (int i = 0; i < meshes.size(); i++) {
  64. BakedMesh bm;
  65. bm.mesh = meshes[i];
  66. ERR_CONTINUE(!bm.mesh.is_valid());
  67. bm.instance = RS::get_singleton()->instance_create();
  68. RS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  69. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  70. if (is_inside_tree()) {
  71. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  72. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  73. }
  74. baked_meshes.push_back(bm);
  75. }
  76. _recreate_octant_data();
  77. } else {
  78. return false;
  79. }
  80. return true;
  81. }
  82. bool GridMap::_get(const StringName &p_name, Variant &r_ret) const {
  83. String name = p_name;
  84. if (name == "data") {
  85. Dictionary d;
  86. Vector<int> cells;
  87. cells.resize(cell_map.size() * 3);
  88. {
  89. int *w = cells.ptrw();
  90. int i = 0;
  91. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  92. encode_uint64(E.key.key, (uint8_t *)&w[i * 3]);
  93. encode_uint32(E.value.cell, (uint8_t *)&w[i * 3 + 2]);
  94. i++;
  95. }
  96. }
  97. d["cells"] = cells;
  98. r_ret = d;
  99. } else if (name == "baked_meshes") {
  100. Array ret;
  101. ret.resize(baked_meshes.size());
  102. for (int i = 0; i < baked_meshes.size(); i++) {
  103. ret[i] = baked_meshes[i].mesh;
  104. }
  105. r_ret = ret;
  106. } else {
  107. return false;
  108. }
  109. return true;
  110. }
  111. void GridMap::_get_property_list(List<PropertyInfo> *p_list) const {
  112. if (baked_meshes.size()) {
  113. p_list->push_back(PropertyInfo(Variant::ARRAY, "baked_meshes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  114. }
  115. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
  116. }
  117. void GridMap::set_collision_layer(uint32_t p_layer) {
  118. collision_layer = p_layer;
  119. _reset_physic_bodies_collision_filters();
  120. }
  121. uint32_t GridMap::get_collision_layer() const {
  122. return collision_layer;
  123. }
  124. void GridMap::set_collision_mask(uint32_t p_mask) {
  125. collision_mask = p_mask;
  126. _reset_physic_bodies_collision_filters();
  127. }
  128. uint32_t GridMap::get_collision_mask() const {
  129. return collision_mask;
  130. }
  131. void GridMap::set_collision_layer_value(int p_layer_number, bool p_value) {
  132. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  133. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  134. uint32_t collision_layer = get_collision_layer();
  135. if (p_value) {
  136. collision_layer |= 1 << (p_layer_number - 1);
  137. } else {
  138. collision_layer &= ~(1 << (p_layer_number - 1));
  139. }
  140. set_collision_layer(collision_layer);
  141. }
  142. bool GridMap::get_collision_layer_value(int p_layer_number) const {
  143. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  144. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  145. return get_collision_layer() & (1 << (p_layer_number - 1));
  146. }
  147. void GridMap::set_collision_mask_value(int p_layer_number, bool p_value) {
  148. ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
  149. ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
  150. uint32_t mask = get_collision_mask();
  151. if (p_value) {
  152. mask |= 1 << (p_layer_number - 1);
  153. } else {
  154. mask &= ~(1 << (p_layer_number - 1));
  155. }
  156. set_collision_mask(mask);
  157. }
  158. void GridMap::set_physics_material(Ref<PhysicsMaterial> p_material) {
  159. physics_material = p_material;
  160. _recreate_octant_data();
  161. }
  162. Ref<PhysicsMaterial> GridMap::get_physics_material() const {
  163. return physics_material;
  164. }
  165. bool GridMap::get_collision_mask_value(int p_layer_number) const {
  166. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
  167. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
  168. return get_collision_mask() & (1 << (p_layer_number - 1));
  169. }
  170. Array GridMap::get_collision_shapes() const {
  171. Array shapes;
  172. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  173. Octant *g = E.value;
  174. RID body = g->static_body;
  175. Transform3D body_xform = PhysicsServer3D::get_singleton()->body_get_state(body, PhysicsServer3D::BODY_STATE_TRANSFORM);
  176. int nshapes = PhysicsServer3D::get_singleton()->body_get_shape_count(body);
  177. for (int i = 0; i < nshapes; i++) {
  178. RID shape = PhysicsServer3D::get_singleton()->body_get_shape(body, i);
  179. Transform3D xform = PhysicsServer3D::get_singleton()->body_get_shape_transform(body, i);
  180. shapes.push_back(body_xform * xform);
  181. shapes.push_back(shape);
  182. }
  183. }
  184. return shapes;
  185. }
  186. void GridMap::set_bake_navigation(bool p_bake_navigation) {
  187. bake_navigation = p_bake_navigation;
  188. _recreate_octant_data();
  189. }
  190. bool GridMap::is_baking_navigation() {
  191. return bake_navigation;
  192. }
  193. void GridMap::set_navigation_layers(uint32_t p_navigation_layers) {
  194. navigation_layers = p_navigation_layers;
  195. _recreate_octant_data();
  196. }
  197. uint32_t GridMap::get_navigation_layers() const {
  198. return navigation_layers;
  199. }
  200. void GridMap::set_navigation_layer_value(int p_layer_number, bool p_value) {
  201. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  202. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  203. uint32_t _navigation_layers = get_navigation_layers();
  204. if (p_value) {
  205. _navigation_layers |= 1 << (p_layer_number - 1);
  206. } else {
  207. _navigation_layers &= ~(1 << (p_layer_number - 1));
  208. }
  209. set_navigation_layers(_navigation_layers);
  210. }
  211. bool GridMap::get_navigation_layer_value(int p_layer_number) const {
  212. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  213. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  214. return get_navigation_layers() & (1 << (p_layer_number - 1));
  215. }
  216. void GridMap::set_mesh_library(const Ref<MeshLibrary> &p_mesh_library) {
  217. if (!mesh_library.is_null()) {
  218. mesh_library->unregister_owner(this);
  219. }
  220. mesh_library = p_mesh_library;
  221. if (!mesh_library.is_null()) {
  222. mesh_library->register_owner(this);
  223. }
  224. _recreate_octant_data();
  225. }
  226. Ref<MeshLibrary> GridMap::get_mesh_library() const {
  227. return mesh_library;
  228. }
  229. void GridMap::set_cell_size(const Vector3 &p_size) {
  230. ERR_FAIL_COND(p_size.x < 0.001 || p_size.y < 0.001 || p_size.z < 0.001);
  231. cell_size = p_size;
  232. _recreate_octant_data();
  233. emit_signal(SNAME("cell_size_changed"), cell_size);
  234. }
  235. Vector3 GridMap::get_cell_size() const {
  236. return cell_size;
  237. }
  238. void GridMap::set_octant_size(int p_size) {
  239. ERR_FAIL_COND(p_size == 0);
  240. octant_size = p_size;
  241. _recreate_octant_data();
  242. }
  243. int GridMap::get_octant_size() const {
  244. return octant_size;
  245. }
  246. void GridMap::set_center_x(bool p_enable) {
  247. center_x = p_enable;
  248. _recreate_octant_data();
  249. }
  250. bool GridMap::get_center_x() const {
  251. return center_x;
  252. }
  253. void GridMap::set_center_y(bool p_enable) {
  254. center_y = p_enable;
  255. _recreate_octant_data();
  256. }
  257. bool GridMap::get_center_y() const {
  258. return center_y;
  259. }
  260. void GridMap::set_center_z(bool p_enable) {
  261. center_z = p_enable;
  262. _recreate_octant_data();
  263. }
  264. bool GridMap::get_center_z() const {
  265. return center_z;
  266. }
  267. void GridMap::set_cell_item(const Vector3i &p_position, int p_item, int p_rot) {
  268. if (baked_meshes.size() && !recreating_octants) {
  269. //if you set a cell item, baked meshes go good bye
  270. clear_baked_meshes();
  271. _recreate_octant_data();
  272. }
  273. ERR_FAIL_INDEX(ABS(p_position.x), 1 << 20);
  274. ERR_FAIL_INDEX(ABS(p_position.y), 1 << 20);
  275. ERR_FAIL_INDEX(ABS(p_position.z), 1 << 20);
  276. IndexKey key;
  277. key.x = p_position.x;
  278. key.y = p_position.y;
  279. key.z = p_position.z;
  280. OctantKey ok;
  281. ok.x = p_position.x / octant_size;
  282. ok.y = p_position.y / octant_size;
  283. ok.z = p_position.z / octant_size;
  284. if (p_item < 0) {
  285. //erase
  286. if (cell_map.has(key)) {
  287. OctantKey octantkey = ok;
  288. ERR_FAIL_COND(!octant_map.has(octantkey));
  289. Octant &g = *octant_map[octantkey];
  290. g.cells.erase(key);
  291. g.dirty = true;
  292. cell_map.erase(key);
  293. _queue_octants_dirty();
  294. }
  295. return;
  296. }
  297. OctantKey octantkey = ok;
  298. if (!octant_map.has(octantkey)) {
  299. //create octant because it does not exist
  300. Octant *g = memnew(Octant);
  301. g->dirty = true;
  302. g->static_body = PhysicsServer3D::get_singleton()->body_create();
  303. PhysicsServer3D::get_singleton()->body_set_mode(g->static_body, PhysicsServer3D::BODY_MODE_STATIC);
  304. PhysicsServer3D::get_singleton()->body_attach_object_instance_id(g->static_body, get_instance_id());
  305. PhysicsServer3D::get_singleton()->body_set_collision_layer(g->static_body, collision_layer);
  306. PhysicsServer3D::get_singleton()->body_set_collision_mask(g->static_body, collision_mask);
  307. if (physics_material.is_valid()) {
  308. PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_FRICTION, physics_material->get_friction());
  309. PhysicsServer3D::get_singleton()->body_set_param(g->static_body, PhysicsServer3D::BODY_PARAM_BOUNCE, physics_material->get_bounce());
  310. }
  311. SceneTree *st = SceneTree::get_singleton();
  312. if (st && st->is_debugging_collisions_hint()) {
  313. g->collision_debug = RenderingServer::get_singleton()->mesh_create();
  314. g->collision_debug_instance = RenderingServer::get_singleton()->instance_create();
  315. RenderingServer::get_singleton()->instance_set_base(g->collision_debug_instance, g->collision_debug);
  316. }
  317. octant_map[octantkey] = g;
  318. if (is_inside_world()) {
  319. _octant_enter_world(octantkey);
  320. _octant_transform(octantkey);
  321. }
  322. }
  323. Octant &g = *octant_map[octantkey];
  324. g.cells.insert(key);
  325. g.dirty = true;
  326. _queue_octants_dirty();
  327. Cell c;
  328. c.item = p_item;
  329. c.rot = p_rot;
  330. cell_map[key] = c;
  331. }
  332. int GridMap::get_cell_item(const Vector3i &p_position) const {
  333. ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, INVALID_CELL_ITEM);
  334. ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, INVALID_CELL_ITEM);
  335. ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, INVALID_CELL_ITEM);
  336. IndexKey key;
  337. key.x = p_position.x;
  338. key.y = p_position.y;
  339. key.z = p_position.z;
  340. if (!cell_map.has(key)) {
  341. return INVALID_CELL_ITEM;
  342. }
  343. return cell_map[key].item;
  344. }
  345. int GridMap::get_cell_item_orientation(const Vector3i &p_position) const {
  346. ERR_FAIL_INDEX_V(ABS(p_position.x), 1 << 20, -1);
  347. ERR_FAIL_INDEX_V(ABS(p_position.y), 1 << 20, -1);
  348. ERR_FAIL_INDEX_V(ABS(p_position.z), 1 << 20, -1);
  349. IndexKey key;
  350. key.x = p_position.x;
  351. key.y = p_position.y;
  352. key.z = p_position.z;
  353. if (!cell_map.has(key)) {
  354. return -1;
  355. }
  356. return cell_map[key].rot;
  357. }
  358. static const Basis _ortho_bases[24] = {
  359. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  360. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  361. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  362. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  363. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  364. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  365. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  366. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  367. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  368. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  369. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  370. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  371. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  372. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  373. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  374. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  375. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  376. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  377. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  378. Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
  379. Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
  380. Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
  381. Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
  382. Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
  383. };
  384. Basis GridMap::get_cell_item_basis(const Vector3i &p_position) const {
  385. int orientation = get_cell_item_orientation(p_position);
  386. if (orientation == -1) {
  387. return Basis();
  388. }
  389. return get_basis_with_orthogonal_index(orientation);
  390. }
  391. Basis GridMap::get_basis_with_orthogonal_index(int p_index) const {
  392. ERR_FAIL_INDEX_V(p_index, 24, Basis());
  393. return _ortho_bases[p_index];
  394. }
  395. int GridMap::get_orthogonal_index_from_basis(const Basis &p_basis) const {
  396. Basis orth = p_basis;
  397. for (int i = 0; i < 3; i++) {
  398. for (int j = 0; j < 3; j++) {
  399. real_t v = orth[i][j];
  400. if (v > 0.5) {
  401. v = 1.0;
  402. } else if (v < -0.5) {
  403. v = -1.0;
  404. } else {
  405. v = 0;
  406. }
  407. orth[i][j] = v;
  408. }
  409. }
  410. for (int i = 0; i < 24; i++) {
  411. if (_ortho_bases[i] == orth) {
  412. return i;
  413. }
  414. }
  415. return 0;
  416. }
  417. Vector3i GridMap::world_to_map(const Vector3 &p_world_position) const {
  418. Vector3 map_position = (p_world_position / cell_size).floor();
  419. return Vector3i(map_position);
  420. }
  421. Vector3 GridMap::map_to_world(const Vector3i &p_map_position) const {
  422. Vector3 offset = _get_offset();
  423. Vector3 world_pos(
  424. p_map_position.x * cell_size.x + offset.x,
  425. p_map_position.y * cell_size.y + offset.y,
  426. p_map_position.z * cell_size.z + offset.z);
  427. return world_pos;
  428. }
  429. void GridMap::_octant_transform(const OctantKey &p_key) {
  430. ERR_FAIL_COND(!octant_map.has(p_key));
  431. Octant &g = *octant_map[p_key];
  432. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  433. if (g.collision_debug_instance.is_valid()) {
  434. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  435. }
  436. // update transform for NavigationServer regions and navigation debugmesh instances
  437. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  438. if (bake_navigation) {
  439. if (E.value.region.is_valid()) {
  440. NavigationServer3D::get_singleton()->region_set_transform(E.value.region, get_global_transform() * E.value.xform);
  441. }
  442. if (E.value.navmesh_debug_instance.is_valid()) {
  443. RS::get_singleton()->instance_set_transform(E.value.navmesh_debug_instance, get_global_transform() * E.value.xform);
  444. }
  445. }
  446. }
  447. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  448. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  449. }
  450. }
  451. bool GridMap::_octant_update(const OctantKey &p_key) {
  452. ERR_FAIL_COND_V(!octant_map.has(p_key), false);
  453. Octant &g = *octant_map[p_key];
  454. if (!g.dirty) {
  455. return false;
  456. }
  457. //erase body shapes
  458. PhysicsServer3D::get_singleton()->body_clear_shapes(g.static_body);
  459. //erase body shapes debug
  460. if (g.collision_debug.is_valid()) {
  461. RS::get_singleton()->mesh_clear(g.collision_debug);
  462. }
  463. //erase navigation
  464. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  465. NavigationServer3D::get_singleton()->free(E.value.region);
  466. if (E.value.navmesh_debug_instance.is_valid()) {
  467. RS::get_singleton()->free(E.value.navmesh_debug_instance);
  468. }
  469. }
  470. g.navmesh_ids.clear();
  471. //erase multimeshes
  472. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  473. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  474. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  475. }
  476. g.multimesh_instances.clear();
  477. if (g.cells.size() == 0) {
  478. //octant no longer needed
  479. _octant_clean_up(p_key);
  480. return true;
  481. }
  482. Vector<Vector3> col_debug;
  483. /*
  484. * foreach item in this octant,
  485. * set item's multimesh's instance count to number of cells which have this item
  486. * and set said multimesh bounding box to one containing all cells which have this item
  487. */
  488. HashMap<int, List<Pair<Transform3D, IndexKey>>> multimesh_items;
  489. for (const IndexKey &E : g.cells) {
  490. ERR_CONTINUE(!cell_map.has(E));
  491. const Cell &c = cell_map[E];
  492. if (!mesh_library.is_valid() || !mesh_library->has_item(c.item)) {
  493. continue;
  494. }
  495. Vector3 cellpos = Vector3(E.x, E.y, E.z);
  496. Vector3 ofs = _get_offset();
  497. Transform3D xform;
  498. xform.basis = _ortho_bases[c.rot];
  499. xform.set_origin(cellpos * cell_size + ofs);
  500. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  501. if (baked_meshes.size() == 0) {
  502. if (mesh_library->get_item_mesh(c.item).is_valid()) {
  503. if (!multimesh_items.has(c.item)) {
  504. multimesh_items[c.item] = List<Pair<Transform3D, IndexKey>>();
  505. }
  506. Pair<Transform3D, IndexKey> p;
  507. p.first = xform * mesh_library->get_item_mesh_transform(c.item);
  508. p.second = E;
  509. multimesh_items[c.item].push_back(p);
  510. }
  511. }
  512. Vector<MeshLibrary::ShapeData> shapes = mesh_library->get_item_shapes(c.item);
  513. // add the item's shape at given xform to octant's static_body
  514. for (int i = 0; i < shapes.size(); i++) {
  515. // add the item's shape
  516. if (!shapes[i].shape.is_valid()) {
  517. continue;
  518. }
  519. PhysicsServer3D::get_singleton()->body_add_shape(g.static_body, shapes[i].shape->get_rid(), xform * shapes[i].local_transform);
  520. if (g.collision_debug.is_valid()) {
  521. shapes.write[i].shape->add_vertices_to_array(col_debug, xform * shapes[i].local_transform);
  522. }
  523. }
  524. // add the item's navmesh at given xform to GridMap's Navigation ancestor
  525. Ref<NavigationMesh> navmesh = mesh_library->get_item_navmesh(c.item);
  526. if (navmesh.is_valid()) {
  527. Octant::NavMesh nm;
  528. nm.xform = xform * mesh_library->get_item_navmesh_transform(c.item);
  529. if (bake_navigation) {
  530. RID region = NavigationServer3D::get_singleton()->region_create();
  531. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  532. NavigationServer3D::get_singleton()->region_set_navmesh(region, navmesh);
  533. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform);
  534. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  535. nm.region = region;
  536. // add navigation debugmesh visual instances if debug is enabled
  537. SceneTree *st = SceneTree::get_singleton();
  538. if (st && st->is_debugging_navigation_hint()) {
  539. if (!nm.navmesh_debug_instance.is_valid()) {
  540. RID navmesh_debug_rid = navmesh->get_debug_mesh()->get_rid();
  541. nm.navmesh_debug_instance = RS::get_singleton()->instance_create();
  542. RS::get_singleton()->instance_set_base(nm.navmesh_debug_instance, navmesh_debug_rid);
  543. RS::get_singleton()->mesh_surface_set_material(navmesh_debug_rid, 0, st->get_debug_navigation_material()->get_rid());
  544. }
  545. if (is_inside_tree()) {
  546. RS::get_singleton()->instance_set_scenario(nm.navmesh_debug_instance, get_world_3d()->get_scenario());
  547. RS::get_singleton()->instance_set_transform(nm.navmesh_debug_instance, get_global_transform() * nm.xform);
  548. }
  549. }
  550. }
  551. g.navmesh_ids[E] = nm;
  552. }
  553. }
  554. //update multimeshes, only if not baked
  555. if (baked_meshes.size() == 0) {
  556. for (const KeyValue<int, List<Pair<Transform3D, IndexKey>>> &E : multimesh_items) {
  557. Octant::MultimeshInstance mmi;
  558. RID mm = RS::get_singleton()->multimesh_create();
  559. RS::get_singleton()->multimesh_allocate_data(mm, E.value.size(), RS::MULTIMESH_TRANSFORM_3D);
  560. RS::get_singleton()->multimesh_set_mesh(mm, mesh_library->get_item_mesh(E.key)->get_rid());
  561. int idx = 0;
  562. for (const Pair<Transform3D, IndexKey> &F : E.value) {
  563. RS::get_singleton()->multimesh_instance_set_transform(mm, idx, F.first);
  564. #ifdef TOOLS_ENABLED
  565. Octant::MultimeshInstance::Item it;
  566. it.index = idx;
  567. it.transform = F.first;
  568. it.key = F.second;
  569. mmi.items.push_back(it);
  570. #endif
  571. idx++;
  572. }
  573. RID instance = RS::get_singleton()->instance_create();
  574. RS::get_singleton()->instance_set_base(instance, mm);
  575. if (is_inside_tree()) {
  576. RS::get_singleton()->instance_set_scenario(instance, get_world_3d()->get_scenario());
  577. RS::get_singleton()->instance_set_transform(instance, get_global_transform());
  578. }
  579. mmi.multimesh = mm;
  580. mmi.instance = instance;
  581. g.multimesh_instances.push_back(mmi);
  582. }
  583. }
  584. if (col_debug.size()) {
  585. Array arr;
  586. arr.resize(RS::ARRAY_MAX);
  587. arr[RS::ARRAY_VERTEX] = col_debug;
  588. RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr);
  589. SceneTree *st = SceneTree::get_singleton();
  590. if (st) {
  591. RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  592. }
  593. }
  594. g.dirty = false;
  595. return false;
  596. }
  597. void GridMap::_reset_physic_bodies_collision_filters() {
  598. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  599. PhysicsServer3D::get_singleton()->body_set_collision_layer(E.value->static_body, collision_layer);
  600. PhysicsServer3D::get_singleton()->body_set_collision_mask(E.value->static_body, collision_mask);
  601. }
  602. }
  603. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  604. ERR_FAIL_COND(!octant_map.has(p_key));
  605. Octant &g = *octant_map[p_key];
  606. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  607. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, get_world_3d()->get_space());
  608. if (g.collision_debug_instance.is_valid()) {
  609. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world_3d()->get_scenario());
  610. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  611. }
  612. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  613. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world_3d()->get_scenario());
  614. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  615. }
  616. if (bake_navigation && mesh_library.is_valid()) {
  617. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  618. if (cell_map.has(F.key) && F.value.region.is_valid() == false) {
  619. Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[F.key].item);
  620. if (nm.is_valid()) {
  621. RID region = NavigationServer3D::get_singleton()->region_create();
  622. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, navigation_layers);
  623. NavigationServer3D::get_singleton()->region_set_navmesh(region, nm);
  624. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform);
  625. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  626. F.value.region = region;
  627. }
  628. }
  629. }
  630. }
  631. }
  632. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  633. ERR_FAIL_COND(!octant_map.has(p_key));
  634. Octant &g = *octant_map[p_key];
  635. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  636. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());
  637. if (g.collision_debug_instance.is_valid()) {
  638. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  639. }
  640. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  641. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  642. }
  643. for (KeyValue<IndexKey, Octant::NavMesh> &F : g.navmesh_ids) {
  644. if (F.value.region.is_valid()) {
  645. NavigationServer3D::get_singleton()->free(F.value.region);
  646. F.value.region = RID();
  647. }
  648. if (F.value.navmesh_debug_instance.is_valid()) {
  649. RS::get_singleton()->free(F.value.navmesh_debug_instance);
  650. F.value.navmesh_debug_instance = RID();
  651. }
  652. }
  653. }
  654. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  655. ERR_FAIL_COND(!octant_map.has(p_key));
  656. Octant &g = *octant_map[p_key];
  657. if (g.collision_debug.is_valid()) {
  658. RS::get_singleton()->free(g.collision_debug);
  659. }
  660. if (g.collision_debug_instance.is_valid()) {
  661. RS::get_singleton()->free(g.collision_debug_instance);
  662. }
  663. PhysicsServer3D::get_singleton()->free(g.static_body);
  664. // Erase navigation
  665. for (const KeyValue<IndexKey, Octant::NavMesh> &E : g.navmesh_ids) {
  666. if (E.value.region.is_valid()) {
  667. NavigationServer3D::get_singleton()->free(E.value.region);
  668. }
  669. if (E.value.navmesh_debug_instance.is_valid()) {
  670. RS::get_singleton()->free(E.value.navmesh_debug_instance);
  671. }
  672. }
  673. g.navmesh_ids.clear();
  674. //erase multimeshes
  675. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  676. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  677. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  678. }
  679. g.multimesh_instances.clear();
  680. }
  681. void GridMap::_notification(int p_what) {
  682. switch (p_what) {
  683. case NOTIFICATION_ENTER_WORLD: {
  684. last_transform = get_global_transform();
  685. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  686. _octant_enter_world(E.key);
  687. }
  688. for (int i = 0; i < baked_meshes.size(); i++) {
  689. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world_3d()->get_scenario());
  690. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  691. }
  692. } break;
  693. case NOTIFICATION_TRANSFORM_CHANGED: {
  694. Transform3D new_xform = get_global_transform();
  695. if (new_xform == last_transform) {
  696. break;
  697. }
  698. //update run
  699. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  700. _octant_transform(E.key);
  701. }
  702. last_transform = new_xform;
  703. for (int i = 0; i < baked_meshes.size(); i++) {
  704. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  705. }
  706. } break;
  707. case NOTIFICATION_EXIT_WORLD: {
  708. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  709. _octant_exit_world(E.key);
  710. }
  711. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  712. //_update_octants_callback();
  713. //_update_area_instances();
  714. for (int i = 0; i < baked_meshes.size(); i++) {
  715. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  716. }
  717. } break;
  718. case NOTIFICATION_VISIBILITY_CHANGED: {
  719. _update_visibility();
  720. } break;
  721. }
  722. }
  723. void GridMap::_update_visibility() {
  724. if (!is_inside_tree()) {
  725. return;
  726. }
  727. for (KeyValue<OctantKey, Octant *> &e : octant_map) {
  728. Octant *octant = e.value;
  729. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  730. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  731. RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  732. }
  733. }
  734. for (int i = 0; i < baked_meshes.size(); i++) {
  735. RS::get_singleton()->instance_set_visible(baked_meshes[i].instance, is_visible_in_tree());
  736. }
  737. }
  738. void GridMap::_queue_octants_dirty() {
  739. if (awaiting_update) {
  740. return;
  741. }
  742. MessageQueue::get_singleton()->push_call(this, "_update_octants_callback");
  743. awaiting_update = true;
  744. }
  745. void GridMap::_recreate_octant_data() {
  746. recreating_octants = true;
  747. HashMap<IndexKey, Cell, IndexKey> cell_copy = cell_map;
  748. _clear_internal();
  749. for (const KeyValue<IndexKey, Cell> &E : cell_copy) {
  750. set_cell_item(Vector3i(E.key), E.value.item, E.value.rot);
  751. }
  752. recreating_octants = false;
  753. }
  754. void GridMap::_clear_internal() {
  755. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  756. if (is_inside_world()) {
  757. _octant_exit_world(E.key);
  758. }
  759. _octant_clean_up(E.key);
  760. memdelete(E.value);
  761. }
  762. octant_map.clear();
  763. cell_map.clear();
  764. }
  765. void GridMap::clear() {
  766. _clear_internal();
  767. clear_baked_meshes();
  768. }
  769. void GridMap::resource_changed(const Ref<Resource> &p_res) {
  770. _recreate_octant_data();
  771. }
  772. void GridMap::_update_octants_callback() {
  773. if (!awaiting_update) {
  774. return;
  775. }
  776. List<OctantKey> to_delete;
  777. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  778. if (_octant_update(E.key)) {
  779. to_delete.push_back(E.key);
  780. }
  781. }
  782. while (to_delete.front()) {
  783. memdelete(octant_map[to_delete.front()->get()]);
  784. octant_map.erase(to_delete.front()->get());
  785. to_delete.pop_front();
  786. }
  787. _update_visibility();
  788. awaiting_update = false;
  789. }
  790. void GridMap::_bind_methods() {
  791. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  792. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  793. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  794. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  795. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &GridMap::set_collision_mask_value);
  796. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &GridMap::get_collision_mask_value);
  797. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &GridMap::set_collision_layer_value);
  798. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &GridMap::get_collision_layer_value);
  799. ClassDB::bind_method(D_METHOD("set_physics_material", "material"), &GridMap::set_physics_material);
  800. ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material);
  801. ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation);
  802. ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation);
  803. ClassDB::bind_method(D_METHOD("set_navigation_layers", "layers"), &GridMap::set_navigation_layers);
  804. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &GridMap::get_navigation_layers);
  805. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &GridMap::set_navigation_layer_value);
  806. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &GridMap::get_navigation_layer_value);
  807. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  808. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  809. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  810. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  811. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  812. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  813. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  814. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  815. ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  816. ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
  817. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
  818. ClassDB::bind_method(D_METHOD("get_cell_item_basis", "position"), &GridMap::get_cell_item_basis);
  819. ClassDB::bind_method(D_METHOD("get_basis_with_orthogonal_index", "index"), &GridMap::get_basis_with_orthogonal_index);
  820. ClassDB::bind_method(D_METHOD("get_orthogonal_index_from_basis", "basis"), &GridMap::get_orthogonal_index_from_basis);
  821. ClassDB::bind_method(D_METHOD("world_to_map", "world_position"), &GridMap::world_to_map);
  822. ClassDB::bind_method(D_METHOD("map_to_world", "map_position"), &GridMap::map_to_world);
  823. ClassDB::bind_method(D_METHOD("_update_octants_callback"), &GridMap::_update_octants_callback);
  824. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  825. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  826. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  827. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  828. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  829. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  830. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  831. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  832. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  833. ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item);
  834. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  835. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  836. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  837. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  838. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  839. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  840. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material");
  841. ADD_GROUP("Cell", "cell_");
  842. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size", PROPERTY_HINT_NONE, "suffix:m"), "set_cell_size", "get_cell_size");
  843. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  844. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  845. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  846. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  847. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale");
  848. ADD_GROUP("Collision", "collision_");
  849. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  850. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  851. ADD_GROUP("Navigation", "");
  852. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation");
  853. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  854. BIND_CONSTANT(INVALID_CELL_ITEM);
  855. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  856. }
  857. void GridMap::set_cell_scale(float p_scale) {
  858. cell_scale = p_scale;
  859. _recreate_octant_data();
  860. }
  861. float GridMap::get_cell_scale() const {
  862. return cell_scale;
  863. }
  864. TypedArray<Vector3i> GridMap::get_used_cells() const {
  865. TypedArray<Vector3i> a;
  866. a.resize(cell_map.size());
  867. int i = 0;
  868. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  869. Vector3i p(E.key.x, E.key.y, E.key.z);
  870. a[i++] = p;
  871. }
  872. return a;
  873. }
  874. TypedArray<Vector3i> GridMap::get_used_cells_by_item(int p_item) const {
  875. TypedArray<Vector3i> a;
  876. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  877. if (E.value.item == p_item) {
  878. Vector3i p(E.key.x, E.key.y, E.key.z);
  879. a.push_back(p);
  880. }
  881. }
  882. return a;
  883. }
  884. Array GridMap::get_meshes() const {
  885. if (mesh_library.is_null()) {
  886. return Array();
  887. }
  888. Vector3 ofs = _get_offset();
  889. Array meshes;
  890. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  891. int id = E.value.item;
  892. if (!mesh_library->has_item(id)) {
  893. continue;
  894. }
  895. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  896. if (mesh.is_null()) {
  897. continue;
  898. }
  899. IndexKey ik = E.key;
  900. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  901. Transform3D xform;
  902. xform.basis = _ortho_bases[E.value.rot];
  903. xform.set_origin(cellpos * cell_size + ofs);
  904. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  905. meshes.push_back(xform * mesh_library->get_item_mesh_transform(id));
  906. meshes.push_back(mesh);
  907. }
  908. return meshes;
  909. }
  910. Vector3 GridMap::_get_offset() const {
  911. return Vector3(
  912. cell_size.x * 0.5 * int(center_x),
  913. cell_size.y * 0.5 * int(center_y),
  914. cell_size.z * 0.5 * int(center_z));
  915. }
  916. void GridMap::clear_baked_meshes() {
  917. for (int i = 0; i < baked_meshes.size(); i++) {
  918. RS::get_singleton()->free(baked_meshes[i].instance);
  919. }
  920. baked_meshes.clear();
  921. _recreate_octant_data();
  922. }
  923. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  924. if (!mesh_library.is_valid()) {
  925. return;
  926. }
  927. //generate
  928. HashMap<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>, OctantKey> surface_map;
  929. for (KeyValue<IndexKey, Cell> &E : cell_map) {
  930. IndexKey key = E.key;
  931. int item = E.value.item;
  932. if (!mesh_library->has_item(item)) {
  933. continue;
  934. }
  935. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  936. if (!mesh.is_valid()) {
  937. continue;
  938. }
  939. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  940. Vector3 ofs = _get_offset();
  941. Transform3D xform;
  942. xform.basis = _ortho_bases[E.value.rot];
  943. xform.set_origin(cellpos * cell_size + ofs);
  944. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  945. OctantKey ok;
  946. ok.x = key.x / octant_size;
  947. ok.y = key.y / octant_size;
  948. ok.z = key.z / octant_size;
  949. if (!surface_map.has(ok)) {
  950. surface_map[ok] = HashMap<Ref<Material>, Ref<SurfaceTool>>();
  951. }
  952. HashMap<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  953. for (int i = 0; i < mesh->get_surface_count(); i++) {
  954. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  955. continue;
  956. }
  957. Ref<Material> surf_mat = mesh->surface_get_material(i);
  958. if (!mat_map.has(surf_mat)) {
  959. Ref<SurfaceTool> st;
  960. st.instantiate();
  961. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  962. st->set_material(surf_mat);
  963. mat_map[surf_mat] = st;
  964. }
  965. mat_map[surf_mat]->append_from(mesh, i, xform);
  966. }
  967. }
  968. for (KeyValue<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) {
  969. Ref<ArrayMesh> mesh;
  970. mesh.instantiate();
  971. for (KeyValue<Ref<Material>, Ref<SurfaceTool>> &F : E.value) {
  972. F.value->commit(mesh);
  973. }
  974. BakedMesh bm;
  975. bm.mesh = mesh;
  976. bm.instance = RS::get_singleton()->instance_create();
  977. RS::get_singleton()->get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  978. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  979. if (is_inside_tree()) {
  980. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  981. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  982. }
  983. if (p_gen_lightmap_uv) {
  984. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  985. }
  986. baked_meshes.push_back(bm);
  987. }
  988. _recreate_octant_data();
  989. }
  990. Array GridMap::get_bake_meshes() {
  991. if (!baked_meshes.size()) {
  992. make_baked_meshes(true);
  993. }
  994. Array arr;
  995. for (int i = 0; i < baked_meshes.size(); i++) {
  996. arr.push_back(baked_meshes[i].mesh);
  997. arr.push_back(Transform3D());
  998. }
  999. return arr;
  1000. }
  1001. RID GridMap::get_bake_mesh_instance(int p_idx) {
  1002. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  1003. return baked_meshes[p_idx].instance;
  1004. }
  1005. GridMap::GridMap() {
  1006. set_notify_transform(true);
  1007. }
  1008. GridMap::~GridMap() {
  1009. if (!mesh_library.is_null()) {
  1010. mesh_library->unregister_owner(this);
  1011. }
  1012. clear();
  1013. }