grid_map.cpp 49 KB

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