spatial_indexer.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*************************************************************************/
  2. /* spatial_indexer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "spatial_indexer.h"
  31. #if 0
  32. #include "camera.h"
  33. #include "proximity_area.h"
  34. #include "scene/scene_string_names.h"
  35. void SpatialIndexer::add_camera(Camera* p_camera) {
  36. cameras.insert(p_camera);
  37. }
  38. void SpatialIndexer::remove_camera(Camera* p_camera){
  39. for (Set<ProximityArea*>::Element *F=proximity_areas.front();F;F=F->next()) {
  40. ProximityArea *prox = F->get();
  41. TK<Camera> k;
  42. k.against=p_camera;
  43. k.area=prox;
  44. if (camera_pairs.has(k)) {
  45. camera_pairs.erase(k);
  46. prox->area_exit(ProximityArea::TRACK_CAMERAS,p_camera);
  47. }
  48. }
  49. cameras.erase(p_camera);
  50. }
  51. void SpatialIndexer::update_camera(Camera* p_camera) {
  52. _request_update();
  53. }
  54. void SpatialIndexer::_update_pairs() {
  55. // brute force interseciton code, no broadphase
  56. // will implement broadphase in the future
  57. for (Set<Camera*>::Element *E=cameras.front();E;E=E->next()) {
  58. Camera *cam = E->get();
  59. Vector<Plane> cplanes = cam->get_frustum();
  60. for (Set<ProximityArea*>::Element *F=proximity_areas.front();F;F=F->next()) {
  61. ProximityArea *prox = F->get();
  62. bool inters=false;
  63. if (prox->get_track_flag(ProximityArea::TRACK_CAMERAS)) {
  64. AABB aabb = prox->get_global_transform().xform(prox->get_aabb());
  65. if (aabb.intersects_convex_shape(cplanes.ptr(),cplanes.size()))
  66. inters=true;
  67. }
  68. TK<Camera> k;
  69. k.against=cam;
  70. k.area=prox;
  71. bool has = camera_pairs.has(k);
  72. if (inters==has)
  73. continue;
  74. if (inters) {
  75. camera_pairs.insert(k);
  76. prox->area_enter(ProximityArea::TRACK_CAMERAS,cam);
  77. } else {
  78. camera_pairs.erase(k);
  79. prox->area_exit(ProximityArea::TRACK_CAMERAS,cam);
  80. }
  81. }
  82. }
  83. pending_update=false;
  84. }
  85. void SpatialIndexer::_bind_methods() {
  86. ObjectTypeDB::bind_method(_MD("_update_pairs"),&SpatialIndexer::_update_pairs);
  87. }
  88. void SpatialIndexer::add_proximity_area(ProximityArea* p_area) {
  89. proximity_areas.insert(p_area);
  90. }
  91. void SpatialIndexer::remove_proximity_area(ProximityArea* p_area) {
  92. for (Set<Camera*>::Element *E=cameras.front();E;E=E->next()) {
  93. Camera *cam = E->get();
  94. TK<Camera> k;
  95. k.against=cam;
  96. k.area=p_area;
  97. if (camera_pairs.has(k)) {
  98. camera_pairs.erase(k);
  99. p_area->area_exit(ProximityArea::TRACK_CAMERAS,cam);
  100. }
  101. }
  102. proximity_areas.erase(p_area);
  103. }
  104. void SpatialIndexer::_request_update() {
  105. if (pending_update)
  106. return;
  107. pending_update=true;
  108. call_deferred(SceneStringNames::get_singleton()->_update_pairs);
  109. }
  110. void SpatialIndexer::update_proximity_area_transform(ProximityArea* p_area) {
  111. _request_update();
  112. }
  113. void SpatialIndexer::update_proximity_area_flags(ProximityArea* p_area) {
  114. _request_update();
  115. }
  116. SpatialIndexer::SpatialIndexer() {
  117. pending_update=false;
  118. }
  119. #endif