grid_map.cpp 58 KB

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