three-rs 0.3.0

A port of three.js core and its WebGPURenderer to Rust on wgpu, graded against three.js's own end-to-end pixel comparison.
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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
//! Ports of `three.js/src/materials/nodes` — under `WebGPURenderer` every
//! material is a `NodeMaterial`, so this is the only material path.

pub(crate) mod blending;
mod dfg_lut;
pub mod environment;
pub(crate) mod fields;
pub mod lighting_model;
pub(crate) mod line2;
mod node_material;
pub mod phong;
pub mod physical;
pub(crate) mod toon;
pub mod transmission;

pub use node_material::{
    background_color_node, background_environment_color_node, background_node_color_node,
    background_pmrem_color_node, background_vertex_node, instanced_range, output_fragment_node,
    quad_vertex_node, render_output, setup, shadow_material, shadow_material_for,
    tone_mapping_node, AoContext, MrtContext, OutputContext, SetupContext,
};

pub use blending::{BlendEquation, BlendFactor, BlendMode, Blending};

use std::cell::Cell;

use crate::math::Color;
use crate::nodes::NodeRef;
use crate::textures::{CubeTexture, Texture};

/// `Material.id` — three.js' module-level `let _materialId = 0` counter, handed
/// out in construction order. It is what the renderer's program cache keys a
/// material on (`RenderObjects.get()` chains on the material object itself),
/// so it has to behave like an object identity even though the material is a
/// value here:
///
/// - every `MeshBasicNodeMaterial::new()` / `default()` gets a fresh id;
/// - **`clone()` gets a fresh id too**, as `Material.clone()` does — `new
///   this.constructor().copy( this )` is a new object with a new `id`. A clone
///   that later diverges (a different `map`, say) therefore can never be
///   served the original's program.
///
/// The renderer snapshots a material into its render list by cloning it, and
/// takes the key from the source before it does, so that per-frame copy is not
/// a new material as far as the cache is concerned.
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MaterialId(usize);

impl MaterialId {
    fn next() -> Self {
        thread_local! {
            static MATERIAL_ID: Cell<usize> = const { Cell::new(0) };
        }
        MATERIAL_ID.with(|id| {
            let next = id.get();
            id.set(next + 1);
            MaterialId(next)
        })
    }

    /// The number itself, for keying on.
    pub fn get(&self) -> usize {
        self.0
    }
}

/// A fresh id, never a copy — see the type's docs.
impl Clone for MaterialId {
    fn clone(&self) -> Self {
        Self::next()
    }
}

/// `three.js/src/constants.js` sides.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Side {
    /// `FrontSide`.
    Front,
    /// `BackSide`.
    Back,
    /// `DoubleSide` — `_getPrimitiveState()` leaves `cullMode` at `'none'`, so
    /// the front-face winding no longer matters. `BatchedText`'s material.
    Double,
}

/// `three.js/src/constants.js` depth functions — `Material.depthFunc`, which
/// `WebGPUPipelineUtils._getDepthCompare()` maps one-to-one onto a WebGPU
/// compare function when `depthTest` is on.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum DepthFunc {
    /// `NeverDepth`.
    Never,
    /// `AlwaysDepth`.
    Always,
    /// `LessDepth`.
    Less,
    /// `LessEqualDepth` — three's default.
    #[default]
    LessEqual,
    /// `EqualDepth`.
    Equal,
    /// `GreaterEqualDepth`.
    GreaterEqual,
    /// `GreaterDepth`.
    Greater,
    /// `NotEqualDepth`.
    NotEqual,
}

/// `three.js/src/constants.js` stencil functions — `Material.stencilFunc`,
/// which `WebGPUPipelineUtils._getStencilCompare()` maps one-to-one onto a
/// WebGPU compare function while `stencil_write` is on.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StencilFunc {
    /// `NeverStencilFunc`.
    Never,
    /// `LessStencilFunc`.
    Less,
    /// `EqualStencilFunc`.
    Equal,
    /// `LessEqualStencilFunc`.
    LessEqual,
    /// `GreaterStencilFunc`.
    Greater,
    /// `NotEqualStencilFunc`.
    NotEqual,
    /// `GreaterEqualStencilFunc`.
    GreaterEqual,
    /// `AlwaysStencilFunc` — three's default.
    #[default]
    Always,
}

/// `three.js/src/constants.js` stencil operations — `Material.stencilFail`,
/// `.stencilZFail` and `.stencilZPass`, as `_getStencilOperation()` maps them.
/// `Increment` and `Decrement` clamp, as WebGPU's `increment-clamp` and
/// `decrement-clamp` do.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum StencilOp {
    /// `ZeroStencilOp`.
    Zero,
    /// `KeepStencilOp` — three's default.
    #[default]
    Keep,
    /// `ReplaceStencilOp` — writes `stencil_ref`.
    Replace,
    /// `IncrementStencilOp` (clamped).
    Increment,
    /// `DecrementStencilOp` (clamped).
    Decrement,
    /// `IncrementWrapStencilOp`.
    IncrementWrap,
    /// `DecrementWrapStencilOp`.
    DecrementWrap,
    /// `InvertStencilOp`.
    Invert,
}

/// `three.js/src/constants.js` tone-mapping modes — the ones the port needs.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ToneMapping {
    /// `NoToneMapping`.
    #[default]
    None,
    /// `ReinhardToneMapping`.
    Reinhard,
    /// `ACESFilmicToneMapping`.
    AcesFilmic,
    /// `LinearToneMapping` — `clamp( color * exposure, 0, 1 )`.
    Linear,
    /// `NeutralToneMapping` — the Khronos PBR Neutral tone mapper.
    Neutral,
    /// `AgXToneMapping` — Blender's AgX, through Rec. 2020.
    AgX,
    /// `CineonToneMapping` — Hejl and Burgess-Dawson's filmic operator,
    /// [`cineon_tone_mapping`](crate::nodes::tsl::cineon_tone_mapping).
    Cineon,
}

/// Which `NodeMaterial` subclass this is — i.e. which `setupLightingModel()`
/// the fragment flow runs. three.js models it with subclasses; here the flow is
/// one function with a `match`, so a new lighting model cannot be silently
/// skipped in one of the setup steps.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MaterialKind {
    /// `MeshBasicNodeMaterial` — `BasicLightingModel`, `lights = false`.
    #[default]
    Basic,
    /// `MeshPhongNodeMaterial` — `PhongLightingModel`.
    Phong,
    /// `MeshLambertNodeMaterial` — `new PhongLightingModel( false )`, three's
    /// own comment being "( specular ) -> force lambert". The same flow as
    /// [`Phong`](MaterialKind::Phong) with the Blinn-Phong specular lobe and
    /// the two material properties that feed it removed.
    Lambert,
    /// `SpriteNodeMaterial` — `BasicLightingModel` like `Basic`, but it
    /// overrides `setupPositionView()` with the billboarded quad.
    Sprite,
    /// `MeshStandardNodeMaterial` — `PhysicalLightingModel` with a constant
    /// dielectric F0 of 0.04 and an F90 of 1.
    Standard,
    /// `MeshPhysicalNodeMaterial` — the same lighting model, but
    /// `setupSpecular()` derives F0 from the `ior` and modulates it by
    /// `specularColor` / `specularColorMap` / `specularIntensity`. That block
    /// is the whole delta over `Standard`; `GLTFLoader` picks it whenever the
    /// asset uses `KHR_materials_specular` or `KHR_materials_ior`.
    Physical,
    /// `PointsNodeMaterial extends SpriteNodeMaterial`. `setupVertex()` returns
    /// `super.setupVertex()` — the plain MVP path — when `object.isPoints`, so
    /// the sprite quad expansion is *not* taken; the only thing it keeps from
    /// `SpriteNodeMaterial` is `setupPositionView()` and `transparent = true`.
    Points,
    /// `MeshNormalNodeMaterial` — no lighting model at all. It overrides
    /// `setupDiffuseColor()` outright with the packed view-space normal, so it
    /// takes neither the `colorNode` path nor the opacity / alpha-test /
    /// opaque-clamp tail.
    Normal,
    /// `Line2NodeMaterial` — the fat line. `BasicLightingModel` like `Basic`,
    /// plus an overridden `setupPosition()` (the screen-space quad) and
    /// `setupDiffuseColor()` (round-cap coverage, per-end instance colour).
    /// See `crate::materials::line2`.
    Line2,
    /// `MeshToonNodeMaterial` — `ToonLightingModel`: the Lambert flow with
    /// the direct light's `dotNL` replaced by a lookup in
    /// [`gradient_map`](MeshBasicNodeMaterial::gradient_map) (or a fixed
    /// two-step ramp without one). See `crate::materials::toon`.
    Toon,
}

