Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/3d/world_3d.cpp
9898 views
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
31
#include "world_3d.h"
32
33
#include "core/config/project_settings.h"
34
#include "scene/3d/camera_3d.h"
35
#include "scene/resources/camera_attributes.h"
36
#include "scene/resources/environment.h"
37
#ifndef NAVIGATION_3D_DISABLED
38
#include "servers/navigation_server_3d.h"
39
#endif // NAVIGATION_3D_DISABLED
40
41
void World3D::_register_camera(Camera3D *p_camera) {
42
cameras.insert(p_camera);
43
}
44
45
void World3D::_remove_camera(Camera3D *p_camera) {
46
cameras.erase(p_camera);
47
}
48
49
RID World3D::get_space() const {
50
#ifndef PHYSICS_3D_DISABLED
51
if (space.is_null()) {
52
space = PhysicsServer3D::get_singleton()->space_create();
53
PhysicsServer3D::get_singleton()->space_set_active(space, true);
54
PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_GRAVITY, GLOBAL_GET("physics/3d/default_gravity"));
55
PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR, GLOBAL_GET("physics/3d/default_gravity_vector"));
56
PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_LINEAR_DAMP, GLOBAL_GET("physics/3d/default_linear_damp"));
57
PhysicsServer3D::get_singleton()->area_set_param(space, PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP, GLOBAL_GET("physics/3d/default_angular_damp"));
58
}
59
#endif // PHYSICS_3D_DISABLED
60
return space;
61
}
62
63
#ifndef NAVIGATION_3D_DISABLED
64
RID World3D::get_navigation_map() const {
65
if (navigation_map.is_null()) {
66
navigation_map = NavigationServer3D::get_singleton()->map_create();
67
NavigationServer3D::get_singleton()->map_set_active(navigation_map, true);
68
NavigationServer3D::get_singleton()->map_set_cell_size(navigation_map, GLOBAL_GET("navigation/3d/default_cell_size"));
69
NavigationServer3D::get_singleton()->map_set_cell_height(navigation_map, GLOBAL_GET("navigation/3d/default_cell_height"));
70
NavigationServer3D::get_singleton()->map_set_up(navigation_map, GLOBAL_GET("navigation/3d/default_up"));
71
NavigationServer3D::get_singleton()->map_set_merge_rasterizer_cell_scale(navigation_map, GLOBAL_GET("navigation/3d/merge_rasterizer_cell_scale"));
72
NavigationServer3D::get_singleton()->map_set_use_edge_connections(navigation_map, GLOBAL_GET("navigation/3d/use_edge_connections"));
73
NavigationServer3D::get_singleton()->map_set_edge_connection_margin(navigation_map, GLOBAL_GET("navigation/3d/default_edge_connection_margin"));
74
NavigationServer3D::get_singleton()->map_set_link_connection_radius(navigation_map, GLOBAL_GET("navigation/3d/default_link_connection_radius"));
75
}
76
return navigation_map;
77
}
78
#endif // NAVIGATION_3D_DISABLED
79
80
RID World3D::get_scenario() const {
81
return scenario;
82
}
83
84
void World3D::set_environment(const Ref<Environment> &p_environment) {
85
if (environment == p_environment) {
86
return;
87
}
88
89
environment = p_environment;
90
if (environment.is_valid()) {
91
RS::get_singleton()->scenario_set_environment(scenario, environment->get_rid());
92
} else {
93
RS::get_singleton()->scenario_set_environment(scenario, RID());
94
}
95
96
emit_changed();
97
}
98
99
Ref<Environment> World3D::get_environment() const {
100
return environment;
101
}
102
103
void World3D::set_fallback_environment(const Ref<Environment> &p_environment) {
104
if (fallback_environment == p_environment) {
105
return;
106
}
107
108
fallback_environment = p_environment;
109
if (fallback_environment.is_valid()) {
110
RS::get_singleton()->scenario_set_fallback_environment(scenario, p_environment->get_rid());
111
} else {
112
RS::get_singleton()->scenario_set_fallback_environment(scenario, RID());
113
}
114
115
emit_changed();
116
}
117
118
Ref<Environment> World3D::get_fallback_environment() const {
119
return fallback_environment;
120
}
121
122
void World3D::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) {
123
camera_attributes = p_camera_attributes;
124
if (camera_attributes.is_valid()) {
125
RS::get_singleton()->scenario_set_camera_attributes(scenario, camera_attributes->get_rid());
126
} else {
127
RS::get_singleton()->scenario_set_camera_attributes(scenario, RID());
128
}
129
}
130
131
Ref<CameraAttributes> World3D::get_camera_attributes() const {
132
return camera_attributes;
133
}
134
135
void World3D::set_compositor(const Ref<Compositor> &p_compositor) {
136
compositor = p_compositor;
137
if (compositor.is_valid()) {
138
RS::get_singleton()->scenario_set_compositor(scenario, compositor->get_rid());
139
} else {
140
RS::get_singleton()->scenario_set_compositor(scenario, RID());
141
}
142
}
143
144
Ref<Compositor> World3D::get_compositor() const {
145
return compositor;
146
}
147
148
#ifndef PHYSICS_3D_DISABLED
149
PhysicsDirectSpaceState3D *World3D::get_direct_space_state() {
150
return PhysicsServer3D::get_singleton()->space_get_direct_state(get_space());
151
}
152
#endif // PHYSICS_3D_DISABLED
153
154
void World3D::_bind_methods() {
155
ClassDB::bind_method(D_METHOD("get_space"), &World3D::get_space);
156
#ifndef NAVIGATION_3D_DISABLED
157
ClassDB::bind_method(D_METHOD("get_navigation_map"), &World3D::get_navigation_map);
158
#endif // NAVIGATION_3D_DISABLED
159
ClassDB::bind_method(D_METHOD("get_scenario"), &World3D::get_scenario);
160
ClassDB::bind_method(D_METHOD("set_environment", "env"), &World3D::set_environment);
161
ClassDB::bind_method(D_METHOD("get_environment"), &World3D::get_environment);
162
ClassDB::bind_method(D_METHOD("set_fallback_environment", "env"), &World3D::set_fallback_environment);
163
ClassDB::bind_method(D_METHOD("get_fallback_environment"), &World3D::get_fallback_environment);
164
ClassDB::bind_method(D_METHOD("set_camera_attributes", "attributes"), &World3D::set_camera_attributes);
165
ClassDB::bind_method(D_METHOD("get_camera_attributes"), &World3D::get_camera_attributes);
166
#ifndef PHYSICS_3D_DISABLED
167
ClassDB::bind_method(D_METHOD("get_direct_space_state"), &World3D::get_direct_space_state);
168
#endif // PHYSICS_3D_DISABLED
169
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
170
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_fallback_environment", "get_fallback_environment");
171
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");
172
ADD_PROPERTY(PropertyInfo(Variant::RID, "space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_space");
173
#ifndef NAVIGATION_3D_DISABLED
174
ADD_PROPERTY(PropertyInfo(Variant::RID, "navigation_map", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_navigation_map");
175
#endif // NAVIGATION_3D_DISABLED
176
ADD_PROPERTY(PropertyInfo(Variant::RID, "scenario", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_scenario");
177
#ifndef PHYSICS_3D_DISABLED
178
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "direct_space_state", PROPERTY_HINT_RESOURCE_TYPE, "PhysicsDirectSpaceState3D", PROPERTY_USAGE_NONE), "", "get_direct_space_state");
179
#endif // PHYSICS_3D_DISABLED
180
}
181
182
World3D::World3D() {
183
scenario = RenderingServer::get_singleton()->scenario_create();
184
}
185
186
World3D::~World3D() {
187
ERR_FAIL_NULL(RenderingServer::get_singleton());
188
189
#ifndef PHYSICS_3D_DISABLED
190
ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
191
#endif // PHYSICS_3D_DISABLED
192
193
#ifndef NAVIGATION_3D_DISABLED
194
ERR_FAIL_NULL(NavigationServer3D::get_singleton());
195
#endif // NAVIGATION_3D_DISABLED
196
197
RenderingServer::get_singleton()->free(scenario);
198
199
#ifndef PHYSICS_3D_DISABLED
200
if (space.is_valid()) {
201
PhysicsServer3D::get_singleton()->free(space);
202
}
203
#endif // PHYSICS_3D_DISABLED
204
205
#ifndef NAVIGATION_3D_DISABLED
206
if (navigation_map.is_valid()) {
207
NavigationServer3D::get_singleton()->free(navigation_map);
208
}
209
#endif // NAVIGATION_3D_DISABLED
210
}
211
212