sgl-3d 0.4.2

Caller-driven 3D rendering for SGL games, native and in the browser (WebGPU)
Documentation
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
//! glTF 2.0 mesh loading for the SGL renderer.
//!
//! Rigid nodes' transforms are baked into vertices, mirrored winding is
//! corrected, and primitives are batched by material, skin and morphed node;
//! skins, morph targets, the node hierarchy and animation clips are imported
//! as plain data (`deformation`). The default scene (or first scene)
//! is loaded. An extension the file requires that SGL3D does not support,
//! and other unsupported visible features, return an error containing the
//! asset path instead of producing a partial model; an unsupported extension
//! the file only uses is left out and listed in [`Asset::ignored`]. Opaque,
//! masked and blended alpha modes are supported. Supported material
//! extensions are scalar clearcoat, anisotropy with authored tangents,
//! emissive strength, unlit, bump mapping, IOR and specular factors.
use std::error::Error;

use glam::Vec3;
use gltf::texture::WrappingMode;

use super::deformation::{MeshDeformation, Rig};
pub use super::gltf::{
    GltfImage, Ignored, ImageSource, LoadOptions, load, load_slice, load_slice_with_options,
    load_with_options,
};
pub use super::images::{CompressedFormat, CompressedImage, Image};
use super::material::{AlphaMode, NormalLayer};

/// A loader failure, including the source asset path for file loads.
pub type Result<T> = std::result::Result<T, Box<dyn Error>>;

/// Interleaved vertex input used by the renderer, in the asset's coordinate system.
/// The scene packs each into 32 bytes when its model is prepared
/// (`PreparedModel::new`; the README's asset limits give the precision).
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
    /// Position after authored node transforms have been applied, kept exact.
    pub position: [f32; 3],
    /// Unit surface normal, transformed by the inverse transpose of the node matrix.
    /// Finite and not zero, or the mesh is refused; kept within 0.01°.
    pub normal: [f32; 3],
    /// First texture coordinate set (`TEXCOORD_0`), kept as 16-bit fractions
    /// of the rectangle its mesh's UVs span. Finite, or the mesh is refused,
    /// as is a mesh whose UVs span more than `f32` holds.
    pub uv: [f32; 2],
    /// Linear vertex color multiplier, including alpha. Clamped to 0..=1 and
    /// kept as 8-bit sRGB with 8-bit linear alpha.
    pub color: [f32; 4],
    /// TEXCOORD_1: secondary normalized UV into the caller-baked static irradiance atlas.
    /// (0, 0) or a negative UV marks a surface without an atlas chart: it has no
    /// baked lighting, so baked scene lights light it. Kept as 16-bit fractions.
    /// Finite, or the mesh is refused.
    pub lightmap_uv: [f32; 2],
    /// Normalized atlas min/max for a conservatively cropped triangle chart.
    /// Supply the same bounds on all triangle vertices; outside is black.
    /// A mesh's vertices may name at most 65,536 distinct bounds.
    pub lightmap_bounds: [f32; 4],
    /// Unit tangent XYZ and bitangent handedness W after node transforms.
    /// Zero means absent for legacy isotropic geometry, and for an imported
    /// tangent along its normal; a tangent within 0.02° of its normal is
    /// treated as absent. Anisotropy requires a valid frame.
    /// Made perpendicular to the normal and unit, then kept within 0.01°.
    pub tangent: [f32; 4],
}

/// An indexed triangle mesh retained on the CPU.
#[derive(Clone)]
pub struct CpuMesh {
    /// Vertex attributes shared by this mesh's triangles.
    pub vertices: Vec<Vertex>,
    /// Triangle-list indices into [`Self::vertices`], three per face.
    pub indices: Vec<u32>,
    /// Index into the containing [`Asset::materials`].
    pub material: usize,
    /// Its skin and morph targets, which name [`Asset::rig`]'s joints and
    /// morph weights; rigid by default.
    pub deformation: MeshDeformation,
}

