grid_map.cpp 55 KB

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