viewport-lib-types 0.2.0

Pure-data types for viewport-lib: submission payloads, handles, and frame config, free of any GPU dependency.
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
//! Per-item material and rendering settings.

/// Per-item render settings: visibility, appearance overrides, pick identity, and selection state.
///
/// Always use `Default::default()` as the base, then set individual fields:
///
/// ```rust
/// # use viewport_lib_types::material::ItemSettings;
/// let mut s = ItemSettings::default();
/// s.hidden = true;
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ItemSettings {
    /// Hide the object entirely. Default `false`.
    pub hidden: bool,
    /// Skip all lighting calculations and output raw colour. Default `false`.
    pub unlit: bool,
    /// Global opacity multiplier. 1.0 = fully opaque, 0.0 = fully transparent. Default 1.0.
    pub opacity: f32,
    /// Render as wireframe regardless of the global wireframe flag. Default `false`. Extended conceptually to non-mesh
    /// objects to render their sub-objects as meshes, e.g., a VolumeItem has its voxels as wireframe boxes.
    pub wireframe: bool,
    /// GPU pick identifier. `PickId::NONE` = not pickable. Default `PickId::NONE`.
    pub pick_id: crate::ids::PickId,
    /// Whether the item is currently selected. Default `false`.
    pub selected: bool,
    /// Whether this item casts shadows in the shadow map pass. Default `true`.
    ///
    /// Set to `false` for items that should be visible but should not contribute
    /// to the shadow map (HUD-style overlays, debug helpers, light source
    /// indicators). Honoured by mesh-family items; non-mesh items currently do
    /// not participate in the shadow pass and treat this flag as a no-op.
    pub cast_shadows: bool,
    /// Whether this item samples the shadow map when shaded. Default `true`.
    ///
    /// Set to `false` to render the item as if no shadow caster occludes it.
    /// Useful for items that should remain fully lit regardless of the scene's
    /// shadow casters (debug visualisations, glowing markers). Honoured by
    /// mesh-family items; non-mesh items currently do not sample shadow maps and
    /// treat this flag as a no-op.
    pub receive_shadows: bool,
    /// Whether this item is exempt from the global clip objects. Default `false`
    /// (the item is clipped like the rest of the scene).
    ///
    /// Set to `true` for annotations and manipulator visuals that must stay fully
    /// visible even where clip planes / box / sphere / cylinder volumes cut the
    /// scene (clip-object outlines, section-plane indicators, debug guides).
    /// Honoured by item types whose shader samples the clip uniform; types that
    /// do not sample it treat this flag as a no-op.
    pub ignore_clip: bool,
}

impl Default for ItemSettings {
    fn default() -> Self {
        Self {
            hidden: false,
            unlit: false,
            opacity: 1.0,
            wireframe: false,
            pick_id: crate::ids::PickId::NONE,
            selected: false,
            cast_shadows: true,
            receive_shadows: true,
            ignore_clip: false,
        }
    }
}

/// Lit shading model selected by a [`Material`].
///
/// The variants are mutually exclusive: a material picks one path. The `unlit`
/// per-item bypass on [`ItemSettings`] is checked independently and takes
/// precedence over the shading model.
///
/// New shading models (Toon, SSS, Gooch, ...) are added here as new variants.
/// The glTF PBR extensions (clearcoat, sheen, anisotropy, iridescence) layer on
/// top of `Pbr` as separate fields on `Material` rather than as variants here.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ShadingModel {
    /// Blinn-Phong lit shading using `ambient` / `diffuse` / `specular` / `shininess`.
    Phong,
    /// Cook-Torrance GGX physically based shading driven by `metallic` and `roughness`.
    Pbr,
    /// Matcap lookup. Replaces the lit path with a captured-lighting texture.
    /// Blendable matcaps tint with `base_colour`; static matcaps override colour entirely.
    Matcap(crate::ids::MatcapId),
    /// Flat (faceted) shading: the per-vertex normal is discarded and a
    /// geometric normal is recomputed per-fragment from screen-space
    /// derivatives of world position. Produces visible polygon edges on
    /// triangulated meshes without changing the lighting block; everything
    /// else (hemisphere ambient, scene lights, shadows, AO, alpha mode)
    /// runs as it does for [`Phong`].
    ///
    /// Edges appear at the triangulation level, not at logical-face
    /// boundaries. A quad authored as two triangles shows both; a CAD
    /// surface tessellated finely shows the tessellation.
    Flat,
}

impl Default for ShadingModel {
    fn default() -> Self {
        Self::Phong
    }
}

