fframes_three_d 1.3.0

3D scenes for fframes: a three.js style scene graph, glTF and STL models, a CPU rasterizer and a wgpu renderer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use std::f32::consts::{FRAC_PI_2, PI, TAU};
use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::{Arc, Mutex, PoisonError};

use glam::{Mat3, Vec2, Vec3, vec3};
use rayon::prelude::*;

use crate::Rgb;
use crate::color::srgb_to_linear;
use crate::math::smoothstep;

/// The light that surrounds the scene, three.js `scene.environment`.
///
/// Metals reflect the environment, and it lights the shadows. Without it, metal looks black.
#[derive(Debug, Clone, Default)]
pub enum Environment {
    /// No light from the environment. The default.
    #[default]
    None,
    /// A procedural photo studio: a gradient backdrop with rectangular softboxes, like the
    /// three.js `RoomEnvironment`. Metal shows clean highlights in it. Without softboxes it
    /// is a plain sky to ground gradient.
    Studio(Studio),
    /// An equirectangular panorama (`.hdr`, `.jpg`, `.png`), three.js `RGBELoader` with
    /// `EquirectangularReflectionMapping`.
    Map {
        map: Arc<EnvironmentMap>,
        intensity: f32,
        /// The rotation around Y in radians.
        rotation: f32,
    },
}

/// A procedural photo studio, see [`Environment::Studio`] and [`Environment::studio`].
#[derive(Debug, Clone, PartialEq)]
pub struct Studio {
    /// The linear radiance of the backdrop straight up.
    pub sky: Rgb,
    /// The linear radiance of the backdrop at the horizon.
    pub horizon: Rgb,
    /// The linear radiance of the backdrop straight down.
    pub ground: Rgb,
    pub softboxes: Vec<SoftBox>,
    /// The multiplier for all light of the studio. Default 1.
    pub intensity: f32,
    /// The rotation around Y in radians. Turn it to move the reflections over a surface.
    pub rotation: f32,
}

/// A rectangular light in a [`Studio`], as seen from the center of the scene.
#[derive(Debug, Clone, PartialEq)]
pub struct SoftBox {
    /// The unit direction from the scene to the center of the box.
    pub direction: Vec3,
    /// Half the width and half the height of the box, as angles in radians.
    pub half_size: Vec2,
    /// The linear radiance. It is usually well above 1.
    pub color: Rgb,
}

impl SoftBox {
    /// A box in `direction`, `width_degrees` wide and `height_degrees` high. The width runs
    /// parallel to the horizon.
    pub fn new(
        direction: Vec3,
        width_degrees: f32,
        height_degrees: f32,
        color: impl Into<Rgb>,
    ) -> Self {
        Self {
            direction: direction.normalize_or(Vec3::Y),
            half_size: Vec2::new(width_degrees, height_degrees).map(f32::to_radians) * 0.5,
            color: color.into(),
        }
    }

    /// The unit axes of the box: across its width and across its height.
    pub(crate) fn axes(&self) -> (Vec3, Vec3) {
        let u = self.direction.cross(Vec3::Y).normalize_or(Vec3::X);
        (u, u.cross(self.direction))
    }
}

impl Environment {
    /// A neutral dark studio for products.
    ///
    /// It has four softboxes. A large key light is above at the front left, and a strip
    /// light is on the right. A rim strip is behind, and a soft light is straight above.
    pub fn studio() -> Self {
        Environment::Studio(Studio {
            sky: Rgb::linear(0.05, 0.055, 0.065),
            horizon: Rgb::linear(0.12, 0.12, 0.13),
            ground: Rgb::linear(0.02, 0.02, 0.022),
            softboxes: vec![
                SoftBox::new(
                    vec3(-0.6, 0.75, 0.6),
                    50.0,
                    30.0,
                    Rgb::linear(3.2, 3.2, 3.3),
                ),
                SoftBox::new(
                    vec3(1.0, 0.2, 0.15),
                    14.0,
                    70.0,
                    Rgb::linear(2.4, 2.45, 2.6),
                ),
                SoftBox::new(
                    vec3(-0.3, 0.35, -1.0),
                    70.0,
                    8.0,
                    Rgb::linear(1.8, 1.9, 2.1),
                ),
                SoftBox::new(Vec3::Y, 40.0, 40.0, Rgb::linear(0.9, 0.9, 0.95)),
            ],
            intensity: 1.0,
            rotation: 0.0,
        })
    }

    /// A panorama without rotation, see [`EnvironmentMap::decode`].
    pub fn map(map: Arc<EnvironmentMap>, intensity: f32) -> Self {
        Environment::Map {
            map,
            intensity,
            rotation: 0.0,
        }
    }

