world.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*************************************************************************/
  2. /* world.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 "world.h"
  31. #include "camera_matrix.h"
  32. #include "octree.h"
  33. #include "scene/3d/camera.h"
  34. #include "scene/3d/spatial_indexer.h"
  35. #include "scene/3d/visibility_notifier.h"
  36. #include "scene/scene_string_names.h"
  37. struct SpatialIndexer {
  38. Octree<VisibilityNotifier> octree;
  39. struct NotifierData {
  40. AABB aabb;
  41. OctreeElementID id;
  42. };
  43. Map<VisibilityNotifier *, NotifierData> notifiers;
  44. struct CameraData {
  45. Map<VisibilityNotifier *, uint64_t> notifiers;
  46. };
  47. Map<Camera *, CameraData> cameras;
  48. enum {
  49. VISIBILITY_CULL_MAX = 32768
  50. };
  51. Vector<VisibilityNotifier *> cull;
  52. bool changed;
  53. uint64_t pass;
  54. uint64_t last_frame;
  55. void _notifier_add(VisibilityNotifier *p_notifier, const AABB &p_rect) {
  56. ERR_FAIL_COND(notifiers.has(p_notifier));
  57. notifiers[p_notifier].aabb = p_rect;
  58. notifiers[p_notifier].id = octree.create(p_notifier, p_rect);
  59. changed = true;
  60. }
  61. void _notifier_update(VisibilityNotifier *p_notifier, const AABB &p_rect) {
  62. Map<VisibilityNotifier *, NotifierData>::Element *E = notifiers.find(p_notifier);
  63. ERR_FAIL_COND(!E);
  64. if (E->get().aabb == p_rect)
  65. return;
  66. E->get().aabb = p_rect;
  67. octree.move(E->get().id, E->get().aabb);
  68. changed = true;
  69. }
  70. void _notifier_remove(VisibilityNotifier *p_notifier) {
  71. Map<VisibilityNotifier *, NotifierData>::Element *E = notifiers.find(p_notifier);
  72. ERR_FAIL_COND(!E);
  73. octree.erase(E->get().id);
  74. notifiers.erase(p_notifier);
  75. List<Camera *> removed;
  76. for (Map<Camera *, CameraData>::Element *F = cameras.front(); F; F = F->next()) {
  77. Map<VisibilityNotifier *, uint64_t>::Element *G = F->get().notifiers.find(p_notifier);
  78. if (G) {
  79. F->get().notifiers.erase(G);
  80. removed.push_back(F->key());
  81. }
  82. }
  83. while (!removed.empty()) {
  84. p_notifier->_exit_camera(removed.front()->get());
  85. removed.pop_front();
  86. }
  87. changed = true;
  88. }
  89. void _add_camera(Camera *p_camera) {
  90. ERR_FAIL_COND(cameras.has(p_camera));
  91. CameraData vd;
  92. cameras[p_camera] = vd;
  93. changed = true;
  94. }
  95. void _update_camera(Camera *p_camera) {
  96. Map<Camera *, CameraData>::Element *E = cameras.find(p_camera);
  97. ERR_FAIL_COND(!E);
  98. changed = true;
  99. }
  100. void _remove_camera(Camera *p_camera) {
  101. ERR_FAIL_COND(!cameras.has(p_camera));
  102. List<VisibilityNotifier *> removed;
  103. for (Map<VisibilityNotifier *, uint64_t>::Element *E = cameras[p_camera].notifiers.front(); E; E = E->next()) {
  104. removed.push_back(E->key());
  105. }
  106. while (!removed.empty()) {
  107. removed.front()->get()->_exit_camera(p_camera);
  108. removed.pop_front();
  109. }
  110. cameras.erase(p_camera);
  111. }
  112. void _update(uint64_t p_frame) {
  113. if (p_frame == last_frame)
  114. return;
  115. last_frame = p_frame;
  116. if (!changed)
  117. return;
  118. for (Map<Camera *, CameraData>::Element *E = cameras.front(); E; E = E->next()) {
  119. pass++;
  120. Camera *c = E->key();
  121. Vector<Plane> planes = c->get_frustum();
  122. int culled = octree.cull_convex(planes, cull.ptr(), cull.size());
  123. VisibilityNotifier **ptr = cull.ptr();
  124. List<VisibilityNotifier *> added;
  125. List<VisibilityNotifier *> removed;
  126. for (int i = 0; i < culled; i++) {
  127. //notifiers in frustum
  128. Map<VisibilityNotifier *, uint64_t>::Element *H = E->get().notifiers.find(ptr[i]);
  129. if (!H) {
  130. E->get().notifiers.insert(ptr[i], pass);
  131. added.push_back(ptr[i]);
  132. } else {
  133. H->get() = pass;
  134. }
  135. }
  136. for (Map<VisibilityNotifier *, uint64_t>::Element *F = E->get().notifiers.front(); F; F = F->next()) {
  137. if (F->get() != pass)
  138. removed.push_back(F->key());
  139. }
  140. while (!added.empty()) {
  141. added.front()->get()->_enter_camera(E->key());
  142. added.pop_front();
  143. }
  144. while (!removed.empty()) {
  145. E->get().notifiers.erase(removed.front()->get());
  146. removed.front()->get()->_exit_camera(E->key());
  147. removed.pop_front();
  148. }
  149. }
  150. changed = false;
  151. }
  152. SpatialIndexer() {
  153. pass = 0;
  154. last_frame = 0;
  155. changed = false;
  156. cull.resize(VISIBILITY_CULL_MAX);
  157. }
  158. };
  159. void World::_register_camera(Camera *p_camera) {
  160. #ifndef _3D_DISABLED
  161. indexer->_add_camera(p_camera);
  162. #endif
  163. }
  164. void World::_update_camera(Camera *p_camera) {
  165. #ifndef _3D_DISABLED
  166. indexer->_update_camera(p_camera);
  167. #endif
  168. }
  169. void World::_remove_camera(Camera *p_camera) {
  170. #ifndef _3D_DISABLED
  171. indexer->_remove_camera(p_camera);
  172. #endif
  173. }
  174. void World::_register_notifier(VisibilityNotifier *p_notifier, const AABB &p_rect) {
  175. #ifndef _3D_DISABLED
  176. indexer->_notifier_add(p_notifier, p_rect);
  177. #endif
  178. }
  179. void World::_update_notifier(VisibilityNotifier *p_notifier, const AABB &p_rect) {
  180. #ifndef _3D_DISABLED
  181. indexer->_notifier_update(p_notifier, p_rect);
  182. #endif
  183. }
  184. void World::_remove_notifier(VisibilityNotifier *p_notifier) {
  185. #ifndef _3D_DISABLED
  186. indexer->_notifier_remove(p_notifier);
  187. #endif
  188. }
  189. void World::_update(uint64_t p_frame) {
  190. #ifndef _3D_DISABLED
  191. indexer->_update(p_frame);
  192. #endif
  193. }
  194. RID World::get_space() const {
  195. return space;
  196. }
  197. RID World::get_scenario() const {
  198. return scenario;
  199. }
  200. RID World::get_sound_space() const {
  201. return sound_space;
  202. }
  203. void World::set_environment(const Ref<Environment> &p_environment) {
  204. environment = p_environment;
  205. if (environment.is_valid())
  206. VS::get_singleton()->scenario_set_environment(scenario, environment->get_rid());
  207. else
  208. VS::get_singleton()->scenario_set_environment(scenario, RID());
  209. }
  210. Ref<Environment> World::get_environment() const {
  211. return environment;
  212. }
  213. PhysicsDirectSpaceState *World::get_direct_space_state() {
  214. return PhysicsServer::get_singleton()->space_get_direct_state(space);
  215. }
  216. void World::_bind_methods() {
  217. ObjectTypeDB::bind_method(_MD("get_space"), &World::get_space);
  218. ObjectTypeDB::bind_method(_MD("get_scenario"), &World::get_scenario);
  219. ObjectTypeDB::bind_method(_MD("get_sound_space"), &World::get_sound_space);
  220. ObjectTypeDB::bind_method(_MD("set_environment", "env:Environment"), &World::set_environment);
  221. ObjectTypeDB::bind_method(_MD("get_environment:Environment"), &World::get_environment);
  222. ObjectTypeDB::bind_method(_MD("get_direct_space_state:PhysicsDirectSpaceState"), &World::get_direct_space_state);
  223. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), _SCS("set_environment"), _SCS("get_environment"));
  224. }
  225. World::World() {
  226. space = PhysicsServer::get_singleton()->space_create();
  227. scenario = VisualServer::get_singleton()->scenario_create();
  228. sound_space = SpatialSoundServer::get_singleton()->space_create();
  229. PhysicsServer::get_singleton()->space_set_active(space, true);
  230. PhysicsServer::get_singleton()->area_set_param(space, PhysicsServer::AREA_PARAM_GRAVITY, GLOBAL_DEF("physics/default_gravity", 9.8));
  231. PhysicsServer::get_singleton()->area_set_param(space, PhysicsServer::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_DEF("physics/default_gravity_vector", Vector3(0, -1, 0)));
  232. PhysicsServer::get_singleton()->area_set_param(space, PhysicsServer::AREA_PARAM_LINEAR_DAMP, GLOBAL_DEF("physics/default_linear_damp", 0.1));
  233. PhysicsServer::get_singleton()->area_set_param(space, PhysicsServer::AREA_PARAM_ANGULAR_DAMP, GLOBAL_DEF("physics/default_angular_damp", 0.1));
  234. #ifdef _3D_DISABLED
  235. indexer = NULL;
  236. #else
  237. indexer = memnew(SpatialIndexer);
  238. #endif
  239. }
  240. World::~World() {
  241. PhysicsServer::get_singleton()->free(space);
  242. VisualServer::get_singleton()->free(scenario);
  243. SpatialSoundServer::get_singleton()->free(sound_space);
  244. #ifndef _3D_DISABLED
  245. memdelete(indexer);
  246. #endif
  247. }