/// Procedural UV visualisation mode for parameterisation inspection.
///
/// When set on a [`Material`], the mesh fragment shader ignores the albedo texture and
/// renders a procedural pattern driven by the mesh UV coordinates instead. Useful for
/// inspecting UV distortion, seams, and parameterisation quality without needing a texture.
///
/// Requires the mesh to have UV coordinates. Has no effect if the mesh lacks UVs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ParamVisMode {
    /// Alternating black/white squares tiled in UV space.
    Checker = 1,
    /// Thin grid lines at UV integer boundaries.
    Grid = 2,
    /// Polar checkerboard centred at UV (0.5, 0.5) : reveals rotational consistency.
    LocalChecker = 3,
    /// Concentric rings centred at UV (0.5, 0.5) : reveals radial distortion.
    LocalRadial = 4,
}

/// UV parameterisation visualisation settings.
///
/// Attach to [`Material::param_vis`] to enable procedural UV pattern rendering.
/// The `scale` controls tile frequency : higher values produce more, smaller tiles.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ParamVis {
    /// Which procedural pattern to render.
    pub mode: ParamVisMode,
    /// Tile frequency multiplier. Default 8.0 : produces 8 checker squares per UV unit.
    pub scale: f32,
}

impl Default for ParamVis {
    fn default() -> Self {
        Self {
            mode: ParamVisMode::Checker,
            scale: 8.0,
        }
    }
}

/// Procedural pattern for back-face rendering.
///
/// Used with [`PatternConfig`] to select which procedural pattern is drawn on back faces.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BackfacePattern {
    /// Alternating squares in world XY.
    Checker = 0,
    /// Diagonal lines (45 degrees).
    Hatching = 1,
    /// Two sets of diagonal lines (45 and 135 degrees).
    Crosshatch = 2,
    /// Horizontal stripes.
    Stripes = 3,
}

/// Configuration for procedural back-face patterns.
///
/// Used with [`BackfacePolicy::Pattern`]. The `scale` controls how many pattern cells
/// fit across the object's longest bounding-box dimension, so the pattern always looks
/// proportional regardless of the mesh's physical size.
///
/// Prefer using `..Default::default()` when constructing so that future fields
/// added to this struct do not require changes at every call site:
///
/// ```rust
/// # use viewport_lib_types::material::{PatternConfig, BackfacePattern};
/// # use viewport_lib_types::colour::Colour;
/// let cfg = PatternConfig {
///     pattern: BackfacePattern::Hatching,
///     colour: Colour::rgb(255, 128, 0),
///     ..Default::default()
/// };
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PatternConfig {
    /// Which procedural pattern to draw on back faces.
    pub pattern: BackfacePattern,
    /// Foreground colour for the pattern.
    pub colour: crate::colour::Colour,
    /// Number of pattern cells across the object's longest bounding-box dimension.
    ///
    /// Default 20.0. Increase for finer detail, decrease for coarser.
    pub scale: f32,
}

impl Default for PatternConfig {
    fn default() -> Self {
        Self {
            pattern: BackfacePattern::Checker,
            colour: crate::colour::Colour::linear_rgb(1.0, 0.5, 0.0),
            scale: 20.0,
        }
    }
}

/// Controls how back faces of a mesh are rendered.
///
/// Use [`BackfacePolicy::Cull`] (the default) to hide back faces, [`BackfacePolicy::Identical`]
/// to show them with the same shading as front faces, or [`BackfacePolicy::DifferentColour`]
/// to shade back faces in a distinct colour : useful for spotting mesh orientation errors or
/// highlighting the interior of open surfaces.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BackfacePolicy {
    /// Back faces are culled (invisible). Default.
    Cull,
    /// Back faces are visible and shaded identically to front faces.
    Identical,
    /// Back faces are visible and shaded in the given colour.
    ///
    /// Front faces receive normal shading; back faces receive Blinn-Phong shading
    /// with the supplied colour and the same ambient/diffuse/specular coefficients.
    /// The normal is flipped so lighting is computed from the back-face perspective.
    DifferentColour(crate::colour::Colour),
    /// Back faces are visible and tinted darker by the given factor (0.0..1.0).
    ///
    /// The base colour is multiplied by `(1.0 - factor)`, so `Tint(0.3)` means
    /// back faces are 30% darker. The normal is flipped for correct lighting.
    Tint(f32),
    /// Back faces are rendered with a procedural pattern scaled to the object's size.
    ///
    /// The pattern density is relative to the object's world-space bounding box, so
    /// the appearance stays consistent across meshes of different physical sizes.
    /// The normal is flipped for correct lighting.
    Pattern(PatternConfig),
}

impl Default for BackfacePolicy {
    fn default() -> Self {
        BackfacePolicy::Cull
    }
}

