Debian Codegraph @

servers/rendering/storage/environment_storage.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* environment_storage.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 "environment_storage.h"
32
33 // Storage
34
35 RendererEnvironmentStorage *RendererEnvironmentStorage::singleton = nullptr;
36
37 RendererEnvironmentStorage::RendererEnvironmentStorage() {
38 singleton = this;
39 }
40
41 RendererEnvironmentStorage::~RendererEnvironmentStorage() {
42 singleton = nullptr;
43 }
44
45 // Environment
46
47 RID RendererEnvironmentStorage::environment_allocate() {
48 return environment_owner.allocate_rid();
49 }
50
51 void RendererEnvironmentStorage::environment_initialize(RID p_rid) {
52 environment_owner.initialize_rid(p_rid, Environment());
53 }
54
55 void RendererEnvironmentStorage::environment_free(RID p_rid) {
56 environment_owner.free(p_rid);
57 }
58
59 // Background
60
61 void RendererEnvironmentStorage::environment_set_background(RID p_env, RS::EnvironmentBG p_bg) {
62 Environment *env = environment_owner.get_or_null(p_env);
63 ERR_FAIL_NULL(env);
64 env->background = p_bg;
65 }
66
67 void RendererEnvironmentStorage::environment_set_sky(RID p_env, RID p_sky) {
68 Environment *env = environment_owner.get_or_null(p_env);
69 ERR_FAIL_NULL(env);
70 env->sky = p_sky;
71 }
72
73 void RendererEnvironmentStorage::environment_set_sky_custom_fov(RID p_env, float p_scale) {
74 Environment *env = environment_owner.get_or_null(p_env);
75 ERR_FAIL_NULL(env);
76 env->sky_custom_fov = p_scale;
77 }
78
79 void RendererEnvironmentStorage::environment_set_sky_orientation(RID p_env, const Basis &p_orientation) {
80 Environment *env = environment_owner.get_or_null(p_env);
81 ERR_FAIL_NULL(env);
82 env->sky_orientation = p_orientation;
83 }
84
85 void RendererEnvironmentStorage::environment_set_bg_color(RID p_env, const Color &p_color) {
86 Environment *env = environment_owner.get_or_null(p_env);
87 ERR_FAIL_NULL(env);
88 env->bg_color = p_color;
89 }
90
91 void RendererEnvironmentStorage::environment_set_bg_energy(RID p_env, float p_multiplier, float p_intensity) {
92 Environment *env = environment_owner.get_or_null(p_env);
93 ERR_FAIL_NULL(env);
94 env->bg_energy_multiplier = p_multiplier;
95 env->bg_intensity = p_intensity;
96 }
97
98 void RendererEnvironmentStorage::environment_set_canvas_max_layer(RID p_env, int p_max_layer) {
99 Environment *env = environment_owner.get_or_null(p_env);
100 ERR_FAIL_NULL(env);
101 env->canvas_max_layer = p_max_layer;
102 }
103
104 void RendererEnvironmentStorage::environment_set_ambient_light(RID p_env, const Color &p_color, RS::EnvironmentAmbientSource p_ambient, float p_energy, float p_sky_contribution, RS::EnvironmentReflectionSource p_reflection_source) {
105 Environment *env = environment_owner.get_or_null(p_env);
106 ERR_FAIL_NULL(env);
107 env->ambient_light = p_color;
108 env->ambient_source = p_ambient;
109 env->ambient_light_energy = p_energy;
110 env->ambient_sky_contribution = p_sky_contribution;
111 env->reflection_source = p_reflection_source;
112 }
113
114 RS::EnvironmentBG RendererEnvironmentStorage::environment_get_background(RID p_env) const {
115 Environment *env = environment_owner.get_or_null(p_env);
116 ERR_FAIL_NULL_V(env, RS::ENV_BG_CLEAR_COLOR);
117 return env->background;
118 }
119
120 RID RendererEnvironmentStorage::environment_get_sky(RID p_env) const {
121 Environment *env = environment_owner.get_or_null(p_env);
122 ERR_FAIL_NULL_V(env, RID());
123 return env->sky;
124 }
125
126 float RendererEnvironmentStorage::environment_get_sky_custom_fov(RID p_env) const {
127 Environment *env = environment_owner.get_or_null(p_env);
128 ERR_FAIL_NULL_V(env, 0.0);
129 return env->sky_custom_fov;
130 }
131
132 Basis RendererEnvironmentStorage::environment_get_sky_orientation(RID p_env) const {
133 Environment *env = environment_owner.get_or_null(p_env);
134 ERR_FAIL_NULL_V(env, Basis());
135 return env->sky_orientation;
136 }
137
138 Color RendererEnvironmentStorage::environment_get_bg_color(RID p_env) const {
139 Environment *env = environment_owner.get_or_null(p_env);
140 ERR_FAIL_NULL_V(env, Color());
141 return env->bg_color;
142 }
143
144 float RendererEnvironmentStorage::environment_get_bg_energy_multiplier(RID p_env) const {
145 Environment *env = environment_owner.get_or_null(p_env);
146 ERR_FAIL_NULL_V(env, 1.0);
147 return env->bg_energy_multiplier;
148 }
149
150 float RendererEnvironmentStorage::environment_get_bg_intensity(RID p_env) const {
151 Environment *env = environment_owner.get_or_null(p_env);
152 ERR_FAIL_NULL_V(env, 1.0);
153 return env->bg_intensity;
154 }
155
156 int RendererEnvironmentStorage::environment_get_canvas_max_layer(RID p_env) const {
157 Environment *env = environment_owner.get_or_null(p_env);
158 ERR_FAIL_NULL_V(env, 0);
159 return env->canvas_max_layer;
160 }
161
162 RS::EnvironmentAmbientSource RendererEnvironmentStorage::environment_get_ambient_source(RID p_env) const {
163 Environment *env = environment_owner.get_or_null(p_env);
164 ERR_FAIL_NULL_V(env, RS::ENV_AMBIENT_SOURCE_BG);
165 return env->ambient_source;
166 }
167
168 Color RendererEnvironmentStorage::environment_get_ambient_light(RID p_env) const {
169 Environment *env = environment_owner.get_or_null(p_env);
170 ERR_FAIL_NULL_V(env, Color());
171 return env->ambient_light;
172 }
173
174 float RendererEnvironmentStorage::environment_get_ambient_light_energy(RID p_env) const {
175 Environment *env = environment_owner.get_or_null(p_env);
176 ERR_FAIL_NULL_V(env, 1.0);
177 return env->ambient_light_energy;
178 }
179
180 float RendererEnvironmentStorage::environment_get_ambient_sky_contribution(RID p_env) const {
181 Environment *env = environment_owner.get_or_null(p_env);
182 ERR_FAIL_NULL_V(env, 1.0);
183 return env->ambient_sky_contribution;
184 }
185
186 RS::EnvironmentReflectionSource RendererEnvironmentStorage::environment_get_reflection_source(RID p_env) const {
187 Environment *env = environment_owner.get_or_null(p_env);
188 ERR_FAIL_NULL_V(env, RS::ENV_REFLECTION_SOURCE_BG);
189 return env->reflection_source;
190 }
191
192 void RendererEnvironmentStorage::environment_set_camera_feed_id(RID p_env, int p_camera_feed_id) {
193 Environment *env = environment_owner.get_or_null(p_env);
194 ERR_FAIL_NULL(env);
195 env->camera_feed_id = p_camera_feed_id;
196 }
197
198 int RendererEnvironmentStorage::environment_get_camera_feed_id(RID p_env) const {
199 Environment *env = environment_owner.get_or_null(p_env);
200 ERR_FAIL_NULL_V(env, -1);
201 return env->camera_feed_id;
202 }
203
204 // Tonemap
205
206 void RendererEnvironmentStorage::environment_set_tonemap(RID p_env, RS::EnvironmentToneMapper p_tone_mapper, float p_exposure, float p_white) {
207 Environment *env = environment_owner.get_or_null(p_env);
208 ERR_FAIL_NULL(env);
209 env->exposure = p_exposure;
210 env->tone_mapper = p_tone_mapper;
211 env->white = p_white;
212 }
213
214 RS::EnvironmentToneMapper RendererEnvironmentStorage::environment_get_tone_mapper(RID p_env) const {
215 Environment *env = environment_owner.get_or_null(p_env);
216 ERR_FAIL_NULL_V(env, RS::ENV_TONE_MAPPER_LINEAR);
217 return env->tone_mapper;
218 }
219
220 float RendererEnvironmentStorage::environment_get_exposure(RID p_env) const {
221 Environment *env = environment_owner.get_or_null(p_env);
222 ERR_FAIL_NULL_V(env, 1.0);
223 return env->exposure;
224 }
225
226 float RendererEnvironmentStorage::environment_get_white(RID p_env, bool p_limit_agx_white) const {
227 Environment *env = environment_owner.get_or_null(p_env);
228 ERR_FAIL_NULL_V(env, 1.0);
229
230 const float output_max_value = 1.0; // SDR always has an output_max_value of 1.0.
231
232 // Glow with screen blend mode does not work when white < 1.0, so make sure
233 // it is at least 1.0 for all tonemappers:
234 if (env->tone_mapper == RS::ENV_TONE_MAPPER_LINEAR) {
235 return output_max_value;
236 } else if (env->tone_mapper == RS::ENV_TONE_MAPPER_FILMIC || env->tone_mapper == RS::ENV_TONE_MAPPER_ACES) {
237 // Filmic and ACES only support SDR; their white is stable regardless
238 // of output_max_value.
239 return MAX(1.0, env->white);
240 } else if (env->tone_mapper == RS::ENV_TONE_MAPPER_AGX) {
241 // AgX works best with a high white. 2.0 is the minimum required for
242 // good behavior with Mobile rendering method.
243 if (p_limit_agx_white) {
244 return 2.0;
245 } else {
246 float agx_white = MAX(2.0, env->white);
247 // Instead of constraining by matching the output_max_value, constrain
248 // by multiplying to ensure the desired non-uniform scaling behavior
249 // is maintained in the shoulder.
250 return agx_white * output_max_value;
251 }
252 } else { // Reinhard
253 // The Reinhard tonemapper is not designed to have a white parameter
254 // that is less than the output max value. This is especially important
255 // in the variable Extended Dynamic Range (EDR) paradigm where the
256 // output max value may change to be greater or less than the white
257 // parameter, depending on the available dynamic range.
258 return MAX(output_max_value, env->white);
259 }
260 }
261
262 void RendererEnvironmentStorage::environment_set_tonemap_agx_contrast(RID p_env, float p_agx_contrast) {
263 Environment *env = environment_owner.get_or_null(p_env);
264 ERR_FAIL_NULL(env);
265 env->tonemap_agx_contrast = p_agx_contrast;
266 }
267
268 float RendererEnvironmentStorage::environment_get_tonemap_agx_contrast(RID p_env) const {
269 Environment *env = environment_owner.get_or_null(p_env);
270 ERR_FAIL_NULL_V(env, 1.0);
271 return env->tonemap_agx_contrast;
272 }
273
274 RendererEnvironmentStorage::TonemapParameters RendererEnvironmentStorage::environment_get_tonemap_parameters(RID p_env, bool p_limit_agx_white) const {
275 Environment *env = environment_owner.get_or_null(p_env);
276 ERR_FAIL_NULL_V(env, TonemapParameters());
277
278 const float output_max_value = 1.0; // SDR always has an output_max_value of 1.0.
279
280 float white = environment_get_white(p_env, p_limit_agx_white);
281 TonemapParameters tonemap_parameters = TonemapParameters();
282
283 if (env->tone_mapper == RS::ENV_TONE_MAPPER_LINEAR) {
284 // Linear has no tonemapping parameters
285 } else if (env->tone_mapper == RS::ENV_TONE_MAPPER_REINHARD) {
286 tonemap_parameters.white_squared = white * white;
287 } else if (env->tone_mapper == RS::ENV_TONE_MAPPER_FILMIC) {
288 // These constants must match those in the shader code.
289 // exposure_bias: Input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers
290 // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values).
291 // Has no effect on the curve's general shape or visual properties.
292 const float exposure_bias = 2.0f;
293 const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance
294 const float B = 0.30f * exposure_bias;
295 const float C = 0.10f;
296 const float D = 0.20f;
297 const float E = 0.01f;
298 const float F = 0.30f;
299
300 tonemap_parameters.white_tonemapped = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F;
301 } else if (env->tone_mapper == RS::ENV_TONE_MAPPER_ACES) {
302 // These constants must match those in the shader code.
303 const float exposure_bias = 1.8f;
304 const float A = 0.0245786f;
305 const float B = 0.000090537f;
306 const float C = 0.983729f;
307 const float D = 0.432951f;
308 const float E = 0.238081f;
309
310 white *= exposure_bias;
311 float white_tonemapped = (white * (white + A) - B) / (white * (C * white + D) + E);
312 tonemap_parameters.white_tonemapped = white_tonemapped;
313 } else if (env->tone_mapper == RS::ENV_TONE_MAPPER_AGX) {
314 // Calculate allenwp tonemapping curve parameters on the CPU to improve shader performance.
315 // Source and details: https://allenwp.com/blog/2025/05/29/allenwp-tonemapping-curve/
316
317 // These constants must match the those in the shader code.
318 // 18% "middle gray" is perceptually 50% of the brightness of reference white.
319 const float awp_crossover_point = 0.18;
320 // When output_max_value and/or awp_crossover_point are no longer constant, awp_shoulder_max can
321 // be calculated on the CPU and passed in as tonemap_parameters.tonemap_e.
322 const float awp_shoulder_max = output_max_value - awp_crossover_point;
323
324 float awp_high_clip = white;
325
326 // awp_toe_a is a solution generated by Mathematica that ensures intersection at awp_crossover_point.
327 float awp_toe_a = ((1.0 / awp_crossover_point) - 1.0) * pow(awp_crossover_point, env->tonemap_agx_contrast);
328 // Slope formula is simply the derivative of the toe function with an input of awp_crossover_point.
329 float awp_slope_denom = pow(awp_crossover_point, env->tonemap_agx_contrast) + awp_toe_a;
330 float awp_slope = (env->tonemap_agx_contrast * pow(awp_crossover_point, env->tonemap_agx_contrast - 1.0) * awp_toe_a) / (awp_slope_denom * awp_slope_denom);
331
332 float awp_w = awp_high_clip - awp_crossover_point;
333 awp_w = awp_w * awp_w;
334 awp_w = awp_w / awp_shoulder_max;
335 awp_w = awp_w * awp_slope;
336
337 tonemap_parameters.awp_contrast = env->tonemap_agx_contrast;
338 tonemap_parameters.awp_toe_a = awp_toe_a;
339 tonemap_parameters.awp_slope = awp_slope;
340 tonemap_parameters.awp_w = awp_w;
341 }
342
343 return tonemap_parameters;
344 }
345
346 // Fog
347
348 void RendererEnvironmentStorage::environment_set_fog(RID p_env, bool p_enable, const Color &p_light_color, float p_light_energy, float p_sun_scatter, float p_density, float p_height, float p_height_density, float p_fog_aerial_perspective, float p_sky_affect, RS::EnvironmentFogMode p_mode) {
349 Environment *env = environment_owner.get_or_null(p_env);
350 ERR_FAIL_NULL(env);
351 env->fog_enabled = p_enable;
352 env->fog_mode = p_mode;
353 env->fog_light_color = p_light_color;
354 env->fog_light_energy = p_light_energy;
355 env->fog_sun_scatter = p_sun_scatter;
356 env->fog_density = p_density;
357 env->fog_height = p_height;
358 env->fog_height_density = p_height_density;
359 env->fog_aerial_perspective = p_fog_aerial_perspective;
360 env->fog_sky_affect = p_sky_affect;
361 }
362
363 bool RendererEnvironmentStorage::environment_get_fog_enabled(RID p_env) const {
364 Environment *env = environment_owner.get_or_null(p_env);
365 ERR_FAIL_NULL_V(env, false);
366 return env->fog_enabled;
367 }
368
369 RS::EnvironmentFogMode RendererEnvironmentStorage::environment_get_fog_mode(RID p_env) const {
370 Environment *env = environment_owner.get_or_null(p_env);
371 ERR_FAIL_NULL_V(env, RS::ENV_FOG_MODE_EXPONENTIAL);
372 return env->fog_mode;
373 }
374
375 Color RendererEnvironmentStorage::environment_get_fog_light_color(RID p_env) const {
376 Environment *env = environment_owner.get_or_null(p_env);
377 ERR_FAIL_NULL_V(env, Color(0.5, 0.6, 0.7));
378 return env->fog_light_color;
379 }
380
381 float RendererEnvironmentStorage::environment_get_fog_light_energy(RID p_env) const {
382 Environment *env = environment_owner.get_or_null(p_env);
383 ERR_FAIL_NULL_V(env, 1.0);
384 return env->fog_light_energy;
385 }
386
387 float RendererEnvironmentStorage::environment_get_fog_sun_scatter(RID p_env) const {
388 Environment *env = environment_owner.get_or_null(p_env);
389 ERR_FAIL_NULL_V(env, 0.0);
390 return env->fog_sun_scatter;
391 }
392
393 float RendererEnvironmentStorage::environment_get_fog_density(RID p_env) const {
394 Environment *env = environment_owner.get_or_null(p_env);
395 ERR_FAIL_NULL_V(env, 0.001);
396 return env->fog_density;
397 }
398
399 float RendererEnvironmentStorage::environment_get_fog_height(RID p_env) const {
400 Environment *env = environment_owner.get_or_null(p_env);
401 ERR_FAIL_NULL_V(env, 0.0);
402 return env->fog_height;
403 }
404
405 float RendererEnvironmentStorage::environment_get_fog_height_density(RID p_env) const {
406 Environment *env = environment_owner.get_or_null(p_env);
407 ERR_FAIL_NULL_V(env, 0.0);
408 return env->fog_height_density;
409 }
410
411 float RendererEnvironmentStorage::environment_get_fog_aerial_perspective(RID p_env) const {
412 Environment *env = environment_owner.get_or_null(p_env);
413 ERR_FAIL_NULL_V(env, 0.0);
414 return env->fog_aerial_perspective;
415 }
416
417 float RendererEnvironmentStorage::environment_get_fog_sky_affect(RID p_env) const {
418 Environment *env = environment_owner.get_or_null(p_env);
419 ERR_FAIL_NULL_V(env, 0.0);
420 return env->fog_sky_affect;
421 }
422
423 // Depth Fog
424
425 void RendererEnvironmentStorage::environment_set_fog_depth(RID p_env, float p_curve, float p_begin, float p_end) {
426 Environment *env = environment_owner.get_or_null(p_env);
427 ERR_FAIL_NULL(env);
428 env->fog_depth_curve = p_curve;
429 env->fog_depth_begin = p_begin;
430 env->fog_depth_end = p_end;
431 }
432
433 float RendererEnvironmentStorage::environment_get_fog_depth_curve(RID p_env) const {
434 Environment *env = environment_owner.get_or_null(p_env);
435 ERR_FAIL_NULL_V(env, 0.0);
436 return env->fog_depth_curve;
437 }
438
439 float RendererEnvironmentStorage::environment_get_fog_depth_begin(RID p_env) const {
440 Environment *env = environment_owner.get_or_null(p_env);
441 ERR_FAIL_NULL_V(env, 0.0);
442 return env->fog_depth_begin;
443 }
444
445 float RendererEnvironmentStorage::environment_get_fog_depth_end(RID p_env) const {
446 Environment *env = environment_owner.get_or_null(p_env);
447 ERR_FAIL_NULL_V(env, 0.0);
448 return env->fog_depth_end;
449 }
450
451 // Volumetric Fog
452
453 void RendererEnvironmentStorage::environment_set_volumetric_fog(RID p_env, bool p_enable, float p_density, const Color &p_albedo, const Color &p_emission, float p_emission_energy, float p_anisotropy, float p_length, float p_detail_spread, float p_gi_inject, bool p_temporal_reprojection, float p_temporal_reprojection_amount, float p_ambient_inject, float p_sky_affect) {
454 Environment *env = environment_owner.get_or_null(p_env);
455 ERR_FAIL_NULL(env);
456 #ifdef DEBUG_ENABLED
457 if (OS::get_singleton()->get_current_rendering_method() != "forward_plus" && p_enable) {
458 WARN_PRINT_ONCE_ED("Volumetric fog is only available when using the Forward+ renderer.");
459 }
460 #endif
461 env->volumetric_fog_enabled = p_enable;
462 env->volumetric_fog_density = p_density;
463 env->volumetric_fog_scattering = p_albedo;
464 env->volumetric_fog_emission = p_emission;
465 env->volumetric_fog_emission_energy = p_emission_energy;
466 env->volumetric_fog_anisotropy = p_anisotropy;
467 env->volumetric_fog_length = p_length;
468 env->volumetric_fog_detail_spread = p_detail_spread;
469 env->volumetric_fog_gi_inject = p_gi_inject;
470 env->volumetric_fog_temporal_reprojection = p_temporal_reprojection;
471 env->volumetric_fog_temporal_reprojection_amount = p_temporal_reprojection_amount;
472 env->volumetric_fog_ambient_inject = p_ambient_inject;
473 env->volumetric_fog_sky_affect = p_sky_affect;
474 }
475
476 bool RendererEnvironmentStorage::environment_get_volumetric_fog_enabled(RID p_env) const {
477 Environment *env = environment_owner.get_or_null(p_env);
478 ERR_FAIL_NULL_V(env, false);
479 return env->volumetric_fog_enabled;
480 }
481
482 float RendererEnvironmentStorage::environment_get_volumetric_fog_density(RID p_env) const {
483 Environment *env = environment_owner.get_or_null(p_env);
484 ERR_FAIL_NULL_V(env, 0.01);
485 return env->volumetric_fog_density;
486 }
487
488 Color RendererEnvironmentStorage::environment_get_volumetric_fog_scattering(RID p_env) const {
489 Environment *env = environment_owner.get_or_null(p_env);
490 ERR_FAIL_NULL_V(env, Color(1, 1, 1));
491 return env->volumetric_fog_scattering;
492 }
493
494 Color RendererEnvironmentStorage::environment_get_volumetric_fog_emission(RID p_env) const {
495 Environment *env = environment_owner.get_or_null(p_env);
496 ERR_FAIL_NULL_V(env, Color(0, 0, 0));
497 return env->volumetric_fog_emission;
498 }
499
500 float RendererEnvironmentStorage::environment_get_volumetric_fog_emission_energy(RID p_env) const {
501 Environment *env = environment_owner.get_or_null(p_env);
502 ERR_FAIL_NULL_V(env, 0.0);
503 return env->volumetric_fog_emission_energy;
504 }
505
506 float RendererEnvironmentStorage::environment_get_volumetric_fog_anisotropy(RID p_env) const {
507 Environment *env = environment_owner.get_or_null(p_env);
508 ERR_FAIL_NULL_V(env, 0.2);
509 return env->volumetric_fog_anisotropy;
510 }
511
512 float RendererEnvironmentStorage::environment_get_volumetric_fog_length(RID p_env) const {
513 Environment *env = environment_owner.get_or_null(p_env);
514 ERR_FAIL_NULL_V(env, 64.0);
515 return env->volumetric_fog_length;
516 }
517
518 float RendererEnvironmentStorage::environment_get_volumetric_fog_detail_spread(RID p_env) const {
519 Environment *env = environment_owner.get_or_null(p_env);
520 ERR_FAIL_NULL_V(env, 2.0);
521 return env->volumetric_fog_detail_spread;
522 }
523
524 float RendererEnvironmentStorage::environment_get_volumetric_fog_gi_inject(RID p_env) const {
525 Environment *env = environment_owner.get_or_null(p_env);
526 ERR_FAIL_NULL_V(env, 0.0);
527 return env->volumetric_fog_gi_inject;
528 }
529
530 float RendererEnvironmentStorage::environment_get_volumetric_fog_sky_affect(RID p_env) const {
531 Environment *env = environment_owner.get_or_null(p_env);
532 ERR_FAIL_NULL_V(env, 0.0);
533 return env->volumetric_fog_sky_affect;
534 }
535
536 bool RendererEnvironmentStorage::environment_get_volumetric_fog_temporal_reprojection(RID p_env) const {
537 Environment *env = environment_owner.get_or_null(p_env);
538 ERR_FAIL_NULL_V(env, true);
539 return env->volumetric_fog_temporal_reprojection;
540 }
541
542 float RendererEnvironmentStorage::environment_get_volumetric_fog_temporal_reprojection_amount(RID p_env) const {
543 Environment *env = environment_owner.get_or_null(p_env);
544 ERR_FAIL_NULL_V(env, 0.9);
545 return env->volumetric_fog_temporal_reprojection_amount;
546 }
547
548 float RendererEnvironmentStorage::environment_get_volumetric_fog_ambient_inject(RID p_env) const {
549 Environment *env = environment_owner.get_or_null(p_env);
550 ERR_FAIL_NULL_V(env, 0.0);
551 return env->volumetric_fog_ambient_inject;
552 }
553
554 // GLOW
555
556 void RendererEnvironmentStorage::environment_set_glow(RID p_env, bool p_enable, Vector<float> p_levels, float p_intensity, float p_strength, float p_mix, float p_bloom_threshold, RS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, float p_glow_map_strength, RID p_glow_map) {
557 Environment *env = environment_owner.get_or_null(p_env);
558 ERR_FAIL_NULL(env);
559 ERR_FAIL_COND_MSG(p_levels.size() != 7, "Size of array of glow levels must be 7");
560 env->glow_enabled = p_enable;
561 env->glow_levels = p_levels;
562 env->glow_intensity = p_intensity;
563 env->glow_strength = p_strength;
564 env->glow_mix = p_mix;
565 env->glow_bloom = p_bloom_threshold;
566 env->glow_blend_mode = p_blend_mode;
567 env->glow_hdr_bleed_threshold = p_hdr_bleed_threshold;
568 env->glow_hdr_bleed_scale = p_hdr_bleed_scale;
569 env->glow_hdr_luminance_cap = p_hdr_luminance_cap;
570 env->glow_map_strength = p_glow_map_strength;
571 env->glow_map = p_glow_map;
572 }
573
574 bool RendererEnvironmentStorage::environment_get_glow_enabled(RID p_env) const {
575 Environment *env = environment_owner.get_or_null(p_env);
576 ERR_FAIL_NULL_V(env, false);
577 return env->glow_enabled;
578 }
579
580 Vector<float> RendererEnvironmentStorage::environment_get_glow_levels(RID p_env) const {
581 Environment *env = environment_owner.get_or_null(p_env);
582 ERR_FAIL_NULL_V(env, Vector<float>());
583 return env->glow_levels;
584 }
585
586 float RendererEnvironmentStorage::environment_get_glow_intensity(RID p_env) const {
587 Environment *env = environment_owner.get_or_null(p_env);
588 ERR_FAIL_NULL_V(env, 0.3);
589 return env->glow_intensity;
590 }
591
592 float RendererEnvironmentStorage::environment_get_glow_strength(RID p_env) const {
593 Environment *env = environment_owner.get_or_null(p_env);
594 ERR_FAIL_NULL_V(env, 1.0);
595 return env->glow_strength;
596 }
597
598 float RendererEnvironmentStorage::environment_get_glow_bloom(RID p_env) const {
599 Environment *env = environment_owner.get_or_null(p_env);
600 ERR_FAIL_NULL_V(env, 0.0);
601 return env->glow_bloom;
602 }
603
604 float RendererEnvironmentStorage::environment_get_glow_mix(RID p_env) const {
605 Environment *env = environment_owner.get_or_null(p_env);
606 ERR_FAIL_NULL_V(env, 0.01);
607 return env->glow_mix;
608 }
609
610 RS::EnvironmentGlowBlendMode RendererEnvironmentStorage::environment_get_glow_blend_mode(RID p_env) const {
611 Environment *env = environment_owner.get_or_null(p_env);
612 ERR_FAIL_NULL_V(env, RS::ENV_GLOW_BLEND_MODE_SCREEN);
613 return env->glow_blend_mode;
614 }
615
616 float RendererEnvironmentStorage::environment_get_glow_hdr_bleed_threshold(RID p_env) const {
617 Environment *env = environment_owner.get_or_null(p_env);
618 ERR_FAIL_NULL_V(env, 1.0);
619 return env->glow_hdr_bleed_threshold;
620 }
621
622 float RendererEnvironmentStorage::environment_get_glow_hdr_luminance_cap(RID p_env) const {
623 Environment *env = environment_owner.get_or_null(p_env);
624 ERR_FAIL_NULL_V(env, 12.0);
625 return env->glow_hdr_luminance_cap;
626 }
627
628 float RendererEnvironmentStorage::environment_get_glow_hdr_bleed_scale(RID p_env) const {
629 Environment *env = environment_owner.get_or_null(p_env);
630 ERR_FAIL_NULL_V(env, 2.0);
631 return env->glow_hdr_bleed_scale;
632 }
633
634 float RendererEnvironmentStorage::environment_get_glow_map_strength(RID p_env) const {
635 Environment *env = environment_owner.get_or_null(p_env);
636 ERR_FAIL_NULL_V(env, 0.0);
637 return env->glow_map_strength;
638 }
639
640 RID RendererEnvironmentStorage::environment_get_glow_map(RID p_env) const {
641 Environment *env = environment_owner.get_or_null(p_env);
642 ERR_FAIL_NULL_V(env, RID());
643 return env->glow_map;
644 }
645
646 // SSR
647
648 void RendererEnvironmentStorage::environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_int, float p_fade_out, float p_depth_tolerance) {
649 Environment *env = environment_owner.get_or_null(p_env);
650 ERR_FAIL_NULL(env);
651 #ifdef DEBUG_ENABLED
652 if (OS::get_singleton()->get_current_rendering_method() != "forward_plus" && p_enable) {
653 WARN_PRINT_ONCE_ED("Screen-space reflections (SSR) are only available when using the Forward+ renderer.");
654 }
655 #endif
656 env->ssr_enabled = p_enable;
657 env->ssr_max_steps = p_max_steps;
658 env->ssr_fade_in = p_fade_int;
659 env->ssr_fade_out = p_fade_out;
660 env->ssr_depth_tolerance = p_depth_tolerance;
661 }
662
663 bool RendererEnvironmentStorage::environment_get_ssr_enabled(RID p_env) const {
664 Environment *env = environment_owner.get_or_null(p_env);
665 ERR_FAIL_NULL_V(env, false);
666 return env->ssr_enabled;
667 }
668
669 int RendererEnvironmentStorage::environment_get_ssr_max_steps(RID p_env) const {
670 Environment *env = environment_owner.get_or_null(p_env);
671 ERR_FAIL_NULL_V(env, 64);
672 return env->ssr_max_steps;
673 }
674
675 float RendererEnvironmentStorage::environment_get_ssr_fade_in(RID p_env) const {
676 Environment *env = environment_owner.get_or_null(p_env);
677 ERR_FAIL_NULL_V(env, 0.15);
678 return env->ssr_fade_in;
679 }
680
681 float RendererEnvironmentStorage::environment_get_ssr_fade_out(RID p_env) const {
682 Environment *env = environment_owner.get_or_null(p_env);
683 ERR_FAIL_NULL_V(env, 2.0);
684 return env->ssr_fade_out;
685 }
686
687 float RendererEnvironmentStorage::environment_get_ssr_depth_tolerance(RID p_env) const {
688 Environment *env = environment_owner.get_or_null(p_env);
689 ERR_FAIL_NULL_V(env, 0.5);
690 return env->ssr_depth_tolerance;
691 }
692
693 // SSAO
694
695 void RendererEnvironmentStorage::environment_set_ssao(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_power, float p_detail, float p_horizon, float p_sharpness, float p_light_affect, float p_ao_channel_affect) {
696 Environment *env = environment_owner.get_or_null(p_env);
697 ERR_FAIL_NULL(env);
698 #ifdef DEBUG_ENABLED
699 if (OS::get_singleton()->get_current_rendering_method() == "mobile" && p_enable) {
700 WARN_PRINT_ONCE_ED("Screen-space ambient occlusion (SSAO) is only available when using the Forward+ or Compatibility renderers.");
701 }
702 #endif
703 env->ssao_enabled = p_enable;
704 env->ssao_radius = p_radius;
705 env->ssao_intensity = p_intensity;
706 env->ssao_power = p_power;
707 env->ssao_detail = p_detail;
708 env->ssao_horizon = p_horizon;
709 env->ssao_sharpness = p_sharpness;
710 env->ssao_direct_light_affect = p_light_affect;
711 env->ssao_ao_channel_affect = p_ao_channel_affect;
712 }
713
714 bool RendererEnvironmentStorage::environment_get_ssao_enabled(RID p_env) const {
715 Environment *env = environment_owner.get_or_null(p_env);
716 ERR_FAIL_NULL_V(env, false);
717 return env->ssao_enabled;
718 }
719
720 float RendererEnvironmentStorage::environment_get_ssao_radius(RID p_env) const {
721 Environment *env = environment_owner.get_or_null(p_env);
722 ERR_FAIL_NULL_V(env, 1.0);
723 return env->ssao_radius;
724 }
725
726 float RendererEnvironmentStorage::environment_get_ssao_intensity(RID p_env) const {
727 Environment *env = environment_owner.get_or_null(p_env);
728 ERR_FAIL_NULL_V(env, 2.0);
729 return env->ssao_intensity;
730 }
731
732 float RendererEnvironmentStorage::environment_get_ssao_power(RID p_env) const {
733 Environment *env = environment_owner.get_or_null(p_env);
734 ERR_FAIL_NULL_V(env, 1.5);
735 return env->ssao_power;
736 }
737
738 float RendererEnvironmentStorage::environment_get_ssao_detail(RID p_env) const {
739 Environment *env = environment_owner.get_or_null(p_env);
740 ERR_FAIL_NULL_V(env, 0.5);
741 return env->ssao_detail;
742 }
743
744 float RendererEnvironmentStorage::environment_get_ssao_horizon(RID p_env) const {
745 Environment *env = environment_owner.get_or_null(p_env);
746 ERR_FAIL_NULL_V(env, 0.06);
747 return env->ssao_horizon;
748 }
749
750 float RendererEnvironmentStorage::environment_get_ssao_sharpness(RID p_env) const {
751 Environment *env = environment_owner.get_or_null(p_env);
752 ERR_FAIL_NULL_V(env, 0.98);
753 return env->ssao_sharpness;
754 }
755
756 float RendererEnvironmentStorage::environment_get_ssao_direct_light_affect(RID p_env) const {
757 Environment *env = environment_owner.get_or_null(p_env);
758 ERR_FAIL_NULL_V(env, 0.0);
759 return env->ssao_direct_light_affect;
760 }
761
762 float RendererEnvironmentStorage::environment_get_ssao_ao_channel_affect(RID p_env) const {
763 Environment *env = environment_owner.get_or_null(p_env);
764 ERR_FAIL_NULL_V(env, 0.0);
765 return env->ssao_ao_channel_affect;
766 }
767
768 // SSIL
769
770 void RendererEnvironmentStorage::environment_set_ssil(RID p_env, bool p_enable, float p_radius, float p_intensity, float p_sharpness, float p_normal_rejection) {
771 Environment *env = environment_owner.get_or_null(p_env);
772 ERR_FAIL_NULL(env);
773 #ifdef DEBUG_ENABLED
774 if (OS::get_singleton()->get_current_rendering_method() != "forward_plus" && p_enable) {
775 WARN_PRINT_ONCE_ED("Screen-space indirect lighting (SSIL) is only available when using the Forward+ renderer.");
776 }
777 #endif
778 env->ssil_enabled = p_enable;
779 env->ssil_radius = p_radius;
780 env->ssil_intensity = p_intensity;
781 env->ssil_sharpness = p_sharpness;
782 env->ssil_normal_rejection = p_normal_rejection;
783 }
784
785 bool RendererEnvironmentStorage::environment_get_ssil_enabled(RID p_env) const {
786 Environment *env = environment_owner.get_or_null(p_env);
787 ERR_FAIL_NULL_V(env, false);
788 return env->ssil_enabled;
789 }
790
791 float RendererEnvironmentStorage::environment_get_ssil_radius(RID p_env) const {
792 Environment *env = environment_owner.get_or_null(p_env);
793 ERR_FAIL_NULL_V(env, 5.0);
794 return env->ssil_radius;
795 }
796
797 float RendererEnvironmentStorage::environment_get_ssil_intensity(RID p_env) const {
798 Environment *env = environment_owner.get_or_null(p_env);
799 ERR_FAIL_NULL_V(env, 1.0);
800 return env->ssil_intensity;
801 }
802
803 float RendererEnvironmentStorage::environment_get_ssil_sharpness(RID p_env) const {
804 Environment *env = environment_owner.get_or_null(p_env);
805 ERR_FAIL_NULL_V(env, 0.98);
806 return env->ssil_sharpness;
807 }
808
809 float RendererEnvironmentStorage::environment_get_ssil_normal_rejection(RID p_env) const {
810 Environment *env = environment_owner.get_or_null(p_env);
811 ERR_FAIL_NULL_V(env, 1.0);
812 return env->ssil_normal_rejection;
813 }
814
815 // SDFGI
816
817 void RendererEnvironmentStorage::environment_set_sdfgi(RID p_env, bool p_enable, int p_cascades, float p_min_cell_size, RS::EnvironmentSDFGIYScale p_y_scale, bool p_use_occlusion, float p_bounce_feedback, bool p_read_sky, float p_energy, float p_normal_bias, float p_probe_bias) {
818 Environment *env = environment_owner.get_or_null(p_env);
819 ERR_FAIL_NULL(env);
820 #ifdef DEBUG_ENABLED
821 if (OS::get_singleton()->get_current_rendering_method() != "forward_plus" && p_enable) {
822 WARN_PRINT_ONCE_ED("SDFGI is only available when using the Forward+ renderer.");
823 }
824 #endif
825 env->sdfgi_enabled = p_enable;
826 env->sdfgi_cascades = p_cascades;
827 env->sdfgi_min_cell_size = p_min_cell_size;
828 env->sdfgi_use_occlusion = p_use_occlusion;
829 env->sdfgi_bounce_feedback = p_bounce_feedback;
830 env->sdfgi_read_sky_light = p_read_sky;
831 env->sdfgi_energy = p_energy;
832 env->sdfgi_normal_bias = p_normal_bias;
833 env->sdfgi_probe_bias = p_probe_bias;
834 env->sdfgi_y_scale = p_y_scale;
835 }
836
837 bool RendererEnvironmentStorage::environment_get_sdfgi_enabled(RID p_env) const {
838 Environment *env = environment_owner.get_or_null(p_env);
839 ERR_FAIL_NULL_V(env, false);
840 return env->sdfgi_enabled;
841 }
842
843 int RendererEnvironmentStorage::environment_get_sdfgi_cascades(RID p_env) const {
844 Environment *env = environment_owner.get_or_null(p_env);
845 ERR_FAIL_NULL_V(env, 4);
846 return env->sdfgi_cascades;
847 }
848
849 float RendererEnvironmentStorage::environment_get_sdfgi_min_cell_size(RID p_env) const {
850 Environment *env = environment_owner.get_or_null(p_env);
851 ERR_FAIL_NULL_V(env, 0.2);
852 return env->sdfgi_min_cell_size;
853 }
854
855 bool RendererEnvironmentStorage::environment_get_sdfgi_use_occlusion(RID p_env) const {
856 Environment *env = environment_owner.get_or_null(p_env);
857 ERR_FAIL_NULL_V(env, false);
858 return env->sdfgi_use_occlusion;
859 }
860
861 float RendererEnvironmentStorage::environment_get_sdfgi_bounce_feedback(RID p_env) const {
862 Environment *env = environment_owner.get_or_null(p_env);
863 ERR_FAIL_NULL_V(env, 0.5);
864 return env->sdfgi_bounce_feedback;
865 }
866
867 bool RendererEnvironmentStorage::environment_get_sdfgi_read_sky_light(RID p_env) const {
868 Environment *env = environment_owner.get_or_null(p_env);
869 ERR_FAIL_NULL_V(env, true);
870 return env->sdfgi_read_sky_light;
871 }
872
873 float RendererEnvironmentStorage::environment_get_sdfgi_energy(RID p_env) const {
874 Environment *env = environment_owner.get_or_null(p_env);
875 ERR_FAIL_NULL_V(env, 1.0);
876 return env->sdfgi_energy;
877 }
878
879 float RendererEnvironmentStorage::environment_get_sdfgi_normal_bias(RID p_env) const {
880 Environment *env = environment_owner.get_or_null(p_env);
881 ERR_FAIL_NULL_V(env, 1.1);
882 return env->sdfgi_normal_bias;
883 }
884
885 float RendererEnvironmentStorage::environment_get_sdfgi_probe_bias(RID p_env) const {
886 Environment *env = environment_owner.get_or_null(p_env);
887 ERR_FAIL_NULL_V(env, 1.1);
888 return env->sdfgi_probe_bias;
889 }
890
891 RS::EnvironmentSDFGIYScale RendererEnvironmentStorage::environment_get_sdfgi_y_scale(RID p_env) const {
892 Environment *env = environment_owner.get_or_null(p_env);
893 ERR_FAIL_NULL_V(env, RS::ENV_SDFGI_Y_SCALE_75_PERCENT);
894 return env->sdfgi_y_scale;
895 }
896
897 // Adjustments
898
899 void RendererEnvironmentStorage::environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, bool p_use_1d_color_correction, RID p_color_correction) {
900 Environment *env = environment_owner.get_or_null(p_env);
901 ERR_FAIL_NULL(env);
902
903 env->adjustments_enabled = p_enable;
904 // Scale brightness via the nonlinear sRGB transfer function to provide a
905 // somewhat perceptually uniform brightness adjustment.
906 env->adjustments_brightness = p_brightness < 0.04045f ? p_brightness * (1.0f / 12.92f) : Math::pow(float((p_brightness + 0.055f) * (1.0f / (1.055f))), 2.4f);
907 env->adjustments_contrast = p_contrast;
908 env->adjustments_saturation = p_saturation;
909 env->use_1d_color_correction = p_use_1d_color_correction;
910 env->color_correction = p_color_correction;
911 }
912
913 bool RendererEnvironmentStorage::environment_get_adjustments_enabled(RID p_env) const {
914 Environment *env = environment_owner.get_or_null(p_env);
915 ERR_FAIL_NULL_V(env, false);
916 return env->adjustments_enabled;
917 }
918
919 float RendererEnvironmentStorage::environment_get_adjustments_brightness(RID p_env) const {
920 Environment *env = environment_owner.get_or_null(p_env);
921 ERR_FAIL_NULL_V(env, 1.0);
922 return env->adjustments_brightness;
923 }
924
925 float RendererEnvironmentStorage::environment_get_adjustments_contrast(RID p_env) const {
926 Environment *env = environment_owner.get_or_null(p_env);
927 ERR_FAIL_NULL_V(env, 1.0);
928 return env->adjustments_contrast;
929 }
930
931 float RendererEnvironmentStorage::environment_get_adjustments_saturation(RID p_env) const {
932 Environment *env = environment_owner.get_or_null(p_env);
933 ERR_FAIL_NULL_V(env, 1.0);
934 return env->adjustments_saturation;
935 }
936
937 bool RendererEnvironmentStorage::environment_get_use_1d_color_correction(RID p_env) const {
938 Environment *env = environment_owner.get_or_null(p_env);
939 ERR_FAIL_NULL_V(env, false);
940 return env->use_1d_color_correction;
941 }
942
943 RID RendererEnvironmentStorage::environment_get_color_correction(RID p_env) const {
944 Environment *env = environment_owner.get_or_null(p_env);
945 ERR_FAIL_NULL_V(env, RID());
946 return env->color_correction;
947 }