    /// Sets the brightness multiplier.
    pub fn intensity(mut self, value: f32) -> Self {
        match &mut self {
            Environment::None => {}
            Environment::Studio(Studio { intensity, .. }) | Environment::Map { intensity, .. } => {
                *intensity = value;
            }
        }
        self
    }

    /// Sets the rotation around Y in radians.
    pub fn rotated(mut self, radians: f32) -> Self {
        match &mut self {
            Environment::Studio(Studio { rotation, .. }) | Environment::Map { rotation, .. } => {
                *rotation = radians;
            }
            Environment::None => {}
        }
        self
    }

    /// The light that arrives from `dir`, blurred as a surface of `roughness` reflects it.
    pub fn radiance(&self, dir: Vec3, roughness: f32) -> Vec3 {
        match self {
            Environment::None => Vec3::ZERO,
            Environment::Studio(studio) => studio.radiance(dir, roughness),
            Environment::Map {
                map,
                intensity,
                rotation,
            } => map.sample(rotate_y(dir, -*rotation), roughness) * *intensity,
        }
    }

    /// The baked panorama of a studio, see [`Studio::baked`]. `None` for the other kinds: a
    /// map is a panorama already.
    pub(crate) fn baked(&self) -> Option<Arc<EnvironmentMap>> {
        match self {
            Environment::Studio(studio) => Some(studio.baked()),
            _ => None,
        }
    }

    /// The diffuse light on a surface that faces `normal`. It is the cosine weighted mean of
    /// the incoming light, divided by π.
    pub fn irradiance(&self, normal: Vec3) -> Vec3 {
        match self {
            Environment::None => Vec3::ZERO,
            Environment::Studio(studio) => studio.irradiance(normal),
            Environment::Map {
                map,
                intensity,
                rotation,
            } => map.irradiance(rotate_y(normal, -*rotation)) * *intensity,
        }
    }
}

/// Rotates `v` by `angle` radians around the Y axis.
pub(crate) fn rotate_y(v: Vec3, angle: f32) -> Vec3 {
    if angle == 0.0 {
        v
    } else {
        Mat3::from_rotation_y(angle) * v
    }
}

/// The color of a gradient for a unit direction with height `y`. A larger `softness` gives a
/// wider blend at the horizon.
fn gradient(y: f32, sky: Rgb, horizon: Rgb, ground: Rgb, softness: f32) -> Vec3 {
    let width = 0.08 + softness * 0.6;
    if y >= 0.0 {
        horizon.0.lerp(sky.0, smoothstep(0.0, width + 0.35, y))
    } else {
        horizon.0.lerp(ground.0, smoothstep(0.0, width, -y))
    }
}

/// Returns `true` when a direction is outside a softbox, where the mask of the box is 0.
///
/// `cos_angle` is the cosine of the angle to the center of the box. `a` and `b` are the half
/// angles of the box plus its soft edge. The test is exact: tan²(angle) is
/// tan²(across) + tan²(along), and tan² is convex. Thus beyond the angle √(a² + b²), one of
/// the two angles is past its edge.
fn outside_box(cos_angle: f32, a: f32, b: f32) -> bool {
    a.max(b) < FRAC_PI_2 && cos_angle <= (a * a + b * b).sqrt().cos().max(0.0)
}

/// The number of levels of a baked [`Studio`]. Level `k` shows the studio through the
/// roughness `(k / (BAKED_LEVELS - 1))²`, the level that [`EnvironmentMap::sample`] picks.
const BAKED_LEVELS: usize = 8;
/// The width of the first baked level in texels: 0.35° for each texel. The softest box edge
/// is 0.86° wide.
const BAKED_WIDTH: usize = 1024;

/// The 8 most recent baked studios, keyed by `Studio::bake_key`.
static BAKED: Mutex<Vec<(u64, Arc<EnvironmentMap>)>> = Mutex::new(Vec::new());

impl Studio {
    /// The light of the studio for all directions and roughness values, as a prefiltered
    /// panorama.
    ///
    /// The first call for a distinct studio bakes the panorama. Later calls get it from a
    /// cache. Rotation and intensity apply at sampling time, so they do not cause a new bake.
    /// A reflection then costs one texture lookup instead of the softbox math. The panorama
    /// matches [`Studio::radiance`] to within its texels.
    pub(crate) fn baked(&self) -> Arc<EnvironmentMap> {
        let key = self.bake_key();
        let cached = || {
            let cache = BAKED.lock().unwrap_or_else(PoisonError::into_inner);
            let map = cache
                .iter()
                .find(|(k, _)| *k == key)
                .map(|(_, map)| Arc::clone(map));
            (cache, map)
        };
        if let (_, Some(map)) = cached() {
            return map;
        }
        // The bake runs without the lock. It runs on rayon, and while a thread waits for
        // its part of the bake, rayon can give it another render that needs the panorama.
        // Threads that start at the same time can bake the same studio, the first map stays.
        let map = Arc::new(self.bake());
        let (mut cache, existing) = cached();
        if let Some(existing) = existing {
            return existing;
        }
        if cache.len() >= 8 {
            cache.remove(0);
        }
        cache.push((key, Arc::clone(&map)));
        map
    }