/// Alpha blending mode for a material, matching the glTF `alphaMode` field.
///
/// Controls how the fragment alpha value is interpreted by the renderer.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum AlphaMode {
    /// Alpha is ignored; the surface is always fully opaque. Default.
    Opaque,
    /// Fragments with alpha below the cutoff are discarded; others are fully opaque.
    /// The f32 value is the cutoff threshold in [0, 1].
    Mask(f32),
    /// Standard (straight, non-premultiplied) alpha blending through the OIT pass.
    /// The fragment colour is treated as un-premultiplied: the pass multiplies it
    /// by alpha before compositing.
    Blend,
    /// Alpha blending for content whose RGB is already premultiplied by alpha.
    ///
    /// Like [`Blend`](AlphaMode::Blend) it routes through the OIT pass, but the
    /// pass does not multiply RGB by alpha a second time. Use it for sources that
    /// deliver premultiplied colour: authored images stored premultiplied, hardware
    /// video-decode surfaces, and external GPU textures brought in through
    /// `register_texture_view` (a Wayland client buffer, for instance, is
    /// premultiplied by convention). Blending premultiplied content with the
    /// straight `Blend` equation double-applies alpha at antialiased edges and
    /// leaves a dark halo; this mode composites it correctly.
    BlendPremultiplied,
}

impl Default for AlphaMode {
    fn default() -> Self {
        AlphaMode::Opaque
    }
}

