scena 1.5.1

A Rust-native scene-graph renderer with typed scene state, glTF assets, and explicit prepare/render lifecycles.
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
use crate::assets::TextureHandle;

use super::scalars::{clamp_unit_or, finite_or, non_negative_or, positive_or};
use super::{Color, MaterialDesc, TextureTransform};

impl MaterialDesc {
    pub const fn clearcoat_texture(&self) -> Option<TextureHandle> {
        self.clearcoat_texture
    }

    pub const fn clearcoat_texture_transform(&self) -> Option<TextureTransform> {
        self.clearcoat_texture_transform
    }

    pub const fn clearcoat_roughness_texture(&self) -> Option<TextureHandle> {
        self.clearcoat_roughness_texture
    }

    pub const fn clearcoat_roughness_texture_transform(&self) -> Option<TextureTransform> {
        self.clearcoat_roughness_texture_transform
    }

    pub const fn clearcoat_normal_texture(&self) -> Option<TextureHandle> {
        self.clearcoat_normal_texture
    }

    pub const fn clearcoat_normal_texture_transform(&self) -> Option<TextureTransform> {
        self.clearcoat_normal_texture_transform
    }

    pub const fn sheen_color_texture(&self) -> Option<TextureHandle> {
        self.sheen_color_texture
    }

    pub const fn sheen_color_texture_transform(&self) -> Option<TextureTransform> {
        self.sheen_color_texture_transform
    }

    pub const fn sheen_roughness_texture(&self) -> Option<TextureHandle> {
        self.sheen_roughness_texture
    }

    pub const fn sheen_roughness_texture_transform(&self) -> Option<TextureTransform> {
        self.sheen_roughness_texture_transform
    }

    pub const fn anisotropy_texture(&self) -> Option<TextureHandle> {
        self.anisotropy_texture
    }

    pub const fn anisotropy_texture_transform(&self) -> Option<TextureTransform> {
        self.anisotropy_texture_transform
    }

    pub const fn iridescence_texture(&self) -> Option<TextureHandle> {
        self.iridescence_texture
    }

    pub const fn iridescence_texture_transform(&self) -> Option<TextureTransform> {
        self.iridescence_texture_transform
    }

    pub const fn iridescence_thickness_texture(&self) -> Option<TextureHandle> {
        self.iridescence_thickness_texture
    }

    pub const fn iridescence_thickness_texture_transform(&self) -> Option<TextureTransform> {
        self.iridescence_thickness_texture_transform
    }

    pub const fn transmission_texture(&self) -> Option<TextureHandle> {
        self.transmission_texture
    }

    pub const fn transmission_texture_transform(&self) -> Option<TextureTransform> {
        self.transmission_texture_transform
    }

    pub const fn thickness_texture(&self) -> Option<TextureHandle> {
        self.thickness_texture
    }

    pub const fn thickness_texture_transform(&self) -> Option<TextureTransform> {
        self.thickness_texture_transform
    }

    /// Returns the scalar `KHR_materials_clearcoat.clearcoatFactor`.
    ///
    /// The renderer multiplies this value by the sampled clearcoat texture
    /// when one is present.
    pub const fn clearcoat_factor(&self) -> f32 {
        self.clearcoat_factor
    }

    /// Returns the untextured `KHR_materials_clearcoat.clearcoatRoughnessFactor`.
    pub const fn clearcoat_roughness_factor(&self) -> f32 {
        self.clearcoat_roughness_factor
    }

    pub const fn clearcoat_normal_scale(&self) -> f32 {
        self.clearcoat_normal_scale
    }

    /// Returns the scalar `KHR_materials_sheen.sheenColorFactor`.
    pub const fn sheen_color_factor(&self) -> Color {
        self.sheen_color_factor
    }

    /// Returns the scalar `KHR_materials_sheen.sheenRoughnessFactor`.
    pub const fn sheen_roughness_factor(&self) -> f32 {
        self.sheen_roughness_factor
    }