    /// A hash of the fields that change the bake. Rotation and intensity do not change it.
    fn bake_key(&self) -> u64 {
        let mut hasher = DefaultHasher::new();
        let mut add = |values: &[f32]| values.iter().for_each(|v| v.to_bits().hash(&mut hasher));
        for color in [self.sky, self.horizon, self.ground] {
            add(&color.0.to_array());
        }
        for b in &self.softboxes {
            add(&b.direction.to_array());
            add(&b.half_size.to_array());
            add(&b.color.0.to_array());
        }
        hasher.finish()
    }

    fn bake(&self) -> EnvironmentMap {
        let plain = Studio {
            rotation: 0.0,
            intensity: 1.0,
            ..self.clone()
        };
        let levels = (0..BAKED_LEVELS)
            .map(|k| {
                let width = BAKED_WIDTH >> k;
                let height = width / 2;
                let roughness = (k as f32 / (BAKED_LEVELS - 1) as f32).powi(2);
                let mut data = vec![Vec3::ZERO; width * height];
                data.par_chunks_mut(width).enumerate().for_each(|(y, row)| {
                    for (x, out) in row.iter_mut().enumerate() {
                        let uv = Vec2::new(
                            (x as f32 + 0.5) / width as f32,
                            (y as f32 + 0.5) / height as f32,
                        );
                        *out = plain.radiance(EnvironmentMap::dir(uv), roughness);
                    }
                });
                Level {
                    width,
                    height,
                    data,
                }
            })
            .collect();
        EnvironmentMap { levels }
    }

    /// The light that arrives from `dir`, blurred as a surface of `roughness` reflects it.
    fn radiance(&self, dir: Vec3, roughness: f32) -> Vec3 {
        let dir = rotate_y(dir, -self.rotation);
        let blur = roughness * roughness * 1.4;
        let soft = blur + 0.015;
        let mut color = gradient(dir.y, self.sky, self.horizon, self.ground, blur);
        for b in &self.softboxes {
            let d = dir.dot(b.direction);
            let half = b.half_size;
            if d <= -0.2 || outside_box(d, half.x + soft, half.y + soft) {
                continue;
            }
            let (u, v) = b.axes();
            let inside =
                |angle: f32, half: f32| 1.0 - smoothstep(half - soft * 0.5, half + soft, angle);
            let mask = inside(dir.dot(u).atan2(d).abs(), half.x)
                * inside(dir.dot(v).atan2(d).abs(), half.y);
            if mask > 0.0 {
                // The soft edge spreads the light over a larger area. This keeps the total
                // energy of the box.
                let energy = (half.x * half.y) / ((half.x + soft * 0.75) * (half.y + soft * 0.75));
                color += b.color.0 * mask * energy;
            }
        }
        color * self.intensity
    }

    /// The diffuse light on a surface that faces `normal`, see [`Environment::irradiance`].
    fn irradiance(&self, normal: Vec3) -> Vec3 {
        let normal = rotate_y(normal, -self.rotation);
        let mut color = gradient(normal.y, self.sky, self.horizon, self.ground, 1.0);
        for b in &self.softboxes {
            let solid_angle = 4.0 * b.half_size.x.sin() * b.half_size.y.sin();
            color += b.color.0 * (solid_angle * normal.dot(b.direction).max(0.0) / PI);
        }
        color * self.intensity
    }
}

/// A prefiltered equirectangular panorama: a chain of levels, each one blurrier and half
/// the size of the one before. Rough surfaces read the blurry levels.
pub struct EnvironmentMap {
    levels: Vec<Level>,
}

impl std::fmt::Debug for EnvironmentMap {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EnvironmentMap")
            .field("levels", &self.levels.len())
            .finish()
    }
}

/// One level of an [`EnvironmentMap`]: linear RGB texels, row by row from the top.
struct Level {
    width: usize,
    height: usize,
    data: Vec<Vec3>,
}

impl Level {
    /// The bilinear sample at `uv`. U wraps around, v stops at the poles.
    fn sample(&self, uv: Vec2) -> Vec3 {
        let x = uv.x * self.width as f32 - 0.5;
        let y = (uv.y * self.height as f32 - 0.5).clamp(0.0, self.height as f32 - 1.0);
        let x0 = x.floor();
        let y0 = y.floor();
        let (fx, fy) = (x - x0, y - y0);
        let xi = (x0 as i64).rem_euclid(self.width as i64) as usize;
        let xi1 = (xi + 1) % self.width;
        let yi = y0 as usize;
        let yi1 = (yi + 1).min(self.height - 1);
        let at = |x: usize, y: usize| self.data[y * self.width + x];
        at(xi, yi)
            .lerp(at(xi1, yi), fx)
            .lerp(at(xi, yi1).lerp(at(xi1, yi1), fx), fy)
    }