/// Per-object material properties for Blinn-Phong and PBR shading.
///
/// Each `SceneRenderItem` carries its own `Material`, so shading parameters
/// vary per object rather than applying globally to the whole scene.
///
/// This struct is `#[non_exhaustive]`: construct via [`Material::default`],
/// [`Material::from_colour`], or spread syntax (`..Default::default()`). This allows new
/// fields to be added later without breaking downstream code.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Material {
    /// Base diffuse colour. Build it with [`Colour::rgb`](crate::colour::Colour::rgb)
    /// / [`hex`](crate::colour::Colour::hex) for an sRGB value, or
    /// [`Colour::linear_rgb`](crate::colour::Colour::linear_rgb) for one already in
    /// linear space. Default is linear `[0.7, 0.7, 0.7]`.
    pub base_colour: crate::colour::Colour,
    /// Ambient light coefficient. Default 0.15.
    pub ambient: f32,
    /// Diffuse light coefficient. Default 0.75.
    pub diffuse: f32,
    /// Specular highlight coefficient. Default 0.4.
    pub specular: f32,
    /// Specular shininess exponent. Default 32.0.
    pub shininess: f32,
    /// Metallic factor for PBR Cook-Torrance shading. 0=dielectric, 1=metal. Default 0.0.
    pub metallic: f32,
    /// Roughness factor for PBR microfacet distribution. 0=mirror, 1=fully rough. Default 0.5.
    pub roughness: f32,
    /// Optional albedo texture identifier. None = no texture applied. Default None.
    pub texture_id: Option<crate::ids::TextureId>,
    /// Optional normal map texture identifier. None = no normal mapping. Default None.
    ///
    /// The normal map must be in tangent-space with XY encoded as RG (0..1 -> -1..+1).
    /// Requires UVs and tangents on the mesh for correct TBN construction.
    pub normal_map_id: Option<crate::ids::TextureId>,
    /// Scales the tangent-space normal's XY before the TBN transform, so a normal
    /// map can be dialled up or down without re-authoring the texture. Default 1.0
    /// (the map is used at authored strength). `0.0` flattens to the geometric
    /// normal; values above 1.0 exaggerate the relief. Matches glTF `normalScale`
    /// and Unity `_BumpScale`. No effect when `normal_map_id` is None.
    #[cfg_attr(feature = "serde", serde(default = "default_normal_strength"))]
    pub normal_strength: f32,
    /// Optional ambient occlusion map texture identifier. None = no AO map. Default None.
    ///
    /// The AO map R channel encodes cavity factor (0=fully occluded, 1=fully lit).
    /// Applied multiplicatively to ambient and diffuse terms.
    pub ao_map_id: Option<crate::ids::TextureId>,
    /// Optional combined metallic-roughness texture (ORM layout). Default None.
    ///
    /// Matches the glTF `metallicRoughnessTexture`: G channel encodes roughness,
    /// B channel encodes metallic. Each channel is multiplied by the corresponding
    /// scalar factor (`roughness`, `metallic`). Only sampled when the material's
    /// `shading_model` is `ShadingModel::Pbr`.
    pub metallic_roughness_texture_id: Option<crate::ids::TextureId>,
    /// Self-illumination colour, added to outgoing radiance after lighting.
    /// Default [0.0, 0.0, 0.0].
    ///
    /// Matches glTF `emissiveFactor` (a 0..1 colour). The emitted luminance in
    /// **nits** is `emissive * emissive_strength`, so this sets the hue and
    /// `emissive_strength` sets the brightness. Applied to both the Blinn-Phong
    /// and PBR paths. When `emissive_texture_id` is set, it is multiplied by the
    /// texture sample.
    pub emissive: crate::colour::Colour,
    /// Emissive luminance in **nits**, multiplying [`Self::emissive`]. Default
    /// `1.0`.
    ///
    /// Matches glTF `KHR_materials_emissive_strength`. Under photometric exposure
    /// an emitter must reach a luminance comparable to the lit scene to read as
    /// glowing: a few hundred nits for a soft indoor glow, thousands to stay
    /// bright against daylight. `emissive = [0, 0, 0]` emits nothing regardless.
    pub emissive_strength: f32,
    /// Optional emissive texture identifier. Default None.
    ///
    /// Matches glTF `emissiveTexture`. Sampled and multiplied by `emissive`
    /// (and hence by `emissive_strength`).
    pub emissive_texture_id: Option<crate::ids::TextureId>,
    /// Alpha handling mode. Default [`AlphaMode::Opaque`].
    ///
    /// `Mask(cutoff)` discards fragments below the cutoff using the alpha channel.
    /// `Blend` routes the mesh through the OIT transparent pass.
    pub alpha_mode: AlphaMode,
    /// Which lit shading model evaluates this surface. Default [`ShadingModel::Phong`].
    ///
    /// `Phong` uses Blinn-Phong (the faithful default: a cheaper model with a
    /// distinct highlight, energy-matched to PBR's diffuse so the two read at the
    /// same brightness under a given light); `Pbr` uses Cook-Torrance GGX driven
    /// by `metallic` and `roughness`; `Matcap(id)` replaces the lit path with a
    /// matcap texture lookup.
    /// The `unlit` per-item bypass on [`ItemSettings`] is checked independently
    /// and takes precedence over any value here.
    pub shading_model: ShadingModel,
    /// UV parameterisation visualisation. When set, replaces albedo/lighting with a
    /// procedural pattern in UV space : useful for inspecting parameterisation quality.
    ///
    /// Requires UV coordinates on the mesh. Default None (standard shading).
    pub param_vis: Option<ParamVis>,
    /// Back-face rendering policy. Default [`BackfacePolicy::Cull`] (back faces hidden).
    ///
    /// Use [`BackfacePolicy::Identical`] for single-sided geometry like planes and open
    /// surfaces. Use [`BackfacePolicy::DifferentColour`] to highlight back faces in a
    /// distinct colour : helpful for diagnosing mesh orientation errors.
    pub backface_policy: BackfacePolicy,
    /// Per-material UV offset applied to all texture samples (albedo, normal,
    /// AO, metallic-roughness, emissive). Default `[0.0, 0.0]`.
    ///
    /// Sampled as `uv = mesh_uv * uv_scale + uv_offset` in the fragment shader.
    /// Lets one mesh be shared between multiple atlas-region materials without
    /// duplicating vertex buffers : an asset pack with a single tree atlas can
    /// pick the trunk or leaves region per material instance.
    pub uv_offset: [f32; 2],
    /// Per-material UV scale. Default `[1.0, 1.0]`.
    ///
    /// See [`uv_offset`](Self::uv_offset) for the full sampling formula.
    pub uv_scale: [f32; 2],
    /// Min/max range applied to the AO map's R sample. Identity `[0.0, 1.0]`
    /// passes the sample through unchanged. Skipped when `ao_map_id` is None.
    ///
    /// The remap is `mix(min, max, raw_sample)`. Useful when a packed mask
    /// map authors AO in a reduced range (e.g. `[0.4, 1.0]`) and the renderer
    /// needs to normalise the sample before it drives lighting.
    ///
    /// This field also expresses glTF `occlusionStrength`: since strength is
    /// `mix(1.0, sample, strength)`, set `ao_range = [1.0 - strength, 1.0]`.
    /// There is deliberately no separate `occlusion_strength` field; an importer
    /// maps the glTF value onto this range. (grep: occlusionStrength, occlusion_strength)
    pub ao_range: [f32; 2],
    /// Min/max range applied to the metallic sample (B channel of the
    /// metallic-roughness texture in the glTF/ORM convention). Identity
    /// `[0.0, 1.0]`. Skipped when `metallic_roughness_texture_id` is None.
    ///
    /// Applied to the texture sample before the scalar `metallic` factor:
    /// `final = clamp(mix(min, max, raw.b) * metallic, 0.0, 1.0)`.
    pub metallic_range: [f32; 2],
    /// Min/max range applied to the roughness sample (G channel of the
    /// metallic-roughness texture). Identity `[0.0, 1.0]`. Skipped when
    /// `metallic_roughness_texture_id` is None.
    ///
    /// Applied to the texture sample before the scalar `roughness` factor:
    /// `final = max(mix(min, max, raw.g) * roughness, 0.04)`.
    pub roughness_range: [f32; 2],
    /// Custom shading through a registered material plugin. Default None (the
    /// built-in shading models above).
    ///
    /// `Some(id)` routes this material through the plugin registered with
    /// `register_material_plugin` under that id: the plugin's `shade_light` /
    /// `shade_ambient` / `recolor` hooks replace the built-in lighting terms,
    /// always on the PBR loop structure (`shading_model` is ignored for the
    /// lit path). Items with a plugin draw per-object rather than instanced.
    /// The id is a small `Copy` handle; plugin parameters live in the plugin's
    /// group-3 params buffer, not on this struct.
    #[cfg_attr(feature = "serde", serde(default))]
    pub shading_plugin: Option<MaterialPluginId>,
}

