fyrox_impl/renderer/
settings.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::core::reflect::prelude::*;
22use fyrox_core::uuid_provider;
23use serde::{Deserialize, Serialize};
24use strum_macros::{AsRefStr, EnumString, VariantNames};
25
26/// Quality settings allows you to find optimal balance between performance and
27/// graphics quality.
28#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize, Reflect)]
29pub struct QualitySettings {
30    /// Point shadows
31    /// Size of cube map face of shadow map texture in pixels.
32    pub point_shadow_map_size: usize,
33    /// Use or not percentage close filtering (smoothing) for point shadows.
34    pub point_soft_shadows: bool,
35    /// Point shadows enabled or not.
36    pub point_shadows_enabled: bool,
37    /// Maximum distance from camera to draw shadows.
38    pub point_shadows_distance: f32,
39    /// Point shadow map precision. Allows you to select compromise between
40    /// quality and performance.
41    pub point_shadow_map_precision: ShadowMapPrecision,
42    /// Point shadows fade out range.
43    /// Specifies the distance from the camera at which point shadows start to fade out.
44    /// Shadows beyond this distance will gradually become less visible.
45    pub point_shadows_fade_out_range: f32,
46
47    /// Spot shadows
48    /// Size of square shadow map texture in pixels
49    pub spot_shadow_map_size: usize,
50    /// Use or not percentage close filtering (smoothing) for spot shadows.
51    pub spot_soft_shadows: bool,
52    /// Spot shadows enabled or not.
53    pub spot_shadows_enabled: bool,
54    /// Maximum distance from camera to draw shadows.
55    pub spot_shadows_distance: f32,
56    /// Spot shadow map precision. Allows you to select compromise between
57    /// quality and performance.
58    pub spot_shadow_map_precision: ShadowMapPrecision,
59    /// Specifies the distance from the camera at which spot shadows start to fade out.
60    /// Shadows beyond this distance will gradually become less visible.
61    pub spot_shadows_fade_out_range: f32,
62
63    /// Cascaded-shadow maps settings.
64    pub csm_settings: CsmSettings,
65
66    /// Whether to use screen space ambient occlusion or not.
67    pub use_ssao: bool,
68    /// Radius of sampling hemisphere used in SSAO, it defines much ambient
69    /// occlusion will be in your scene.
70    pub ssao_radius: f32,
71
72    /// Global switch to enable or disable light scattering. Each light can have
73    /// its own scatter switch, but this one is able to globally disable scatter.
74    pub light_scatter_enabled: bool,
75
76    /// Whether to use Fast Approximate AntiAliasing or not.
77    pub fxaa: bool,
78
79    /// Whether to use Parallax Mapping or not.
80    pub use_parallax_mapping: bool,
81
82    /// Whether to use bloom effect.
83    pub use_bloom: bool,
84
85    /// Whether to use occlusion culling for geometry or not. Warning: this is experimental feature
86    /// that may have bugs and unstable behavior. Disabled by default.
87    #[serde(default)]
88    pub use_occlusion_culling: bool,
89
90    /// Whether to use occlusion culling for light sources or not. Warning: this is experimental
91    /// feature that may have bugs and unstable behavior. Disabled by default.
92    #[serde(default)]
93    pub use_light_occlusion_culling: bool,
94}
95
96impl Default for QualitySettings {
97    fn default() -> Self {
98        Self::high()
99    }
100}
101
102impl QualitySettings {
103    /// Highest possible graphics quality. Requires very powerful GPU.
104    pub fn ultra() -> Self {
105        Self {
106            point_shadow_map_size: 2048,
107            point_shadows_distance: 20.0,
108            point_shadows_enabled: true,
109            point_soft_shadows: true,
110            point_shadows_fade_out_range: 1.0,
111
112            spot_shadow_map_size: 2048,
113            spot_shadows_distance: 20.0,
114            spot_shadows_enabled: true,
115            spot_soft_shadows: true,
116            spot_shadows_fade_out_range: 1.0,
117
118            use_ssao: true,
119            ssao_radius: 0.5,
120
121            light_scatter_enabled: true,
122
123            point_shadow_map_precision: ShadowMapPrecision::Full,
124            spot_shadow_map_precision: ShadowMapPrecision::Full,
125
126            fxaa: true,
127
128            use_bloom: true,
129
130            use_parallax_mapping: true,
131
132            csm_settings: Default::default(),
133
134            use_occlusion_culling: false,
135            use_light_occlusion_culling: false,
136        }
137    }
138
139    /// High graphics quality, includes all graphical effects. Requires powerful GPU.
140    pub fn high() -> Self {
141        Self {
142            point_shadow_map_size: 1024,
143            point_shadows_distance: 15.0,
144            point_shadows_enabled: true,
145            point_soft_shadows: true,
146            point_shadows_fade_out_range: 1.0,
147
148            spot_shadow_map_size: 1024,
149            spot_shadows_distance: 15.0,
150            spot_shadows_enabled: true,
151            spot_soft_shadows: true,
152            spot_shadows_fade_out_range: 1.0,
153
154            use_ssao: true,
155            ssao_radius: 0.5,
156
157            light_scatter_enabled: true,
158
159            point_shadow_map_precision: ShadowMapPrecision::Full,
160            spot_shadow_map_precision: ShadowMapPrecision::Full,
161
162            fxaa: true,
163
164            use_bloom: true,
165
166            use_parallax_mapping: true,
167
168            csm_settings: CsmSettings {
169                enabled: true,
170                size: 2048,
171                precision: ShadowMapPrecision::Full,
172                pcf: true,
173            },
174
175            use_occlusion_culling: false,
176            use_light_occlusion_culling: false,
177        }
178    }
179
180    /// Medium graphics quality, some of effects are disabled, shadows will have sharp edges.
181    pub fn medium() -> Self {
182        Self {
183            point_shadow_map_size: 512,
184            point_shadows_distance: 5.0,
185            point_shadows_enabled: true,
186            point_soft_shadows: false,
187            point_shadows_fade_out_range: 1.0,
188
189            spot_shadow_map_size: 512,
190            spot_shadows_distance: 5.0,
191            spot_shadows_enabled: true,
192            spot_soft_shadows: false,
193            spot_shadows_fade_out_range: 1.0,
194
195            use_ssao: true,
196            ssao_radius: 0.5,
197
198            light_scatter_enabled: false,
199
200            point_shadow_map_precision: ShadowMapPrecision::Half,
201            spot_shadow_map_precision: ShadowMapPrecision::Half,
202
203            fxaa: true,
204
205            use_bloom: true,
206
207            use_parallax_mapping: false,
208
209            csm_settings: CsmSettings {
210                enabled: true,
211                size: 512,
212                precision: ShadowMapPrecision::Full,
213                pcf: false,
214            },
215
216            use_occlusion_culling: false,
217            use_light_occlusion_culling: false,
218        }
219    }
220
221    /// Lowest graphics quality, all effects are disabled.
222    pub fn low() -> Self {
223        Self {
224            point_shadow_map_size: 1, // Zero is unsupported.
225            point_shadows_distance: 0.0,
226            point_shadows_enabled: false,
227            point_soft_shadows: false,
228            point_shadows_fade_out_range: 1.0,
229
230            spot_shadow_map_size: 1,
231            spot_shadows_distance: 0.0,
232            spot_shadows_enabled: false,
233            spot_soft_shadows: false,
234            spot_shadows_fade_out_range: 1.0,
235
236            use_ssao: false,
237            ssao_radius: 0.5,
238
239            light_scatter_enabled: false,
240
241            point_shadow_map_precision: ShadowMapPrecision::Half,
242            spot_shadow_map_precision: ShadowMapPrecision::Half,
243
244            fxaa: false,
245
246            use_bloom: false,
247
248            use_parallax_mapping: false,
249
250            csm_settings: CsmSettings {
251                enabled: true,
252                size: 512,
253                precision: ShadowMapPrecision::Half,
254                pcf: false,
255            },
256
257            use_occlusion_culling: false,
258            use_light_occlusion_culling: false,
259        }
260    }
261}
262
263/// Cascaded-shadow maps settings.
264#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize, Reflect, Eq)]
265pub struct CsmSettings {
266    /// Whether cascaded shadow maps enabled or not.
267    pub enabled: bool,
268
269    /// Size of texture for each cascade.
270    pub size: usize,
271
272    /// Bit-wise precision for each cascade, the lower precision the better performance is,
273    /// but the more artifacts may occur.
274    pub precision: ShadowMapPrecision,
275
276    /// Whether to use Percentage-Closer Filtering or not.
277    pub pcf: bool,
278}
279
280impl Default for CsmSettings {
281    fn default() -> Self {
282        Self {
283            enabled: true,
284            size: 2048,
285            precision: ShadowMapPrecision::Full,
286            pcf: true,
287        }
288    }
289}
290
291/// Shadow map precision allows you to select compromise between quality and performance.
292#[derive(
293    Copy,
294    Clone,
295    Hash,
296    PartialOrd,
297    PartialEq,
298    Eq,
299    Ord,
300    Debug,
301    Serialize,
302    Deserialize,
303    Reflect,
304    AsRefStr,
305    EnumString,
306    VariantNames,
307)]
308pub enum ShadowMapPrecision {
309    /// Shadow map will use 2 times less memory by switching to 16bit pixel format,
310    /// but "shadow acne" may occur.
311    Half,
312    /// Shadow map will use 32bit pixel format. This option gives highest quality,
313    /// but could be less performant than `Half`.
314    Full,
315}
316
317uuid_provider!(ShadowMapPrecision = "f9b2755b-248e-46ba-bcab-473eac1acdb8");