    /// Returns `KHR_materials_anisotropy.anisotropyStrength`.
    pub const fn anisotropy_strength_factor(&self) -> f32 {
        self.anisotropy_strength_factor
    }

    /// Returns `KHR_materials_anisotropy.anisotropyRotation` in radians.
    pub const fn anisotropy_rotation_radians(&self) -> f32 {
        self.anisotropy_rotation_radians
    }

    /// Returns `KHR_materials_iridescence.iridescenceFactor`.
    pub const fn iridescence_factor(&self) -> f32 {
        self.iridescence_factor
    }

    /// Returns `KHR_materials_iridescence.iridescenceIor`.
    pub const fn iridescence_ior(&self) -> f32 {
        self.iridescence_ior
    }

    /// Returns `KHR_materials_iridescence.iridescenceThicknessMinimum`, in nanometers.
    pub const fn iridescence_thickness_minimum_nm(&self) -> f32 {
        self.iridescence_thickness_minimum_nm
    }

    /// Returns `KHR_materials_iridescence.iridescenceThicknessMaximum`, in nanometers.
    pub const fn iridescence_thickness_maximum_nm(&self) -> f32 {
        self.iridescence_thickness_maximum_nm
    }

    /// Returns `KHR_materials_dispersion.dispersion` as `20 / Abbe number`.
    pub const fn dispersion_factor(&self) -> f32 {
        self.dispersion_factor
    }

    /// Returns `KHR_materials_transmission.transmissionFactor`.
    pub const fn transmission_factor(&self) -> f32 {
        self.transmission_factor
    }

    /// Returns `KHR_materials_ior.ior`.
    pub const fn ior(&self) -> f32 {
        self.ior
    }

    /// Returns `KHR_materials_volume.thicknessFactor`, in scene units.
    pub const fn thickness_factor(&self) -> f32 {
        self.thickness_factor
    }

    /// Returns `KHR_materials_volume.attenuationDistance`, in scene units.
    pub const fn attenuation_distance(&self) -> f32 {
        self.attenuation_distance
    }

    /// Returns `KHR_materials_volume.attenuationColor`.
    pub const fn attenuation_color(&self) -> Color {
        self.attenuation_color
    }

    pub const fn with_clearcoat_texture(mut self, texture: TextureHandle) -> Self {
        self.clearcoat_texture = Some(texture);
        self
    }

    pub const fn with_clearcoat_texture_transform(mut self, transform: TextureTransform) -> Self {
        self.clearcoat_texture_transform = Some(transform);
        self
    }

    pub const fn with_clearcoat_roughness_texture(mut self, texture: TextureHandle) -> Self {
        self.clearcoat_roughness_texture = Some(texture);
        self
    }

    pub const fn with_clearcoat_roughness_texture_transform(
        mut self,
        transform: TextureTransform,
    ) -> Self {
        self.clearcoat_roughness_texture_transform = Some(transform);
        self
    }

    pub const fn with_clearcoat_normal_texture(mut self, texture: TextureHandle) -> Self {
        self.clearcoat_normal_texture = Some(texture);
        self
    }

    pub const fn with_clearcoat_normal_texture_transform(
        mut self,
        transform: TextureTransform,
    ) -> Self {
        self.clearcoat_normal_texture_transform = Some(transform);
        self
    }

    pub const fn with_clearcoat_normal_scale(mut self, scale: f32) -> Self {
        self.clearcoat_normal_scale = non_negative_or(scale, 1.0);
        self
    }

    pub const fn with_sheen_color_texture(mut self, texture: TextureHandle) -> Self {
        self.sheen_color_texture = Some(texture);
        self
    }

    pub const fn with_sheen_color_texture_transform(mut self, transform: TextureTransform) -> Self {
        self.sheen_color_texture_transform = Some(transform);
        self
    }