/// Handle to a material plugin registered with `register_material_plugin`,
/// optionally qualified to one of its variants
/// (`create_material_plugin_variant`).
///
/// A small `Copy` id carried on [`Material::shading_plugin`]. All variants of
/// a plugin share its WGSL and pipelines; each variant owns its group-3
/// params window and texture set, which is how two materials run the same
/// plugin with different parameters. `register_material_plugin` returns the
/// default variant (index 0). The renderer resolves the id against the
/// registry at draw time; an id from a different session (e.g. a deserialized
/// scene rendered before the plugin is re-registered) simply falls back to
/// built-in shading for the frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MaterialPluginId {
    pub(crate) plugin: u32,
    pub(crate) variant: u32,
}

impl MaterialPluginId {
    /// Index of the underlying shading-hook registration.
    pub fn plugin_index(&self) -> u32 {
        self.plugin
    }

    /// Variant index within the plugin (0 is the default variant).
    pub fn variant_index(&self) -> u32 {
        self.variant
    }

    /// Build a handle from a plugin registration index and variant index.
    /// Crate-internal: the shading-hook registry mints these; outside code
    /// obtains them from registration calls and treats them as opaque.
    #[doc(hidden)]
    pub fn from_parts(plugin: u32, variant: u32) -> Self {
        Self { plugin, variant }
    }
}

/// serde default for [`Material::normal_strength`]: the neutral 1.0, so a material
/// deserialized from data saved before this field existed reads as full strength
/// rather than the type default of 0.0 (which would flatten normals).
#[cfg(feature = "serde")]
fn default_normal_strength() -> f32 {
    1.0
}

impl Default for Material {
    fn default() -> Self {
        Self {
            base_colour: crate::colour::Colour::linear_rgb(0.7, 0.7, 0.7),
            ambient: 0.15,
            diffuse: 0.75,
            specular: 0.4,
            shininess: 32.0,
            metallic: 0.0,
            roughness: 0.5,
            texture_id: None,
            normal_map_id: None,
            normal_strength: 1.0,
            ao_map_id: None,
            metallic_roughness_texture_id: None,
            emissive: crate::colour::Colour::BLACK,
            emissive_strength: 1.0,
            emissive_texture_id: None,
            alpha_mode: AlphaMode::Opaque,
            shading_model: ShadingModel::Phong,
            param_vis: None,
            backface_policy: BackfacePolicy::Cull,
            uv_offset: [0.0, 0.0],
            uv_scale: [1.0, 1.0],
            ao_range: [0.0, 1.0],
            metallic_range: [0.0, 1.0],
            roughness_range: [0.0, 1.0],
            shading_plugin: None,
        }
    }
}

impl Material {
    /// Returns `true` if the backface policy makes back faces visible.
    pub fn is_two_sided(&self) -> bool {
        !matches!(self.backface_policy, BackfacePolicy::Cull)
    }

    /// Returns `true` if back faces need the per-object draw path.
    ///
    /// `Cull` and `Identical` render correctly through the instanced path: `Cull`
    /// uses back-face culling and `Identical` disables culling but shades back
    /// faces with the geometric normal, neither of which needs per-item state the
    /// instanced shader does not carry. The styled policies (`DifferentColour`,
    /// `Tint`, `Pattern`) flip the normal and read a per-item back-face colour, so
    /// they stay on the per-object path.
    pub fn backface_needs_per_object(&self) -> bool {
        matches!(
            self.backface_policy,
            BackfacePolicy::DifferentColour(_)
                | BackfacePolicy::Tint(_)
                | BackfacePolicy::Pattern(_)
        )
    }