/// Metallic/roughness material inputs understood by the renderer.
#[derive(Clone)]
pub struct Material {
    /// Authored material label, retained for diagnostics.
    pub name: String,
    /// Caller-selected visibility group. Zero is always visible; nonzero groups
    /// are visible when selected by the scene. Imported materials start at zero.
    pub visibility_group: u32,
    /// Whether this material casts directional shadows when its visibility group is enabled.
    /// Imported materials cast by default; games may opt out decorative inlays explicitly.
    pub casts_directional_shadow: bool,
    /// Linear base color multiplier, each channel finite and nonnegative,
    /// and alpha in `0..=1`.
    pub base: [f32; 4],
    /// Linear emissive color with emissive strength already applied, each
    /// channel finite and nonnegative.
    pub emissive: [f32; 3],
    /// Metallic factor in `0..=1`.
    pub metallic: f32,
    /// Perceptual roughness factor in `0..=1`.
    pub roughness: f32,
    /// Index of refraction (`KHR_materials_ior`): at least 1, or
    /// `f32::INFINITY` (glTF's `ior` 0). It sets the dielectric F0,
    /// ((ior − 1) / (ior + 1))²: 0.04 at glTF's default 1.5, 0.02 for water
    /// at 1.33.
    pub ior: f32,
    /// Strength of the dielectric specular reflection in `0..=1`
    /// (`KHR_materials_specular`'s `specularFactor`): 1 as the IOR gives
    /// it, 0 none. It scales the dielectric F0 and is its reflectance at
    /// grazing incidence (F90), which metallic mixes toward 1.
    pub specular: f32,
    /// Linear tint of the dielectric F0, each channel finite and
    /// nonnegative (`KHR_materials_specular`'s `specularColorFactor`): the
    /// F0 is the IOR's times this, at most 1, times `specular`.
    pub specular_color: [f32; 3],
    /// Clearcoat intensity in `0..=1` (`KHR_materials_clearcoat`'s
    /// `clearcoatFactor`).
    pub clearcoat: f32,
    /// Clearcoat perceptual roughness in `0..=1`
    /// (`clearcoatRoughnessFactor`).
    pub coat_roughness: f32,
    /// Clearcoat image index, sampled as linear data: its red channel
    /// multiplies `clearcoat` (`clearcoatTexture`).
    pub clearcoat_texture: Option<usize>,
    /// Clearcoat roughness image index, sampled as linear data: its green
    /// channel multiplies `coat_roughness` (`clearcoatRoughnessTexture`).
    pub coat_roughness_texture: Option<usize>,
    /// Tangent-space clearcoat normal image index, sampled as linear data
    /// (`clearcoatNormalTexture`): the coat's normal, on the base normal
    /// map's tangent frame. Without it the coat follows the geometry
    /// normal, whatever the base's normal map does.
    pub coat_normal_texture: Option<usize>,
    /// Finite scale applied to the clearcoat normal map's tangent-space X and Y
    /// components (`clearcoatNormalTexture.scale`).
    pub coat_normal_scale: f32,
    /// Strength of a thin film over the surface in `0..=1`
    /// (`KHR_materials_iridescence`'s `iridescenceFactor`): 0 none.
    pub iridescence: f32,
    /// The film's index of refraction, at least 1 (`iridescenceIor`).
    pub iridescence_ior: f32,
    /// The film's thinnest and thickest thickness in nanometres
    /// (`iridescenceThicknessMinimum`, `iridescenceThicknessMaximum`), each
    /// finite and nonnegative: the thickness map's green channel mixes from
    /// the first to the second, and without the map the film is the second.
    pub iridescence_thickness: [f32; 2],
    /// Iridescence image index, sampled as linear data: its red channel
    /// multiplies `iridescence` (`iridescenceTexture`).
    pub iridescence_texture: Option<usize>,
    /// Iridescence thickness image index, sampled as linear data: its green
    /// channel mixes `iridescence_thickness` (`iridescenceThicknessTexture`).
    pub iridescence_thickness_texture: Option<usize>,
    /// Linear colour of a sheen over the surface, each channel in `0..=1`
    /// (`KHR_materials_sheen`'s `sheenColorFactor`): black none. Fabric and
    /// cloth.
    pub sheen_color: [f32; 3],
    /// The sheen's perceptual roughness in `0..=1` (`sheenRoughnessFactor`).
    pub sheen_roughness: f32,
    /// Sheen colour image index, sampled as sRGB: its RGB multiplies
    /// `sheen_color` (`sheenColorTexture`).
    pub sheen_color_texture: Option<usize>,
    /// Sheen roughness image index, sampled as linear data: its alpha
    /// channel multiplies `sheen_roughness` (`sheenRoughnessTexture`).
    pub sheen_roughness_texture: Option<usize>,
    /// The share in `0..=1` of the light the base diffuses that passes
    /// through to the surface's other side
    /// (`KHR_materials_diffuse_transmission`'s `diffuseTransmissionFactor`):
    /// 0 none. Leaves, paper, flags and lampshades.
    pub diffuse_transmission: f32,
    /// Linear colour of the light it passes through, each channel finite
    /// and nonnegative (`diffuseTransmissionColorFactor`).
    pub diffuse_transmission_color: [f32; 3],
    /// Diffuse transmission image index, sampled as linear data: its alpha
    /// channel multiplies `diffuse_transmission`
    /// (`diffuseTransmissionTexture`).
    pub diffuse_transmission_texture: Option<usize>,
    /// Diffuse transmission colour image index, sampled as sRGB: its RGB
    /// multiplies `diffuse_transmission_color`
    /// (`diffuseTransmissionColorTexture`).
    pub diffuse_transmission_color_texture: Option<usize>,
    /// KHR_materials_anisotropy strength in `0..=1`; zero preserves isotropic shading.
    pub anisotropy_strength: f32,
    /// Counter-clockwise direction rotation in tangent/bitangent space, in radians.
    pub anisotropy_rotation: f32,
    /// Linear image index: normalized remapped RG direction and B strength
    /// multiplier. A device of the `Basic` binding tier binds no anisotropy
    /// map and shades the factors alone (`Renderer::binding_tier`).
    pub anisotropy_texture: Option<usize>,
    /// Base color image index into [`Asset::images`], sampled as sRGB.
    pub base_texture: Option<usize>,
    /// Metallic (blue) / roughness (green) image index, sampled as linear data.
    pub mr_texture: Option<usize>,
    /// Occlusion image index (glTF's `occlusionTexture` on `TEXCOORD_0`;
    /// the loader leaves out a map on another UV set): its red channel is
    /// the share of ambient light that reaches the surface, which occludes
    /// its ambient light, its baked light (lightmap, atlas chart or
    /// ambient cube) and its environment specular. SGL3D samples it where
    /// it is `mr_texture`'s image (ORM packing: occlusion, roughness and
    /// metallic in red, green and blue), and not yet from an image of its
    /// own.
    pub occlusion_texture: Option<usize>,
    /// How much of the occlusion map applies, in `0..=1` (glTF's
    /// `occlusionTexture.strength`): 1 as authored, 0 none.
    pub occlusion_strength: f32,
    /// Emissive image index, sampled as sRGB.
    pub emissive_texture: Option<usize>,
    /// Tangent-space normal image index, sampled as linear data.
    pub normal_texture: Option<usize>,
    /// Finite scale applied to the normal map's tangent-space X and Y components.
    pub normal_scale: f32,
    /// The normal map drawn as two scrolling layers
    /// ([`SurfaceMaterial::normal_layers`](crate::SurfaceMaterial::normal_layers));
    /// `None` draws it once. glTF has no such extension: the loader leaves
    /// it `None`.
    pub normal_layers: Option<[NormalLayer; 2]>,
    /// Bump image index, sampled as linear height data.
    pub bump_texture: Option<usize>,
    /// Finite bump height multiplier
    /// ([`SurfaceMaterial::bump_scale`](crate::SurfaceMaterial::bump_scale)).
    pub bump_scale: f32,
    /// The share of the light behind the surface that passes through it,
    /// refracted, in `0..=1` (`KHR_materials_transmission`'s
    /// `transmissionFactor`); see
    /// [`SurfaceMaterial::transmission`](crate::SurfaceMaterial::transmission).
    pub transmission: f32,
    /// Transmission image index: its red channel multiplies `transmission`,
    /// sampled as linear data. A device of the `Basic` binding tier binds
    /// none and takes the factor alone.
    pub transmission_texture: Option<usize>,
    /// The thickness of the volume beneath the surface in the mesh's units,
    /// 0 for a thin wall (`KHR_materials_volume`'s `thicknessFactor`).
    pub thickness: f32,
    /// Thickness image index: its green channel multiplies `thickness`,
    /// sampled as linear data; `Basic` takes the factor alone.
    pub thickness_texture: Option<usize>,
    /// The distance in metres light travels in the volume before it takes
    /// `attenuation_color`, positive or `f32::INFINITY` for none
    /// (`KHR_materials_volume`).
    pub attenuation_distance: f32,
    /// The linear colour white light turns into at `attenuation_distance`,
    /// each channel in `0..=1`.
    pub attenuation_color: [f32; 3],
    /// How far transmitted light's colours spread, 20 over the Abbe number,
    /// nonnegative (`KHR_materials_dispersion`): 0 none.
    pub dispersion: f32,
    /// U and V wrapping shared by all texture channels of this material.
    pub wrap: [WrappingMode; 2],
    /// Whether both sides of each triangle are rendered.
    pub double_sided: bool,
    /// Whether the material bypasses lighting (`KHR_materials_unlit`).
    pub unlit: bool,
    /// Whether global illumination gathers the light the material gives off
    /// itself
    /// ([`SurfaceMaterial::emits_into_gi`](crate::SurfaceMaterial::emits_into_gi)).
    /// Imported materials do; set it false on a fixture that a scene light
    /// stands for.
    pub emits_into_gi: bool,
    /// How the base alpha is used: opaque, masked or blended.
    pub alpha: AlphaMode,
}