    pub const fn with_sheen_roughness_texture(mut self, texture: TextureHandle) -> Self {
        self.sheen_roughness_texture = Some(texture);
        self
    }

    pub const fn with_sheen_roughness_texture_transform(
        mut self,
        transform: TextureTransform,
    ) -> Self {
        self.sheen_roughness_texture_transform = Some(transform);
        self
    }

    pub const fn with_anisotropy_texture(mut self, texture: TextureHandle) -> Self {
        self.anisotropy_texture = Some(texture);
        self
    }

    pub const fn with_anisotropy_texture_transform(mut self, transform: TextureTransform) -> Self {
        self.anisotropy_texture_transform = Some(transform);
        self
    }

    pub const fn with_iridescence_texture(mut self, texture: TextureHandle) -> Self {
        self.iridescence_texture = Some(texture);
        self
    }

    pub const fn with_iridescence_texture_transform(mut self, transform: TextureTransform) -> Self {
        self.iridescence_texture_transform = Some(transform);
        self
    }

    pub const fn with_iridescence_thickness_texture(mut self, texture: TextureHandle) -> Self {
        self.iridescence_thickness_texture = Some(texture);
        self
    }

    pub const fn with_iridescence_thickness_texture_transform(
        mut self,
        transform: TextureTransform,
    ) -> Self {
        self.iridescence_thickness_texture_transform = Some(transform);
        self
    }

    pub const fn with_transmission_texture(mut self, texture: TextureHandle) -> Self {
        self.transmission_texture = Some(texture);
        self
    }

    pub const fn with_transmission_texture_transform(
        mut self,
        transform: TextureTransform,
    ) -> Self {
        self.transmission_texture_transform = Some(transform);
        self
    }

    pub const fn with_thickness_texture(mut self, texture: TextureHandle) -> Self {
        self.thickness_texture = Some(texture);
        self
    }

    pub const fn with_thickness_texture_transform(mut self, transform: TextureTransform) -> Self {
        self.thickness_texture_transform = Some(transform);
        self
    }

    /// Sets the scalar clearcoat layer strength from `KHR_materials_clearcoat`.
    ///
    /// Values are clamped to `[0, 1]`; `NaN` falls back to `0`.
    pub const fn with_clearcoat_factor(mut self, clearcoat_factor: f32) -> Self {
        self.clearcoat_factor = clamp_unit_or(clearcoat_factor, 0.0);
        self
    }

    /// Sets the scalar clearcoat roughness from `KHR_materials_clearcoat`.
    ///
    /// Values are clamped to `[0, 1]`; `NaN` falls back to `0`.
    pub const fn with_clearcoat_roughness_factor(
        mut self,
        clearcoat_roughness_factor: f32,
    ) -> Self {
        self.clearcoat_roughness_factor = clamp_unit_or(clearcoat_roughness_factor, 0.0);
        self
    }

    /// Sets the scalar sheen color from `KHR_materials_sheen`.
    pub const fn with_sheen_color_factor(mut self, sheen_color_factor: Color) -> Self {
        self.sheen_color_factor = Color::from_linear_rgb(
            clamp_unit_or(sheen_color_factor.r, 0.0),
            clamp_unit_or(sheen_color_factor.g, 0.0),
            clamp_unit_or(sheen_color_factor.b, 0.0),
        );
        self
    }

    /// Sets the scalar sheen roughness from `KHR_materials_sheen`.
    pub const fn with_sheen_roughness_factor(mut self, sheen_roughness_factor: f32) -> Self {
        self.sheen_roughness_factor = clamp_unit_or(sheen_roughness_factor, 0.0);
        self
    }

    /// Sets the scalar anisotropy strength from `KHR_materials_anisotropy`.
    ///
    /// Values are clamped to `[0, 1]`; `NaN` falls back to `0`.
    pub const fn with_anisotropy_strength_factor(
        mut self,
        anisotropy_strength_factor: f32,
    ) -> Self {
        self.anisotropy_strength_factor = clamp_unit_or(anisotropy_strength_factor, 0.0);
        self
    }

