1/**************************************************************************/
2/* reflection_probe.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 "reflection_probe.h"
32
33void ReflectionProbe::set_intensity(float p_intensity) {
34 intensity = p_intensity;
35 RS::get_singleton()->reflection_probe_set_intensity(probe, p_intensity);
36}
37
38float ReflectionProbe::get_intensity() const {
39 return intensity;
40}
41
42void ReflectionProbe::set_ambient_mode(AmbientMode p_mode) {
43 ambient_mode = p_mode;
44 RS::get_singleton()->reflection_probe_set_ambient_mode(probe, RS::ReflectionProbeAmbientMode(p_mode));
45 notify_property_list_changed();
46}
47
48ReflectionProbe::AmbientMode ReflectionProbe::get_ambient_mode() const {
49 return ambient_mode;
50}
51
52void ReflectionProbe::set_ambient_color(Color p_ambient) {
53 ambient_color = p_ambient;
54 RS::get_singleton()->reflection_probe_set_ambient_color(probe, p_ambient);
55}
56
57void ReflectionProbe::set_ambient_color_energy(float p_energy) {
58 ambient_color_energy = p_energy;
59 RS::get_singleton()->reflection_probe_set_ambient_energy(probe, p_energy);
60}
61
62float ReflectionProbe::get_ambient_color_energy() const {
63 return ambient_color_energy;
64}
65
66Color ReflectionProbe::get_ambient_color() const {
67 return ambient_color;
68}
69
70void ReflectionProbe::set_max_distance(float p_distance) {
71 max_distance = p_distance;
72 RS::get_singleton()->reflection_probe_set_max_distance(probe, p_distance);
73}
74
75float ReflectionProbe::get_max_distance() const {
76 return max_distance;
77}
78
79void ReflectionProbe::set_mesh_lod_threshold(float p_pixels) {
80 mesh_lod_threshold = p_pixels;
81 RS::get_singleton()->reflection_probe_set_mesh_lod_threshold(probe, p_pixels);
82}
83
84float ReflectionProbe::get_mesh_lod_threshold() const {
85 return mesh_lod_threshold;
86}
87
88void ReflectionProbe::set_size(const Vector3 &p_size) {
89 size = p_size;
90
91 for (int i = 0; i < 3; i++) {
92 float half_size = size[i] / 2;
93 if (half_size < 0.01) {
94 half_size = 0.01;
95 }
96
97 if (half_size - 0.01 < ABS(origin_offset[i])) {
98 origin_offset[i] = SIGN(origin_offset[i]) * (half_size - 0.01);
99 }
100 }
101
102 RS::get_singleton()->reflection_probe_set_size(probe, size);
103 RS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
104
105 update_gizmos();
106}
107
108Vector3 ReflectionProbe::get_size() const {
109 return size;
110}
111
112void ReflectionProbe::set_origin_offset(const Vector3 &p_offset) {
113 origin_offset = p_offset;
114
115 for (int i = 0; i < 3; i++) {
116 float half_size = size[i] / 2;
117 if (half_size - 0.01 < ABS(origin_offset[i])) {
118 origin_offset[i] = SIGN(origin_offset[i]) * (half_size - 0.01);
119 }
120 }
121 RS::get_singleton()->reflection_probe_set_size(probe, size);
122 RS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
123
124 update_gizmos();
125}
126
127Vector3 ReflectionProbe::get_origin_offset() const {
128 return origin_offset;
129}
130
131void ReflectionProbe::set_enable_box_projection(bool p_enable) {
132 box_projection = p_enable;
133 RS::get_singleton()->reflection_probe_set_enable_box_projection(probe, p_enable);
134}
135
136bool ReflectionProbe::is_box_projection_enabled() const {
137 return box_projection;
138}
139
140void ReflectionProbe::set_as_interior(bool p_enable) {
141 interior = p_enable;
142 RS::get_singleton()->reflection_probe_set_as_interior(probe, interior);
143}
144
145bool ReflectionProbe::is_set_as_interior() const {
146 return interior;
147}
148
149void ReflectionProbe::set_enable_shadows(bool p_enable) {
150 enable_shadows = p_enable;
151 RS::get_singleton()->reflection_probe_set_enable_shadows(probe, p_enable);
152}
153
154bool ReflectionProbe::are_shadows_enabled() const {
155 return enable_shadows;
156}
157
158void ReflectionProbe::set_cull_mask(uint32_t p_layers) {
159 cull_mask = p_layers;
160 RS::get_singleton()->reflection_probe_set_cull_mask(probe, p_layers);
161}
162
163uint32_t ReflectionProbe::get_cull_mask() const {
164 return cull_mask;
165}
166
167void ReflectionProbe::set_update_mode(UpdateMode p_mode) {
168 update_mode = p_mode;
169 RS::get_singleton()->reflection_probe_set_update_mode(probe, RS::ReflectionProbeUpdateMode(p_mode));
170}
171
172ReflectionProbe::UpdateMode ReflectionProbe::get_update_mode() const {
173 return update_mode;
174}
175
176AABB ReflectionProbe::get_aabb() const {
177 AABB aabb;
178 aabb.position = -origin_offset;
179 aabb.size = origin_offset + size / 2;
180 return aabb;
181}
182
183PackedStringArray ReflectionProbe::get_configuration_warnings() const {
184 PackedStringArray warnings = Node::get_configuration_warnings();
185
186 if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
187 warnings.push_back(RTR("ReflectionProbes are not supported when using the GL Compatibility backend yet. Support will be added in a future release."));
188 return warnings;
189 }
190
191 return warnings;
192}
193
194void ReflectionProbe::_validate_property(PropertyInfo &p_property) const {
195 if (p_property.name == "ambient_color" || p_property.name == "ambient_color_energy") {
196 if (ambient_mode != AMBIENT_COLOR) {
197 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
198 }
199 }
200}
201
202void ReflectionProbe::_bind_methods() {
203 ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &ReflectionProbe::set_intensity);
204 ClassDB::bind_method(D_METHOD("get_intensity"), &ReflectionProbe::get_intensity);
205
206 ClassDB::bind_method(D_METHOD("set_ambient_mode", "ambient"), &ReflectionProbe::set_ambient_mode);
207 ClassDB::bind_method(D_METHOD("get_ambient_mode"), &ReflectionProbe::get_ambient_mode);
208
209 ClassDB::bind_method(D_METHOD("set_ambient_color", "ambient"), &ReflectionProbe::set_ambient_color);
210 ClassDB::bind_method(D_METHOD("get_ambient_color"), &ReflectionProbe::get_ambient_color);
211
212 ClassDB::bind_method(D_METHOD("set_ambient_color_energy", "ambient_energy"), &ReflectionProbe::set_ambient_color_energy);
213 ClassDB::bind_method(D_METHOD("get_ambient_color_energy"), &ReflectionProbe::get_ambient_color_energy);
214
215 ClassDB::bind_method(D_METHOD("set_max_distance", "max_distance"), &ReflectionProbe::set_max_distance);
216 ClassDB::bind_method(D_METHOD("get_max_distance"), &ReflectionProbe::get_max_distance);
217
218 ClassDB::bind_method(D_METHOD("set_mesh_lod_threshold", "ratio"), &ReflectionProbe::set_mesh_lod_threshold);
219 ClassDB::bind_method(D_METHOD("get_mesh_lod_threshold"), &ReflectionProbe::get_mesh_lod_threshold);
220
221 ClassDB::bind_method(D_METHOD("set_size", "size"), &ReflectionProbe::set_size);
222 ClassDB::bind_method(D_METHOD("get_size"), &ReflectionProbe::get_size);
223
224 ClassDB::bind_method(D_METHOD("set_origin_offset", "origin_offset"), &ReflectionProbe::set_origin_offset);
225 ClassDB::bind_method(D_METHOD("get_origin_offset"), &ReflectionProbe::get_origin_offset);
226
227 ClassDB::bind_method(D_METHOD("set_as_interior", "enable"), &ReflectionProbe::set_as_interior);
228 ClassDB::bind_method(D_METHOD("is_set_as_interior"), &ReflectionProbe::is_set_as_interior);
229
230 ClassDB::bind_method(D_METHOD("set_enable_box_projection", "enable"), &ReflectionProbe::set_enable_box_projection);
231 ClassDB::bind_method(D_METHOD("is_box_projection_enabled"), &ReflectionProbe::is_box_projection_enabled);
232
233 ClassDB::bind_method(D_METHOD("set_enable_shadows", "enable"), &ReflectionProbe::set_enable_shadows);
234 ClassDB::bind_method(D_METHOD("are_shadows_enabled"), &ReflectionProbe::are_shadows_enabled);
235
236 ClassDB::bind_method(D_METHOD("set_cull_mask", "layers"), &ReflectionProbe::set_cull_mask);
237 ClassDB::bind_method(D_METHOD("get_cull_mask"), &ReflectionProbe::get_cull_mask);
238
239 ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &ReflectionProbe::set_update_mode);
240 ClassDB::bind_method(D_METHOD("get_update_mode"), &ReflectionProbe::get_update_mode);
241
242 ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Once (Fast),Always (Slow)"), "set_update_mode", "get_update_mode");
243 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "intensity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_intensity", "get_intensity");
244 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_distance", PROPERTY_HINT_RANGE, "0,16384,0.1,or_greater,exp,suffix:m"), "set_max_distance", "get_max_distance");
245 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
246 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "origin_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_origin_offset", "get_origin_offset");
247 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "box_projection"), "set_enable_box_projection", "is_box_projection_enabled");
248 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_as_interior", "is_set_as_interior");
249 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_shadows"), "set_enable_shadows", "are_shadows_enabled");
250 ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
251 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mesh_lod_threshold", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_mesh_lod_threshold", "get_mesh_lod_threshold");
252
253 ADD_GROUP("Ambient", "ambient_");
254 ADD_PROPERTY(PropertyInfo(Variant::INT, "ambient_mode", PROPERTY_HINT_ENUM, "Disabled,Environment,Constant Color"), "set_ambient_mode", "get_ambient_mode");
255 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ambient_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_ambient_color", "get_ambient_color");
256 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ambient_color_energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_ambient_color_energy", "get_ambient_color_energy");
257
258 BIND_ENUM_CONSTANT(UPDATE_ONCE);
259 BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
260
261 BIND_ENUM_CONSTANT(AMBIENT_DISABLED);
262 BIND_ENUM_CONSTANT(AMBIENT_ENVIRONMENT);
263 BIND_ENUM_CONSTANT(AMBIENT_COLOR);
264}
265
266#ifndef DISABLE_DEPRECATED
267bool ReflectionProbe::_set(const StringName &p_name, const Variant &p_value) {
268 if (p_name == "extents") { // Compatibility with Godot 3.x.
269 set_size((Vector3)p_value * 2);
270 return true;
271 }
272 return false;
273}
274
275bool ReflectionProbe::_get(const StringName &p_name, Variant &r_property) const {
276 if (p_name == "extents") { // Compatibility with Godot 3.x.
277 r_property = size / 2;
278 return true;
279 }
280 return false;
281}
282#endif // DISABLE_DEPRECATED
283
284ReflectionProbe::ReflectionProbe() {
285 probe = RenderingServer::get_singleton()->reflection_probe_create();
286 RS::get_singleton()->instance_set_base(get_instance(), probe);
287 set_disable_scale(true);
288}
289
290ReflectionProbe::~ReflectionProbe() {
291 ERR_FAIL_NULL(RenderingServer::get_singleton());
292 RS::get_singleton()->free(probe);
293}
294