/// Port of `MeshBasicNodeMaterial.js` + the `NodeMaterial.js` / `Material.js`
/// fields the ladder uses. Defaults mirror three.js: white, opaque,
/// `FrontSide`, depth test on with `LessEqualDepth`, depth write on,
/// `reflectivity = 1`.
///
/// # Changing a material after its first frame
///
/// The renderer builds a material's shader program once and looks it up by
/// `id` and `version` from then on (`docs/scene-graph.md`, "Program cache"),
/// exactly as `WebGPURenderer` does. So the rule is three.js':
///
/// - a field the *program* depends on — any node (`color_node`,
///   `position_node`, `fragment_node`, …), any map or `env_map`, `kind`,
///   `lights`, `lights_node`, `lighting_model`, `environment_specular`, `flat_shading`, `fog`, `transparent`,
///   `blending`, `alpha_to_coverage`, `world_units`, `size_attenuation`, `mask_node`, `cast_shadow_node` — needs
///   [`set_needs_update`](Self::set_needs_update) after it changes, which is
///   `material.needsUpdate = true`. Without it the old program keeps drawing.
/// - a field the program reads as a **uniform** — `color`, `opacity`,
///   `specular`, `shininess`, `emissive`, `emissive_intensity`, `metalness`,
///   `roughness`, `bump_scale`, `rotation`, `reflectivity`, `refraction_ratio` — is uploaded every
///   frame and needs nothing, as in three.js.
/// - `side`, `depth_test`, `depth_write`, `color_write`, the `stencil_*`
///   fields and the blend factors are pipeline state, keyed per draw, and need
///   nothing either.
/// - a map's (or `env_map`'s) filters and wrap modes need nothing: they are
///   part of the program's cache key, read off the textures at every draw, as
///   three's WebGPU backend reads them in `getMaterialCacheKey()`.
#[derive(Clone, Debug)]
pub struct MeshBasicNodeMaterial {
    /// `Material.id`. Read-only in spirit; see [`MaterialId`] for why a clone
    /// gets a new one.
    pub id: MaterialId,
    /// `Material.version` — "starts at 0 and counts how many times
    /// `needsUpdate` is set to true". Part of the program cache key.
    pub version: u32,
    /// Which `NodeMaterial` subclass this stands in for — see [`MaterialKind`].
    pub kind: MaterialKind,
    /// `Material.color` — `MeshBasicMaterial`'s diffuse colour.
    pub color: Color,
    /// `Material.opacity`.
    pub opacity: f64,
    /// `MeshBasicMaterial.reflectivity` — the mix factor `BasicEnvironmentNode`
    /// blends the reflected environment colour in by.
    pub reflectivity: f64,
    /// `MeshBasicMaterial.refractionRatio` — the index ratio
    /// [`refract_view`](crate::nodes::tsl::refract_view) bends the view ray by
    /// (`materialRefractionRatio`).
    ///
    /// 0.98 for the kinds whose three.js material has the property — Basic
    /// ([`new`](Self::new)), [`lambert`](Self::lambert) and
    /// [`phong`](Self::phong) — and 0 from every other constructor, whose
    /// material has none. Three's `materialRefractionRatio` is one shared
    /// `uniform( 0 )` that `onObjectUpdate` leaves alone when
    /// `material.refractionRatio` is `undefined`, so such a material reads 0
    /// until a Basic, Lambert or Phong draw writes it, and the last value
    /// written after that; the port writes this field every draw instead.
    pub refraction_ratio: f64,
    /// `MeshBasicMaterial.envMap` — `setupEnvironment()` turns it into
    /// `BasicEnvironmentNode( cubeTexture( envMap ) )`.
    pub env_map: Option<CubeTexture>,
    /// `MeshStandardMaterial.envMap` on a PBR material, which
    /// `NodeMaterial.setupEnvironment()` turns into `EnvironmentNode(
    /// pmremTexture( envMap ) )` rather than a plain cube read. The handle
    /// comes from a
    /// [`PmremEnvironment`](crate::nodes::pmrem_node::PmremEnvironment), which
    /// owns the generated atlas; `scene.environment` reaches the same place by
    /// being copied onto every material that has none of its own.
    pub pmrem_env: Option<environment::PmremHandle>,
    /// `NodeMaterial.colorNode` — replaces the `materialColor` uniform in
    /// `setupDiffuseColor()`.
    pub color_node: Option<NodeRef>,
    /// `NodeMaterial.opacityNode` — replaces the `materialOpacity` uniform in
    /// `setupDiffuseColor()`, so `DiffuseColor.a` is multiplied by a node's
    /// value instead. `float( opacityNode )` narrows a wider node to its first
    /// component, which is how `opacityNode = texture( map )` becomes
    /// `DiffuseColor.w * texel.x`.
    pub opacity_node: Option<NodeRef>,
    /// `NodeMaterial.alphaTestNode` — `diffuseColor.a.lessThanEqual( node
    /// ).discard()` at the end of `setupDiffuseColor()`.
    ///
    /// Takes precedence over [`alpha_test`](Self::alpha_test), as in three.
    pub alpha_test_node: Option<NodeRef>,
    /// `Material.alphaTest` — when positive, `setupDiffuseColor()` discards
    /// against the `materialAlphaTest` object uniform
    /// (`webgpu_shadowmap_pointlight`'s spheres use 0.5). The shadow pass
    /// copies it onto its override material, so cut-away texels cast no
    /// shadow either.
    pub alpha_test: f64,
    /// `Material.alphaHash` — when set, `setupDiffuseColor()` discards the
    /// fragments whose alpha is below `getAlphaHashThreshold( positionLocal )`
    /// (see `crate::nodes::alpha_hash`), the stochastic stand-in for
    /// blending that `webgpu_materials_alphahash` renders.
    pub alpha_hash: bool,
    /// `Material.alphaMap` — `materialOpacity` becomes `opacity * texture(
    /// alphaMap )` (`MaterialNode.OPACITY`), a `vec4` product that the alpha
    /// assign narrows back to its `.x`. Copied onto the shadow pass's
    /// override material like [`alpha_test`](Self::alpha_test).
    pub alpha_map: Option<Texture>,
    /// `NodeMaterial.emissiveNode` — `setupLighting()`'s EMISSIVE tail:
    /// `EmissiveColor = vec3( emissiveNode )` and `outgoingLight +=
    /// EmissiveColor`. On a Phong or Standard material the `materialEmissive`
    /// uniform already takes that path; on an unlit `MeshBasicNodeMaterial`
    /// this node is the only way in, which is what `webgpu_instance_uniform`
    /// uses it for.
    pub emissive_node: Option<NodeRef>,
    /// `MeshPhongMaterial.specular` / `.shininess` / `.emissive` /
    /// `.emissiveIntensity`. The dumps show all four reaching the shader as
    /// object-group uniforms on every Phong material, even the ones the example
    /// never sets.
    /// `MeshPhongMaterial.specular` — the Blinn-Phong specular tint. Read by
    /// [`Phong`](MaterialKind::Phong) only; [`Lambert`](MaterialKind::Lambert)
    /// forces the specular lobe off.
    pub specular: Color,
    /// `MeshPhongMaterial.shininess` — the Blinn-Phong specular exponent.
    pub shininess: f64,
    /// `Material.emissive` — the uniform tail `NodeMaterial.setupLighting()`
    /// adds to `outgoingLight` on a lit material.
    pub emissive: Color,
    /// `Material.emissiveIntensity` — multiplies [`emissive`](Self::emissive).
    pub emissive_intensity: f64,
    /// `Material.flatShading` — `NodeMaterial.setupNormal()` picks `normalFlat`
    /// (the screen-space derivative frame) over the interpolated vertex normal,
    /// so there is no `normal` attribute and no normal varying at all.
    pub flat_shading: bool,
    /// `NodeMaterial.lights`. `false` is the light spheres' material: no
    /// lighting flow at all, `outgoingLight = DiffuseColor.rgb`.
    pub lights: bool,
    /// `material.lightsNode = lights( [ light1 ] )` — the selective-lights
    /// form. Indices into `Scene.lights`, since a `PointLight` is owned by the
    /// scene here rather than shared through an `Rc`. `None` means "every light
    /// in the scene", which is what `LightsNode` defaults to.
    pub lights_node: Option<Vec<usize>>,
    /// The `lightingModel` of `material.lightsNode = lights( [ … ] ).context(
    /// { lightingModel } )` — a user-defined
    /// [`LightingModel`](lighting_model::LightingModel) that the `LightsNode`
    /// drives in place of a built-in one.
    ///
    /// `LightingContextNode.setup()` reads `this.lightingModel ||
    /// builder.context.lightingModel`, so the material's own
    /// `setupLightingModel()` wins: this is read only by the kinds that have
    /// none of their own — `Points`, `Sprite`, and `Basic` standing in for a
    /// bare `NodeMaterial` — and ignored, as in three, by Phong, Lambert,
    /// Standard and Physical. A program input, like `lights_node`.
    pub lighting_model: Option<std::rc::Rc<dyn lighting_model::LightingModel>>,
    /// Whether `PhysicalLightingModel.indirectSpecular()` reads the
    /// environment's specular radiance. `true` is three's model.
    ///
    /// `false` is the patch `webgpu_postprocessing_ssr_denoise` applies to
    /// `PhysicalLightingModel.prototype.indirectSpecular`, where screen-space
    /// reflections supply all the specular: `builder.context.radiance =
    /// vec3( 0 )` and, on a clearcoat model, `clearcoatRadiance.assign(
    /// vec3( 0 ) )`, before the original method runs. The environment's
    /// diffuse term (`iblIrradiance`) is untouched, and `EnvironmentNode`
    /// still samples `radiance`, as three's dump does; only the read in
    /// `indirectSpecular` becomes the zero vector. Three patches the
    /// prototype for the whole page; the port scopes it to the material,
    /// which the page applies to every material in the scene. Read by
    /// Standard and Physical only, and a program input, like `lights`
    /// (`docs/nodes.md` §89).
    pub environment_specular: bool,
    /// `material.maskNode` — `NodeMaterial.setupDiscard()` turns it into
    /// `If( mask.not(), () => Discard() )` at the top of the fragment.
    pub mask_node: Option<NodeRef>,
    /// `material.castShadowNode` — the colour this material casts into the
    /// shadow map's colour target, which receivers tint their shadow by when
    /// `renderer.shadowMap.transmitted` is on (`docs/nodes.md` §43).
    /// `Renderer._getShadowNodes()` reads its `.rgb` and `.a`.
    pub cast_shadow_node: Option<NodeRef>,
    /// `material.receivedShadowPositionNode` —
    /// `ShadowBaseNode.setupShadowPosition()` assigns it to
    /// `shadowPositionWorld` instead of `positionWorld`.
    pub received_shadow_position_node: Option<NodeRef>,
    /// `Material.fog` — `false` on the shadow-pass material, which is why the
    /// shadow programs carry no fog mix even though `scene.fog` is set.
    pub fog: bool,
    /// `MeshStandardMaterial.metalness` / `.roughness` and the three maps the
    /// example uses. `map` multiplies the diffuse colour; `roughnessMap` takes
    /// its green channel and `metalnessMap` its blue, which is the glTF
    /// packing three.js follows.
    pub metalness: f64,
    /// `MeshStandardMaterial.roughness`.
    pub roughness: f64,
    /// `Material.map` — the base colour texture; multiplies
    /// [`color`](Self::color).
    pub map: Option<Texture>,
    /// `Material.vertexColors` — `setupDiffuseColor()` multiplies the diffuse
    /// colour by `vertexColor()`, so one `LineSegments` (or one mesh) can carry
    /// a colour per vertex instead of one per draw call. The geometry needs a
    /// three-component `color` attribute; see
    /// [`vertex_color`](crate::nodes::tsl::vertex_color) for the one divergence
    /// from three's node.
    pub vertex_colors: bool,
    /// `MeshStandardMaterial.roughnessMap` — its green channel multiplies
    /// [`roughness`](Self::roughness).
    pub roughness_map: Option<Texture>,
    /// `MeshStandardMaterial.metalnessMap` — its blue channel multiplies
    /// [`metalness`](Self::metalness).
    pub metalness_map: Option<Texture>,
    /// `MeshStandardMaterial.emissiveMap` — `MaterialNode.EMISSIVE`'s
    /// `emissiveNode.mul( texture )`, where `emissiveNode` is already
    /// `emissive * emissiveIntensity`. The multiply is a `vec4` one in three
    /// (`vec4( emissive, 1 ) * tex`) and the `.xyz` is taken afterwards, which
    /// is why the port builds it the same way rather than multiplying `vec3`s.
    pub emissive_map: Option<Texture>,
    /// `MeshToonMaterial.gradientMap` — the ramp `ToonLightingModel` reads
    /// the direct light's `dotNL * 0.5 + 0.5` through. Read by the `Toon`
    /// kind only.
    pub gradient_map: Option<Texture>,
    /// `MeshStandardMaterial.aoMap` / `.aoMapIntensity` — `materialAO`,
    /// `tex.r.sub( 1 ).mul( aoMapIntensity ).add( 1 )`, assigned to the
    /// `AmbientOcclusion` property by `NodeMaterial.setupAmbientOcclusion()`.
    /// Read by every lit mesh kind (Basic, Lambert, Phong, Toon, Standard,
    /// Physical), as three's `aoMap` is.
    pub ao_map: Option<Texture>,
    /// `MeshStandardMaterial.aoMapIntensity` — multiplies
    /// [`ao_map`](Self::ao_map)'s contribution.
    pub ao_map_intensity: f64,
    /// `material.lightMap` (`MeshBasicMaterial`, `MeshLambertMaterial`,
    /// `MeshPhongMaterial`, `MeshStandardMaterial`, `MeshToonMaterial`) —
    /// read by [`material_light_map`](crate::nodes::tsl::material_light_map)
    /// only: three's `setupLightMap()` (an `IrradianceNode` in the lights
    /// list) is not ported, so the material's own flow does not apply it.
    pub light_map: Option<Texture>,
    /// `material.lightMapIntensity`, on the same materials as
    /// [`light_map`](Self::light_map) — the `materialLightMap` scale.
    pub light_map_intensity: f64,
    /// `material.specularMap` (`MeshBasicMaterial`, `MeshLambertMaterial`,
    /// `MeshPhongMaterial`) — read by
    /// [`material_specular_strength`](crate::nodes::tsl::material_specular_strength)
    /// only; the Phong flow does not apply it.
    pub specular_map: Option<Texture>,
    /// `MeshStandardMaterial.bumpMap` / `.bumpScale` — `BumpMapNode`.
    pub bump_map: Option<Texture>,
    /// `MeshStandardMaterial.bumpScale` — scales [`bump_map`](Self::bump_map)'s
    /// derivative before it perturbs the normal.
    pub bump_scale: f64,
    /// `MeshPhysicalMaterial`'s own fields. Nothing on this rung sets them, but
    /// rung 10's glTF materials will: `KHR_materials_specular` is
    /// `specularIntensity` + `specularColor`, and `ior` drives the dielectric
    /// F0. They are carried here so the material API does not have to change
    /// shape when `MaterialKind::Physical` arrives.
    pub clearcoat: f64,
    /// `MeshPhysicalMaterial.clearcoatRoughness`.
    pub clearcoat_roughness: f64,
    /// `MeshPhysicalMaterial.clearcoatMap` — `MaterialNode.CLEARCOAT`
    /// multiplies `clearcoat` by the texel's red channel.
    pub clearcoat_map: Option<Texture>,
    /// `MeshPhysicalMaterial.clearcoatRoughnessMap` —
    /// `MaterialNode.CLEARCOAT_ROUGHNESS` multiplies `clearcoatRoughness` by
    /// the texel's green channel.
    pub clearcoat_roughness_map: Option<Texture>,
    /// `MeshPhysicalMaterial.sheen` / `.sheenColor` / `.sheenRoughness` —
    /// `KHR_materials_sheen`. `sheen` is the intensity and `sheen_color` the
    /// tint; `MaterialNode.SHEEN` is `sheenColor.mul( sheen )` and the shader
    /// carries both as separate uniforms, which is why the multiply is in the
    /// WGSL and not here.
    ///
    /// `sheen > 0` is `MeshPhysicalNodeMaterial.useSheen`: it is what turns the
    /// whole sheen half of `PhysicalLightingModel` on, so a sheen colour with a
    /// zero intensity generates exactly the shader it did before.
    pub sheen: f64,
    /// `MeshPhysicalMaterial.sheenColor` — the sheen lobe's tint.
    pub sheen_color: Color,
    /// `MeshPhysicalMaterial.sheenRoughness`.
    pub sheen_roughness: f64,
    /// `MeshPhysicalMaterial.diffuseRoughness` — `KHR_materials_diffuse_roughness`.
    /// `diffuse_roughness > 0` is `MeshPhysicalNodeMaterial.useDiffuseRoughness`,
    /// which swaps the Lambert diffuse lobe of `PhysicalLightingModel` for
    /// the energy-preserving Oren–Nayar one (EON). `diffuseRoughnessMap` is
    /// not ported: no graded page carries one.
    pub diffuse_roughness: f64,
    /// `MeshPhysicalMaterial.ior` — drives the dielectric F0 that
    /// `setupSpecular()` derives before `specular_color` modulates it.
    pub ior: f64,
    /// `MeshPhysicalMaterial.specularIntensity` — `KHR_materials_specular`.
    pub specular_intensity: f64,
    /// `MeshPhysicalMaterial.specularColor` — `KHR_materials_specular`.
    pub specular_color: Color,
    /// `material.specularNode`.
    pub specular_node: Option<NodeRef>,
    /// `MeshStandardMaterial.normalMap` and `.normalScale`. `GLTFLoader`
    /// flips `normalScale.y` on a geometry that has a normal map but no
    /// `tangent` attribute (`useDerivativeTangents`), which is where Michelle's
    /// `( 1, -1 )` comes from — it is not in the asset.
    pub normal_map: Option<Texture>,
    /// `Material.normalScale` — scales [`normal_map`](Self::normal_map)'s `x`
    /// and `y` before it perturbs the surface normal.
    pub normal_scale: crate::math::Vector2,
    /// `MeshPhysicalMaterial.specularColorMap` — multiplies `specularColor`.
    pub specular_color_map: Option<Texture>,
    /// `MeshPhysicalMaterial.anisotropy` / `.anisotropyRotation` /
    /// `.anisotropyMap` — `KHR_materials_anisotropy`. The pair reaches the
    /// shader as one `materialAnisotropyVector` uniform,
    /// `vec2( anisotropy * cos( rotation ), anisotropy * sin( rotation ) )`,
    /// exactly as three's `MeshPhysicalNodeMaterial` builds it.
    pub anisotropy: f64,
    /// `MeshPhysicalMaterial.anisotropyRotation`, in radians.
    pub anisotropy_rotation: f64,
    /// `MeshPhysicalMaterial.anisotropyMap` — its red and green channels give
    /// the per-texel anisotropy strength and rotation.
    pub anisotropy_map: Option<Texture>,
    /// `MeshPhysicalMaterial.iridescence` — `KHR_materials_iridescence`'s
    /// strength. `iridescence > 0` is `MeshPhysicalNodeMaterial.useIridescence`,
    /// the flag that turns on `evalIridescence` and the iridescent F0 in
    /// `computeMultiscattering`, so every material with the default 0
    /// generates exactly the shader it did before. `iridescenceMap` is not
    /// ported: `MaterialNode.IRIDESCENCE` has no map branch in three
    /// (`docs/nodes.md` §95).
    pub iridescence: f64,
    /// `MeshPhysicalMaterial.iridescenceIOR` — the thin film's index of
    /// refraction.
    pub iridescence_ior: f64,
    /// `MeshPhysicalMaterial.iridescenceThicknessRange` — `[ minimum, maximum ]`
    /// in nanometres. With no
    /// [`iridescence_thickness_map`](Self::iridescence_thickness_map) only the
    /// maximum is read, which is three's own behaviour, not a typo.
    pub iridescence_thickness_range: [f64; 2],
    /// `MeshPhysicalMaterial.iridescenceThicknessMap` — its **green** channel
    /// interpolates the film thickness between the two ends of
    /// [`iridescence_thickness_range`](Self::iridescence_thickness_range).
    pub iridescence_thickness_map: Option<Texture>,
    /// `MeshPhysicalMaterial.clearcoatNormalMap` / `.clearcoatNormalScale`.
    /// The clearcoat lobe's own normal, through the same TBN sub-build the
    /// base normal map uses.
    pub clearcoat_normal_map: Option<Texture>,
    /// `MeshPhysicalMaterial.clearcoatNormalScale` — scales
    /// [`clearcoat_normal_map`](Self::clearcoat_normal_map)'s `x` and `y`.
    pub clearcoat_normal_scale: crate::math::Vector2,
    /// `MeshPhysicalMaterial.transmission` / `.thickness` /
    /// `.attenuationDistance` / `.attenuationColor` —
    /// `KHR_materials_transmission` and `KHR_materials_volume`. A non-zero
    /// `transmission` moves the object into the renderer's transmission pass.
    pub transmission: f64,
    /// `MeshPhysicalMaterial.transmissionMap` — `MaterialNode.TRANSMISSION`
    /// multiplies `transmission` by the texel's red channel.
    pub transmission_map: Option<Texture>,
    /// `MeshPhysicalMaterial.thickness` — `KHR_materials_volume`; the modelled
    /// thickness of the transmissive medium.
    pub thickness: f64,
    /// `MeshPhysicalMaterial.thicknessMap` — `MaterialNode.THICKNESS`
    /// multiplies `thickness` by the texel's green channel.
    pub thickness_map: Option<Texture>,
    /// `MeshPhysicalMaterial.attenuationDistance` — `KHR_materials_volume`;
    /// the distance light travels through the medium before it is tinted by
    /// [`attenuation_color`](Self::attenuation_color).
    pub attenuation_distance: f64,
    /// `MeshPhysicalMaterial.attenuationColor` — `KHR_materials_volume`.
    pub attenuation_color: Color,
    /// `material.normalNode` — e.g. `normalMap( texture( map ) )`.
    pub normal_node: Option<NodeRef>,
    /// `NodeMaterial.positionNode` — replaces `positionLocal`.
    ///
    /// **Deviation from r186, deliberate** (plan §5.2, `docs/nodes.md` §10):
    /// three's `setupPosition()` assigns the instance transform *first* and
    /// `positionNode` *after*, so `positionNode` discards the instance matrix.
    /// The port assigns `positionNode` first and then the instance transform.
    /// `SpriteNodeMaterial` reads it a second time in `setupPositionView()`.
    pub position_node: Option<NodeRef>,
    /// `SpriteNodeMaterial.scaleNode` / `.rotationNode`.
    pub scale_node: Option<NodeRef>,
    /// `SpriteNodeMaterial.rotationNode` — replaces the `materialRotation`
    /// uniform.
    pub rotation_node: Option<NodeRef>,
    /// `PointsNodeMaterial.sizeNode` — the point size in pixels when the
    /// material draws a [`Sprite`](crate::objects::Sprite) (instanced quads)
    /// rather than `Points`. Read by `setupVertexSprite()` only.
    pub size_node: Option<NodeRef>,
    /// `PointsMaterial.size` — the `materialPointSize` uniform. Read by
    /// [`material_point_size`](crate::nodes::tsl::material_point_size) only;
    /// the points flow sizes a sprite by [`size_node`](Self::size_node).
    pub size: f64,
    /// `SpriteMaterial.rotation` — the `materialRotation` uniform.
    pub rotation: f64,
    /// `LineBasicMaterial.linewidth` — the `materialLineWidth` uniform.
    ///
    /// A hairline `Line` ignores it, as three.js says: "WebGL and WebGPU ignore
    /// this setting and always render line primitives with a width of one
    /// pixel". It is read by the fat-line material, which turns each segment
    /// into a screen-space quad and so can honour a width.
    pub linewidth: f64,
    /// `SpriteNodeMaterial.sizeAttenuation`. `true` (the default) is the branch
    /// that *omits* the `mvPosition.z.negate()` scale factor.
    pub size_attenuation: bool,
    /// `NodeMaterial.vertexNode` — replaces the whole clip-position flow.
    pub vertex_node: Option<NodeRef>,
    /// `NodeMaterial.fragmentNode` — replaces the whole fragment flow.
    pub fragment_node: Option<NodeRef>,
    /// `NodeMaterial.outputNode` — replaces only what `output.color` is written
    /// from. The standard flow still runs and still writes the `Output`
    /// property, so the custom node can read `DiffuseColor`, `Output` and the
    /// normal accessors.
    pub output_node: Option<NodeRef>,
    /// `NodeMaterial.backdropNode` — what the diffuse light is replaced
    /// with: `LightsNode.setup()` assigns it to `totalDiffuse` on a lit
    /// material, `setupLighting()` makes it the outgoing light on an unlit
    /// one. Usually a [`viewport_shared_texture`] tap — the scene behind the
    /// object, read back. A material with one goes in the transparent list
    /// whatever [`transparent`](Self::transparent) says, so that it draws
    /// after what it reads.
    ///
    /// [`viewport_shared_texture`]: crate::nodes::display::viewport_shared_texture
    pub backdrop_node: Option<NodeRef>,
    /// `NodeMaterial.backdropAlphaNode` — blend [`backdrop_node`](Self::backdrop_node)
    /// over the lit diffuse by this much instead of replacing it.
    pub backdrop_alpha_node: Option<NodeRef>,
    /// `NodeMaterial.mrtNode` — the material's own MRT overrides, merged over
    /// the renderer's (the pass's) by `NodeMaterial.setup()`. Read only when a
    /// render target with more than one colour attachment is bound, which is
    /// what makes it inert for every material that is not drawn into one.
    ///
    /// `webgpu_postprocessing_bloom_selective` gives each of its fifty spheres
    /// a material whose `mrtNode` is `mrt( { bloomIntensity: uniform( 0 or 1 )
    /// } )`, over the pass's `mrt( { output, bloomIntensity: float( 0 ) } )`.
    pub mrt_node: Option<crate::nodes::MrtNode>,
    /// `MeshStandardNodeMaterial.metalnessNode` / `.roughnessNode` — the two
    /// `setupVariants()` inputs, replacing the `materialMetalness` /
    /// `materialRoughness` uniforms (and their maps). `webgpu_deferred`'s
    /// resolve material reads both out of the G-buffer.
    pub metalness_node: Option<NodeRef>,
    /// `MeshStandardNodeMaterial.roughnessNode` — see
    /// [`metalness_node`](Self::metalness_node).
    pub roughness_node: Option<NodeRef>,
    /// `NodeMaterial.depthNode` — `setupDepth()`'s value, written to the
    /// fragment stage's `@builtin( frag_depth )` output. Set only when the
    /// material is drawn with a depth buffer (three checks
    /// `depthWrite || depthTest` and the target's `depthBuffer`).
    pub depth_node: Option<NodeRef>,
    /// `NodeMaterial.contextNode = overrideNodes( [ … ] )` — the three
    /// accessors `webgpu_deferred`'s resolve material reads out of the
    /// G-buffer instead of out of the geometry. See `docs/nodes.md` §27.
    pub context_overrides: Option<crate::nodes::tsl::OverrideNodes>,
    /// `NodeMaterial.contextNode = context( { … } )` — builder-context keys
    /// installed for the whole of the material's setup, the way
    /// [`context`](crate::nodes::tsl::context) installs them for one subgraph.
    /// The keys the port reads are the ones `RetroPassNode` sets: `getUV`,
    /// which every [`texture`](crate::nodes::tsl::texture) tap of a material
    /// map takes in place of its default `uv()`, and `getTextureLevel`, which
    /// turns its `textureSample` into a `textureSampleLevel`. See
    /// `docs/nodes.md` §79.
    pub context_node: Option<crate::nodes::ContextValue>,
    /// `Material.side` — which face(s) are drawn.
    pub side: Side,
    /// `Material.visible` — `_projectObject()` skips an object whose material is
    /// not visible.
    pub visible: bool,
    /// `Material.transparent` — which of the render list's two arrays the object
    /// goes into, and so whether it is sorted front-to-back or back-to-front.
    pub transparent: bool,
    /// `Material.forceSinglePass` — a transparent `DoubleSide` material is
    /// drawn once, both faces together, instead of as three's back-then-front
    /// pair.
    pub force_single_pass: bool,
    /// `Material.wireframe` — only `MeshBasicMaterial` and its kin expose it.
    /// It reaches no shader: `WebGPUUtils.getPrimitiveTopology()` turns a
    /// wireframe `Mesh` into a `line-list`, and `Geometries.getIndex()`
    /// swaps the geometry's index for `getWireframeIndex()`'s — each
    /// triangle's three edges as six indices (see `Renderer`'s
    /// `wireframe_index`).
    pub wireframe: bool,
    /// `Material.blending` — `NormalBlending` by default, which together with
    /// `transparent: false` is what keeps a pipeline blend-state-free.
    pub blending: Blending,
    /// `Material.premultipliedAlpha` — selects the other half of the
    /// `_getBlending()` table.
    pub premultiplied_alpha: bool,
    /// `Material.alphaToCoverage` — read by `builder.isOpaque()`, by
    /// `Line2NodeMaterial`'s `alphaLine`, and by the pipeline's
    /// `alphaToCoverageEnabled` (with more than one sample).
    pub alpha_to_coverage: bool,
    /// `Line2NodeMaterial.worldUnits` (`_useWorldUnits`) — the fat line's
    /// `linewidth` is in world units rather than screen pixels. Read by
    /// `setup()` and by `LineSegments2.raycast()`; ignored by every other
    /// material. A program input: set it before the first frame, or call
    /// [`set_needs_update`](Self::set_needs_update).
    pub world_units: bool,
    /// `Material.blendSrc` / `.blendDst` / `.blendEquation` and the three
    /// `*Alpha` overrides (`None` is Three's `null`), read only under
    /// `CustomBlending`.
    pub blend_src: BlendFactor,
    /// `Material.blendDst`.
    pub blend_dst: BlendFactor,
    /// `Material.blendEquation`.
    pub blend_equation: BlendEquation,
    /// `Material.blendSrcAlpha`.
    pub blend_src_alpha: Option<BlendFactor>,
    /// `Material.blendDstAlpha`.
    pub blend_dst_alpha: Option<BlendFactor>,
    /// `Material.blendEquationAlpha`.
    pub blend_equation_alpha: Option<BlendEquation>,
    /// `Material.depthTest`.
    pub depth_test: bool,
    /// `Material.depthWrite`.
    pub depth_write: bool,
    /// `Material.depthFunc` — the depth compare while `depth_test` is on.
    pub depth_func: DepthFunc,
    /// `Material.colorWrite` — `false` masks every colour channel
    /// (`GPUColorWrite.NONE`), as the stencil-writing caps in
    /// `webgpu_clipping_stencil` do. Pipeline state.
    pub color_write: bool,
    /// `Material.stencilWrite` — turns the stencil test and the `stencil_*`
    /// operations on. Without it the pipeline keeps WebGPU's default stencil
    /// face state, as `_getStencilState()` does. Takes effect only on a
    /// renderer created with
    /// [`RendererParameters::stencil`](crate::renderer::RendererParameters::stencil),
    /// whose depth buffer has a stencil aspect. Pipeline state.
    pub stencil_write: bool,
    /// `Material.stencilWriteMask` — the pipeline's `stencilWriteMask`.
    pub stencil_write_mask: u32,
    /// `Material.stencilFunc` — the stencil compare.
    pub stencil_func: StencilFunc,
    /// `Material.stencilRef` — `setStencilReference()` for each draw.
    pub stencil_ref: u32,
    /// `Material.stencilFuncMask` — the pipeline's `stencilReadMask`.
    pub stencil_func_mask: u32,
    /// `Material.stencilFail` — the op when the stencil test fails.
    pub stencil_fail: StencilOp,
    /// `Material.stencilZFail` — the op when the stencil test passes and the
    /// depth test fails.
    pub stencil_z_fail: StencilOp,
    /// `Material.stencilZPass` — the op when both tests pass.
    pub stencil_z_pass: StencilOp,
    /// `Background`'s material samples the cube map through the background
    /// uniforms rather than an env map.
    pub name: &'static str,
}