    /// Sets the anisotropy rotation in tangent space, in radians.
    pub const fn with_anisotropy_rotation_radians(
        mut self,
        anisotropy_rotation_radians: f32,
    ) -> Self {
        self.anisotropy_rotation_radians = finite_or(anisotropy_rotation_radians, 0.0);
        self
    }

    /// Sets the scalar iridescence strength from `KHR_materials_iridescence`.
    ///
    /// Values are clamped to `[0, 1]`; `NaN` falls back to `0`.
    pub const fn with_iridescence_factor(mut self, iridescence_factor: f32) -> Self {
        self.iridescence_factor = clamp_unit_or(iridescence_factor, 0.0);
        self
    }

    /// Sets the thin-film index of refraction from `KHR_materials_iridescence`.
    pub const fn with_iridescence_ior(mut self, iridescence_ior: f32) -> Self {
        self.iridescence_ior = positive_or(iridescence_ior, 1.3);
        self
    }

    /// Sets the thin-film thickness range in nanometers.
    ///
    /// The glTF extension permits `minimum > maximum`, so values are sanitized
    /// independently rather than reordered.
    pub const fn with_iridescence_thickness_range_nm(
        mut self,
        minimum_nm: f32,
        maximum_nm: f32,
    ) -> Self {
        self.iridescence_thickness_minimum_nm = non_negative_or(minimum_nm, 100.0);
        self.iridescence_thickness_maximum_nm = non_negative_or(maximum_nm, 400.0);
        self
    }

    /// Sets the scalar dispersion strength from `KHR_materials_dispersion`.
    ///
    /// Values are sanitized to `>= 0`; `NaN` falls back to `0`.
    pub const fn with_dispersion_factor(mut self, dispersion_factor: f32) -> Self {
        self.dispersion_factor = non_negative_or(dispersion_factor, 0.0);
        self
    }

    /// Sets the scalar transmission strength from `KHR_materials_transmission`.
    ///
    /// Values are clamped to `[0, 1]`; `NaN` falls back to `0`.
    pub const fn with_transmission_factor(mut self, transmission_factor: f32) -> Self {
        self.transmission_factor = clamp_unit_or(transmission_factor, 0.0);
        self
    }

    /// Sets the material index of refraction from `KHR_materials_ior`.
    pub const fn with_ior(mut self, ior: f32) -> Self {
        self.ior = ior_or(ior, 1.5);
        self
    }

    /// Sets `KHR_materials_volume.thicknessFactor`, in scene units.
    pub const fn with_thickness_factor(mut self, thickness_factor: f32) -> Self {
        self.thickness_factor = non_negative_or(thickness_factor, 0.0);
        self
    }

    /// Sets `KHR_materials_volume.attenuationDistance`, in scene units.
    pub const fn with_attenuation_distance(mut self, attenuation_distance: f32) -> Self {
        self.attenuation_distance = attenuation_distance_or(attenuation_distance);
        self
    }

    /// Sets `KHR_materials_volume.attenuationColor`.
    pub const fn with_attenuation_color(mut self, attenuation_color: Color) -> Self {
        self.attenuation_color = Color::from_linear_rgb(
            clamp_unit_or(attenuation_color.r, 1.0),
            clamp_unit_or(attenuation_color.g, 1.0),
            clamp_unit_or(attenuation_color.b, 1.0),
        );
        self
    }
}

const fn ior_or(value: f32, fallback: f32) -> f32 {
    if value.is_finite() && (value == 0.0 || value >= 1.0) {
        value
    } else {
        fallback
    }
}

const fn attenuation_distance_or(value: f32) -> f32 {
    if value.is_nan() || value <= 0.0 {
        f32::INFINITY
    } else {
        value
    }
}