scena 1.7.2

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
use std::collections::BTreeSet;

use serde_json::Value;

use crate::MaterialDesc;
use crate::scene::recipe::types::SceneRecipeDiagnosticV1;

use super::super::{validate_known_fields, validate_required_id};
use super::material_fields::{
    validate_alpha_mode, validate_color_ref, validate_optional_finite, validate_optional_ior,
    validate_optional_non_negative, validate_optional_positive, validate_optional_range,
    validate_texture_slot, validate_unit_float,
};
use crate::scene::recipe::validation::diagnostic;

const MATERIAL_FIELDS: &[&str] = &[
    "id",
    "kind",
    "preset",
    "base_color",
    "metallic",
    "roughness",
    "double_sided",
    "emissive",
    "emissive_strength",
    "alpha_mode",
    "stroke_width_px",
    "edge_angle_threshold_degrees",
    "base_color_texture",
    "normal_texture",
    "metallic_roughness_texture",
    "occlusion_texture",
    "emissive_texture",
    "clearcoat_factor",
    "clearcoat_roughness_factor",
    "clearcoat_normal_scale",
    "clearcoat_texture",
    "clearcoat_roughness_texture",
    "clearcoat_normal_texture",
    "sheen_color_factor",
    "sheen_roughness_factor",
    "sheen_color_texture",
    "sheen_roughness_texture",
    "anisotropy_strength_factor",
    "anisotropy_rotation_radians",
    "anisotropy_texture",
    "iridescence_factor",
    "iridescence_ior",
    "iridescence_thickness_minimum_nm",
    "iridescence_thickness_maximum_nm",
    "iridescence_texture",
    "iridescence_thickness_texture",
    "dispersion_factor",
    "transmission_factor",
    "ior",
    "thickness_factor",
    "attenuation_distance",
    "attenuation_color",
    "transmission_texture",
    "thickness_texture",
];

const ADVANCED_PBR_SCALAR_FIELDS: &[&str] = &[
    "clearcoat_factor",
    "clearcoat_roughness_factor",
    "clearcoat_normal_scale",
    "sheen_color_factor",
    "sheen_roughness_factor",
    "anisotropy_strength_factor",
    "anisotropy_rotation_radians",
    "iridescence_factor",
    "iridescence_ior",
    "iridescence_thickness_minimum_nm",
    "iridescence_thickness_maximum_nm",
    "dispersion_factor",
    "transmission_factor",
    "ior",
    "thickness_factor",
    "attenuation_distance",
    "attenuation_color",
];

const GPU_SUPPORTED_ADVANCED_PBR_TEXTURE_FIELDS: &[&str] = &[
    "clearcoat_texture",
    "clearcoat_roughness_texture",
    "clearcoat_normal_texture",
    "sheen_color_texture",
    "sheen_roughness_texture",
    "anisotropy_texture",
    "iridescence_texture",
    "iridescence_thickness_texture",
];

const GPU_UNSUPPORTED_VOLUME_TEXTURE_FIELDS: &[&str] =
    &["transmission_texture", "thickness_texture"];

const ADVANCED_PBR_TEXTURE_FIELDS: &[&str] = &[
    "clearcoat_texture",
    "clearcoat_roughness_texture",
    "clearcoat_normal_texture",
    "sheen_color_texture",
    "sheen_roughness_texture",
    "anisotropy_texture",
    "iridescence_texture",
    "iridescence_thickness_texture",
    "transmission_texture",
    "thickness_texture",
];