impl Material {
    /// Whether its occlusion map is the red channel of its metallic-roughness
    /// image (ORM packing), the one place SGL3D samples occlusion from.
    pub(crate) fn packed_occlusion(&self) -> bool {
        self.occlusion_texture.is_some() && self.occlusion_texture == self.mr_texture
    }

    /// Every image index it names, each field once: a new map's field
    /// fails to compile here until it is listed.
    pub(crate) fn texture_indices(&self) -> [Option<usize>; 18] {
        let Self {
            name: _,
            visibility_group: _,
            casts_directional_shadow: _,
            base: _,
            emissive: _,
            metallic: _,
            roughness: _,
            ior: _,
            specular: _,
            specular_color: _,
            clearcoat: _,
            coat_roughness: _,
            clearcoat_texture,
            coat_roughness_texture,
            coat_normal_texture,
            coat_normal_scale: _,
            iridescence: _,
            iridescence_ior: _,
            iridescence_thickness: _,
            iridescence_texture,
            iridescence_thickness_texture,
            sheen_color: _,
            sheen_roughness: _,
            sheen_color_texture,
            sheen_roughness_texture,
            diffuse_transmission: _,
            diffuse_transmission_color: _,
            diffuse_transmission_texture,
            diffuse_transmission_color_texture,
            anisotropy_strength: _,
            anisotropy_rotation: _,
            anisotropy_texture,
            base_texture,
            mr_texture,
            occlusion_texture,
            occlusion_strength: _,
            emissive_texture,
            normal_texture,
            normal_scale: _,
            normal_layers: _,
            bump_texture,
            bump_scale: _,
            transmission: _,
            transmission_texture,
            thickness: _,
            thickness_texture,
            attenuation_distance: _,
            attenuation_color: _,
            dispersion: _,
            wrap: _,
            double_sided: _,
            unlit: _,
            emits_into_gi: _,
            alpha: _,
        } = self;
        [
            *base_texture,
            *mr_texture,
            *occlusion_texture,
            *emissive_texture,
            *normal_texture,
            *bump_texture,
            *anisotropy_texture,
            *clearcoat_texture,
            *coat_roughness_texture,
            *coat_normal_texture,
            *iridescence_texture,
            *iridescence_thickness_texture,
            *transmission_texture,
            *thickness_texture,
            *sheen_color_texture,
            *sheen_roughness_texture,
            *diffuse_transmission_texture,
            *diffuse_transmission_color_texture,
        ]
    }
}