    /// Returns `true` if this material uses alpha blending and should go through
    /// the OIT pass. True for both straight [`AlphaMode::Blend`] and
    /// [`AlphaMode::BlendPremultiplied`]; the two differ only in whether the pass
    /// premultiplies RGB, not in which pass they route to.
    pub fn is_blend(&self) -> bool {
        matches!(
            self.alpha_mode,
            AlphaMode::Blend | AlphaMode::BlendPremultiplied
        )
    }

    /// Returns `true` if this material's colour is already premultiplied by alpha
    /// ([`AlphaMode::BlendPremultiplied`]), so the OIT pass must skip its own
    /// premultiply step.
    pub fn is_premultiplied(&self) -> bool {
        matches!(self.alpha_mode, AlphaMode::BlendPremultiplied)
    }

    /// The emitted luminance in nits: `emissive` scaled by `emissive_strength`.
    /// This is the value the renderer packs into the GPU material and adds to
    /// outgoing radiance (an emissive texture, when present, multiplies it too).
    #[doc(hidden)]
    pub fn emissive_nits(&self) -> [f32; 3] {
        let e = self.emissive.to_linear_rgb();
        [
            e[0] * self.emissive_strength,
            e[1] * self.emissive_strength,
            e[2] * self.emissive_strength,
        ]
    }

    /// Construct from a base colour, all other parameters at their defaults.
    ///
    /// Pass a [`Colour`](crate::colour::Colour) (`Colour::rgb` / `hex` for sRGB,
    /// `Colour::linear_rgb` for linear); a bare `[f32; 3]` is taken as linear.
    pub fn from_colour(colour: impl Into<crate::colour::Colour>) -> Self {
        Self {
            base_colour: colour.into(),
            ..Default::default()
        }
    }

    /// A self-illuminated material glowing in the given `colour` at `nits`
    /// luminance. `base_colour` is set to the same colour so the surface reads
    /// consistently where the emission is dim. All other parameters take their
    /// defaults.
    pub fn emissive(colour: impl Into<crate::colour::Colour>, nits: f32) -> Self {
        let colour = colour.into();
        Self {
            base_colour: colour,
            emissive: colour,
            emissive_strength: nits,
            ..Default::default()
        }
    }

    /// Construct a Cook-Torrance PBR material.
    ///
    /// - `metallic`: 0.0 = dielectric, 1.0 = full metal
    /// - `roughness`: 0.0 = mirror, 1.0 = fully rough
    ///
    /// All other parameters take their defaults. Enable post-processing
    /// (`PostProcessSettings::enabled = true`) for correct HDR tone mapping.
    pub fn pbr(
        base_colour: impl Into<crate::colour::Colour>,
        metallic: f32,
        roughness: f32,
    ) -> Self {
        Self {
            base_colour: base_colour.into(),
            shading_model: ShadingModel::Pbr,
            metallic,
            roughness,
            ..Default::default()
        }
    }

    /// Returns `true` when the material's shading model is Cook-Torrance PBR.
    pub fn is_pbr(&self) -> bool {
        matches!(self.shading_model, ShadingModel::Pbr)
    }

    /// Returns `true` when the material's shading model is flat / faceted.
    pub fn is_flat(&self) -> bool {
        matches!(self.shading_model, ShadingModel::Flat)
    }

    /// Construct a flat (faceted) material with the given base colour. The
    /// per-vertex normal is replaced at the fragment stage by a geometric
    /// normal recovered from screen-space derivatives, so polygon edges
    /// remain visible on otherwise smooth meshes. Everything else takes
    /// material defaults.
    pub fn flat(base_colour: impl Into<crate::colour::Colour>) -> Self {
        Self {
            base_colour: base_colour.into(),
            shading_model: ShadingModel::Flat,
            ..Default::default()
        }
    }

    /// Returns the matcap id when the material uses a matcap shading model.
    pub fn matcap_id(&self) -> Option<crate::ids::MatcapId> {
        match self.shading_model {
            ShadingModel::Matcap(id) => Some(id),
            _ => None,
        }
    }

    /// Alias for [`Material::from_colour`].
    ///
    /// Returns a material with the given base colour and all other fields at defaults.
    /// "solid" is a more familiar name in some graphics contexts; both names are kept
    /// so existing `from_colour` call sites do not need updating.
    pub fn solid(colour: impl Into<crate::colour::Colour>) -> Self {
        Self::from_colour(colour)
    }