pub(in crate::scene::recipe::validation::authoring) fn validate_materials(
    value: Option<&Value>,
    colors: &BTreeSet<String>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    let Some(value) = value else {
        return;
    };
    let Some(materials) = value.as_array() else {
        diagnostics.push(diagnostic(
            "invalid_materials",
            "error",
            "$.materials",
            "materials must be an array",
            "emit materials:[{id,kind,base_color}]",
            None,
            false,
        ));
        return;
    };
    for (index, material) in materials.iter().enumerate() {
        let path = format!("$.materials[{index}]");
        let Some(object) = material.as_object() else {
            diagnostics.push(diagnostic(
                "invalid_material",
                "error",
                &path,
                "material entry must be an object",
                "emit material entries as {id, kind, base_color}",
                None,
                false,
            ));
            continue;
        };
        validate_known_fields(&path, object, MATERIAL_FIELDS, diagnostics);
        validate_required_id(&path, object.get("id"), diagnostics);
        let kind = object.get("kind").and_then(Value::as_str);
        let preset = object.get("preset").and_then(Value::as_str);
        if object.contains_key("kind") && object.contains_key("preset") {
            diagnostics.push(diagnostic(
                "invalid_material",
                "error",
                format!("{path}.preset"),
                "material must use either preset or kind, not both",
                "remove kind when using material.preset, or remove preset and use the low-level material kind",
                None,
                false,
            ));
        }
        if let Some(value) = object.get("preset")
            && !value.is_string()
        {
            diagnostics.push(diagnostic(
                "invalid_material_preset",
                "error",
                format!("{path}.preset"),
                "material preset must be a string",
                "use a documented MaterialDesc preset name such as chrome, plastic, or brushed_steel",
                None,
                false,
            ));
        }
        if let Some(preset) = preset
            && MaterialDesc::from_preset_name(preset, None).is_none()
        {
            diagnostics.push(diagnostic(
                "invalid_material_preset",
                "error",
                format!("{path}.preset"),
                format!("material preset '{preset}' is not supported"),
                format!("use one of: {}", MaterialDesc::PRESET_NAMES.join(", ")),
                None,
                false,
            ));
        }
        match kind {
            Some("unlit" | "pbr_metallic_roughness" | "line" | "wireframe" | "edge") => {}
            Some(kind) => diagnostics.push(diagnostic(
                "unsupported_feature",
                "error",
                format!("{path}.kind"),
                format!("material kind '{kind}' is not implemented in this slice"),
                "use kind:\"unlit\", \"pbr_metallic_roughness\", \"line\", \"wireframe\", or \"edge\"",
                None,
                false,
            )),
            None if preset.is_none() => diagnostics.push(diagnostic(
                "missing_material_kind",
                "error",
                format!("{path}.kind"),
                "material must include either a preset string or a kind string",
                "use preset:\"chrome\" for ergonomic materials or kind:\"pbr_metallic_roughness\" with base_color",
                None,
                false,
            )),
            None => {}
        }
        if kind.is_some() || object.contains_key("base_color") {
            validate_color_ref(
                &format!("{path}.base_color"),
                object.get("base_color"),
                colors,
                diagnostics,
            );
        }
        if let Some(emissive) = object.get("emissive") {
            validate_color_ref(
                &format!("{path}.emissive"),
                Some(emissive),
                colors,
                diagnostics,
            );
        }
        validate_optional_non_negative(
            &format!("{path}.emissive_strength"),
            object.get("emissive_strength"),
            diagnostics,
        );
        validate_alpha_mode(
            &format!("{path}.alpha_mode"),
            object.get("alpha_mode"),
            diagnostics,
        );
        for field in [
            "base_color_texture",
            "normal_texture",
            "metallic_roughness_texture",
            "occlusion_texture",
            "emissive_texture",
        ] {
            validate_texture_slot(&format!("{path}.{field}"), object.get(field), diagnostics);
        }
        for field in GPU_SUPPORTED_ADVANCED_PBR_TEXTURE_FIELDS {
            validate_texture_slot(&format!("{path}.{field}"), object.get(*field), diagnostics);
        }
        match kind {
            Some("unlit" | "line" | "wireframe" | "edge") => {
                for field in ["metallic", "roughness"] {
                    if object.contains_key(field) {
                        diagnostics.push(diagnostic(
                            "unsupported_feature",
                            "error",
                            format!("{path}.{field}"),
                            format!("unlit materials do not use {field}"),
                            "remove the field or use kind:\"pbr_metallic_roughness\"",
                            None,
                            false,
                        ));
                    }
                }
            }
            Some("pbr_metallic_roughness") => {
                validate_unit_float(
                    &format!("{path}.metallic"),
                    object.get("metallic"),
                    diagnostics,
                );
                validate_unit_float(
                    &format!("{path}.roughness"),
                    object.get("roughness"),
                    diagnostics,
                );
                validate_advanced_pbr_fields(&path, object, colors, diagnostics);
            }
            Some(_) | None => {}
        }
        if kind != Some("pbr_metallic_roughness") && preset.is_none() {
            reject_advanced_pbr_fields_for_non_pbr(&path, object, diagnostics);
        } else if preset.is_some() {
            validate_unit_float(
                &format!("{path}.metallic"),
                object.get("metallic"),
                diagnostics,
            );
            validate_unit_float(
                &format!("{path}.roughness"),
                object.get("roughness"),
                diagnostics,
            );
            validate_advanced_pbr_fields(&path, object, colors, diagnostics);
        }
        match kind {
            Some("line" | "wireframe" | "edge") => validate_optional_positive(
                &format!("{path}.stroke_width_px"),
                object.get("stroke_width_px"),
                diagnostics,
            ),
            Some(_) | None => {
                if object.contains_key("stroke_width_px") {
                    diagnostics.push(diagnostic(
                        "unsupported_feature",
                        "error",
                        format!("{path}.stroke_width_px"),
                        "stroke_width_px only applies to line, wireframe, and edge materials",
                        "remove the field or use a stroke material kind",
                        None,
                        false,
                    ));
                }
            }
        }
        if kind == Some("edge") {
            validate_optional_range(
                &format!("{path}.edge_angle_threshold_degrees"),
                object.get("edge_angle_threshold_degrees"),
                0.0,
                180.0,
                diagnostics,
            );
        } else if object.contains_key("edge_angle_threshold_degrees") {
            diagnostics.push(diagnostic(
                "unsupported_feature",
                "error",
                format!("{path}.edge_angle_threshold_degrees"),
                "edge_angle_threshold_degrees only applies to edge materials",
                "remove the field or use kind:\"edge\"",
                None,
                false,
            ));
        }
    }
}