impl Default for Material {
    /// glTF 2.0's default material, which the loader gives a primitive
    /// without one: unnamed, a white base, metallic and roughness 1, IOR
    /// 1.5, specular 1 untinted, no emission, clearcoat, iridescence (its
    /// film's IOR 1.3 and thickness 100 to 400 nm as glTF's defaults),
    /// sheen, diffuse transmission (its colour white, as glTF's default),
    /// anisotropy, bump, transmission, thickness, attenuation or
    /// dispersion, no textures, normal, coat normal and occlusion scales 1,
    /// no normal layers, repeating, single-sided, lit and opaque, in visibility
    /// group 0, casting directional shadows and emitting into global
    /// illumination. Set what differs and take the rest with
    /// `..Default::default()`.
    fn default() -> Self {
        Self {
            name: String::new(),
            visibility_group: 0,
            casts_directional_shadow: true,
            base: [1.0; 4],
            emissive: [0.0; 3],
            metallic: 1.0,
            roughness: 1.0,
            ior: 1.5,
            specular: 1.0,
            specular_color: [1.0; 3],
            clearcoat: 0.0,
            coat_roughness: 0.0,
            clearcoat_texture: None,
            coat_roughness_texture: None,
            coat_normal_texture: None,
            coat_normal_scale: 1.0,
            iridescence: 0.0,
            iridescence_ior: 1.3,
            iridescence_thickness: [100.0, 400.0],
            iridescence_texture: None,
            iridescence_thickness_texture: None,
            sheen_color: [0.0; 3],
            sheen_roughness: 0.0,
            sheen_color_texture: None,
            sheen_roughness_texture: None,
            diffuse_transmission: 0.0,
            diffuse_transmission_color: [1.0; 3],
            diffuse_transmission_texture: None,
            diffuse_transmission_color_texture: None,
            anisotropy_strength: 0.0,
            anisotropy_rotation: 0.0,
            anisotropy_texture: None,
            base_texture: None,
            mr_texture: None,
            occlusion_texture: None,
            occlusion_strength: 1.0,
            emissive_texture: None,
            normal_texture: None,
            normal_scale: 1.0,
            normal_layers: None,
            bump_texture: None,
            bump_scale: 0.0,
            transmission: 0.0,
            transmission_texture: None,
            thickness: 0.0,
            thickness_texture: None,
            attenuation_distance: f32::INFINITY,
            attenuation_color: [1.0; 3],
            dispersion: 0.0,
            wrap: [WrappingMode::Repeat; 2],
            double_sided: false,
            unlit: false,
            emits_into_gi: true,
            alpha: AlphaMode::Opaque,
        }
    }
}

