grid_map.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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. RS::ShadowCastingSetting cast_shadows = (RS::ShadowCastingSetting)mesh_library->get_item_mesh_cast_shadow(E.key);
  601. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(instance, cast_shadows);
  602. mmi.multimesh = mm;
  603. mmi.instance = instance;
  604. g.multimesh_instances.push_back(mmi);
  605. }
  606. }
  607. if (col_debug.size()) {
  608. Array arr;
  609. arr.resize(RS::ARRAY_MAX);
  610. arr[RS::ARRAY_VERTEX] = col_debug;
  611. RS::get_singleton()->mesh_add_surface_from_arrays(g.collision_debug, RS::PRIMITIVE_LINES, arr);
  612. SceneTree *st = SceneTree::get_singleton();
  613. if (st) {
  614. RS::get_singleton()->mesh_surface_set_material(g.collision_debug, 0, st->get_debug_collision_material()->get_rid());
  615. }
  616. }
  617. g.dirty = false;
  618. return false;
  619. }
  620. void GridMap::_update_physics_bodies_collision_properties() {
  621. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  622. PhysicsServer3D::get_singleton()->body_set_collision_layer(E.value->static_body, collision_layer);
  623. PhysicsServer3D::get_singleton()->body_set_collision_mask(E.value->static_body, collision_mask);
  624. PhysicsServer3D::get_singleton()->body_set_collision_priority(E.value->static_body, collision_priority);
  625. }
  626. }
  627. void GridMap::_update_physics_bodies_characteristics() {
  628. real_t friction = 1.0;
  629. real_t bounce = 0.0;
  630. if (physics_material.is_valid()) {
  631. friction = physics_material->computed_friction();
  632. bounce = physics_material->computed_bounce();
  633. }
  634. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  635. PhysicsServer3D::get_singleton()->body_set_param(E.value->static_body, PhysicsServer3D::BODY_PARAM_FRICTION, friction);
  636. PhysicsServer3D::get_singleton()->body_set_param(E.value->static_body, PhysicsServer3D::BODY_PARAM_BOUNCE, bounce);
  637. }
  638. }
  639. void GridMap::_octant_enter_world(const OctantKey &p_key) {
  640. ERR_FAIL_COND(!octant_map.has(p_key));
  641. Octant &g = *octant_map[p_key];
  642. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  643. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, get_world_3d()->get_space());
  644. if (g.collision_debug_instance.is_valid()) {
  645. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, get_world_3d()->get_scenario());
  646. RS::get_singleton()->instance_set_transform(g.collision_debug_instance, get_global_transform());
  647. }
  648. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  649. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, get_world_3d()->get_scenario());
  650. RS::get_singleton()->instance_set_transform(g.multimesh_instances[i].instance, get_global_transform());
  651. }
  652. if (bake_navigation && mesh_library.is_valid()) {
  653. for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
  654. if (cell_map.has(F.key) && F.value.region.is_valid() == false) {
  655. Ref<NavigationMesh> navigation_mesh = mesh_library->get_item_navigation_mesh(cell_map[F.key].item);
  656. if (navigation_mesh.is_valid()) {
  657. RID region = NavigationServer3D::get_singleton()->region_create();
  658. NavigationServer3D::get_singleton()->region_set_owner_id(region, get_instance_id());
  659. NavigationServer3D::get_singleton()->region_set_navigation_layers(region, F.value.navigation_layers);
  660. NavigationServer3D::get_singleton()->region_set_navigation_mesh(region, navigation_mesh);
  661. NavigationServer3D::get_singleton()->region_set_transform(region, get_global_transform() * F.value.xform);
  662. if (map_override.is_valid()) {
  663. NavigationServer3D::get_singleton()->region_set_map(region, map_override);
  664. } else {
  665. NavigationServer3D::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
  666. }
  667. F.value.region = region;
  668. }
  669. }
  670. }
  671. #ifdef DEBUG_ENABLED
  672. if (bake_navigation) {
  673. if (!g.navigation_debug_edge_connections_instance.is_valid()) {
  674. g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  675. }
  676. if (g.navigation_debug_edge_connections_mesh.is_null()) {
  677. g.navigation_debug_edge_connections_mesh.instantiate();
  678. }
  679. _update_octant_navigation_debug_edge_connections_mesh(p_key);
  680. }
  681. #endif // DEBUG_ENABLED
  682. }
  683. }
  684. void GridMap::_octant_exit_world(const OctantKey &p_key) {
  685. ERR_FAIL_NULL(RenderingServer::get_singleton());
  686. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  687. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  688. ERR_FAIL_COND(!octant_map.has(p_key));
  689. Octant &g = *octant_map[p_key];
  690. PhysicsServer3D::get_singleton()->body_set_state(g.static_body, PhysicsServer3D::BODY_STATE_TRANSFORM, get_global_transform());
  691. PhysicsServer3D::get_singleton()->body_set_space(g.static_body, RID());
  692. if (g.collision_debug_instance.is_valid()) {
  693. RS::get_singleton()->instance_set_scenario(g.collision_debug_instance, RID());
  694. }
  695. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  696. RS::get_singleton()->instance_set_scenario(g.multimesh_instances[i].instance, RID());
  697. }
  698. for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
  699. if (F.value.region.is_valid()) {
  700. NavigationServer3D::get_singleton()->free(F.value.region);
  701. F.value.region = RID();
  702. }
  703. if (F.value.navigation_mesh_debug_instance.is_valid()) {
  704. RS::get_singleton()->free(F.value.navigation_mesh_debug_instance);
  705. F.value.navigation_mesh_debug_instance = RID();
  706. }
  707. }
  708. #ifdef DEBUG_ENABLED
  709. if (bake_navigation) {
  710. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  711. RenderingServer::get_singleton()->free(g.navigation_debug_edge_connections_instance);
  712. g.navigation_debug_edge_connections_instance = RID();
  713. }
  714. if (g.navigation_debug_edge_connections_mesh.is_valid()) {
  715. RenderingServer::get_singleton()->free(g.navigation_debug_edge_connections_mesh->get_rid());
  716. }
  717. }
  718. #endif // DEBUG_ENABLED
  719. }
  720. void GridMap::_octant_clean_up(const OctantKey &p_key) {
  721. ERR_FAIL_NULL(RenderingServer::get_singleton());
  722. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  723. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  724. ERR_FAIL_COND(!octant_map.has(p_key));
  725. Octant &g = *octant_map[p_key];
  726. if (g.collision_debug.is_valid()) {
  727. RS::get_singleton()->free(g.collision_debug);
  728. }
  729. if (g.collision_debug_instance.is_valid()) {
  730. RS::get_singleton()->free(g.collision_debug_instance);
  731. }
  732. PhysicsServer3D::get_singleton()->free(g.static_body);
  733. // Erase navigation
  734. for (const KeyValue<IndexKey, Octant::NavigationCell> &E : g.navigation_cell_ids) {
  735. if (E.value.region.is_valid()) {
  736. NavigationServer3D::get_singleton()->free(E.value.region);
  737. }
  738. if (E.value.navigation_mesh_debug_instance.is_valid()) {
  739. RS::get_singleton()->free(E.value.navigation_mesh_debug_instance);
  740. }
  741. }
  742. g.navigation_cell_ids.clear();
  743. #ifdef DEBUG_ENABLED
  744. if (bake_navigation) {
  745. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  746. RenderingServer::get_singleton()->free(g.navigation_debug_edge_connections_instance);
  747. g.navigation_debug_edge_connections_instance = RID();
  748. }
  749. if (g.navigation_debug_edge_connections_mesh.is_valid()) {
  750. RenderingServer::get_singleton()->free(g.navigation_debug_edge_connections_mesh->get_rid());
  751. }
  752. }
  753. #endif // DEBUG_ENABLED
  754. //erase multimeshes
  755. for (int i = 0; i < g.multimesh_instances.size(); i++) {
  756. RS::get_singleton()->free(g.multimesh_instances[i].instance);
  757. RS::get_singleton()->free(g.multimesh_instances[i].multimesh);
  758. }
  759. g.multimesh_instances.clear();
  760. }
  761. void GridMap::_notification(int p_what) {
  762. switch (p_what) {
  763. case NOTIFICATION_ENTER_WORLD: {
  764. last_transform = get_global_transform();
  765. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  766. _octant_enter_world(E.key);
  767. }
  768. for (int i = 0; i < baked_meshes.size(); i++) {
  769. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, get_world_3d()->get_scenario());
  770. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  771. }
  772. } break;
  773. case NOTIFICATION_ENTER_TREE: {
  774. #ifdef DEBUG_ENABLED
  775. if (bake_navigation && NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  776. _update_navigation_debug_edge_connections();
  777. }
  778. #endif // DEBUG_ENABLED
  779. _update_visibility();
  780. } break;
  781. case NOTIFICATION_TRANSFORM_CHANGED: {
  782. Transform3D new_xform = get_global_transform();
  783. if (new_xform == last_transform) {
  784. break;
  785. }
  786. //update run
  787. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  788. _octant_transform(E.key);
  789. }
  790. last_transform = new_xform;
  791. for (int i = 0; i < baked_meshes.size(); i++) {
  792. RS::get_singleton()->instance_set_transform(baked_meshes[i].instance, get_global_transform());
  793. }
  794. } break;
  795. case NOTIFICATION_EXIT_WORLD: {
  796. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  797. _octant_exit_world(E.key);
  798. }
  799. //_queue_octants_dirty(MAP_DIRTY_INSTANCES|MAP_DIRTY_TRANSFORMS);
  800. //_update_octants_callback();
  801. //_update_area_instances();
  802. for (int i = 0; i < baked_meshes.size(); i++) {
  803. RS::get_singleton()->instance_set_scenario(baked_meshes[i].instance, RID());
  804. }
  805. } break;
  806. case NOTIFICATION_VISIBILITY_CHANGED: {
  807. _update_visibility();
  808. } break;
  809. }
  810. }
  811. void GridMap::_update_visibility() {
  812. if (!is_inside_tree()) {
  813. return;
  814. }
  815. for (KeyValue<OctantKey, Octant *> &e : octant_map) {
  816. Octant *octant = e.value;
  817. for (int i = 0; i < octant->multimesh_instances.size(); i++) {
  818. const Octant::MultimeshInstance &mi = octant->multimesh_instances[i];
  819. RS::get_singleton()->instance_set_visible(mi.instance, is_visible_in_tree());
  820. }
  821. }
  822. for (int i = 0; i < baked_meshes.size(); i++) {
  823. RS::get_singleton()->instance_set_visible(baked_meshes[i].instance, is_visible_in_tree());
  824. }
  825. }
  826. void GridMap::_queue_octants_dirty() {
  827. if (awaiting_update) {
  828. return;
  829. }
  830. callable_mp(this, &GridMap::_update_octants_callback).call_deferred();
  831. awaiting_update = true;
  832. }
  833. void GridMap::_recreate_octant_data() {
  834. recreating_octants = true;
  835. HashMap<IndexKey, Cell, IndexKey> cell_copy = cell_map;
  836. _clear_internal();
  837. for (const KeyValue<IndexKey, Cell> &E : cell_copy) {
  838. set_cell_item(Vector3i(E.key), E.value.item, E.value.rot);
  839. }
  840. recreating_octants = false;
  841. }
  842. void GridMap::_clear_internal() {
  843. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  844. if (is_inside_world()) {
  845. _octant_exit_world(E.key);
  846. }
  847. _octant_clean_up(E.key);
  848. memdelete(E.value);
  849. }
  850. octant_map.clear();
  851. cell_map.clear();
  852. }
  853. void GridMap::clear() {
  854. _clear_internal();
  855. clear_baked_meshes();
  856. }
  857. #ifndef DISABLE_DEPRECATED
  858. void GridMap::resource_changed(const Ref<Resource> &p_res) {
  859. }
  860. #endif
  861. void GridMap::_update_octants_callback() {
  862. if (!awaiting_update) {
  863. return;
  864. }
  865. List<OctantKey> to_delete;
  866. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  867. if (_octant_update(E.key)) {
  868. to_delete.push_back(E.key);
  869. }
  870. }
  871. while (to_delete.front()) {
  872. memdelete(octant_map[to_delete.front()->get()]);
  873. octant_map.erase(to_delete.front()->get());
  874. to_delete.pop_front();
  875. }
  876. _update_visibility();
  877. awaiting_update = false;
  878. }
  879. void GridMap::_bind_methods() {
  880. ClassDB::bind_method(D_METHOD("set_collision_layer", "layer"), &GridMap::set_collision_layer);
  881. ClassDB::bind_method(D_METHOD("get_collision_layer"), &GridMap::get_collision_layer);
  882. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &GridMap::set_collision_mask);
  883. ClassDB::bind_method(D_METHOD("get_collision_mask"), &GridMap::get_collision_mask);
  884. ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &GridMap::set_collision_mask_value);
  885. ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &GridMap::get_collision_mask_value);
  886. ClassDB::bind_method(D_METHOD("set_collision_layer_value", "layer_number", "value"), &GridMap::set_collision_layer_value);
  887. ClassDB::bind_method(D_METHOD("get_collision_layer_value", "layer_number"), &GridMap::get_collision_layer_value);
  888. ClassDB::bind_method(D_METHOD("set_collision_priority", "priority"), &GridMap::set_collision_priority);
  889. ClassDB::bind_method(D_METHOD("get_collision_priority"), &GridMap::get_collision_priority);
  890. ClassDB::bind_method(D_METHOD("set_physics_material", "material"), &GridMap::set_physics_material);
  891. ClassDB::bind_method(D_METHOD("get_physics_material"), &GridMap::get_physics_material);
  892. ClassDB::bind_method(D_METHOD("set_bake_navigation", "bake_navigation"), &GridMap::set_bake_navigation);
  893. ClassDB::bind_method(D_METHOD("is_baking_navigation"), &GridMap::is_baking_navigation);
  894. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &GridMap::set_navigation_map);
  895. ClassDB::bind_method(D_METHOD("get_navigation_map"), &GridMap::get_navigation_map);
  896. ClassDB::bind_method(D_METHOD("set_mesh_library", "mesh_library"), &GridMap::set_mesh_library);
  897. ClassDB::bind_method(D_METHOD("get_mesh_library"), &GridMap::get_mesh_library);
  898. ClassDB::bind_method(D_METHOD("set_cell_size", "size"), &GridMap::set_cell_size);
  899. ClassDB::bind_method(D_METHOD("get_cell_size"), &GridMap::get_cell_size);
  900. ClassDB::bind_method(D_METHOD("set_cell_scale", "scale"), &GridMap::set_cell_scale);
  901. ClassDB::bind_method(D_METHOD("get_cell_scale"), &GridMap::get_cell_scale);
  902. ClassDB::bind_method(D_METHOD("set_octant_size", "size"), &GridMap::set_octant_size);
  903. ClassDB::bind_method(D_METHOD("get_octant_size"), &GridMap::get_octant_size);
  904. ClassDB::bind_method(D_METHOD("set_cell_item", "position", "item", "orientation"), &GridMap::set_cell_item, DEFVAL(0));
  905. ClassDB::bind_method(D_METHOD("get_cell_item", "position"), &GridMap::get_cell_item);
  906. ClassDB::bind_method(D_METHOD("get_cell_item_orientation", "position"), &GridMap::get_cell_item_orientation);
  907. ClassDB::bind_method(D_METHOD("get_cell_item_basis", "position"), &GridMap::get_cell_item_basis);
  908. ClassDB::bind_method(D_METHOD("get_basis_with_orthogonal_index", "index"), &GridMap::get_basis_with_orthogonal_index);
  909. ClassDB::bind_method(D_METHOD("get_orthogonal_index_from_basis", "basis"), &GridMap::get_orthogonal_index_from_basis);
  910. ClassDB::bind_method(D_METHOD("local_to_map", "local_position"), &GridMap::local_to_map);
  911. ClassDB::bind_method(D_METHOD("map_to_local", "map_position"), &GridMap::map_to_local);
  912. #ifndef DISABLE_DEPRECATED
  913. ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &GridMap::resource_changed);
  914. #endif
  915. ClassDB::bind_method(D_METHOD("set_center_x", "enable"), &GridMap::set_center_x);
  916. ClassDB::bind_method(D_METHOD("get_center_x"), &GridMap::get_center_x);
  917. ClassDB::bind_method(D_METHOD("set_center_y", "enable"), &GridMap::set_center_y);
  918. ClassDB::bind_method(D_METHOD("get_center_y"), &GridMap::get_center_y);
  919. ClassDB::bind_method(D_METHOD("set_center_z", "enable"), &GridMap::set_center_z);
  920. ClassDB::bind_method(D_METHOD("get_center_z"), &GridMap::get_center_z);
  921. ClassDB::bind_method(D_METHOD("clear"), &GridMap::clear);
  922. ClassDB::bind_method(D_METHOD("get_used_cells"), &GridMap::get_used_cells);
  923. ClassDB::bind_method(D_METHOD("get_used_cells_by_item", "item"), &GridMap::get_used_cells_by_item);
  924. ClassDB::bind_method(D_METHOD("get_meshes"), &GridMap::get_meshes);
  925. ClassDB::bind_method(D_METHOD("get_bake_meshes"), &GridMap::get_bake_meshes);
  926. ClassDB::bind_method(D_METHOD("get_bake_mesh_instance", "idx"), &GridMap::get_bake_mesh_instance);
  927. ClassDB::bind_method(D_METHOD("clear_baked_meshes"), &GridMap::clear_baked_meshes);
  928. ClassDB::bind_method(D_METHOD("make_baked_meshes", "gen_lightmap_uv", "lightmap_uv_texel_size"), &GridMap::make_baked_meshes, DEFVAL(false), DEFVAL(0.1));
  929. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh_library", PROPERTY_HINT_RESOURCE_TYPE, "MeshLibrary"), "set_mesh_library", "get_mesh_library");
  930. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "physics_material", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsMaterial"), "set_physics_material", "get_physics_material");
  931. ADD_GROUP("Cell", "cell_");
  932. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "cell_size", PROPERTY_HINT_NONE, "suffix:m"), "set_cell_size", "get_cell_size");
  933. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_octant_size", PROPERTY_HINT_RANGE, "1,1024,1"), "set_octant_size", "get_octant_size");
  934. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_x"), "set_center_x", "get_center_x");
  935. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_y"), "set_center_y", "get_center_y");
  936. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cell_center_z"), "set_center_z", "get_center_z");
  937. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "cell_scale"), "set_cell_scale", "get_cell_scale");
  938. ADD_GROUP("Collision", "collision_");
  939. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_layer", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_layer", "get_collision_layer");
  940. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  941. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "collision_priority"), "set_collision_priority", "get_collision_priority");
  942. ADD_GROUP("Navigation", "");
  943. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_navigation"), "set_bake_navigation", "is_baking_navigation");
  944. BIND_CONSTANT(INVALID_CELL_ITEM);
  945. ADD_SIGNAL(MethodInfo("cell_size_changed", PropertyInfo(Variant::VECTOR3, "cell_size")));
  946. ADD_SIGNAL(MethodInfo(CoreStringName(changed)));
  947. }
  948. void GridMap::set_cell_scale(float p_scale) {
  949. cell_scale = p_scale;
  950. _recreate_octant_data();
  951. }
  952. float GridMap::get_cell_scale() const {
  953. return cell_scale;
  954. }
  955. TypedArray<Vector3i> GridMap::get_used_cells() const {
  956. TypedArray<Vector3i> a;
  957. a.resize(cell_map.size());
  958. int i = 0;
  959. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  960. Vector3i p(E.key.x, E.key.y, E.key.z);
  961. a[i++] = p;
  962. }
  963. return a;
  964. }
  965. TypedArray<Vector3i> GridMap::get_used_cells_by_item(int p_item) const {
  966. TypedArray<Vector3i> a;
  967. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  968. if ((int)E.value.item == p_item) {
  969. Vector3i p(E.key.x, E.key.y, E.key.z);
  970. a.push_back(p);
  971. }
  972. }
  973. return a;
  974. }
  975. Array GridMap::get_meshes() const {
  976. if (mesh_library.is_null()) {
  977. return Array();
  978. }
  979. Vector3 ofs = _get_offset();
  980. Array meshes;
  981. for (const KeyValue<IndexKey, Cell> &E : cell_map) {
  982. int id = E.value.item;
  983. if (!mesh_library->has_item(id)) {
  984. continue;
  985. }
  986. Ref<Mesh> mesh = mesh_library->get_item_mesh(id);
  987. if (mesh.is_null()) {
  988. continue;
  989. }
  990. IndexKey ik = E.key;
  991. Vector3 cellpos = Vector3(ik.x, ik.y, ik.z);
  992. Transform3D xform;
  993. xform.basis = _ortho_bases[E.value.rot];
  994. xform.set_origin(cellpos * cell_size + ofs);
  995. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  996. meshes.push_back(xform * mesh_library->get_item_mesh_transform(id));
  997. meshes.push_back(mesh);
  998. }
  999. return meshes;
  1000. }
  1001. Vector3 GridMap::_get_offset() const {
  1002. return Vector3(
  1003. cell_size.x * 0.5 * int(center_x),
  1004. cell_size.y * 0.5 * int(center_y),
  1005. cell_size.z * 0.5 * int(center_z));
  1006. }
  1007. void GridMap::clear_baked_meshes() {
  1008. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1009. for (int i = 0; i < baked_meshes.size(); i++) {
  1010. RS::get_singleton()->free(baked_meshes[i].instance);
  1011. }
  1012. baked_meshes.clear();
  1013. _recreate_octant_data();
  1014. }
  1015. void GridMap::make_baked_meshes(bool p_gen_lightmap_uv, float p_lightmap_uv_texel_size) {
  1016. if (!mesh_library.is_valid()) {
  1017. return;
  1018. }
  1019. //generate
  1020. HashMap<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>, OctantKey> surface_map;
  1021. for (KeyValue<IndexKey, Cell> &E : cell_map) {
  1022. IndexKey key = E.key;
  1023. int item = E.value.item;
  1024. if (!mesh_library->has_item(item)) {
  1025. continue;
  1026. }
  1027. Ref<Mesh> mesh = mesh_library->get_item_mesh(item);
  1028. if (!mesh.is_valid()) {
  1029. continue;
  1030. }
  1031. Vector3 cellpos = Vector3(key.x, key.y, key.z);
  1032. Vector3 ofs = _get_offset();
  1033. Transform3D xform;
  1034. xform.basis = _ortho_bases[E.value.rot];
  1035. xform.set_origin(cellpos * cell_size + ofs);
  1036. xform.basis.scale(Vector3(cell_scale, cell_scale, cell_scale));
  1037. OctantKey ok;
  1038. ok.x = key.x / octant_size;
  1039. ok.y = key.y / octant_size;
  1040. ok.z = key.z / octant_size;
  1041. if (!surface_map.has(ok)) {
  1042. surface_map[ok] = HashMap<Ref<Material>, Ref<SurfaceTool>>();
  1043. }
  1044. HashMap<Ref<Material>, Ref<SurfaceTool>> &mat_map = surface_map[ok];
  1045. for (int i = 0; i < mesh->get_surface_count(); i++) {
  1046. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  1047. continue;
  1048. }
  1049. Ref<Material> surf_mat = mesh->surface_get_material(i);
  1050. if (!mat_map.has(surf_mat)) {
  1051. Ref<SurfaceTool> st;
  1052. st.instantiate();
  1053. st->begin(Mesh::PRIMITIVE_TRIANGLES);
  1054. st->set_material(surf_mat);
  1055. mat_map[surf_mat] = st;
  1056. }
  1057. mat_map[surf_mat]->append_from(mesh, i, xform);
  1058. }
  1059. }
  1060. for (KeyValue<OctantKey, HashMap<Ref<Material>, Ref<SurfaceTool>>> &E : surface_map) {
  1061. Ref<ArrayMesh> mesh;
  1062. mesh.instantiate();
  1063. for (KeyValue<Ref<Material>, Ref<SurfaceTool>> &F : E.value) {
  1064. F.value->commit(mesh);
  1065. }
  1066. BakedMesh bm;
  1067. bm.mesh = mesh;
  1068. bm.instance = RS::get_singleton()->instance_create();
  1069. RS::get_singleton()->instance_set_base(bm.instance, bm.mesh->get_rid());
  1070. RS::get_singleton()->instance_attach_object_instance_id(bm.instance, get_instance_id());
  1071. if (is_inside_tree()) {
  1072. RS::get_singleton()->instance_set_scenario(bm.instance, get_world_3d()->get_scenario());
  1073. RS::get_singleton()->instance_set_transform(bm.instance, get_global_transform());
  1074. }
  1075. if (p_gen_lightmap_uv) {
  1076. mesh->lightmap_unwrap(get_global_transform(), p_lightmap_uv_texel_size);
  1077. }
  1078. baked_meshes.push_back(bm);
  1079. }
  1080. _recreate_octant_data();
  1081. }
  1082. Array GridMap::get_bake_meshes() {
  1083. if (!baked_meshes.size()) {
  1084. make_baked_meshes(true);
  1085. }
  1086. Array arr;
  1087. for (int i = 0; i < baked_meshes.size(); i++) {
  1088. arr.push_back(baked_meshes[i].mesh);
  1089. arr.push_back(Transform3D());
  1090. }
  1091. return arr;
  1092. }
  1093. RID GridMap::get_bake_mesh_instance(int p_idx) {
  1094. ERR_FAIL_INDEX_V(p_idx, baked_meshes.size(), RID());
  1095. return baked_meshes[p_idx].instance;
  1096. }
  1097. GridMap::GridMap() {
  1098. set_notify_transform(true);
  1099. #ifdef DEBUG_ENABLED
  1100. NavigationServer3D::get_singleton()->connect("map_changed", callable_mp(this, &GridMap::_navigation_map_changed));
  1101. NavigationServer3D::get_singleton()->connect("navigation_debug_changed", callable_mp(this, &GridMap::_update_navigation_debug_edge_connections));
  1102. #endif // DEBUG_ENABLED
  1103. }
  1104. #ifdef DEBUG_ENABLED
  1105. void GridMap::_update_navigation_debug_edge_connections() {
  1106. if (bake_navigation) {
  1107. for (const KeyValue<OctantKey, Octant *> &E : octant_map) {
  1108. _update_octant_navigation_debug_edge_connections_mesh(E.key);
  1109. }
  1110. }
  1111. }
  1112. void GridMap::_navigation_map_changed(RID p_map) {
  1113. if (bake_navigation && is_inside_tree() && p_map == get_world_3d()->get_navigation_map()) {
  1114. _update_navigation_debug_edge_connections();
  1115. }
  1116. }
  1117. #endif // DEBUG_ENABLED
  1118. GridMap::~GridMap() {
  1119. clear();
  1120. #ifdef DEBUG_ENABLED
  1121. NavigationServer3D::get_singleton()->disconnect("map_changed", callable_mp(this, &GridMap::_navigation_map_changed));
  1122. NavigationServer3D::get_singleton()->disconnect("navigation_debug_changed", callable_mp(this, &GridMap::_update_navigation_debug_edge_connections));
  1123. #endif // DEBUG_ENABLED
  1124. }
  1125. #ifdef DEBUG_ENABLED
  1126. void GridMap::_update_octant_navigation_debug_edge_connections_mesh(const OctantKey &p_key) {
  1127. ERR_FAIL_COND(!octant_map.has(p_key));
  1128. Octant &g = *octant_map[p_key];
  1129. if (!NavigationServer3D::get_singleton()->get_debug_navigation_enabled()) {
  1130. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  1131. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1132. }
  1133. return;
  1134. }
  1135. if (!is_inside_tree()) {
  1136. return;
  1137. }
  1138. if (!bake_navigation) {
  1139. if (g.navigation_debug_edge_connections_instance.is_valid()) {
  1140. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1141. }
  1142. return;
  1143. }
  1144. if (!g.navigation_debug_edge_connections_instance.is_valid()) {
  1145. g.navigation_debug_edge_connections_instance = RenderingServer::get_singleton()->instance_create();
  1146. }
  1147. if (g.navigation_debug_edge_connections_mesh.is_null()) {
  1148. g.navigation_debug_edge_connections_mesh.instantiate();
  1149. }
  1150. g.navigation_debug_edge_connections_mesh->clear_surfaces();
  1151. float edge_connection_margin = NavigationServer3D::get_singleton()->map_get_edge_connection_margin(get_world_3d()->get_navigation_map());
  1152. float half_edge_connection_margin = edge_connection_margin * 0.5;
  1153. Vector<Vector3> vertex_array;
  1154. for (KeyValue<IndexKey, Octant::NavigationCell> &F : g.navigation_cell_ids) {
  1155. if (cell_map.has(F.key) && F.value.region.is_valid()) {
  1156. int connections_count = NavigationServer3D::get_singleton()->region_get_connections_count(F.value.region);
  1157. if (connections_count == 0) {
  1158. continue;
  1159. }
  1160. for (int i = 0; i < connections_count; i++) {
  1161. Vector3 connection_pathway_start = NavigationServer3D::get_singleton()->region_get_connection_pathway_start(F.value.region, i);
  1162. Vector3 connection_pathway_end = NavigationServer3D::get_singleton()->region_get_connection_pathway_end(F.value.region, i);
  1163. Vector3 direction_start_end = connection_pathway_start.direction_to(connection_pathway_end);
  1164. Vector3 direction_end_start = connection_pathway_end.direction_to(connection_pathway_start);
  1165. Vector3 start_right_dir = direction_start_end.cross(Vector3(0, 1, 0));
  1166. Vector3 start_left_dir = -start_right_dir;
  1167. Vector3 end_right_dir = direction_end_start.cross(Vector3(0, 1, 0));
  1168. Vector3 end_left_dir = -end_right_dir;
  1169. Vector3 left_start_pos = connection_pathway_start + (start_left_dir * half_edge_connection_margin);
  1170. Vector3 right_start_pos = connection_pathway_start + (start_right_dir * half_edge_connection_margin);
  1171. Vector3 left_end_pos = connection_pathway_end + (end_right_dir * half_edge_connection_margin);
  1172. Vector3 right_end_pos = connection_pathway_end + (end_left_dir * half_edge_connection_margin);
  1173. vertex_array.push_back(right_end_pos);
  1174. vertex_array.push_back(left_start_pos);
  1175. vertex_array.push_back(right_start_pos);
  1176. vertex_array.push_back(left_end_pos);
  1177. vertex_array.push_back(right_end_pos);
  1178. vertex_array.push_back(right_start_pos);
  1179. }
  1180. }
  1181. }
  1182. if (vertex_array.size() == 0) {
  1183. return;
  1184. }
  1185. Ref<StandardMaterial3D> edge_connections_material = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connections_material();
  1186. Array mesh_array;
  1187. mesh_array.resize(Mesh::ARRAY_MAX);
  1188. mesh_array[Mesh::ARRAY_VERTEX] = vertex_array;
  1189. g.navigation_debug_edge_connections_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, mesh_array);
  1190. g.navigation_debug_edge_connections_mesh->surface_set_material(0, edge_connections_material);
  1191. RS::get_singleton()->instance_set_base(g.navigation_debug_edge_connections_instance, g.navigation_debug_edge_connections_mesh->get_rid());
  1192. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, is_visible_in_tree());
  1193. if (is_inside_tree()) {
  1194. RS::get_singleton()->instance_set_scenario(g.navigation_debug_edge_connections_instance, get_world_3d()->get_scenario());
  1195. }
  1196. bool enable_edge_connections = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_connections();
  1197. if (!enable_edge_connections) {
  1198. RS::get_singleton()->instance_set_visible(g.navigation_debug_edge_connections_instance, false);
  1199. }
  1200. }
  1201. #endif // DEBUG_ENABLED