| 1 | /**************************************************************************/ |
| 2 | /* sky_material.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 "sky_material.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/version.h" |
| 35 | |
| 36 | Mutex ProceduralSkyMaterial::shader_mutex; |
| 37 | RID ProceduralSkyMaterial::shader_cache[4]; |
| 38 | |
| 39 | void ProceduralSkyMaterial::set_sky_top_color(const Color &p_sky_top) { |
| 40 | sky_top_color = p_sky_top; |
| 41 | RS::get_singleton()->material_set_param(_get_material(), "sky_top_color", sky_top_color * sky_energy_multiplier); |
| 42 | } |
| 43 | |
| 44 | Color ProceduralSkyMaterial::get_sky_top_color() const { |
| 45 | return sky_top_color; |
| 46 | } |
| 47 | |
| 48 | void ProceduralSkyMaterial::set_sky_horizon_color(const Color &p_sky_horizon) { |
| 49 | sky_horizon_color = p_sky_horizon; |
| 50 | RS::get_singleton()->material_set_param(_get_material(), "sky_horizon_color", sky_horizon_color * sky_energy_multiplier); |
| 51 | } |
| 52 | |
| 53 | Color ProceduralSkyMaterial::get_sky_horizon_color() const { |
| 54 | return sky_horizon_color; |
| 55 | } |
| 56 | |
| 57 | void ProceduralSkyMaterial::set_sky_curve(float p_curve) { |
| 58 | sky_curve = p_curve; |
| 59 | // Actual curve passed to shader includes an ad hoc adjustment because the curve used to be |
| 60 | // in calculated in angles and now uses cosines. |
| 61 | RS::get_singleton()->material_set_param(_get_material(), "inv_sky_curve", 0.6 / sky_curve); |
| 62 | } |
| 63 | |
| 64 | float ProceduralSkyMaterial::get_sky_curve() const { |
| 65 | return sky_curve; |
| 66 | } |
| 67 | |
| 68 | void ProceduralSkyMaterial::set_sky_energy_multiplier(float p_multiplier) { |
| 69 | sky_energy_multiplier = p_multiplier; |
| 70 | RS::get_singleton()->material_set_param(_get_material(), "sky_top_color", sky_top_color * sky_energy_multiplier); |
| 71 | RS::get_singleton()->material_set_param(_get_material(), "sky_horizon_color", sky_horizon_color * sky_energy_multiplier); |
| 72 | RS::get_singleton()->material_set_param(_get_material(), "sky_cover_modulate", Color(sky_cover_modulate.r, sky_cover_modulate.g, sky_cover_modulate.b, sky_cover_modulate.a * sky_energy_multiplier)); |
| 73 | } |
| 74 | |
| 75 | float ProceduralSkyMaterial::get_sky_energy_multiplier() const { |
| 76 | return sky_energy_multiplier; |
| 77 | } |
| 78 | |
| 79 | void ProceduralSkyMaterial::set_sky_cover(const Ref<Texture2D> &p_sky_cover) { |
| 80 | sky_cover = p_sky_cover; |
| 81 | |
| 82 | if (p_sky_cover.is_valid()) { |
| 83 | RS::get_singleton()->material_set_param(_get_material(), "sky_cover", p_sky_cover->get_rid()); |
| 84 | } else { |
| 85 | RS::get_singleton()->material_set_param(_get_material(), "sky_cover", Variant()); |
| 86 | } |
| 87 | |
| 88 | _update_shader(use_debanding, sky_cover.is_valid()); |
| 89 | |
| 90 | if (shader_set) { |
| 91 | RS::get_singleton()->material_set_shader(_get_material(), get_shader_cache()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | Ref<Texture2D> ProceduralSkyMaterial::get_sky_cover() const { |
| 96 | return sky_cover; |
| 97 | } |
| 98 | |
| 99 | void ProceduralSkyMaterial::set_sky_cover_modulate(const Color &p_sky_cover_modulate) { |
| 100 | sky_cover_modulate = p_sky_cover_modulate; |
| 101 | RS::get_singleton()->material_set_param(_get_material(), "sky_cover_modulate", Color(sky_cover_modulate.r, sky_cover_modulate.g, sky_cover_modulate.b, sky_cover_modulate.a * sky_energy_multiplier)); |
| 102 | } |
| 103 | |
| 104 | Color ProceduralSkyMaterial::get_sky_cover_modulate() const { |
| 105 | return sky_cover_modulate; |
| 106 | } |
| 107 | |
| 108 | void ProceduralSkyMaterial::set_ground_bottom_color(const Color &p_ground_bottom) { |
| 109 | ground_bottom_color = p_ground_bottom; |
| 110 | RS::get_singleton()->material_set_param(_get_material(), "ground_bottom_color", ground_bottom_color * ground_energy_multiplier); |
| 111 | } |
| 112 | |
| 113 | Color ProceduralSkyMaterial::get_ground_bottom_color() const { |
| 114 | return ground_bottom_color; |
| 115 | } |
| 116 | |
| 117 | void ProceduralSkyMaterial::set_ground_horizon_color(const Color &p_ground_horizon) { |
| 118 | ground_horizon_color = p_ground_horizon; |
| 119 | RS::get_singleton()->material_set_param(_get_material(), "ground_horizon_color", ground_horizon_color * ground_energy_multiplier); |
| 120 | } |
| 121 | |
| 122 | Color ProceduralSkyMaterial::get_ground_horizon_color() const { |
| 123 | return ground_horizon_color; |
| 124 | } |
| 125 | |
| 126 | void ProceduralSkyMaterial::set_ground_curve(float p_curve) { |
| 127 | ground_curve = p_curve; |
| 128 | // Actual curve passed to shader includes an ad hoc adjustment because the curve used to be |
| 129 | // in calculated in angles and now uses cosines. |
| 130 | RS::get_singleton()->material_set_param(_get_material(), "inv_ground_curve", 0.6 / ground_curve); |
| 131 | } |
| 132 | |
| 133 | float ProceduralSkyMaterial::get_ground_curve() const { |
| 134 | return ground_curve; |
| 135 | } |
| 136 | |
| 137 | void ProceduralSkyMaterial::set_ground_energy_multiplier(float p_multiplier) { |
| 138 | ground_energy_multiplier = p_multiplier; |
| 139 | RS::get_singleton()->material_set_param(_get_material(), "ground_bottom_color", ground_bottom_color * ground_energy_multiplier); |
| 140 | RS::get_singleton()->material_set_param(_get_material(), "ground_horizon_color", ground_horizon_color * ground_energy_multiplier); |
| 141 | } |
| 142 | |
| 143 | float ProceduralSkyMaterial::get_ground_energy_multiplier() const { |
| 144 | return ground_energy_multiplier; |
| 145 | } |
| 146 | |
| 147 | void ProceduralSkyMaterial::set_sun_angle_max(float p_angle) { |
| 148 | sun_angle_max = p_angle; |
| 149 | RS::get_singleton()->material_set_param(_get_material(), "sun_angle_max", Math::cos(Math::deg_to_rad(sun_angle_max))); |
| 150 | } |
| 151 | |
| 152 | float ProceduralSkyMaterial::get_sun_angle_max() const { |
| 153 | return sun_angle_max; |
| 154 | } |
| 155 | |
| 156 | void ProceduralSkyMaterial::set_sun_curve(float p_curve) { |
| 157 | sun_curve = p_curve; |
| 158 | // Actual curve passed to shader includes an ad hoc adjustment because the curve used to be |
| 159 | // in calculated in angles and now uses cosines. |
| 160 | RS::get_singleton()->material_set_param(_get_material(), "inv_sun_curve", 1.6f / Math::pow(sun_curve, 1.4f)); |
| 161 | } |
| 162 | |
| 163 | float ProceduralSkyMaterial::get_sun_curve() const { |
| 164 | return sun_curve; |
| 165 | } |
| 166 | |
| 167 | void ProceduralSkyMaterial::set_use_debanding(bool p_use_debanding) { |
| 168 | use_debanding = p_use_debanding; |
| 169 | _update_shader(use_debanding, sky_cover.is_valid()); |
| 170 | // Only set if shader already compiled |
| 171 | if (shader_set) { |
| 172 | RS::get_singleton()->material_set_shader(_get_material(), get_shader_cache()); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | bool ProceduralSkyMaterial::get_use_debanding() const { |
| 177 | return use_debanding; |
| 178 | } |
| 179 | |
| 180 | void ProceduralSkyMaterial::set_energy_multiplier(float p_multiplier) { |
| 181 | global_energy_multiplier = p_multiplier; |
| 182 | RS::get_singleton()->material_set_param(_get_material(), "exposure", global_energy_multiplier); |
| 183 | } |
| 184 | |
| 185 | float ProceduralSkyMaterial::get_energy_multiplier() const { |
| 186 | return global_energy_multiplier; |
| 187 | } |
| 188 | |
| 189 | Shader::Mode ProceduralSkyMaterial::get_shader_mode() const { |
| 190 | return Shader::MODE_SKY; |
| 191 | } |
| 192 | |
| 193 | // Internal function to grab the current shader RID. |
| 194 | // Must only be called if the shader is initialized. |
| 195 | RID ProceduralSkyMaterial::get_shader_cache() const { |
| 196 | return shader_cache[int(use_debanding) + (sky_cover.is_valid() ? 2 : 0)]; |
| 197 | } |
| 198 | |
| 199 | RID ProceduralSkyMaterial::get_rid() const { |
| 200 | _update_shader(use_debanding, sky_cover.is_valid()); |
| 201 | if (!shader_set) { |
| 202 | RS::get_singleton()->material_set_shader(_get_material(), get_shader_cache()); |
| 203 | shader_set = true; |
| 204 | } |
| 205 | return _get_material(); |
| 206 | } |
| 207 | |
| 208 | RID ProceduralSkyMaterial::get_shader_rid() const { |
| 209 | _update_shader(use_debanding, sky_cover.is_valid()); |
| 210 | return get_shader_cache(); |
| 211 | } |
| 212 | |
| 213 | void ProceduralSkyMaterial::_validate_property(PropertyInfo &p_property) const { |
| 214 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 215 | return; |
| 216 | } |
| 217 | if ((p_property.name == "sky_luminance" || p_property.name == "ground_luminance") && !GLOBAL_GET_CACHED(bool, "rendering/lights_and_shadows/use_physical_light_units")) { |
| 218 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | void ProceduralSkyMaterial::_bind_methods() { |
| 223 | ClassDB::bind_method(D_METHOD("set_sky_top_color", "color"), &ProceduralSkyMaterial::set_sky_top_color); |
| 224 | ClassDB::bind_method(D_METHOD("get_sky_top_color"), &ProceduralSkyMaterial::get_sky_top_color); |
| 225 | |
| 226 | ClassDB::bind_method(D_METHOD("set_sky_horizon_color", "color"), &ProceduralSkyMaterial::set_sky_horizon_color); |
| 227 | ClassDB::bind_method(D_METHOD("get_sky_horizon_color"), &ProceduralSkyMaterial::get_sky_horizon_color); |
| 228 | |
| 229 | ClassDB::bind_method(D_METHOD("set_sky_curve", "curve"), &ProceduralSkyMaterial::set_sky_curve); |
| 230 | ClassDB::bind_method(D_METHOD("get_sky_curve"), &ProceduralSkyMaterial::get_sky_curve); |
| 231 | |
| 232 | ClassDB::bind_method(D_METHOD("set_sky_energy_multiplier", "multiplier"), &ProceduralSkyMaterial::set_sky_energy_multiplier); |
| 233 | ClassDB::bind_method(D_METHOD("get_sky_energy_multiplier"), &ProceduralSkyMaterial::get_sky_energy_multiplier); |
| 234 | |
| 235 | ClassDB::bind_method(D_METHOD("set_sky_cover", "sky_cover"), &ProceduralSkyMaterial::set_sky_cover); |
| 236 | ClassDB::bind_method(D_METHOD("get_sky_cover"), &ProceduralSkyMaterial::get_sky_cover); |
| 237 | |
| 238 | ClassDB::bind_method(D_METHOD("set_sky_cover_modulate", "color"), &ProceduralSkyMaterial::set_sky_cover_modulate); |
| 239 | ClassDB::bind_method(D_METHOD("get_sky_cover_modulate"), &ProceduralSkyMaterial::get_sky_cover_modulate); |
| 240 | |
| 241 | ClassDB::bind_method(D_METHOD("set_ground_bottom_color", "color"), &ProceduralSkyMaterial::set_ground_bottom_color); |
| 242 | ClassDB::bind_method(D_METHOD("get_ground_bottom_color"), &ProceduralSkyMaterial::get_ground_bottom_color); |
| 243 | |
| 244 | ClassDB::bind_method(D_METHOD("set_ground_horizon_color", "color"), &ProceduralSkyMaterial::set_ground_horizon_color); |
| 245 | ClassDB::bind_method(D_METHOD("get_ground_horizon_color"), &ProceduralSkyMaterial::get_ground_horizon_color); |
| 246 | |
| 247 | ClassDB::bind_method(D_METHOD("set_ground_curve", "curve"), &ProceduralSkyMaterial::set_ground_curve); |
| 248 | ClassDB::bind_method(D_METHOD("get_ground_curve"), &ProceduralSkyMaterial::get_ground_curve); |
| 249 | |
| 250 | ClassDB::bind_method(D_METHOD("set_ground_energy_multiplier", "energy"), &ProceduralSkyMaterial::set_ground_energy_multiplier); |
| 251 | ClassDB::bind_method(D_METHOD("get_ground_energy_multiplier"), &ProceduralSkyMaterial::get_ground_energy_multiplier); |
| 252 | |
| 253 | ClassDB::bind_method(D_METHOD("set_sun_angle_max", "degrees"), &ProceduralSkyMaterial::set_sun_angle_max); |
| 254 | ClassDB::bind_method(D_METHOD("get_sun_angle_max"), &ProceduralSkyMaterial::get_sun_angle_max); |
| 255 | |
| 256 | ClassDB::bind_method(D_METHOD("set_sun_curve", "curve"), &ProceduralSkyMaterial::set_sun_curve); |
| 257 | ClassDB::bind_method(D_METHOD("get_sun_curve"), &ProceduralSkyMaterial::get_sun_curve); |
| 258 | |
| 259 | ClassDB::bind_method(D_METHOD("set_use_debanding", "use_debanding"), &ProceduralSkyMaterial::set_use_debanding); |
| 260 | ClassDB::bind_method(D_METHOD("get_use_debanding"), &ProceduralSkyMaterial::get_use_debanding); |
| 261 | |
| 262 | ClassDB::bind_method(D_METHOD("set_energy_multiplier", "multiplier"), &ProceduralSkyMaterial::set_energy_multiplier); |
| 263 | ClassDB::bind_method(D_METHOD("get_energy_multiplier"), &ProceduralSkyMaterial::get_energy_multiplier); |
| 264 | |
| 265 | ADD_GROUP("Sky", "sky_"); |
| 266 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_top_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_sky_top_color", "get_sky_top_color"); |
| 267 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_horizon_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_sky_horizon_color", "get_sky_horizon_color"); |
| 268 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sky_curve", PROPERTY_HINT_EXP_EASING), "set_sky_curve", "get_sky_curve"); |
| 269 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sky_energy_multiplier", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_sky_energy_multiplier", "get_sky_energy_multiplier"); |
| 270 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "sky_cover", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_sky_cover", "get_sky_cover"); |
| 271 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "sky_cover_modulate"), "set_sky_cover_modulate", "get_sky_cover_modulate"); |
| 272 | |
| 273 | ADD_GROUP("Ground", "ground_"); |
| 274 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_bottom_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_ground_bottom_color", "get_ground_bottom_color"); |
| 275 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_horizon_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_ground_horizon_color", "get_ground_horizon_color"); |
| 276 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ground_curve", PROPERTY_HINT_EXP_EASING), "set_ground_curve", "get_ground_curve"); |
| 277 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "ground_energy_multiplier", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_ground_energy_multiplier", "get_ground_energy_multiplier"); |
| 278 | |
| 279 | ADD_GROUP("Sun", "sun_"); |
| 280 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sun_angle_max", PROPERTY_HINT_RANGE, "0,360,0.01,degrees"), "set_sun_angle_max", "get_sun_angle_max"); |
| 281 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sun_curve", PROPERTY_HINT_EXP_EASING), "set_sun_curve", "get_sun_curve"); |
| 282 | |
| 283 | ADD_GROUP("", ""); |
| 284 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_debanding"), "set_use_debanding", "get_use_debanding"); |
| 285 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy_multiplier", PROPERTY_HINT_RANGE, "0,128,0.01"), "set_energy_multiplier", "get_energy_multiplier"); |
| 286 | } |
| 287 | |
| 288 | void ProceduralSkyMaterial::cleanup_shader() { |
| 289 | for (int i = 0; i < 4; i++) { |
| 290 | if (shader_cache[i].is_valid()) { |
| 291 | RS::get_singleton()->free_rid(shader_cache[i]); |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void ProceduralSkyMaterial::_update_shader(bool p_use_debanding, bool p_use_sky_cover) { |
| 297 | MutexLock shader_lock(shader_mutex); |
| 298 | int index = int(p_use_debanding) + int(p_use_sky_cover) * 2; |
| 299 | if (shader_cache[index].is_null()) { |
| 300 | shader_cache[index] = RS::get_singleton()->shader_create(); |
| 301 | |
| 302 | // Add a comment to describe the shader origin (useful when converting to ShaderMaterial). |
| 303 | RS::get_singleton()->shader_set_code(shader_cache[index], vformat(R"( |
| 304 | // NOTE: Shader automatically converted from )" GODOT_VERSION_NAME " " GODOT_VERSION_FULL_CONFIG R"('s ProceduralSkyMaterial. |
| 305 | |
| 306 | shader_type sky; |
| 307 | %s |
| 308 | |
| 309 | uniform vec4 sky_top_color : source_color = vec4(0.385, 0.454, 0.55, 1.0); |
| 310 | uniform vec4 sky_horizon_color : source_color = vec4(0.646, 0.656, 0.67, 1.0); |
| 311 | uniform float inv_sky_curve : hint_range(1, 100) = 4.0; |
| 312 | uniform vec4 ground_bottom_color : source_color = vec4(0.2, 0.169, 0.133, 1.0); |
| 313 | uniform vec4 ground_horizon_color : source_color = vec4(0.646, 0.656, 0.67, 1.0); |
| 314 | uniform float inv_ground_curve : hint_range(1, 100) = 30.0; |
| 315 | uniform float sun_angle_max = 0.877; |
| 316 | uniform float inv_sun_curve : hint_range(1, 100) = 22.78; |
| 317 | uniform float exposure : hint_range(0, 128) = 1.0; |
| 318 | |
| 319 | uniform sampler2D sky_cover : filter_linear, source_color, hint_default_black; |
| 320 | uniform vec4 sky_cover_modulate : source_color = vec4(1.0, 1.0, 1.0, 1.0); |
| 321 | |
| 322 | void sky() { |
| 323 | float v_angle = clamp(EYEDIR.y, -1.0, 1.0); |
| 324 | vec3 sky = mix(sky_top_color.rgb, sky_horizon_color.rgb, clamp(pow(1.0 - v_angle, inv_sky_curve), 0.0, 1.0)); |
| 325 | |
| 326 | if (LIGHT0_ENABLED) { |
| 327 | float sun_angle = dot(LIGHT0_DIRECTION, EYEDIR); |
| 328 | float sun_size = cos(LIGHT0_SIZE); |
| 329 | if (sun_angle > sun_size) { |
| 330 | sky = LIGHT0_COLOR * LIGHT0_ENERGY; |
| 331 | } else if (sun_angle > sun_angle_max) { |
| 332 | float c2 = (sun_size - sun_angle) / (sun_size - sun_angle_max); |
| 333 | sky = mix(sky, LIGHT0_COLOR * LIGHT0_ENERGY, clamp(pow(1.0 - c2, inv_sun_curve), 0.0, 1.0)); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if (LIGHT1_ENABLED) { |
| 338 | float sun_angle = dot(LIGHT1_DIRECTION, EYEDIR); |
| 339 | float sun_size = cos(LIGHT1_SIZE); |
| 340 | if (sun_angle > sun_size) { |
| 341 | sky = LIGHT1_COLOR * LIGHT1_ENERGY; |
| 342 | } else if (sun_angle > sun_angle_max) { |
| 343 | float c2 = (sun_size - sun_angle) / (sun_size - sun_angle_max); |
| 344 | sky = mix(sky, LIGHT1_COLOR * LIGHT1_ENERGY, clamp(pow(1.0 - c2, inv_sun_curve), 0.0, 1.0)); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (LIGHT2_ENABLED) { |
| 349 | float sun_angle = dot(LIGHT2_DIRECTION, EYEDIR); |
| 350 | float sun_size = cos(LIGHT2_SIZE); |
| 351 | if (sun_angle > sun_size) { |
| 352 | sky = LIGHT2_COLOR * LIGHT2_ENERGY; |
| 353 | } else if (sun_angle > sun_angle_max) { |
| 354 | float c2 = (sun_size - sun_angle) / (sun_size - sun_angle_max); |
| 355 | sky = mix(sky, LIGHT2_COLOR * LIGHT2_ENERGY, clamp(pow(1.0 - c2, inv_sun_curve), 0.0, 1.0)); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | if (LIGHT3_ENABLED) { |
| 360 | float sun_angle = dot(LIGHT3_DIRECTION, EYEDIR); |
| 361 | float sun_size = cos(LIGHT3_SIZE); |
| 362 | if (sun_angle > sun_size) { |
| 363 | sky = LIGHT3_COLOR * LIGHT3_ENERGY; |
| 364 | } else if (sun_angle > sun_angle_max) { |
| 365 | float c2 = (sun_size - sun_angle) / (sun_size - sun_angle_max); |
| 366 | sky = mix(sky, LIGHT3_COLOR * LIGHT3_ENERGY, clamp(pow(1.0 - c2, inv_sun_curve), 0.0, 1.0)); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | %s |
| 371 | %s |
| 372 | vec3 ground = mix(ground_bottom_color.rgb, ground_horizon_color.rgb, clamp(pow(1.0 + v_angle, inv_ground_curve), 0.0, 1.0)); |
| 373 | |
| 374 | COLOR = mix(ground, sky, step(0.0, EYEDIR.y)) * exposure; |
| 375 | } |
| 376 | )", |
| 377 | p_use_debanding ? "render_mode use_debanding;" : "", p_use_sky_cover ? "vec4 sky_cover_texture = texture(sky_cover, SKY_COORDS);" : "", p_use_sky_cover ? "sky += (sky_cover_texture.rgb * sky_cover_modulate.rgb) * sky_cover_texture.a * sky_cover_modulate.a;" : "")); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | ProceduralSkyMaterial::ProceduralSkyMaterial() { |
| 382 | _set_material(RS::get_singleton()->material_create()); |
| 383 | set_sky_top_color(Color(0.385, 0.454, 0.55)); |
| 384 | set_sky_horizon_color(Color(0.6463, 0.6558, 0.6708)); |
| 385 | set_sky_curve(0.15); |
| 386 | set_sky_energy_multiplier(1.0); |
| 387 | set_sky_cover_modulate(Color(1, 1, 1)); |
| 388 | |
| 389 | set_ground_bottom_color(Color(0.2, 0.169, 0.133)); |
| 390 | set_ground_horizon_color(Color(0.6463, 0.6558, 0.6708)); |
| 391 | set_ground_curve(0.02); |
| 392 | set_ground_energy_multiplier(1.0); |
| 393 | |
| 394 | set_sun_angle_max(30.0); |
| 395 | set_sun_curve(0.15); |
| 396 | set_use_debanding(true); |
| 397 | set_energy_multiplier(1.0); |
| 398 | } |
| 399 | |
| 400 | ProceduralSkyMaterial::~ProceduralSkyMaterial() { |
| 401 | } |
| 402 | |
| 403 | ///////////////////////////////////////// |
| 404 | /* PanoramaSkyMaterial */ |
| 405 | |
| 406 | void PanoramaSkyMaterial::set_panorama(const Ref<Texture2D> &p_panorama) { |
| 407 | panorama = p_panorama; |
| 408 | if (p_panorama.is_valid()) { |
| 409 | RS::get_singleton()->material_set_param(_get_material(), "source_panorama", p_panorama->get_rid()); |
| 410 | } else { |
| 411 | RS::get_singleton()->material_set_param(_get_material(), "source_panorama", Variant()); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | Ref<Texture2D> PanoramaSkyMaterial::get_panorama() const { |
| 416 | return panorama; |
| 417 | } |
| 418 | |
| 419 | void PanoramaSkyMaterial::set_filtering_enabled(bool p_enabled) { |
| 420 | filter = p_enabled; |
| 421 | notify_property_list_changed(); |
| 422 | _update_shader(filter); |
| 423 | // Only set if shader already compiled |
| 424 | if (shader_set) { |
| 425 | RS::get_singleton()->material_set_shader(_get_material(), shader_cache[int(filter)]); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | bool PanoramaSkyMaterial::is_filtering_enabled() const { |
| 430 | return filter; |
| 431 | } |
| 432 | |
| 433 | void PanoramaSkyMaterial::set_energy_multiplier(float p_multiplier) { |
| 434 | energy_multiplier = p_multiplier; |
| 435 | RS::get_singleton()->material_set_param(_get_material(), "exposure", energy_multiplier); |
| 436 | } |
| 437 | |
| 438 | float PanoramaSkyMaterial::get_energy_multiplier() const { |
| 439 | return energy_multiplier; |
| 440 | } |
| 441 | |
| 442 | Shader::Mode PanoramaSkyMaterial::get_shader_mode() const { |
| 443 | return Shader::MODE_SKY; |
| 444 | } |
| 445 | |
| 446 | RID PanoramaSkyMaterial::get_rid() const { |
| 447 | _update_shader(filter); |
| 448 | if (!shader_set) { |
| 449 | RS::get_singleton()->material_set_shader(_get_material(), shader_cache[int(filter)]); |
| 450 | shader_set = true; |
| 451 | } |
| 452 | return _get_material(); |
| 453 | } |
| 454 | |
| 455 | RID PanoramaSkyMaterial::get_shader_rid() const { |
| 456 | _update_shader(filter); |
| 457 | return shader_cache[int(filter)]; |
| 458 | } |
| 459 | |
| 460 | void PanoramaSkyMaterial::_bind_methods() { |
| 461 | ClassDB::bind_method(D_METHOD("set_panorama", "texture"), &PanoramaSkyMaterial::set_panorama); |
| 462 | ClassDB::bind_method(D_METHOD("get_panorama"), &PanoramaSkyMaterial::get_panorama); |
| 463 | |
| 464 | ClassDB::bind_method(D_METHOD("set_filtering_enabled", "enabled"), &PanoramaSkyMaterial::set_filtering_enabled); |
| 465 | ClassDB::bind_method(D_METHOD("is_filtering_enabled"), &PanoramaSkyMaterial::is_filtering_enabled); |
| 466 | |
| 467 | ClassDB::bind_method(D_METHOD("set_energy_multiplier", "multiplier"), &PanoramaSkyMaterial::set_energy_multiplier); |
| 468 | ClassDB::bind_method(D_METHOD("get_energy_multiplier"), &PanoramaSkyMaterial::get_energy_multiplier); |
| 469 | |
| 470 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "panorama", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_panorama", "get_panorama"); |
| 471 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter"), "set_filtering_enabled", "is_filtering_enabled"); |
| 472 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy_multiplier", PROPERTY_HINT_RANGE, "0,128,0.01"), "set_energy_multiplier", "get_energy_multiplier"); |
| 473 | } |
| 474 | |
| 475 | Mutex PanoramaSkyMaterial::shader_mutex; |
| 476 | RID PanoramaSkyMaterial::shader_cache[2]; |
| 477 | |
| 478 | void PanoramaSkyMaterial::cleanup_shader() { |
| 479 | for (int i = 0; i < 2; i++) { |
| 480 | if (shader_cache[i].is_valid()) { |
| 481 | RS::get_singleton()->free_rid(shader_cache[i]); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | void PanoramaSkyMaterial::_update_shader(bool p_filter) { |
| 487 | MutexLock shader_lock(shader_mutex); |
| 488 | int index = int(p_filter); |
| 489 | if (shader_cache[index].is_null()) { |
| 490 | shader_cache[index] = RS::get_singleton()->shader_create(); |
| 491 | |
| 492 | // Add a comment to describe the shader origin (useful when converting to ShaderMaterial). |
| 493 | RS::get_singleton()->shader_set_code(shader_cache[index], vformat(R"( |
| 494 | // NOTE: Shader automatically converted from )" GODOT_VERSION_NAME " " GODOT_VERSION_FULL_CONFIG R"('s PanoramaSkyMaterial. |
| 495 | |
| 496 | shader_type sky; |
| 497 | |
| 498 | uniform sampler2D source_panorama : %s, source_color, hint_default_black; |
| 499 | uniform float exposure : hint_range(0, 128) = 1.0; |
| 500 | |
| 501 | void sky() { |
| 502 | COLOR = texture(source_panorama, SKY_COORDS).rgb * exposure; |
| 503 | } |
| 504 | )", |
| 505 | p_filter ? "filter_linear" : "filter_nearest")); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | PanoramaSkyMaterial::PanoramaSkyMaterial() { |
| 510 | _set_material(RS::get_singleton()->material_create()); |
| 511 | set_energy_multiplier(1.0); |
| 512 | } |
| 513 | |
| 514 | PanoramaSkyMaterial::~PanoramaSkyMaterial() { |
| 515 | } |
| 516 | |
| 517 | ////////////////////////////////// |
| 518 | /* PhysicalSkyMaterial */ |
| 519 | |
| 520 | void PhysicalSkyMaterial::set_rayleigh_coefficient(float p_rayleigh) { |
| 521 | rayleigh = p_rayleigh; |
| 522 | RS::get_singleton()->material_set_param(_get_material(), "rayleigh", rayleigh); |
| 523 | } |
| 524 | |
| 525 | float PhysicalSkyMaterial::get_rayleigh_coefficient() const { |
| 526 | return rayleigh; |
| 527 | } |
| 528 | |
| 529 | void PhysicalSkyMaterial::set_rayleigh_color(Color p_rayleigh_color) { |
| 530 | rayleigh_color = p_rayleigh_color; |
| 531 | RS::get_singleton()->material_set_param(_get_material(), "rayleigh_color", rayleigh_color); |
| 532 | } |
| 533 | |
| 534 | Color PhysicalSkyMaterial::get_rayleigh_color() const { |
| 535 | return rayleigh_color; |
| 536 | } |
| 537 | |
| 538 | void PhysicalSkyMaterial::set_mie_coefficient(float p_mie) { |
| 539 | mie = p_mie; |
| 540 | RS::get_singleton()->material_set_param(_get_material(), "mie", mie); |
| 541 | } |
| 542 | |
| 543 | float PhysicalSkyMaterial::get_mie_coefficient() const { |
| 544 | return mie; |
| 545 | } |
| 546 | |
| 547 | void PhysicalSkyMaterial::set_mie_eccentricity(float p_eccentricity) { |
| 548 | mie_eccentricity = p_eccentricity; |
| 549 | RS::get_singleton()->material_set_param(_get_material(), "mie_eccentricity", mie_eccentricity); |
| 550 | } |
| 551 | |
| 552 | float PhysicalSkyMaterial::get_mie_eccentricity() const { |
| 553 | return mie_eccentricity; |
| 554 | } |
| 555 | |
| 556 | void PhysicalSkyMaterial::set_mie_color(Color p_mie_color) { |
| 557 | mie_color = p_mie_color; |
| 558 | RS::get_singleton()->material_set_param(_get_material(), "mie_color", mie_color); |
| 559 | } |
| 560 | |
| 561 | Color PhysicalSkyMaterial::get_mie_color() const { |
| 562 | return mie_color; |
| 563 | } |
| 564 | |
| 565 | void PhysicalSkyMaterial::set_turbidity(float p_turbidity) { |
| 566 | turbidity = p_turbidity; |
| 567 | RS::get_singleton()->material_set_param(_get_material(), "turbidity", turbidity); |
| 568 | } |
| 569 | |
| 570 | float PhysicalSkyMaterial::get_turbidity() const { |
| 571 | return turbidity; |
| 572 | } |
| 573 | |
| 574 | void PhysicalSkyMaterial::set_sun_disk_scale(float p_sun_disk_scale) { |
| 575 | sun_disk_scale = p_sun_disk_scale; |
| 576 | RS::get_singleton()->material_set_param(_get_material(), "sun_disk_scale", sun_disk_scale); |
| 577 | } |
| 578 | |
| 579 | float PhysicalSkyMaterial::get_sun_disk_scale() const { |
| 580 | return sun_disk_scale; |
| 581 | } |
| 582 | |
| 583 | void PhysicalSkyMaterial::set_ground_color(Color p_ground_color) { |
| 584 | ground_color = p_ground_color; |
| 585 | RS::get_singleton()->material_set_param(_get_material(), "ground_color", ground_color); |
| 586 | } |
| 587 | |
| 588 | Color PhysicalSkyMaterial::get_ground_color() const { |
| 589 | return ground_color; |
| 590 | } |
| 591 | |
| 592 | void PhysicalSkyMaterial::set_energy_multiplier(float p_multiplier) { |
| 593 | energy_multiplier = p_multiplier; |
| 594 | RS::get_singleton()->material_set_param(_get_material(), "exposure", energy_multiplier); |
| 595 | } |
| 596 | |
| 597 | float PhysicalSkyMaterial::get_energy_multiplier() const { |
| 598 | return energy_multiplier; |
| 599 | } |
| 600 | |
| 601 | void PhysicalSkyMaterial::set_use_debanding(bool p_use_debanding) { |
| 602 | use_debanding = p_use_debanding; |
| 603 | _update_shader(use_debanding, night_sky.is_valid()); |
| 604 | // Only set if shader already compiled |
| 605 | if (shader_set) { |
| 606 | RS::get_singleton()->material_set_shader(_get_material(), get_shader_cache()); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | bool PhysicalSkyMaterial::get_use_debanding() const { |
| 611 | return use_debanding; |
| 612 | } |
| 613 | |
| 614 | void PhysicalSkyMaterial::set_night_sky(const Ref<Texture2D> &p_night_sky) { |
| 615 | night_sky = p_night_sky; |
| 616 | if (p_night_sky.is_valid()) { |
| 617 | RS::get_singleton()->material_set_param(_get_material(), "night_sky", p_night_sky->get_rid()); |
| 618 | } else { |
| 619 | RS::get_singleton()->material_set_param(_get_material(), "night_sky", Variant()); |
| 620 | } |
| 621 | |
| 622 | _update_shader(use_debanding, night_sky.is_valid()); |
| 623 | |
| 624 | if (shader_set) { |
| 625 | RS::get_singleton()->material_set_shader(_get_material(), get_shader_cache()); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | Ref<Texture2D> PhysicalSkyMaterial::get_night_sky() const { |
| 630 | return night_sky; |
| 631 | } |
| 632 | |
| 633 | Shader::Mode PhysicalSkyMaterial::get_shader_mode() const { |
| 634 | return Shader::MODE_SKY; |
| 635 | } |
| 636 | |
| 637 | // Internal function to grab the current shader RID. |
| 638 | // Must only be called if the shader is initialized. |
| 639 | RID PhysicalSkyMaterial::get_shader_cache() const { |
| 640 | return shader_cache[int(use_debanding) + (night_sky.is_valid() ? 2 : 0)]; |
| 641 | } |
| 642 | |
| 643 | RID PhysicalSkyMaterial::get_rid() const { |
| 644 | _update_shader(use_debanding, night_sky.is_valid()); |
| 645 | if (!shader_set) { |
| 646 | RS::get_singleton()->material_set_shader(_get_material(), get_shader_cache()); |
| 647 | shader_set = true; |
| 648 | } |
| 649 | return _get_material(); |
| 650 | } |
| 651 | |
| 652 | RID PhysicalSkyMaterial::get_shader_rid() const { |
| 653 | _update_shader(use_debanding, night_sky.is_valid()); |
| 654 | return get_shader_cache(); |
| 655 | } |
| 656 | |
| 657 | void PhysicalSkyMaterial::_validate_property(PropertyInfo &p_property) const { |
| 658 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 659 | return; |
| 660 | } |
| 661 | if (p_property.name == "exposure_value" && !GLOBAL_GET_CACHED(bool, "rendering/lights_and_shadows/use_physical_light_units")) { |
| 662 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | Mutex PhysicalSkyMaterial::shader_mutex; |
| 667 | RID PhysicalSkyMaterial::shader_cache[4]; |
| 668 | |
| 669 | void PhysicalSkyMaterial::_bind_methods() { |
| 670 | ClassDB::bind_method(D_METHOD("set_rayleigh_coefficient", "rayleigh"), &PhysicalSkyMaterial::set_rayleigh_coefficient); |
| 671 | ClassDB::bind_method(D_METHOD("get_rayleigh_coefficient"), &PhysicalSkyMaterial::get_rayleigh_coefficient); |
| 672 | |
| 673 | ClassDB::bind_method(D_METHOD("set_rayleigh_color", "color"), &PhysicalSkyMaterial::set_rayleigh_color); |
| 674 | ClassDB::bind_method(D_METHOD("get_rayleigh_color"), &PhysicalSkyMaterial::get_rayleigh_color); |
| 675 | |
| 676 | ClassDB::bind_method(D_METHOD("set_mie_coefficient", "mie"), &PhysicalSkyMaterial::set_mie_coefficient); |
| 677 | ClassDB::bind_method(D_METHOD("get_mie_coefficient"), &PhysicalSkyMaterial::get_mie_coefficient); |
| 678 | |
| 679 | ClassDB::bind_method(D_METHOD("set_mie_eccentricity", "eccentricity"), &PhysicalSkyMaterial::set_mie_eccentricity); |
| 680 | ClassDB::bind_method(D_METHOD("get_mie_eccentricity"), &PhysicalSkyMaterial::get_mie_eccentricity); |
| 681 | |
| 682 | ClassDB::bind_method(D_METHOD("set_mie_color", "color"), &PhysicalSkyMaterial::set_mie_color); |
| 683 | ClassDB::bind_method(D_METHOD("get_mie_color"), &PhysicalSkyMaterial::get_mie_color); |
| 684 | |
| 685 | ClassDB::bind_method(D_METHOD("set_turbidity", "turbidity"), &PhysicalSkyMaterial::set_turbidity); |
| 686 | ClassDB::bind_method(D_METHOD("get_turbidity"), &PhysicalSkyMaterial::get_turbidity); |
| 687 | |
| 688 | ClassDB::bind_method(D_METHOD("set_sun_disk_scale", "scale"), &PhysicalSkyMaterial::set_sun_disk_scale); |
| 689 | ClassDB::bind_method(D_METHOD("get_sun_disk_scale"), &PhysicalSkyMaterial::get_sun_disk_scale); |
| 690 | |
| 691 | ClassDB::bind_method(D_METHOD("set_ground_color", "color"), &PhysicalSkyMaterial::set_ground_color); |
| 692 | ClassDB::bind_method(D_METHOD("get_ground_color"), &PhysicalSkyMaterial::get_ground_color); |
| 693 | |
| 694 | ClassDB::bind_method(D_METHOD("set_energy_multiplier", "multiplier"), &PhysicalSkyMaterial::set_energy_multiplier); |
| 695 | ClassDB::bind_method(D_METHOD("get_energy_multiplier"), &PhysicalSkyMaterial::get_energy_multiplier); |
| 696 | |
| 697 | ClassDB::bind_method(D_METHOD("set_use_debanding", "use_debanding"), &PhysicalSkyMaterial::set_use_debanding); |
| 698 | ClassDB::bind_method(D_METHOD("get_use_debanding"), &PhysicalSkyMaterial::get_use_debanding); |
| 699 | |
| 700 | ClassDB::bind_method(D_METHOD("set_night_sky", "night_sky"), &PhysicalSkyMaterial::set_night_sky); |
| 701 | ClassDB::bind_method(D_METHOD("get_night_sky"), &PhysicalSkyMaterial::get_night_sky); |
| 702 | |
| 703 | ADD_GROUP("Rayleigh", "rayleigh_"); |
| 704 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rayleigh_coefficient", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_rayleigh_coefficient", "get_rayleigh_coefficient"); |
| 705 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "rayleigh_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_rayleigh_color", "get_rayleigh_color"); |
| 706 | |
| 707 | ADD_GROUP("Mie", "mie_"); |
| 708 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mie_coefficient", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_mie_coefficient", "get_mie_coefficient"); |
| 709 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mie_eccentricity", PROPERTY_HINT_RANGE, "-1,1,0.01"), "set_mie_eccentricity", "get_mie_eccentricity"); |
| 710 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "mie_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_mie_color", "get_mie_color"); |
| 711 | |
| 712 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "turbidity", PROPERTY_HINT_RANGE, "0,1000,0.01"), "set_turbidity", "get_turbidity"); |
| 713 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "sun_disk_scale", PROPERTY_HINT_RANGE, "0,360,0.01"), "set_sun_disk_scale", "get_sun_disk_scale"); |
| 714 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "ground_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_ground_color", "get_ground_color"); |
| 715 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy_multiplier", PROPERTY_HINT_RANGE, "0,128,0.01"), "set_energy_multiplier", "get_energy_multiplier"); |
| 716 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_debanding"), "set_use_debanding", "get_use_debanding"); |
| 717 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "night_sky", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_night_sky", "get_night_sky"); |
| 718 | } |
| 719 | |
| 720 | void PhysicalSkyMaterial::cleanup_shader() { |
| 721 | for (int i = 0; i < 4; i++) { |
| 722 | if (shader_cache[i].is_valid()) { |
| 723 | RS::get_singleton()->free_rid(shader_cache[i]); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | void PhysicalSkyMaterial::_update_shader(bool p_use_debanding, bool p_use_night_sky) { |
| 729 | MutexLock shader_lock(shader_mutex); |
| 730 | int index = int(p_use_debanding) + int(p_use_night_sky) * 2; |
| 731 | if (shader_cache[index].is_null()) { |
| 732 | shader_cache[index] = RS::get_singleton()->shader_create(); |
| 733 | |
| 734 | // Add a comment to describe the shader origin (useful when converting to ShaderMaterial). |
| 735 | RS::get_singleton()->shader_set_code(shader_cache[index], vformat(R"( |
| 736 | // NOTE: Shader automatically converted from )" GODOT_VERSION_NAME " " GODOT_VERSION_FULL_CONFIG R"('s PhysicalSkyMaterial. |
| 737 | |
| 738 | shader_type sky; |
| 739 | %s |
| 740 | |
| 741 | uniform float rayleigh : hint_range(0, 64) = 2.0; |
| 742 | uniform vec4 rayleigh_color : source_color = vec4(0.3, 0.405, 0.6, 1.0); |
| 743 | uniform float mie : hint_range(0, 1) = 0.005; |
| 744 | uniform float mie_eccentricity : hint_range(-1, 1) = 0.8; |
| 745 | uniform vec4 mie_color : source_color = vec4(0.69, 0.729, 0.812, 1.0); |
| 746 | |
| 747 | uniform float turbidity : hint_range(0, 1000) = 10.0; |
| 748 | uniform float sun_disk_scale : hint_range(0, 360) = 1.0; |
| 749 | uniform vec4 ground_color : source_color = vec4(0.1, 0.07, 0.034, 1.0); |
| 750 | uniform float exposure : hint_range(0, 128) = 1.0; |
| 751 | |
| 752 | uniform sampler2D night_sky : filter_linear, source_color, hint_default_black; |
| 753 | |
| 754 | const vec3 UP = vec3( 0.0, 1.0, 0.0 ); |
| 755 | |
| 756 | // Optical length at zenith for molecules. |
| 757 | const float rayleigh_zenith_size = 8.4e3; |
| 758 | const float mie_zenith_size = 1.25e3; |
| 759 | |
| 760 | float henyey_greenstein(float cos_theta, float g) { |
| 761 | const float k = 0.0795774715459; |
| 762 | return k * (1.0 - g * g) / (pow(1.0 + g * g - 2.0 * g * cos_theta, 1.5)); |
| 763 | } |
| 764 | |
| 765 | void sky() { |
| 766 | if (LIGHT0_ENABLED) { |
| 767 | float zenith_angle = clamp( dot(UP, normalize(LIGHT0_DIRECTION)), -1.0, 1.0 ); |
| 768 | float sun_energy = max(0.0, 0.757 * zenith_angle) * LIGHT0_ENERGY; |
| 769 | float sun_fade = 1.0 - clamp(1.0 - exp(LIGHT0_DIRECTION.y), 0.0, 1.0); |
| 770 | |
| 771 | // Rayleigh coefficients. |
| 772 | float rayleigh_coefficient = rayleigh - ( 1.0 * ( 1.0 - sun_fade ) ); |
| 773 | vec3 rayleigh_beta = rayleigh_coefficient * rayleigh_color.rgb * 0.0001; |
| 774 | // mie coefficients from Preetham |
| 775 | vec3 mie_beta = turbidity * mie * mie_color.rgb * 0.000434; |
| 776 | |
| 777 | // Optical length. |
| 778 | float zenith = max(0.0, dot(UP, EYEDIR)); |
| 779 | float optical_mass = 1.0 / (zenith + 0.15 * pow(3.885 + 54.5 * zenith, -1.253)); |
| 780 | float rayleigh_scatter = rayleigh_zenith_size * optical_mass; |
| 781 | float mie_scatter = mie_zenith_size * optical_mass; |
| 782 | |
| 783 | // Light extinction based on thickness of atmosphere. |
| 784 | vec3 extinction = exp(-(rayleigh_beta * rayleigh_scatter + mie_beta * mie_scatter)); |
| 785 | |
| 786 | // In scattering. |
| 787 | float cos_theta = dot(EYEDIR, normalize(LIGHT0_DIRECTION)); |
| 788 | |
| 789 | float rayleigh_phase = (3.0 / (16.0 * PI)) * (1.0 + pow(cos_theta * 0.5 + 0.5, 2.0)); |
| 790 | vec3 betaRTheta = rayleigh_beta * rayleigh_phase; |
| 791 | |
| 792 | float mie_phase = henyey_greenstein(cos_theta, mie_eccentricity); |
| 793 | vec3 betaMTheta = mie_beta * mie_phase; |
| 794 | |
| 795 | vec3 Lin = pow(sun_energy * ((betaRTheta + betaMTheta) / (rayleigh_beta + mie_beta)) * (1.0 - extinction), vec3(1.5)); |
| 796 | // Hack from https://github.com/mrdoob/three.js/blob/master/examples/jsm/objects/Sky.js |
| 797 | Lin *= mix(vec3(1.0), pow(sun_energy * ((betaRTheta + betaMTheta) / (rayleigh_beta + mie_beta)) * extinction, vec3(0.5)), clamp(pow(1.0 - zenith_angle, 5.0), 0.0, 1.0)); |
| 798 | |
| 799 | // Hack in the ground color. |
| 800 | Lin *= mix(ground_color.rgb, vec3(1.0), smoothstep(-0.1, 0.1, dot(UP, EYEDIR))); |
| 801 | |
| 802 | // Solar disk and out-scattering. |
| 803 | float sunAngularDiameterCos = cos(LIGHT0_SIZE * sun_disk_scale); |
| 804 | float sunAngularDiameterCos2 = cos(LIGHT0_SIZE * sun_disk_scale * 0.5); |
| 805 | float sundisk = smoothstep(sunAngularDiameterCos, sunAngularDiameterCos2, cos_theta); |
| 806 | vec3 L0 = (sun_energy * extinction) * sundisk * LIGHT0_COLOR; |
| 807 | %s |
| 808 | |
| 809 | vec3 color = Lin + L0; |
| 810 | COLOR = pow(color, vec3(1.0 / (1.2 + (1.2 * sun_fade)))); |
| 811 | COLOR *= exposure; |
| 812 | } else { |
| 813 | // There is no sun, so display night_sky and nothing else. |
| 814 | %s |
| 815 | COLOR *= exposure; |
| 816 | } |
| 817 | } |
| 818 | )", |
| 819 | p_use_debanding ? "render_mode use_debanding;" : "", p_use_night_sky ? "L0 += texture(night_sky, SKY_COORDS).xyz * extinction;" : "", p_use_night_sky ? "COLOR = texture(night_sky, SKY_COORDS).xyz;" : "")); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | PhysicalSkyMaterial::PhysicalSkyMaterial() { |
| 824 | _set_material(RS::get_singleton()->material_create()); |
| 825 | set_rayleigh_coefficient(2.0); |
| 826 | set_rayleigh_color(Color(0.3, 0.405, 0.6)); |
| 827 | set_mie_coefficient(0.005); |
| 828 | set_mie_eccentricity(0.8); |
| 829 | set_mie_color(Color(0.69, 0.729, 0.812)); |
| 830 | set_turbidity(10.0); |
| 831 | set_sun_disk_scale(1.0); |
| 832 | set_ground_color(Color(0.1, 0.07, 0.034)); |
| 833 | set_energy_multiplier(1.0); |
| 834 | set_use_debanding(true); |
| 835 | } |
| 836 | |
| 837 | PhysicalSkyMaterial::~PhysicalSkyMaterial() { |
| 838 | } |