/// CPU-side meshes, material inputs, and images ready for GPU upload.
///
/// The caller owns this data and decides how to compose it into a scene.
#[derive(Clone)]
pub struct Asset {
    /// Triangle meshes with baked transforms, batched by material when loaded.
    pub meshes: Vec<CpuMesh>,
    /// Materials addressed by [`CpuMesh::material`].
    pub materials: Vec<Material>,
    /// Images addressed by the materials' texture indices; colour-space
    /// interpretation belongs to each channel. A glTF image no sampled map
    /// uses is a placeholder ([`Ignored::Image`]).
    pub images: Vec<Image>,
    /// What poses its deforming meshes, and its animation clips; empty when
    /// nothing deforms.
    pub rig: Rig,
    /// What a glTF load left unrendered, which the file did not require
    /// (glTF lets a loader leave it out); empty for content built in code.
    pub ignored: Vec<Ignored>,
}

/// Whether every vertex carries a finite, nonzero authored tangent frame with
/// handedness +1 or -1, which anisotropy requires of each mesh drawn with an
/// anisotropic material.
pub(crate) fn tangent_frames(vertices: &[Vertex]) -> bool {
    vertices.iter().all(tangent_frame)
}

/// Whether `vertex` carries a finite, nonzero authored tangent frame: a
/// tangent off its normal (`tangent_off_normal`), with handedness +1 or -1.
pub(crate) fn tangent_frame(vertex: &Vertex) -> bool {
    tangent_off_normal(
        Vec3::from_array(vertex.normal),
        Vec3::from_slice(&vertex.tangent[..3]),
    )
    .is_some()
        && matches!(vertex.tangent[3], -1.0 | 1.0)
}

/// The squared sine of the angle within which a tangent lies along its
/// normal: f32::EPSILON, so within 0.0198°. Filament's `randomPerp`
/// (libs/geometry/src/SurfaceOrientation.cpp) treats a unit vector as along
/// an axis by the same test, `dot(perp, perp) <= epsilon`, and Godot's glTF
/// importer counts a tangent as off perpendicular past `|t·n| > 0.0001`
/// (modules/gltf/gltf_document.cpp). An exact-zero test, as MikkTSpace's
/// `VNotZero` (FLT_MIN), would let a tangent a few f32 ulps (6e-8 each) off
/// its normal normalise that rounding into an arbitrary direction (#573);
/// past this threshold a few ulps turn the tangent by under about 0.1°.
/// Authored tangents (MikkTSpace makes them perpendicular to the normal)
/// sit near 90°, far from it.
const ALONG_NORMAL_SIN_SQUARED: f32 = f32::EPSILON;

/// `tangent` made unit and perpendicular to `normal`, or `None` when either
/// cannot be normalised or the tangent lies along the normal (within
/// `ALONG_NORMAL_SIN_SQUARED`), where it spans no frame. The one rule for
/// both glTF import and procedural vertices.
pub(crate) fn tangent_off_normal(normal: Vec3, tangent: Vec3) -> Option<Vec3> {
    let normal = normal.try_normalize()?;
    let tangent = tangent.try_normalize()?;
    let projected = tangent - normal * normal.dot(tangent);
    (projected.length_squared() > ALONG_NORMAL_SIN_SQUARED).then(|| projected.normalize())
}

pub(crate) fn valid_anisotropy(strength: f32, rotation: f32) -> bool {
    (0.0..=1.0).contains(&strength) && rotation.is_finite()
}

/// Create an asset with no geometry, materials, or images.
pub fn empty() -> Asset {
    Asset {
        meshes: Vec::new(),
        materials: Vec::new(),
        images: Vec::new(),
        rig: Rig::default(),
        ignored: Vec::new(),
    }
}