world.cpp 10 KB

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