impl Default for MeshBasicNodeMaterial {
    fn default() -> Self {
        Self {
            id: MaterialId::next(),
            version: 0,
            kind: MaterialKind::Basic,
            color: Color::new(1.0, 1.0, 1.0),
            opacity: 1.0,
            // `MeshPhongMaterial`'s own defaults: specular 0x111111,
            // shininess 30, emissive black, emissiveIntensity 1. `new Color(
            // 0x111111 )` is `setHex( hex, SRGBColorSpace )`, so the stored
            // value is the *linear* 0.0056, not 17/255 — eleven times dimmer,
            // and visible as a blown-out highlight on any Phong material that
            // does not override `specularNode`.
            specular: Color::from_hex(0x111111),
            shininess: 30.0,
            emissive: Color::new(0.0, 0.0, 0.0),
            emissive_intensity: 1.0,
            flat_shading: false,
            lights: false,
            lights_node: None,
            lighting_model: None,
            environment_specular: true,
            mask_node: None,
            cast_shadow_node: None,
            received_shadow_position_node: None,
            fog: true,
            // `MeshStandardMaterial` defaults.
            metalness: 0.0,
            roughness: 1.0,
            map: None,
            vertex_colors: false,
            roughness_map: None,
            metalness_map: None,
            emissive_map: None,
            gradient_map: None,
            ao_map: None,
            ao_map_intensity: 1.0,
            light_map: None,
            light_map_intensity: 1.0,
            specular_map: None,
            bump_map: None,
            bump_scale: 1.0,
            // `MeshPhysicalMaterial` defaults.
            clearcoat: 0.0,
            clearcoat_roughness: 0.0,
            clearcoat_map: None,
            clearcoat_roughness_map: None,
            sheen: 0.0,
            sheen_color: Color::new(0.0, 0.0, 0.0),
            sheen_roughness: 1.0,
            diffuse_roughness: 0.0,
            ior: 1.5,
            specular_intensity: 1.0,
            specular_color: Color::new(1.0, 1.0, 1.0),
            specular_node: None,
            normal_map: None,
            normal_scale: crate::math::Vector2::new(1.0, 1.0),
            specular_color_map: None,
            anisotropy: 0.0,
            anisotropy_rotation: 0.0,
            anisotropy_map: None,
            iridescence: 0.0,
            iridescence_ior: 1.3,
            iridescence_thickness_range: [100.0, 400.0],
            iridescence_thickness_map: None,
            clearcoat_normal_map: None,
            clearcoat_normal_scale: crate::math::Vector2::new(1.0, 1.0),
            transmission: 0.0,
            // three's `MeshPhysicalMaterial` defaults: no volume at all.
            thickness: 0.0,
            transmission_map: None,
            thickness_map: None,
            attenuation_distance: f64::INFINITY,
            attenuation_color: Color::new(1.0, 1.0, 1.0),
            normal_node: None,
            position_node: None,
            reflectivity: 1.0,
            refraction_ratio: 0.98,
            env_map: None,
            pmrem_env: None,
            color_node: None,
            opacity_node: None,
            alpha_test_node: None,
            alpha_test: 0.0,
            alpha_hash: false,
            alpha_map: None,
            emissive_node: None,
            scale_node: None,
            size_node: None,
            size: 1.0,
            rotation_node: None,
            rotation: 0.0,
            linewidth: 1.0,
            size_attenuation: true,
            vertex_node: None,
            metalness_node: None,
            roughness_node: None,
            depth_node: None,
            context_overrides: None,
            context_node: None,
            fragment_node: None,
            output_node: None,
            backdrop_node: None,
            backdrop_alpha_node: None,
            mrt_node: None,
            side: Side::Front,
            visible: true,
            transparent: false,
            force_single_pass: false,
            wireframe: false,
            blending: Blending::Normal,
            premultiplied_alpha: false,
            alpha_to_coverage: false,
            world_units: false,
            blend_src: BlendFactor::SrcAlpha,
            blend_dst: BlendFactor::OneMinusSrcAlpha,
            blend_equation: BlendEquation::Add,
            blend_src_alpha: None,
            blend_dst_alpha: None,
            blend_equation_alpha: None,
            depth_test: true,
            depth_write: true,
            depth_func: DepthFunc::LessEqual,
            color_write: true,
            stencil_write: false,
            stencil_write_mask: 0xff,
            stencil_func: StencilFunc::Always,
            stencil_ref: 0,
            stencil_func_mask: 0xff,
            stencil_fail: StencilOp::Keep,
            stencil_z_fail: StencilOp::Keep,
            stencil_z_pass: StencilOp::Keep,
            name: "",
        }
    }
}

