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