navigation_mesh.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*************************************************************************/
  2. /* navigation_mesh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "navigation_mesh.h"
  30. #include "navigation.h"
  31. #include "mesh_instance.h"
  32. void NavigationMesh::create_from_mesh(const Ref<Mesh>& p_mesh) {
  33. vertices=PoolVector<Vector3>();
  34. clear_polygons();
  35. for(int i=0;i<p_mesh->get_surface_count();i++) {
  36. if (p_mesh->surface_get_primitive_type(i)!=Mesh::PRIMITIVE_TRIANGLES)
  37. continue;
  38. Array arr = p_mesh->surface_get_arrays(i);
  39. PoolVector<Vector3> varr = arr[Mesh::ARRAY_VERTEX];
  40. PoolVector<int> iarr = arr[Mesh::ARRAY_INDEX];
  41. if (varr.size()==0 || iarr.size()==0)
  42. continue;
  43. int from = vertices.size();
  44. vertices.append_array(varr);
  45. int rlen = iarr.size();
  46. PoolVector<int>::Read r = iarr.read();
  47. for(int j=0;j<rlen;j+=3) {
  48. Vector<int> vi;
  49. vi.resize(3);
  50. vi[0]=r[j+0]+from;
  51. vi[1]=r[j+1]+from;
  52. vi[2]=r[j+2]+from;
  53. add_polygon(vi);
  54. }
  55. }
  56. }
  57. void NavigationMesh::set_vertices(const PoolVector<Vector3>& p_vertices) {
  58. vertices=p_vertices;
  59. }
  60. PoolVector<Vector3> NavigationMesh::get_vertices() const{
  61. return vertices;
  62. }
  63. void NavigationMesh::_set_polygons(const Array& p_array) {
  64. polygons.resize(p_array.size());
  65. for(int i=0;i<p_array.size();i++) {
  66. polygons[i].indices=p_array[i];
  67. }
  68. }
  69. Array NavigationMesh::_get_polygons() const {
  70. Array ret;
  71. ret.resize(polygons.size());
  72. for(int i=0;i<ret.size();i++) {
  73. ret[i]=polygons[i].indices;
  74. }
  75. return ret;
  76. }
  77. void NavigationMesh::add_polygon(const Vector<int>& p_polygon){
  78. Polygon polygon;
  79. polygon.indices=p_polygon;
  80. polygons.push_back(polygon);
  81. }
  82. int NavigationMesh::get_polygon_count() const{
  83. return polygons.size();
  84. }
  85. Vector<int> NavigationMesh::get_polygon(int p_idx){
  86. ERR_FAIL_INDEX_V(p_idx,polygons.size(),Vector<int>());
  87. return polygons[p_idx].indices;
  88. }
  89. void NavigationMesh::clear_polygons(){
  90. polygons.clear();
  91. }
  92. Ref<Mesh> NavigationMesh::get_debug_mesh() {
  93. if (debug_mesh.is_valid())
  94. return debug_mesh;
  95. PoolVector<Vector3> vertices = get_vertices();
  96. PoolVector<Vector3>::Read vr=vertices.read();
  97. List<Face3> faces;
  98. for(int i=0;i<get_polygon_count();i++) {
  99. Vector<int> p = get_polygon(i);
  100. for(int j=2;j<p.size();j++) {
  101. Face3 f;
  102. f.vertex[0]=vr[p[0]];
  103. f.vertex[1]=vr[p[j-1]];
  104. f.vertex[2]=vr[p[j]];
  105. faces.push_back(f);
  106. }
  107. }
  108. Map<_EdgeKey,bool> edge_map;
  109. PoolVector<Vector3> tmeshfaces;
  110. tmeshfaces.resize(faces.size()*3);
  111. {
  112. PoolVector<Vector3>::Write tw=tmeshfaces.write();
  113. int tidx=0;
  114. for(List<Face3>::Element *E=faces.front();E;E=E->next()) {
  115. const Face3 &f = E->get();
  116. for(int j=0;j<3;j++) {
  117. tw[tidx++]=f.vertex[j];
  118. _EdgeKey ek;
  119. ek.from=f.vertex[j].snapped(CMP_EPSILON);
  120. ek.to=f.vertex[(j+1)%3].snapped(CMP_EPSILON);
  121. if (ek.from<ek.to)
  122. SWAP(ek.from,ek.to);
  123. Map<_EdgeKey,bool>::Element *E=edge_map.find(ek);
  124. if (E) {
  125. E->get()=false;
  126. } else {
  127. edge_map[ek]=true;
  128. }
  129. }
  130. }
  131. }
  132. List<Vector3> lines;
  133. for(Map<_EdgeKey,bool>::Element *E=edge_map.front();E;E=E->next()) {
  134. if (E->get()) {
  135. lines.push_back(E->key().from);
  136. lines.push_back(E->key().to);
  137. }
  138. }
  139. PoolVector<Vector3> varr;
  140. varr.resize(lines.size());
  141. {
  142. PoolVector<Vector3>::Write w = varr.write();
  143. int idx=0;
  144. for(List<Vector3>::Element *E=lines.front();E;E=E->next()) {
  145. w[idx++]=E->get();
  146. }
  147. }
  148. debug_mesh = Ref<Mesh>( memnew( Mesh ) );
  149. Array arr;
  150. arr.resize(Mesh::ARRAY_MAX);
  151. arr[Mesh::ARRAY_VERTEX]=varr;
  152. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES,arr);
  153. return debug_mesh;
  154. }
  155. void NavigationMesh::_bind_methods() {
  156. ClassDB::bind_method(_MD("set_vertices","vertices"),&NavigationMesh::set_vertices);
  157. ClassDB::bind_method(_MD("get_vertices"),&NavigationMesh::get_vertices);
  158. ClassDB::bind_method(_MD("add_polygon","polygon"),&NavigationMesh::add_polygon);
  159. ClassDB::bind_method(_MD("get_polygon_count"),&NavigationMesh::get_polygon_count);
  160. ClassDB::bind_method(_MD("get_polygon","idx"),&NavigationMesh::get_polygon);
  161. ClassDB::bind_method(_MD("clear_polygons"),&NavigationMesh::clear_polygons);
  162. ClassDB::bind_method(_MD("_set_polygons","polygons"),&NavigationMesh::_set_polygons);
  163. ClassDB::bind_method(_MD("_get_polygons"),&NavigationMesh::_get_polygons);
  164. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR3_ARRAY,"vertices",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),"set_vertices","get_vertices");
  165. ADD_PROPERTY(PropertyInfo(Variant::ARRAY,"polygons",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),"_set_polygons","_get_polygons");
  166. }
  167. NavigationMesh::NavigationMesh() {
  168. }
  169. void NavigationMeshInstance::set_enabled(bool p_enabled) {
  170. if (enabled==p_enabled)
  171. return;
  172. enabled=p_enabled;
  173. if (!is_inside_tree())
  174. return;
  175. if (!enabled) {
  176. if (nav_id!=-1) {
  177. navigation->navmesh_remove(nav_id);
  178. nav_id=-1;
  179. }
  180. } else {
  181. if (navigation) {
  182. if (navmesh.is_valid()) {
  183. nav_id = navigation->navmesh_create(navmesh,get_relative_transform(navigation),this);
  184. }
  185. }
  186. }
  187. if (debug_view) {
  188. MeshInstance *dm=debug_view->cast_to<MeshInstance>();
  189. if (is_enabled()) {
  190. dm->set_material_override( get_tree()->get_debug_navigation_material() );
  191. } else {
  192. dm->set_material_override( get_tree()->get_debug_navigation_disabled_material() );
  193. }
  194. }
  195. update_gizmo();
  196. }
  197. bool NavigationMeshInstance::is_enabled() const {
  198. return enabled;
  199. }
  200. /////////////////////////////
  201. void NavigationMeshInstance::_notification(int p_what) {
  202. switch(p_what) {
  203. case NOTIFICATION_ENTER_TREE: {
  204. Spatial *c=this;
  205. while(c) {
  206. navigation=c->cast_to<Navigation>();
  207. if (navigation) {
  208. if (enabled && navmesh.is_valid()) {
  209. nav_id = navigation->navmesh_create(navmesh,get_relative_transform(navigation),this);
  210. }
  211. break;
  212. }
  213. c=c->get_parent_spatial();
  214. }
  215. if (navmesh.is_valid() && get_tree()->is_debugging_navigation_hint()) {
  216. MeshInstance *dm = memnew( MeshInstance );
  217. dm->set_mesh( navmesh->get_debug_mesh() );
  218. if (is_enabled()) {
  219. dm->set_material_override( get_tree()->get_debug_navigation_material() );
  220. } else {
  221. dm->set_material_override( get_tree()->get_debug_navigation_disabled_material() );
  222. }
  223. add_child(dm);
  224. debug_view=dm;
  225. }
  226. } break;
  227. case NOTIFICATION_TRANSFORM_CHANGED: {
  228. if (navigation && nav_id!=-1) {
  229. navigation->navmesh_set_transform(nav_id,get_relative_transform(navigation));
  230. }
  231. } break;
  232. case NOTIFICATION_EXIT_TREE: {
  233. if (navigation) {
  234. if (nav_id!=-1) {
  235. navigation->navmesh_remove(nav_id);
  236. nav_id=-1;
  237. }
  238. }
  239. if (debug_view) {
  240. debug_view->queue_delete();
  241. debug_view=NULL;
  242. }
  243. navigation=NULL;
  244. } break;
  245. }
  246. }
  247. void NavigationMeshInstance::set_navigation_mesh(const Ref<NavigationMesh>& p_navmesh) {
  248. if (p_navmesh==navmesh)
  249. return;
  250. if (navigation && nav_id!=-1) {
  251. navigation->navmesh_remove(nav_id);
  252. nav_id=-1;
  253. }
  254. navmesh=p_navmesh;
  255. if (navigation && navmesh.is_valid() && enabled) {
  256. nav_id = navigation->navmesh_create(navmesh,get_relative_transform(navigation),this);
  257. }
  258. if (debug_view && navmesh.is_valid()) {
  259. debug_view->cast_to<MeshInstance>()->set_mesh( navmesh->get_debug_mesh() );
  260. }
  261. update_gizmo();
  262. update_configuration_warning();
  263. }
  264. Ref<NavigationMesh> NavigationMeshInstance::get_navigation_mesh() const{
  265. return navmesh;
  266. }
  267. String NavigationMeshInstance::get_configuration_warning() const {
  268. if (!is_visible_in_tree() || !is_inside_tree())
  269. return String();
  270. if (!navmesh.is_valid()) {
  271. return TTR("A NavigationMesh resource must be set or created for this node to work.");
  272. }
  273. const Spatial *c=this;
  274. while(c) {
  275. if (c->cast_to<Navigation>())
  276. return String();
  277. c=c->get_parent()->cast_to<Spatial>();
  278. }
  279. return TTR("NavigationMeshInstance must be a child or grandchild to a Navigation node. It only provides navigation data.");
  280. }
  281. void NavigationMeshInstance::_bind_methods() {
  282. ClassDB::bind_method(_MD("set_navigation_mesh","navmesh"),&NavigationMeshInstance::set_navigation_mesh);
  283. ClassDB::bind_method(_MD("get_navigation_mesh"),&NavigationMeshInstance::get_navigation_mesh);
  284. ClassDB::bind_method(_MD("set_enabled","enabled"),&NavigationMeshInstance::set_enabled);
  285. ClassDB::bind_method(_MD("is_enabled"),&NavigationMeshInstance::is_enabled);
  286. ADD_PROPERTY( PropertyInfo(Variant::OBJECT,"navmesh",PROPERTY_HINT_RESOURCE_TYPE,"NavigationMesh"),"set_navigation_mesh","get_navigation_mesh");
  287. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"enabled"),"set_enabled","is_enabled");
  288. }
  289. NavigationMeshInstance::NavigationMeshInstance() {
  290. debug_view=NULL;
  291. navigation=NULL;
  292. nav_id=-1;
  293. enabled=true;
  294. set_notify_transform(true);
  295. }