impl MeshBasicNodeMaterial {
    /// `material.dispose()` — drops this material, and nothing more. A
    /// material is a value here and the renderer only sees per-frame clones,
    /// so there is no count to read: what the renderer built for it ages out
    /// once no render has drawn it for a few renders (`docs/scene-graph.md`,
    /// "Identity and eviction"). Dropping the material does the same; this
    /// exists so three.js code ports line for line.
    pub fn dispose(self) {}

    /// `new MeshBasicMaterial()` — a white, opaque [`Basic`](MaterialKind::Basic)
    /// material. Equivalent to [`Default::default`].
    pub fn new() -> Self {
        Self::default()
    }

    /// The fields set on this material that nothing in the port reads for its
    /// [`kind`](Self::kind), by their three.js names — the audit in
    /// `docs/api.md` ("Material fields the port does not read"). The renderer
    /// logs each one once per material when it builds the program; call
    /// [`check_supported`](Self::check_supported) to fail on them instead.
    ///
    /// The list is the kind's declared loud fields (the per-kind table in
    /// `src/materials/fields.rs`, which the flow dispatch in [`setup`] reads
    /// too) that this material sets, in the table's order. A field three's
    /// own class for that kind lacks (a `clearcoat` on a Standard material,
    /// say) is not listed: three ignores it too.
    pub fn unsupported_fields(&self) -> Vec<&'static str> {
        self.kind
            .table()
            .loud
            .iter()
            .filter(|field| field.is_set(self))
            .map(|field| field.name())
            .collect()
    }

    /// The maps set on this material that the built-in lighting flow does not
    /// apply but that an accessor does read: `lightMap` (three's
    /// `setupLightMap()`, an `IrradianceNode` in the lights list, is not
    /// ported) reaches a shader only through
    /// [`material_light_map`](crate::nodes::tsl::material_light_map), and
    /// Phong's `specularMap` only through
    /// [`material_specular_strength`](crate::nodes::tsl::material_specular_strength),
    /// in a node the application builds. Not in
    /// [`unsupported_fields`](Self::unsupported_fields), so
    /// [`check_supported`](Self::check_supported) passes them; the renderer
    /// still says once per material that the flow leaves them out.
    pub(crate) fn accessor_only_fields(&self) -> Vec<&'static str> {
        fields::ACCESSOR_ONLY
            .iter()
            .filter(|field| field.is_set(self))
            .map(|field| field.name())
            .collect()
    }

    /// `Err(Error::Unsupported)` for the first of
    /// [`unsupported_fields`](Self::unsupported_fields), so an application
    /// that would rather fail than draw a material differently from three can
    /// ask before its first frame.
    pub fn check_supported(&self) -> Result<(), crate::Error> {
        match self.unsupported_fields().first() {
            Some(&field) => Err(crate::Error::Unsupported {
                field,
                kind: self.kind,
            }),
            None => Ok(()),
        }
    }

    /// `material.needsUpdate = true`: `Material.js`' setter, which bumps
    /// `version` so the next frame builds the program afresh. Call it after
    /// changing anything the program depends on — the struct docs list what
    /// does and does not.
    pub fn set_needs_update(&mut self) {
        self.version += 1;
    }

    /// The sampler half of `RenderObject.getMaterialCacheKey()` on the WebGPU
    /// backend: for every texture-valued property, its `magFilter`,
    /// `minFilter`, `wrapS` and `wrapT` (`wrapR` and `mapping` where the
    /// texture type has them). See [`TextureSamplerKey`].
    pub(crate) fn texture_sampler_key(&self) -> TextureSamplerKey<'_> {
        TextureSamplerKey(self)
    }

    /// The blending fields `WebGPUPipelineUtils._getBlending()` reads, gathered
    /// into the struct the table takes.
    pub(crate) fn blend_mode(&self) -> BlendMode {
        BlendMode {
            blending: self.blending,
            premultiplied_alpha: self.premultiplied_alpha,
            blend_src: self.blend_src,
            blend_dst: self.blend_dst,
            blend_equation: self.blend_equation,
            blend_src_alpha: self.blend_src_alpha,
            blend_dst_alpha: self.blend_dst_alpha,
            blend_equation_alpha: self.blend_equation_alpha,
        }
    }

    /// `WebGPUPipelineUtils.createRenderPipeline()`'s `materialBlending`: the
    /// blend state of the colour target, or `None` when the gate says the
    /// pipeline gets none at all.
    pub(crate) fn blend_state(&self) -> Option<wgpu::BlendState> {
        let mode = self.blend_mode();
        if blending::needs_blend_state(&mode, self.transparent) {
            blending::blending(&mode)
        } else {
            None
        }
    }

    /// `_getStencilState()`'s face — the same on both sides, as
    /// `gl.stencilOp()` is — or `None` (WebGPU's default face) while
    /// `stencil_write` is off, as `createRenderPipeline()` leaves it.
    pub(crate) fn stencil_face(&self) -> Option<wgpu::StencilFaceState> {
        if !self.stencil_write {
            return None;
        }
        let op = |op: StencilOp| match op {
            StencilOp::Zero => wgpu::StencilOperation::Zero,
            StencilOp::Keep => wgpu::StencilOperation::Keep,
            StencilOp::Replace => wgpu::StencilOperation::Replace,
            StencilOp::Increment => wgpu::StencilOperation::IncrementClamp,
            StencilOp::Decrement => wgpu::StencilOperation::DecrementClamp,
            StencilOp::IncrementWrap => wgpu::StencilOperation::IncrementWrap,
            StencilOp::DecrementWrap => wgpu::StencilOperation::DecrementWrap,
            StencilOp::Invert => wgpu::StencilOperation::Invert,
        };
        Some(wgpu::StencilFaceState {
            compare: match self.stencil_func {
                StencilFunc::Never => wgpu::CompareFunction::Never,
                StencilFunc::Less => wgpu::CompareFunction::Less,
                StencilFunc::Equal => wgpu::CompareFunction::Equal,
                StencilFunc::LessEqual => wgpu::CompareFunction::LessEqual,
                StencilFunc::Greater => wgpu::CompareFunction::Greater,
                StencilFunc::NotEqual => wgpu::CompareFunction::NotEqual,
                StencilFunc::GreaterEqual => wgpu::CompareFunction::GreaterEqual,
                StencilFunc::Always => wgpu::CompareFunction::Always,
            },
            fail_op: op(self.stencil_fail),
            depth_fail_op: op(self.stencil_z_fail),
            pass_op: op(self.stencil_z_pass),
        })
    }

    /// `RenderList.push()`'s choice of list: `transparent === true ||
    /// transmission > 0 || backdropNode` — a material that reads the frame
    /// has to be drawn after it.
    pub(crate) fn in_transparent_list(&self) -> bool {
        self.transparent || self.transmission > 0.0 || self.backdrop_node.is_some()
    }

    /// `NodeBuilder.isOpaque()` — `transparent === false && blending ===
    /// NormalBlending && alphaToCoverage === false`. What decides whether
    /// `setupDiffuseColor()` ends with `diffuseColor.a = 1.0`.
    pub(crate) fn is_opaque(&self) -> bool {
        !self.transparent && self.blending == Blending::Normal && !self.alpha_to_coverage
    }

    /// `new SpriteNodeMaterial()`. `SpriteNodeMaterial`'s constructor sets
    /// `transparent = true` explicitly — "In Sprites, the transparent property
    /// is enabled by default" — which also takes it out of `isOpaque()`, so the
    /// fragment flow keeps its per-fragment alpha.
    pub fn sprite() -> Self {
        Self {
            kind: MaterialKind::Sprite,
            transparent: true,
            refraction_ratio: 0.0,
            ..Self::default()
        }
    }

    /// `new PointsNodeMaterial()`.
    ///
    /// `PointsNodeMaterial extends SpriteNodeMaterial`
    /// (`src/materials/nodes/PointsNodeMaterial.js`), so it inherits
    /// `transparent = true` — which puts a `Points` object in the *transparent*
    /// render list, gives its pipeline a `NormalBlending` blend state, and
    /// takes it out of `is_opaque` so the fragment flow
    /// keeps its per-fragment alpha. `alphaToCoverage` stays **off**: three.js'
    /// own dump of this pipeline has `alphaToCoverageEnabled: false`.
    pub fn points() -> Self {
        Self {
            kind: MaterialKind::Points,
            transparent: true,
            refraction_ratio: 0.0,
            ..Self::default()
        }
    }

    /// `new LineBasicNodeMaterial( { color } )`.
    ///
    /// **No `MaterialKind` of its own, and that is a finding, not a shortcut.**
    /// `LineBasicNodeMaterial` is a bare `NodeMaterial` that only calls
    /// `setDefaultValues( new LineBasicMaterial() )`, and every default it
    /// brings that this port models — `color` white, `opacity` 1, `fog` true,
    /// `lights` false (`NodeMaterial`'s own default), `transparent` false — is
    /// already `MeshBasicNodeMaterial`'s. `linewidth` / `linecap` / `linejoin`
    /// are SVGRenderer-only ("WebGL and WebGPU ignore this setting and always
    /// render line primitives with a width of one pixel"). `vertexColors` is on
    /// the shared material, since `setupDiffuseColor()` is where three reads
    /// it — see [`MeshBasicNodeMaterial::vertex_colors`]. Three's own dump of this
    /// material from the d33 page (`docs/lines/LineBasicNodeMaterial_27.*`) is
    /// the `Basic` program statement for statement, so a second kind would
    /// generate identical WGSL.
    ///
    /// What makes a line a line is the *object*, not the material:
    /// `WebGPUUtils.getPrimitiveTopology( object, material )` reads
    /// `object.isLine` / `object.isLineSegments`.
    pub fn line(color: Color) -> Self {
        Self {
            color,
            refraction_ratio: 0.0,
            ..Self::default()
        }
    }

    /// `new Line2NodeMaterial( { color, linewidth, vertexColors } )` — the
    /// fat line.
    ///
    /// `Line2NodeMaterial.setDefaultValues( new LineBasicMaterial() )` and then
    /// `this.blending = NoBlending` — three sets it in the constructor because
    /// the material writes its own coverage through the `discard` and must not
    /// have the result blended a second time. That default has teeth here: it
    /// makes `is_opaque` false, so the fragment flow does
    /// **not** emit `DiffuseColor.w = 1.0` (`docs/nodes.md` §8).
    ///
    /// `this._useAlphaToCoverage = true` is the constructor's other default,
    /// so `alpha_to_coverage` starts true; `webgpu_lines_fat` turns it off.
    pub fn line2(color: Color) -> Self {
        Self {
            kind: MaterialKind::Line2,
            color,
            blending: Blending::No,
            alpha_to_coverage: true,
            refraction_ratio: 0.0,
            ..Self::default()
        }
    }

    /// `new MeshNormalNodeMaterial()` — `MeshNormalMaterial` under
    /// `WebGPURenderer`.
    ///
    /// The whole subclass is one overridden `setupDiffuseColor()`:
    /// `diffuseColor = colorSpaceToWorking( vec4( packNormalToRGB( normalView
    /// ), opacity ), SRGBColorSpace )`. Nothing else about it differs from a
    /// `MeshBasicNodeMaterial`, so it is a [`MaterialKind`] rather than a type.
    pub fn normal() -> Self {
        Self {
            kind: MaterialKind::Normal,
            refraction_ratio: 0.0,
            ..Self::default()
        }
    }

    /// `new MeshPhongNodeMaterial( { color } )`. `NodeMaterial.lights` is true
    /// for every lit material, which is what puts the `LightsNode` flow in the
    /// fragment stage.
    pub fn phong(color: Color) -> Self {
        Self {
            kind: MaterialKind::Phong,
            color,
            lights: true,
            ..Self::default()
        }
    }

    /// `new MeshLambertNodeMaterial( { color } )`.
    ///
    /// `MeshLambertMaterial`'s own defaults over `Material`'s are all shared
    /// with the Phong ones the struct already carries, so the constructor is
    /// the Phong one under a different [`MaterialKind`].
    pub fn lambert(color: Color) -> Self {
        Self {
            kind: MaterialKind::Lambert,
            color,
            lights: true,
            ..Self::default()
        }
    }
}

