1 /*************************************************************************/
2 /*  reflection_probe.cpp                                                 */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #include "reflection_probe.h"
32 
set_intensity(float p_intensity)33 void ReflectionProbe::set_intensity(float p_intensity) {
34 
35 	intensity = p_intensity;
36 	VS::get_singleton()->reflection_probe_set_intensity(probe, p_intensity);
37 }
38 
get_intensity() const39 float ReflectionProbe::get_intensity() const {
40 
41 	return intensity;
42 }
43 
set_interior_ambient(Color p_ambient)44 void ReflectionProbe::set_interior_ambient(Color p_ambient) {
45 
46 	interior_ambient = p_ambient;
47 	VS::get_singleton()->reflection_probe_set_interior_ambient(probe, p_ambient);
48 }
49 
set_interior_ambient_energy(float p_energy)50 void ReflectionProbe::set_interior_ambient_energy(float p_energy) {
51 	interior_ambient_energy = p_energy;
52 	VS::get_singleton()->reflection_probe_set_interior_ambient_energy(probe, p_energy);
53 }
54 
get_interior_ambient_energy() const55 float ReflectionProbe::get_interior_ambient_energy() const {
56 	return interior_ambient_energy;
57 }
58 
get_interior_ambient() const59 Color ReflectionProbe::get_interior_ambient() const {
60 
61 	return interior_ambient;
62 }
63 
set_interior_ambient_probe_contribution(float p_contribution)64 void ReflectionProbe::set_interior_ambient_probe_contribution(float p_contribution) {
65 
66 	interior_ambient_probe_contribution = p_contribution;
67 	VS::get_singleton()->reflection_probe_set_interior_ambient_probe_contribution(probe, p_contribution);
68 }
69 
get_interior_ambient_probe_contribution() const70 float ReflectionProbe::get_interior_ambient_probe_contribution() const {
71 
72 	return interior_ambient_probe_contribution;
73 }
74 
set_max_distance(float p_distance)75 void ReflectionProbe::set_max_distance(float p_distance) {
76 
77 	max_distance = p_distance;
78 	VS::get_singleton()->reflection_probe_set_max_distance(probe, p_distance);
79 }
get_max_distance() const80 float ReflectionProbe::get_max_distance() const {
81 
82 	return max_distance;
83 }
84 
set_extents(const Vector3 & p_extents)85 void ReflectionProbe::set_extents(const Vector3 &p_extents) {
86 
87 	extents = p_extents;
88 
89 	for (int i = 0; i < 3; i++) {
90 		if (extents[i] < 0.01) {
91 			extents[i] = 0.01;
92 		}
93 
94 		if (extents[i] - 0.01 < ABS(origin_offset[i])) {
95 			origin_offset[i] = SGN(origin_offset[i]) * (extents[i] - 0.01);
96 			_change_notify("origin_offset");
97 		}
98 	}
99 
100 	VS::get_singleton()->reflection_probe_set_extents(probe, extents);
101 	VS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
102 	_change_notify("extents");
103 	update_gizmo();
104 }
get_extents() const105 Vector3 ReflectionProbe::get_extents() const {
106 
107 	return extents;
108 }
109 
set_origin_offset(const Vector3 & p_extents)110 void ReflectionProbe::set_origin_offset(const Vector3 &p_extents) {
111 
112 	origin_offset = p_extents;
113 
114 	for (int i = 0; i < 3; i++) {
115 
116 		if (extents[i] - 0.01 < ABS(origin_offset[i])) {
117 			origin_offset[i] = SGN(origin_offset[i]) * (extents[i] - 0.01);
118 		}
119 	}
120 	VS::get_singleton()->reflection_probe_set_extents(probe, extents);
121 	VS::get_singleton()->reflection_probe_set_origin_offset(probe, origin_offset);
122 
123 	_change_notify("origin_offset");
124 	update_gizmo();
125 }
get_origin_offset() const126 Vector3 ReflectionProbe::get_origin_offset() const {
127 
128 	return origin_offset;
129 }
130 
set_enable_box_projection(bool p_enable)131 void ReflectionProbe::set_enable_box_projection(bool p_enable) {
132 
133 	box_projection = p_enable;
134 	VS::get_singleton()->reflection_probe_set_enable_box_projection(probe, p_enable);
135 }
is_box_projection_enabled() const136 bool ReflectionProbe::is_box_projection_enabled() const {
137 
138 	return box_projection;
139 }
140 
set_as_interior(bool p_enable)141 void ReflectionProbe::set_as_interior(bool p_enable) {
142 
143 	interior = p_enable;
144 	VS::get_singleton()->reflection_probe_set_as_interior(probe, interior);
145 	_change_notify();
146 }
147 
is_set_as_interior() const148 bool ReflectionProbe::is_set_as_interior() const {
149 
150 	return interior;
151 }
152 
set_enable_shadows(bool p_enable)153 void ReflectionProbe::set_enable_shadows(bool p_enable) {
154 
155 	enable_shadows = p_enable;
156 	VS::get_singleton()->reflection_probe_set_enable_shadows(probe, p_enable);
157 }
are_shadows_enabled() const158 bool ReflectionProbe::are_shadows_enabled() const {
159 
160 	return enable_shadows;
161 }
162 
set_cull_mask(uint32_t p_layers)163 void ReflectionProbe::set_cull_mask(uint32_t p_layers) {
164 
165 	cull_mask = p_layers;
166 	VS::get_singleton()->reflection_probe_set_cull_mask(probe, p_layers);
167 }
get_cull_mask() const168 uint32_t ReflectionProbe::get_cull_mask() const {
169 
170 	return cull_mask;
171 }
172 
set_update_mode(UpdateMode p_mode)173 void ReflectionProbe::set_update_mode(UpdateMode p_mode) {
174 	update_mode = p_mode;
175 	VS::get_singleton()->reflection_probe_set_update_mode(probe, VS::ReflectionProbeUpdateMode(p_mode));
176 }
177 
get_update_mode() const178 ReflectionProbe::UpdateMode ReflectionProbe::get_update_mode() const {
179 	return update_mode;
180 }
181 
get_aabb() const182 AABB ReflectionProbe::get_aabb() const {
183 
184 	AABB aabb;
185 	aabb.position = -origin_offset;
186 	aabb.size = origin_offset + extents;
187 	return aabb;
188 }
get_faces(uint32_t p_usage_flags) const189 PoolVector<Face3> ReflectionProbe::get_faces(uint32_t p_usage_flags) const {
190 
191 	return PoolVector<Face3>();
192 }
193 
_validate_property(PropertyInfo & property) const194 void ReflectionProbe::_validate_property(PropertyInfo &property) const {
195 
196 	if (property.name == "interior/ambient_color" || property.name == "interior/ambient_energy" || property.name == "interior/ambient_contrib") {
197 		if (!interior) {
198 			property.usage = PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL;
199 		}
200 	}
201 }
202 
_bind_methods()203 void ReflectionProbe::_bind_methods() {
204 
205 	ClassDB::bind_method(D_METHOD("set_intensity", "intensity"), &ReflectionProbe::set_intensity);
206 	ClassDB::bind_method(D_METHOD("get_intensity"), &ReflectionProbe::get_intensity);
207 
208 	ClassDB::bind_method(D_METHOD("set_interior_ambient", "ambient"), &ReflectionProbe::set_interior_ambient);
209 	ClassDB::bind_method(D_METHOD("get_interior_ambient"), &ReflectionProbe::get_interior_ambient);
210 
211 	ClassDB::bind_method(D_METHOD("set_interior_ambient_energy", "ambient_energy"), &ReflectionProbe::set_interior_ambient_energy);
212 	ClassDB::bind_method(D_METHOD("get_interior_ambient_energy"), &ReflectionProbe::get_interior_ambient_energy);
213 
214 	ClassDB::bind_method(D_METHOD("set_interior_ambient_probe_contribution", "ambient_probe_contribution"), &ReflectionProbe::set_interior_ambient_probe_contribution);
215 	ClassDB::bind_method(D_METHOD("get_interior_ambient_probe_contribution"), &ReflectionProbe::get_interior_ambient_probe_contribution);
216 
217 	ClassDB::bind_method(D_METHOD("set_max_distance", "max_distance"), &ReflectionProbe::set_max_distance);
218 	ClassDB::bind_method(D_METHOD("get_max_distance"), &ReflectionProbe::get_max_distance);
219 
220 	ClassDB::bind_method(D_METHOD("set_extents", "extents"), &ReflectionProbe::set_extents);
221 	ClassDB::bind_method(D_METHOD("get_extents"), &ReflectionProbe::get_extents);
222 
223 	ClassDB::bind_method(D_METHOD("set_origin_offset", "origin_offset"), &ReflectionProbe::set_origin_offset);
224 	ClassDB::bind_method(D_METHOD("get_origin_offset"), &ReflectionProbe::get_origin_offset);
225 
226 	ClassDB::bind_method(D_METHOD("set_as_interior", "enable"), &ReflectionProbe::set_as_interior);
227 	ClassDB::bind_method(D_METHOD("is_set_as_interior"), &ReflectionProbe::is_set_as_interior);
228 
229 	ClassDB::bind_method(D_METHOD("set_enable_box_projection", "enable"), &ReflectionProbe::set_enable_box_projection);
230 	ClassDB::bind_method(D_METHOD("is_box_projection_enabled"), &ReflectionProbe::is_box_projection_enabled);
231 
232 	ClassDB::bind_method(D_METHOD("set_enable_shadows", "enable"), &ReflectionProbe::set_enable_shadows);
233 	ClassDB::bind_method(D_METHOD("are_shadows_enabled"), &ReflectionProbe::are_shadows_enabled);
234 
235 	ClassDB::bind_method(D_METHOD("set_cull_mask", "layers"), &ReflectionProbe::set_cull_mask);
236 	ClassDB::bind_method(D_METHOD("get_cull_mask"), &ReflectionProbe::get_cull_mask);
237 
238 	ClassDB::bind_method(D_METHOD("set_update_mode", "mode"), &ReflectionProbe::set_update_mode);
239 	ClassDB::bind_method(D_METHOD("get_update_mode"), &ReflectionProbe::get_update_mode);
240 
241 	ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Once,Always"), "set_update_mode", "get_update_mode");
242 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "intensity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_intensity", "get_intensity");
243 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_distance", PROPERTY_HINT_EXP_RANGE, "0,16384,0.1,or_greater"), "set_max_distance", "get_max_distance");
244 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents"), "set_extents", "get_extents");
245 	ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "origin_offset"), "set_origin_offset", "get_origin_offset");
246 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "box_projection"), "set_enable_box_projection", "is_box_projection_enabled");
247 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_shadows"), "set_enable_shadows", "are_shadows_enabled");
248 	ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
249 
250 	ADD_GROUP("Interior", "interior_");
251 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior_enable"), "set_as_interior", "is_set_as_interior");
252 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "interior_ambient_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_interior_ambient", "get_interior_ambient");
253 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "interior_ambient_energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_interior_ambient_energy", "get_interior_ambient_energy");
254 	ADD_PROPERTY(PropertyInfo(Variant::REAL, "interior_ambient_contrib", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_interior_ambient_probe_contribution", "get_interior_ambient_probe_contribution");
255 
256 	BIND_ENUM_CONSTANT(UPDATE_ONCE);
257 	BIND_ENUM_CONSTANT(UPDATE_ALWAYS);
258 }
259 
ReflectionProbe()260 ReflectionProbe::ReflectionProbe() {
261 
262 	intensity = 1.0;
263 	interior_ambient = Color(0, 0, 0);
264 	interior_ambient_probe_contribution = 0;
265 	interior_ambient_energy = 1.0;
266 	max_distance = 0;
267 	extents = Vector3(1, 1, 1);
268 	origin_offset = Vector3(0, 0, 0);
269 	box_projection = false;
270 	interior = false;
271 	enable_shadows = false;
272 	cull_mask = (1 << 20) - 1;
273 	update_mode = UPDATE_ONCE;
274 
275 	probe = VisualServer::get_singleton()->reflection_probe_create();
276 	VS::get_singleton()->instance_set_base(get_instance(), probe);
277 	set_disable_scale(true);
278 }
279 
~ReflectionProbe()280 ReflectionProbe::~ReflectionProbe() {
281 
282 	VS::get_singleton()->free(probe);
283 }
284