    /// The level at half the size, filtered with a 4x4 tent.
    fn downsample(&self) -> Level {
        const TENT: [f32; 4] = [1.0, 3.0, 3.0, 1.0];
        let width = (self.width / 2).max(2);
        let height = (self.height / 2).max(1);
        let mut data = vec![Vec3::ZERO; width * height];
        for y in 0..height {
            for x in 0..width {
                let mut sum = Vec3::ZERO;
                for (dy, wy) in (-1i64..=2).zip(TENT) {
                    for (dx, wx) in (-1i64..=2).zip(TENT) {
                        let sx = (x as i64 * 2 + dx).rem_euclid(self.width as i64) as usize;
                        let sy = (y as i64 * 2 + dy).clamp(0, self.height as i64 - 1) as usize;
                        sum += self.data[sy * self.width + sx] * (wx * wy);
                    }
                }
                // The 16 weights add up to 64.
                data[y * width + x] = sum / 64.0;
            }
        }
        Level {
            width,
            height,
            data,
        }
    }
}

impl EnvironmentMap {
    /// Decodes an equirectangular panorama. A Radiance `.hdr` file is linear, an 8-bit
    /// image is sRGB.
    pub fn decode(bytes: &[u8]) -> Result<Self, image::ImageError> {
        let image = image::load_from_memory(bytes)?;
        let is_float = matches!(
            image,
            image::DynamicImage::ImageRgb32F(_) | image::DynamicImage::ImageRgba32F(_)
        );
        let to_linear = |c: f32| if is_float { c } else { srgb_to_linear(c) };
        let rgb = image.to_rgb32f();
        let data = rgb
            .pixels()
            .map(|p| Vec3::from(p.0.map(to_linear)))
            .collect();
        Ok(Self::from_linear(
            rgb.width() as usize,
            rgb.height() as usize,
            data,
        ))
    }

    /// A panorama from linear RGB texels, row by row from the top. Levels wider than 1024
    /// texels are halved until they fit. The smallest level is 8 texels wide or less.
    pub fn from_linear(width: usize, height: usize, data: Vec<Vec3>) -> Self {
        let mut level = Level {
            width,
            height,
            data,
        };
        while level.width > 1024 {
            level = level.downsample();
        }
        let mut levels = vec![level];
        while let Some(last) = levels.last().filter(|l| l.width > 8) {
            levels.push(last.downsample());
        }
        Self { levels }
    }

    /// The prefiltered levels as width, height and linear RGB texels, sharpest first.
    #[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
    pub(crate) fn levels(&self) -> impl Iterator<Item = (usize, usize, &[Vec3])> {
        self.levels
            .iter()
            .map(|l| (l.width, l.height, l.data.as_slice()))
    }

    /// The panorama position of a direction.
    fn uv(dir: Vec3) -> Vec2 {
        let dir = dir.normalize_or(Vec3::Y);
        Vec2::new(
            dir.z.atan2(dir.x) / TAU + 0.5,
            0.5 - dir.y.clamp(-1.0, 1.0).asin() / PI,
        )
    }

    /// The unit direction of a panorama position, the inverse of [`Self::uv`].
    fn dir(uv: Vec2) -> Vec3 {
        let azimuth = (uv.x - 0.5) * TAU;
        let elevation = (0.5 - uv.y) * PI;
        let (sin_e, cos_e) = elevation.sin_cos();
        let (sin_a, cos_a) = azimuth.sin_cos();
        Vec3::new(cos_e * cos_a, sin_e, cos_e * sin_a)
    }

    /// The light from `dir`, blurred as a surface of `roughness` reflects it. A mirror
    /// reads level 0, a rough surface the smallest levels.
    pub(crate) fn sample(&self, dir: Vec3, roughness: f32) -> Vec3 {
        let uv = Self::uv(dir);
        let last = self.levels.len() - 1;
        let lod = (roughness.clamp(0.0, 1.0).sqrt() * last as f32).min(last as f32);
        let l0 = lod.floor() as usize;
        let l1 = (l0 + 1).min(last);
        self.levels[l0]
            .sample(uv)
            .lerp(self.levels[l1].sample(uv), lod - l0 as f32)
    }

    /// The diffuse light on a surface that faces `normal`, from the smallest level.
    fn irradiance(&self, normal: Vec3) -> Vec3 {
        self.levels[self.levels.len() - 1].sample(Self::uv(normal))
    }
}