impl MeshBasicNodeMaterial {
    /// `new MeshToonNodeMaterial( { color, gradientMap } )`.
    ///
    /// `MeshToonMaterial`'s defaults over `Material`'s — white, `emissive`
    /// black at intensity 1, no maps — are all ones the struct already
    /// carries, and `lights = true` is the constructor's own.
    pub fn toon(color: Color, gradient_map: Option<Texture>) -> Self {
        Self {
            kind: MaterialKind::Toon,
            color,
            gradient_map,
            lights: true,
            refraction_ratio: 0.0,
            ..Self::default()
        }
    }

    /// `new MeshStandardNodeMaterial( { color, roughness, metalness } )`.
    pub fn standard(color: Color, roughness: f64, metalness: f64) -> Self {
        Self {
            kind: MaterialKind::Standard,
            color,
            roughness,
            metalness,
            lights: true,
            refraction_ratio: 0.0,
            ..Self::default()
        }
    }

    /// `new MeshPhysicalNodeMaterial()` — a Standard material plus
    /// `setupSpecular()`'s ior/specular block. `GLTFLoader` builds one whenever
    /// the asset declares `KHR_materials_specular` or `KHR_materials_ior`.
    pub fn physical(color: Color, roughness: f64, metalness: f64) -> Self {
        Self {
            kind: MaterialKind::Physical,
            ..Self::standard(color, roughness, metalness)
        }
    }
}

