world_3d.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**************************************************************************/
  2. /* world_3d.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 "world_3d.h"
  31. #include "core/config/project_settings.h"
  32. #include "scene/3d/camera_3d.h"
  33. #include "scene/resources/camera_attributes.h"
  34. #include "scene/resources/environment.h"
  35. #include "servers/navigation_server_3d.h"
  36. void World3D::_register_camera(Camera3D *p_camera) {
  37. cameras.insert(p_camera);
  38. }
  39. void World3D::_remove_camera(Camera3D *p_camera) {
  40. cameras.erase(p_camera);
  41. }
  42. RID World3D::get_space() const {
  43. if (space.is_null()) {
  44. space = PhysicsServer3D::get_singleton()->space_create();
  45. PhysicsServer3D::get_singleton()->space_set_active(space, true);
  46. PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_GRAVITY, GLOBAL_GET("physics/3d/default_gravity"));
  47. PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_GET("physics/3d/default_gravity_vector"));
  48. PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_LINEAR_DAMP, GLOBAL_GET("physics/3d/default_linear_damp"));
  49. PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP, GLOBAL_GET("physics/3d/default_angular_damp"));
  50. }
  51. return space;
  52. }
  53. RID World3D::get_navigation_map() const {
  54. if (navigation_map.is_null()) {
  55. navigation_map = NavigationServer3D::get_singleton()->map_create();
  56. NavigationServer3D::get_singleton()->map_set_active(navigation_map, true);
  57. NavigationServer3D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_GET("navigation/3d/default_cell_size"));
  58. NavigationServer3D::get_singleton()->map_set_cell_height(navigation_map, GLOBAL_GET("navigation/3d/default_cell_height"));
  59. NavigationServer3D::get_singleton()->map_set_up(navigation_map, GLOBAL_GET("navigation/3d/default_up"));
  60. NavigationServer3D::get_singleton()->map_set_merge_rasterizer_cell_scale(navigation_map, GLOBAL_GET("navigation/3d/merge_rasterizer_cell_scale"));
  61. NavigationServer3D::get_singleton()->map_set_use_edge_connections(navigation_map, GLOBAL_GET("navigation/3d/use_edge_connections"));
  62. NavigationServer3D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_GET("navigation/3d/default_edge_connection_margin"));
  63. NavigationServer3D::get_singleton()->map_set_link_connection_radius(navigation_map, GLOBAL_GET("navigation/3d/default_link_connection_radius"));
  64. }
  65. return navigation_map;
  66. }
  67. RID World3D::get_scenario() const {
  68. return scenario;
  69. }
  70. void World3D::set_environment(const Ref<Environment> &p_environment) {
  71. if (environment == p_environment) {
  72. return;
  73. }
  74. environment = p_environment;
  75. if (environment.is_valid()) {
  76. RS::get_singleton()->scenario_set_environment(scenario, environment->get_rid());
  77. } else {
  78. RS::get_singleton()->scenario_set_environment(scenario, RID());
  79. }
  80. emit_changed();
  81. }
  82. Ref<Environment> World3D::get_environment() const {
  83. return environment;
  84. }
  85. void World3D::set_fallback_environment(const Ref<Environment> &p_environment) {
  86. if (fallback_environment == p_environment) {
  87. return;
  88. }
  89. fallback_environment = p_environment;
  90. if (fallback_environment.is_valid()) {
  91. RS::get_singleton()->scenario_set_fallback_environment(scenario, p_environment->get_rid());
  92. } else {
  93. RS::get_singleton()->scenario_set_fallback_environment(scenario, RID());
  94. }
  95. emit_changed();
  96. }
  97. Ref<Environment> World3D::get_fallback_environment() const {
  98. return fallback_environment;
  99. }
  100. void World3D::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) {
  101. camera_attributes = p_camera_attributes;
  102. if (camera_attributes.is_valid()) {
  103. RS::get_singleton()->scenario_set_camera_attributes(scenario, camera_attributes->get_rid());
  104. } else {
  105. RS::get_singleton()->scenario_set_camera_attributes(scenario, RID());
  106. }
  107. }
  108. Ref<CameraAttributes> World3D::get_camera_attributes() const {
  109. return camera_attributes;
  110. }
  111. void World3D::set_compositor(const Ref<Compositor> &p_compositor) {
  112. compositor = p_compositor;
  113. if (compositor.is_valid()) {
  114. RS::get_singleton()->scenario_set_compositor(scenario, compositor->get_rid());
  115. } else {
  116. RS::get_singleton()->scenario_set_compositor(scenario, RID());
  117. }
  118. }
  119. Ref<Compositor> World3D::get_compositor() const {
  120. return compositor;
  121. }
  122. PhysicsDirectSpaceState3D *World3D::get_direct_space_state() {
  123. return PhysicsServer3D::get_singleton()->space_get_direct_state(get_space());
  124. }
  125. void World3D::_bind_methods() {
  126. ClassDB::bind_method(D_METHOD("get_space"), &World3D::get_space);
  127. ClassDB::bind_method(D_METHOD("get_navigation_map"), &World3D::get_navigation_map);
  128. ClassDB::bind_method(D_METHOD("get_scenario"), &World3D::get_scenario);
  129. ClassDB::bind_method(D_METHOD("set_environment", "env"), &World3D::set_environment);
  130. ClassDB::bind_method(D_METHOD("get_environment"), &World3D::get_environment);
  131. ClassDB::bind_method(D_METHOD("set_fallback_environment", "env"), &World3D::set_fallback_environment);
  132. ClassDB::bind_method(D_METHOD("get_fallback_environment"), &World3D::get_fallback_environment);
  133. ClassDB::bind_method(D_METHOD("set_camera_attributes", "attributes"), &World3D::set_camera_attributes);
  134. ClassDB::bind_method(D_METHOD("get_camera_attributes"), &World3D::get_camera_attributes);
  135. ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World3D::get_direct_space_state);
  136. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  137. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_fallback_environment", "get_fallback_environment");
  138. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");
  139. ADD_PROPERTY(PropertyInfo(Variant::RID, "space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_space");
  140. ADD_PROPERTY(PropertyInfo(Variant::RID, "navigation_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_navigation_map");
  141. ADD_PROPERTY(PropertyInfo(Variant::RID, "scenario", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_scenario");
  142. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectSpaceState3D", PROPERTY_USAGE_NONE), "", "get_direct_space_state");
  143. }
  144. World3D::World3D() {
  145. scenario = RenderingServer::get_singleton()->scenario_create();
  146. }
  147. World3D::~World3D() {
  148. ERR_FAIL_NULL(RenderingServer::get_singleton());
  149. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  150. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  151. RenderingServer::get_singleton()->free(scenario);
  152. if (space.is_valid()) {
  153. PhysicsServer3D::get_singleton()->free(space);
  154. }
  155. if (navigation_map.is_valid()) {
  156. NavigationServer3D::get_singleton()->free(navigation_map);
  157. }
  158. }