fn validate_advanced_pbr_fields(
    path: &str,
    object: &serde_json::Map<String, Value>,
    colors: &BTreeSet<String>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    for field in [
        "clearcoat_factor",
        "clearcoat_roughness_factor",
        "sheen_roughness_factor",
        "anisotropy_strength_factor",
        "iridescence_factor",
        "transmission_factor",
    ] {
        validate_unit_float(&format!("{path}.{field}"), object.get(field), diagnostics);
    }
    for field in [
        "clearcoat_normal_scale",
        "iridescence_thickness_minimum_nm",
        "iridescence_thickness_maximum_nm",
        "dispersion_factor",
        "thickness_factor",
    ] {
        validate_optional_non_negative(&format!("{path}.{field}"), object.get(field), diagnostics);
    }
    validate_optional_finite(
        &format!("{path}.anisotropy_rotation_radians"),
        object.get("anisotropy_rotation_radians"),
        diagnostics,
    );
    validate_optional_positive(
        &format!("{path}.iridescence_ior"),
        object.get("iridescence_ior"),
        diagnostics,
    );
    validate_optional_positive(
        &format!("{path}.attenuation_distance"),
        object.get("attenuation_distance"),
        diagnostics,
    );
    validate_optional_ior(&format!("{path}.ior"), object.get("ior"), diagnostics);
    for field in ["sheen_color_factor", "attenuation_color"] {
        if let Some(value) = object.get(field) {
            validate_color_ref(&format!("{path}.{field}"), Some(value), colors, diagnostics);
        }
    }
    for field in GPU_UNSUPPORTED_VOLUME_TEXTURE_FIELDS {
        if object.contains_key(*field) {
            diagnostics.push(diagnostic(
                "unsupported_feature",
                "error",
                format!("{path}.{field}"),
                format!(
                    "{field} is not exposed by scene_recipe.v1 until the GPU path supports it without exceeding the WebGL2 texture-unit floor"
                ),
                "remove this texture slot; transmission_factor remains supported for recipe-authored glass",
                None,
                false,
            ));
        }
    }
}

fn reject_advanced_pbr_fields_for_non_pbr(
    path: &str,
    object: &serde_json::Map<String, Value>,
    diagnostics: &mut Vec<SceneRecipeDiagnosticV1>,
) {
    for field in ADVANCED_PBR_SCALAR_FIELDS
        .iter()
        .chain(ADVANCED_PBR_TEXTURE_FIELDS)
    {
        if object.contains_key(*field) {
            diagnostics.push(diagnostic(
                "unsupported_feature",
                "error",
                format!("{path}.{field}"),
                format!("{field} only applies to pbr_metallic_roughness materials"),
                "remove the field or use kind:\"pbr_metallic_roughness\"",
                None,
                false,
            ));
        }
    }
}