    /// Returns a material with only `texture_id` set; all other fields take defaults.
    ///
    /// Useful when the mesh already carries a UV layout and the only requirement is
    /// to apply a texture. Base colour, lighting model, and all other fields stay at
    /// their default values.
    pub fn textured(texture_id: crate::ids::TextureId) -> Self {
        Self {
            texture_id: Some(texture_id),
            ..Default::default()
        }
    }

    /// PBR constructor that also sets a baked ambient-occlusion texture.
    ///
    /// - `base_colour`: RGB albedo in linear 0..1
    /// - `metallic`: 0.0 = dielectric, 1.0 = full metal
    /// - `roughness`: 0.0 = mirror, 1.0 = fully rough
    /// - `ao_map`: baked AO texture id, or `None` to disable AO
    ///
    /// Sets `shading_model = ShadingModel::Pbr`. All other fields take defaults.
    /// Use [`Material::pbr`] when no AO map is needed.
    pub fn pbr_with_ao(
        base_colour: impl Into<crate::colour::Colour>,
        metallic: f32,
        roughness: f32,
        ao_map: Option<crate::ids::TextureId>,
    ) -> Self {
        Self {
            base_colour: base_colour.into(),
            shading_model: ShadingModel::Pbr,
            metallic,
            roughness,
            ao_map_id: ao_map,
            ..Default::default()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn item_settings_defaults() {
        let a = ItemSettings::default();
        assert!(!a.hidden);
        assert!(!a.unlit);
        assert!((a.opacity - 1.0).abs() < 1e-6);
        assert!(!a.wireframe);
        assert!(!a.selected);
    }

    #[test]
    fn item_settings_spread_syntax() {
        let a = ItemSettings {
            unlit: true,
            ..Default::default()
        };
        assert!(a.unlit);
        assert!(!a.hidden);
        assert!((a.opacity - 1.0).abs() < 1e-6);
    }

    #[test]
    fn flat_constructor_and_helpers() {
        let m = Material::flat([0.4, 0.5, 0.6]);
        assert!(m.is_flat());
        assert!(!m.is_pbr());
        assert!(m.matcap_id().is_none());
        assert!(matches!(m.shading_model, ShadingModel::Flat));
        assert_eq!(
            m.base_colour,
            crate::colour::Colour::linear_rgb(0.4, 0.5, 0.6)
        );

        let p = Material::default();
        assert!(!p.is_flat());
    }

    #[test]
    fn default_values() {
        let m = Material::default();
        assert!((m.base_colour.to_linear_rgb()[0] - 0.7).abs() < 1e-6);
        assert!((m.ambient - 0.15).abs() < 1e-6);
        assert!((m.diffuse - 0.75).abs() < 1e-6);
        assert!(!m.is_pbr());
        assert!(m.texture_id.is_none());
        assert!(m.normal_map_id.is_none());
        assert!(m.ao_map_id.is_none());
        assert!(m.matcap_id().is_none());
        assert!(m.param_vis.is_none());
    }

    #[test]
    fn from_colour_sets_base_colour() {
        let m = Material::from_colour([1.0, 0.0, 0.5]);
        assert!((m.base_colour.to_linear_rgb()[0] - 1.0).abs() < 1e-6);
        assert!((m.base_colour.to_linear_rgb()[1]).abs() < 1e-6);
        assert!((m.base_colour.to_linear_rgb()[2] - 0.5).abs() < 1e-6);
        // Other fields should be defaults
        assert!((m.ambient - 0.15).abs() < 1e-6);
    }

    #[test]
    fn pbr_constructor() {
        let m = Material::pbr([0.8, 0.2, 0.1], 0.9, 0.3);
        assert!(m.is_pbr());
        assert!((m.metallic - 0.9).abs() < 1e-6);
        assert!((m.roughness - 0.3).abs() < 1e-6);
        assert!((m.base_colour.to_linear_rgb()[0] - 0.8).abs() < 1e-6);
    }

    #[test]
    fn is_two_sided_cull() {
        let m = Material::default();
        assert!(!m.is_two_sided());
    }

    #[test]
    fn is_two_sided_identical() {
        let m = Material {
            backface_policy: BackfacePolicy::Identical,
            ..Default::default()
        };
        assert!(m.is_two_sided());
    }

    #[test]
    fn is_two_sided_different_colour() {
        let m = Material {
            backface_policy: BackfacePolicy::DifferentColour(crate::colour::Colour::linear_rgb(
                1.0, 0.0, 0.0,
            )),
            ..Default::default()
        };
        assert!(m.is_two_sided());
    }

    #[test]
    fn is_two_sided_tint() {
        let m = Material {
            backface_policy: BackfacePolicy::Tint(0.3),
            ..Default::default()
        };
        assert!(m.is_two_sided());
    }

    #[test]
    fn is_two_sided_pattern() {
        let m = Material {
            backface_policy: BackfacePolicy::Pattern(PatternConfig {
                pattern: BackfacePattern::Hatching,
                colour: crate::colour::Colour::linear_rgb(0.5, 0.5, 0.5),
                ..Default::default()
            }),
            ..Default::default()
        };
        assert!(m.is_two_sided());
    }

    #[test]
    fn blend_modes_route_through_oit() {
        let straight = Material {
            alpha_mode: AlphaMode::Blend,
            ..Default::default()
        };
        assert!(straight.is_blend());
        assert!(!straight.is_premultiplied());

        let premult = Material {
            alpha_mode: AlphaMode::BlendPremultiplied,
            ..Default::default()
        };
        // Premultiplied routes through the same OIT pass as straight blend...
        assert!(premult.is_blend());
        // ...but flags itself so the pass skips its premultiply step.
        assert!(premult.is_premultiplied());

        let opaque = Material::default();
        assert!(!opaque.is_blend());
        assert!(!opaque.is_premultiplied());

        let mask = Material {
            alpha_mode: AlphaMode::Mask(0.5),
            ..Default::default()
        };
        assert!(!mask.is_blend());
        assert!(!mask.is_premultiplied());
    }

    #[test]
    fn param_vis_default() {
        let pv = ParamVis::default();
        assert_eq!(pv.mode, ParamVisMode::Checker);
        assert!((pv.scale - 8.0).abs() < 1e-6);
    }

    #[test]
    fn solid_delegates_to_from_colour() {
        let colour = [1.0_f32, 0.0, 0.5];
        let a = Material::solid(colour);
        let b = Material::from_colour(colour);
        assert!((a.base_colour.to_linear_rgb()[0] - b.base_colour.to_linear_rgb()[0]).abs() < 1e-6);
        assert!((a.base_colour.to_linear_rgb()[1] - b.base_colour.to_linear_rgb()[1]).abs() < 1e-6);
        assert!((a.base_colour.to_linear_rgb()[2] - b.base_colour.to_linear_rgb()[2]).abs() < 1e-6);
        assert_eq!(a.is_pbr(), b.is_pbr());
        assert_eq!(a.texture_id, b.texture_id);
        assert_eq!(a.ao_map_id, b.ao_map_id);
        assert!((a.ambient - b.ambient).abs() < 1e-6);
    }

    #[test]
    fn textured_sets_texture_id() {
        let m = Material::textured(crate::ids::TextureId::from_raw(42));
        assert_eq!(m.texture_id, Some(crate::ids::TextureId::from_raw(42)));
        let def = Material::default();
        assert!(
            (m.base_colour.to_linear_rgb()[0] - def.base_colour.to_linear_rgb()[0]).abs() < 1e-6
        );
        assert!(!m.is_pbr());
    }

    #[test]
    fn pbr_with_ao_sets_fields() {
        let m = Material::pbr_with_ao(
            [0.8, 0.2, 0.1],
            0.9,
            0.3,
            Some(crate::ids::TextureId::from_raw(7)),
        );
        assert!(m.is_pbr());
        assert!((m.metallic - 0.9).abs() < 1e-6);
        assert!((m.roughness - 0.3).abs() < 1e-6);
        assert_eq!(m.ao_map_id, Some(crate::ids::TextureId::from_raw(7)));
        assert!((m.base_colour.to_linear_rgb()[0] - 0.8).abs() < 1e-6);
    }

    #[test]
    fn pbr_with_ao_accepts_none() {
        let m = Material::pbr_with_ao([0.5; 3], 0.0, 1.0, None);
        assert_eq!(m.ao_map_id, None);
    }

    #[test]
    fn channel_ranges_default_to_identity() {
        let m = Material::default();
        assert_eq!(m.ao_range, [0.0, 1.0]);
        assert_eq!(m.metallic_range, [0.0, 1.0]);
        assert_eq!(m.roughness_range, [0.0, 1.0]);
    }

    #[test]
    fn channel_ranges_round_trip_with_spread() {
        let m = Material {
            ao_range: [0.4, 1.0],
            metallic_range: [0.1, 0.9],
            roughness_range: [0.2, 0.8],
            ..Default::default()
        };
        assert_eq!(m.ao_range, [0.4, 1.0]);
        assert_eq!(m.metallic_range, [0.1, 0.9]);
        assert_eq!(m.roughness_range, [0.2, 0.8]);
        // Other defaults unaffected.
        assert_eq!(m.uv_offset, [0.0, 0.0]);
        assert_eq!(m.uv_scale, [1.0, 1.0]);
    }
}