world_3d.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*************************************************************************/
  2. /* world_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_3d.h"
  31. #include "core/config/project_settings.h"
  32. #include "scene/3d/camera_3d.h"
  33. #include "scene/3d/visible_on_screen_notifier_3d.h"
  34. #include "scene/resources/camera_attributes.h"
  35. #include "scene/resources/environment.h"
  36. #include "scene/scene_string_names.h"
  37. #include "servers/navigation_server_3d.h"
  38. void World3D::_register_camera(Camera3D *p_camera) {
  39. #ifndef _3D_DISABLED
  40. cameras.insert(p_camera);
  41. #endif
  42. }
  43. void World3D::_remove_camera(Camera3D *p_camera) {
  44. #ifndef _3D_DISABLED
  45. cameras.erase(p_camera);
  46. #endif
  47. }
  48. RID World3D::get_space() const {
  49. return space;
  50. }
  51. RID World3D::get_navigation_map() const {
  52. return navigation_map;
  53. }
  54. RID World3D::get_scenario() const {
  55. return scenario;
  56. }
  57. void World3D::set_environment(const Ref<Environment> &p_environment) {
  58. if (environment == p_environment) {
  59. return;
  60. }
  61. environment = p_environment;
  62. if (environment.is_valid()) {
  63. RS::get_singleton()->scenario_set_environment(scenario, environment->get_rid());
  64. } else {
  65. RS::get_singleton()->scenario_set_environment(scenario, RID());
  66. }
  67. emit_changed();
  68. }
  69. Ref<Environment> World3D::get_environment() const {
  70. return environment;
  71. }
  72. void World3D::set_fallback_environment(const Ref<Environment> &p_environment) {
  73. if (fallback_environment == p_environment) {
  74. return;
  75. }
  76. fallback_environment = p_environment;
  77. if (fallback_environment.is_valid()) {
  78. RS::get_singleton()->scenario_set_fallback_environment(scenario, p_environment->get_rid());
  79. } else {
  80. RS::get_singleton()->scenario_set_fallback_environment(scenario, RID());
  81. }
  82. emit_changed();
  83. }
  84. Ref<Environment> World3D::get_fallback_environment() const {
  85. return fallback_environment;
  86. }
  87. void World3D::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) {
  88. camera_attributes = p_camera_attributes;
  89. if (camera_attributes.is_valid()) {
  90. RS::get_singleton()->scenario_set_camera_attributes(scenario, camera_attributes->get_rid());
  91. } else {
  92. RS::get_singleton()->scenario_set_camera_attributes(scenario, RID());
  93. }
  94. }
  95. Ref<CameraAttributes> World3D::get_camera_attributes() const {
  96. return camera_attributes;
  97. }
  98. PhysicsDirectSpaceState3D *World3D::get_direct_space_state() {
  99. return PhysicsServer3D::get_singleton()->space_get_direct_state(space);
  100. }
  101. void World3D::_bind_methods() {
  102. ClassDB::bind_method(D_METHOD("get_space"), &World3D::get_space);
  103. ClassDB::bind_method(D_METHOD("get_navigation_map"), &World3D::get_navigation_map);
  104. ClassDB::bind_method(D_METHOD("get_scenario"), &World3D::get_scenario);
  105. ClassDB::bind_method(D_METHOD("set_environment", "env"), &World3D::set_environment);
  106. ClassDB::bind_method(D_METHOD("get_environment"), &World3D::get_environment);
  107. ClassDB::bind_method(D_METHOD("set_fallback_environment", "env"), &World3D::set_fallback_environment);
  108. ClassDB::bind_method(D_METHOD("get_fallback_environment"), &World3D::get_fallback_environment);
  109. ClassDB::bind_method(D_METHOD("set_camera_attributes", "attributes"), &World3D::set_camera_attributes);
  110. ClassDB::bind_method(D_METHOD("get_camera_attributes"), &World3D::get_camera_attributes);
  111. ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World3D::get_direct_space_state);
  112. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  113. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_fallback_environment", "get_fallback_environment");
  114. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");
  115. ADD_PROPERTY(PropertyInfo(Variant::RID, "space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_space");
  116. ADD_PROPERTY(PropertyInfo(Variant::RID, "navigation_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_navigation_map");
  117. ADD_PROPERTY(PropertyInfo(Variant::RID, "scenario", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_scenario");
  118. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectSpaceState3D", PROPERTY_USAGE_NONE), "", "get_direct_space_state");
  119. }
  120. World3D::World3D() {
  121. space = PhysicsServer3D::get_singleton()->space_create();
  122. scenario = RenderingServer::get_singleton()->scenario_create();
  123. PhysicsServer3D::get_singleton()->space_set_active(space, true);
  124. PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_GRAVITY, GLOBAL_DEF_BASIC("physics/3d/default_gravity", 9.8));
  125. PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_DEF_BASIC("physics/3d/default_gravity_vector", Vector3(0, -1, 0)));
  126. PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_LINEAR_DAMP, GLOBAL_DEF("physics/3d/default_linear_damp", 0.1));
  127. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/default_linear_damp", PropertyInfo(Variant::FLOAT, "physics/3d/default_linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"));
  128. PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP, GLOBAL_DEF("physics/3d/default_angular_damp", 0.1));
  129. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/default_angular_damp", PropertyInfo(Variant::FLOAT, "physics/3d/default_angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"));
  130. navigation_map = NavigationServer3D::get_singleton()->map_create();
  131. NavigationServer3D::get_singleton()->map_set_active(navigation_map, true);
  132. NavigationServer3D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_DEF("navigation/3d/default_cell_size", 0.25));
  133. NavigationServer3D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_DEF("navigation/3d/default_edge_connection_margin", 0.25));
  134. }
  135. World3D::~World3D() {
  136. PhysicsServer3D::get_singleton()->free(space);
  137. RenderingServer::get_singleton()->free(scenario);
  138. NavigationServer3D::get_singleton()->free(navigation_map);
  139. }