/// three.js' name for a `NodeMaterial` whose kind is `Phong`. The struct is
/// shared because `WebGPURenderer` treats every material as a `NodeMaterial`
/// and the renderer must hold them in one list.
pub type MeshPhongNodeMaterial = MeshBasicNodeMaterial;

/// three.js' name for a `NodeMaterial` whose kind is `Lambert`. The struct is
/// shared for the reason [`MeshPhongNodeMaterial`] is.
pub type MeshLambertNodeMaterial = MeshBasicNodeMaterial;

/// three.js' name for a `NodeMaterial` whose kind is `Toon` — see
/// [`MeshBasicNodeMaterial::toon`].
pub type MeshToonNodeMaterial = MeshBasicNodeMaterial;

/// three.js' name for a `NodeMaterial` whose kind is `Sprite`.
pub type SpriteNodeMaterial = MeshBasicNodeMaterial;
/// three.js' name for a `NodeMaterial` whose kind is `Points`.
pub type PointsNodeMaterial = MeshBasicNodeMaterial;
/// three.js' name for a `NodeMaterial` whose kind is `Standard` — one struct,
/// one renderer list.
pub type MeshStandardNodeMaterial = MeshBasicNodeMaterial;
/// three.js' name for a `NodeMaterial` whose kind is `Physical` — see
/// [`MeshBasicNodeMaterial::physical`].
pub type MeshPhysicalNodeMaterial = MeshBasicNodeMaterial;

/// three.js' name for a `NodeMaterial` whose kind is `Normal` — see
/// [`MeshBasicNodeMaterial::normal`].
pub type MeshNormalNodeMaterial = MeshBasicNodeMaterial;

/// three.js' name for a `NodeMaterial` whose kind is `Line2` — see
/// [`MeshBasicNodeMaterial::line2`] and [`crate::addons::lines`].
pub type Line2NodeMaterial = MeshBasicNodeMaterial;

/// three.js' name for the `NodeMaterial` a `Line` / `LineSegments` draws with.
/// It carries no state of its own — see [`MeshBasicNodeMaterial::line`].
pub type LineBasicNodeMaterial = MeshBasicNodeMaterial;

/// What [`MeshBasicNodeMaterial::texture_sampler_key`] hashes: the sampler
/// state of every texture the material holds as a property, which is what
/// `RenderObject.getMaterialCacheKey()` appends for each `value.isTexture`
/// when `renderer.backend.isWebGPUBackend`:
///
/// ```js
/// valueKey += value.mapping;
/// valueKey += value.magFilter;
/// valueKey += value.minFilter;
/// valueKey += value.wrapS;
/// valueKey += value.wrapT;
/// valueKey += value.wrapR;
/// ```
///
/// They reach the generated WGSL. `NearestFilter` on both filters makes a map
/// unfilterable (`isUnfilterable()`): bound `non-filtering`, no sampler, every
/// tap a `textureLoad`. And that `textureLoad` wraps its coordinate through
/// `generateWrapFunction()`'s `tsl_coord_<s>S_<t>T_2d`, named after the wrap
/// pair. A program built before the change is the wrong program after it
/// (issue #276).
///
/// The renderer hashes this into the dynamic half of a draw's cache key at
/// every draw, rather than having the `Texture` setters bump
/// [`version`](MeshBasicNodeMaterial::version), for three reasons. A texture
/// is a shared handle with no way back to the materials that hold it, so a
/// setter has no material to bump. It is what three does: the state is read
/// off the texture when the key is computed, never pushed into the material.
/// And a key keeps both programs: a map toggled between `Linear` and
/// `Nearest` finds each in the cache after the first time, where a version
/// bump would throw the material's programs away on every toggle.
///
/// Like three's, it covers the material's own texture properties only, not a
/// `texture()` node inside `color_node` or another graph: those are values
/// of the node, not of the material. Each property is hashed as `None` or
/// its state, so the key is positional, as three's comma-joined string is.
pub(crate) struct TextureSamplerKey<'a>(&'a MeshBasicNodeMaterial);

impl std::hash::Hash for TextureSamplerKey<'_> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        let m = self.0;
        m.env_map
            .as_ref()
            .map(|t| {
                let t = t.borrow();
                (t.mapping, t.mag_filter, t.min_filter)
            })
            .hash(state);
        for map in [
            &m.alpha_map,
            &m.map,
            &m.roughness_map,
            &m.metalness_map,
            &m.emissive_map,
            &m.gradient_map,
            &m.ao_map,
            &m.light_map,
            &m.specular_map,
            &m.bump_map,
            &m.clearcoat_map,
            &m.clearcoat_roughness_map,
            &m.normal_map,
            &m.specular_color_map,
            &m.anisotropy_map,
            &m.iridescence_thickness_map,
            &m.clearcoat_normal_map,
            &m.transmission_map,
            &m.thickness_map,
        ] {
            map.as_ref()
                .map(|t| {
                    let t = t.borrow();
                    (t.mag_filter, t.min_filter, t.wrap_s, t.wrap_t)
                })
                .hash(state);
        }
    }
}