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
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
//! The thin-wrapper tier of TSL (issue #141): the constructors, constants and
//! named math functions that three.js exports as one-liners over `ConstNode`,
//! `ConvertNode`, `JoinNode`, `MathNode`, `OperatorNode` and a few inlined
//! `Fn()`s. Each item's doc comment names the JS it ports; the WGSL each one
//! emits is pinned against three's own dump in `tests/nodes_tsl_batch.rs`.
//!
//! Where three's output and the port's differ only in how a shared
//! intermediate is named (`let nodeConstN` in three, `var<private> nodeVarN`
//! here), the divergence is the general one `docs/nodes.md` §8 records, not
//! something specific to these wrappers.

use std::rc::Rc;

use super::{
    binary, block, call, constant, float, join, math, rotate, shader_fn, to_const, to_var,
};
use super::{
    camera_position, camera_projection_matrix, camera_view_matrix, model_world_matrix,
    position_geometry, position_world, uniform,
};
use crate::math::{Color, Matrix2, Matrix3, Matrix4};
use crate::nodes::node::{FnDef, Lazy, Node, NodeRef, Type, UniformGroup, UniformSource};

// ---------------------------------------------------------------------------
// type helpers
// ---------------------------------------------------------------------------

/// `MathNode.getInputType()`: the widest operand, matrices counting as
/// width 0, with `a` winning ties.
fn input_type(args: &[&NodeRef]) -> Type {
    let len = |n: Option<&&NodeRef>| match n {
        Some(n) if !n.ty().is_matrix() => n.ty().components(),
        _ => 0,
    };
    let (a, b, c) = (args.first(), args.get(1), args.get(2));
    let (la, lb, lc) = (len(a), len(b), len(c));
    if la > lb && la > lc {
        a.unwrap().ty()
    } else if lb > lc {
        b.unwrap().ty()
    } else if lc > la {
        c.unwrap().ty()
    } else {
        a.unwrap().ty()
    }
}

/// `NodeBuilder.getIntegerType()`: an integer type keeps itself, anything
/// else becomes the `i32` vector of its width.
fn integer_type(ty: Type) -> Type {
    match ty.component_type() {
        Type::I32 | Type::U32 => ty,
        _ => Type::vector_of(Type::I32, ty.components()),
    }
}

/// A one-operand `MathNode`, typed by its operand.
fn unary(name: &'static str, x: NodeRef) -> NodeRef {
    let ty = x.ty();
    math(name, vec![x], ty)
}

/// `ConvertType( type, ...params )` from `TSLCore.js`: what `uvec3( … )`,
/// `bvec2( … )` and friends are.
///
/// - Every parameter a number: one `ConstNode` of the target type (a single
///   scalar splats), which `generateConst()` writes component by component —
///   `vec3<u32>( 1u, 2u, 3u )`.
/// - One node: a `ConvertNode` (`u32( x )`), or the node itself when it
///   already has the type.
/// - Several nodes: a `JoinNode`.
///
/// Rust has no JS-number/node distinction at the call site, so a scalar
/// `Node::Const` stands in for a JS number. Three would join a `float( 1 )`
/// *node* rather than fold it; nothing ported passes one, and folding is the
/// literal three emits for the number form every example uses.
fn convert_type(ty: Type, params: Vec<NodeRef>) -> NodeRef {
    let n = ty.components();
    if params.is_empty() {
        return constant(ty, vec![0.0; n]);
    }
    let all_const = params
        .iter()
        .all(|p| matches!(p.node(), Node::Const { .. }));
    if all_const {
        let mut values: Vec<f64> = params
            .iter()
            .flat_map(|p| match p.node() {
                Node::Const { values, .. } => values.clone(),
                _ => unreachable!(),
            })
            .collect();
        if values.len() == 1 {
            values = vec![values[0]; n];
        }
        values.resize(n, 0.0);
        return constant(ty, values);
    }
    if params.len() == 1 {
        return params[0].to(ty);
    }
    join(ty, params)
}

// ---------------------------------------------------------------------------
// constructors (`TSLCore.js`)
// ---------------------------------------------------------------------------

/// `color( 0xff8000 )` — `TSLCore.js`' `color = new ConvertType( 'color' )`
/// over a `Color`. The hex is sRGB and the const is the working (linear)
/// value, exactly as `new Color( hex )` stores it.
pub fn color(hex: u32) -> NodeRef {
    Color::from_hex(hex).into()
}

/// `color( r, g, b )` — the three-number form, which `Color.setRGB()` takes
/// as working-space components unchanged.
pub fn color_rgb(r: f64, g: f64, b: f64) -> NodeRef {
    constant(Type::Vec3, vec![r, g, b])
}

/// `bool( x )` — `TSLCore.js`' `bool = new ConvertType( 'bool' )`.
pub fn bool(x: impl Into<NodeRef>) -> NodeRef {
    convert_type(Type::Bool, vec![x.into()])
}

macro_rules! vector_ctor {
    ($(#[$m:meta])* $name:ident, $ty:expr, $($arg:ident),+) => {
        $(#[$m])*
        pub fn $name($($arg: impl Into<NodeRef>),+) -> NodeRef {
            convert_type($ty, vec![$($arg.into()),+])
        }
    };
}

vector_ctor!(
    /// `uvec2( x, y )` — `TSLCore.js`' `ConvertType( 'uvec2' )`.
    uvec2, Type::UVec2, x, y
);
vector_ctor!(
    /// `uvec3( x, y, z )` — `TSLCore.js`' `ConvertType( 'uvec3' )`.
    uvec3, Type::UVec3, x, y, z
);
vector_ctor!(
    /// `uvec4( x, y, z, w )` — `TSLCore.js`' `ConvertType( 'uvec4' )`.
    uvec4, Type::UVec4, x, y, z, w
);
vector_ctor!(
    /// `ivec3( x, y, z )` — `TSLCore.js`' `ConvertType( 'ivec3' )`.
    ivec3, Type::IVec3, x, y, z
);
vector_ctor!(
    /// `ivec4( x, y, z, w )` — `TSLCore.js`' `ConvertType( 'ivec4' )`.
    ivec4, Type::IVec4, x, y, z, w
);
vector_ctor!(
    /// `bvec2( x, y )` — `TSLCore.js`' `ConvertType( 'bvec2' )`.
    bvec2, Type::BVec2, x, y
);
vector_ctor!(
    /// `bvec3( x, y, z )` — `TSLCore.js`' `ConvertType( 'bvec3' )`.
    bvec3, Type::BVec3, x, y, z
);
vector_ctor!(
    /// `bvec4( x, y, z, w )` — `TSLCore.js`' `ConvertType( 'bvec4' )`.
    bvec4, Type::BVec4, x, y, z, w
);

/// `mat2( matrix2 )` / `mat2( a, b, c, d )` with numbers — `TSLCore.js`'
/// `ConvertType( 'mat2' )`, whose number form goes through `Matrix2.set()`
/// (row-major arguments). The const is written column-major, as
/// `generateConst()` reads `matrix.elements`: `mat2( 1, 2, 3, 4 )` is
/// `mat2x2<f32>( 1.0, 3.0, 2.0, 4.0 )`.
pub fn mat2(m: Matrix2) -> NodeRef {
    constant(Type::Mat2, m.elements.to_vec())
}

/// `mat3( matrix3 )` — `TSLCore.js`' `ConvertType( 'mat3' )` over a value.
pub fn mat3(m: Matrix3) -> NodeRef {
    constant(Type::Mat3, m.elements.to_vec())
}

/// `mat4( matrix4 )` — `TSLCore.js`' `ConvertType( 'mat4' )` over a value.
pub fn mat4(m: Matrix4) -> NodeRef {
    constant(Type::Mat4, m.elements.to_vec())
}

/// `mat2( a, b, c, d )` with nodes — a `JoinNode` in column order.
pub fn mat2_join(args: Vec<NodeRef>) -> NodeRef {
    convert_type(Type::Mat2, args)
}

/// `mat3( … )` with nodes — a `JoinNode` (or a `ConvertNode` for one node:
/// `mat3( modelWorldMatrix )` is the `mat4` → `mat3` narrowing).
pub fn mat3_join(args: Vec<NodeRef>) -> NodeRef {
    convert_type(Type::Mat3, args)
}

/// `mat4( … )` with nodes.
pub fn mat4_join(args: Vec<NodeRef>) -> NodeRef {
    convert_type(Type::Mat4, args)
}

// ---------------------------------------------------------------------------
// constants (`math/MathNode.js`)
// ---------------------------------------------------------------------------

/// `PI` — `MathNode.js`: `float( Math.PI )`.
pub fn pi() -> NodeRef {
    float(std::f64::consts::PI)
}

/// `PI2` — `MathNode.js`: `float( Math.PI * 2 )`, the deprecated spelling of
/// `TWO_PI` ([`super::two_pi`]).
pub fn pi2() -> NodeRef {
    float(std::f64::consts::TAU)
}

/// `HALF_PI` — `MathNode.js`: `float( Math.PI * 0.5 )`.
pub fn half_pi() -> NodeRef {
    float(std::f64::consts::FRAC_PI_2)
}

/// `EPSILON` — `MathNode.js`: `float( 1e-6 )`.
pub fn epsilon() -> NodeRef {
    float(1e-6)
}

/// `INFINITY` — `MathNode.js`: `float( 1e6 )`. Not IEEE infinity: three's
/// constant is a large finite number, and so is this.
pub fn infinity() -> NodeRef {
    float(1e6)
}

// ---------------------------------------------------------------------------
// one-operand math (`math/MathNode.js`)
// ---------------------------------------------------------------------------

macro_rules! unary_math {
    ($(#[$m:meta])* $name:ident, $wgsl:literal) => {
        $(#[$m])*
        pub fn $name(x: impl Into<NodeRef>) -> NodeRef {
            unary($wgsl, x.into())
        }
    };
}

unary_math!(
    /// `atan( x )` — `MathNode.ATAN` with one argument. The two-argument form
    /// is [`NodeRef::atan2`].
    atan, "atan"
);
unary_math!(
    /// `acos( x )` — `MathNode.ACOS`.
    acos, "acos"
);
unary_math!(
    /// `asin( x )` — `MathNode.ASIN`.
    asin, "asin"
);
unary_math!(
    /// `tan( x )` — `MathNode.TAN`.
    tan, "tan"
);
unary_math!(
    /// `sinh( x )` — `MathNode.SINH`.
    sinh, "sinh"
);
unary_math!(
    /// `cosh( x )` — `MathNode.COSH`.
    cosh, "cosh"
);
unary_math!(
    /// `tanh( x )` — `MathNode.TANH`.
    tanh, "tanh"
);
unary_math!(
    /// `asinh( x )` — `MathNode.ASINH`.
    asinh, "asinh"
);
unary_math!(
    /// `acosh( x )` — `MathNode.ACOSH`.
    acosh, "acosh"
);
unary_math!(
    /// `atanh( x )` — `MathNode.ATANH`.
    atanh, "atanh"
);
unary_math!(
    /// `round( x )` — `MathNode.ROUND`.
    round, "round"
);
unary_math!(
    /// `trunc( x )` — `MathNode.TRUNC`.
    trunc, "trunc"
);
unary_math!(
    /// `degrees( x )` — `MathNode.DEGREES`.
    degrees, "degrees"
);
unary_math!(
    /// `radians( x )` — `MathNode.RADIANS`.
    radians, "radians"
);
unary_math!(
    /// `transpose( m )` — `MathNode.TRANSPOSE`.
    transpose, "transpose"
);
unary_math!(
    /// `countOneBits( x )` — `BitcountNode.COUNT_ONE_BITS`, WGSL's builtin
    /// of the same name, typed as its operand.
    count_one_bits, "countOneBits"
);
unary_math!(
    /// `countTrailingZeros( x )` — `BitcountNode.COUNT_TRAILING_ZEROS`.
    count_trailing_zeros, "countTrailingZeros"
);
unary_math!(
    /// `countLeadingZeros( x )` — `BitcountNode.COUNT_LEADING_ZEROS`.
    count_leading_zeros, "countLeadingZeros"
);

/// `determinant( m )` — `MathNode.DETERMINANT`, a `float` out of a matrix.
pub fn determinant(m: impl Into<NodeRef>) -> NodeRef {
    math("determinant", vec![m.into()], Type::F32)
}

/// `inverse( m )` — `MathNode.INVERSE`. WGSL has no `inverse`, so
/// `WGSLNodeBuilder`'s polyfill table lowers it to `tsl_inverse_mat2/3/4`,
/// whose code the builder adds on first use (`wgsl::polyfill`).
pub fn inverse(m: impl Into<NodeRef>) -> NodeRef {
    let m = m.into();
    let name = match m.ty() {
        Type::Mat2 => "tsl_inverse_mat2",
        Type::Mat3 => "tsl_inverse_mat3",
        Type::Mat4 => "tsl_inverse_mat4",
        other => panic!("three-rs: inverse() of a {other:?}"),
    };
    unary(name, m)
}

// ---------------------------------------------------------------------------
// powers and shaping (`math/MathNode.js`, `math/MathUtils.js`)
// ---------------------------------------------------------------------------

/// `pow( a, b )` typed by `MathNode.getInputType()`.
fn pow_of(a: NodeRef, b: NodeRef) -> NodeRef {
    let ty = input_type(&[&a, &b]);
    math("pow", vec![a, b], ty)
}

/// `pow2( x )` — `MathNode.js`: `mul( x, x )`.
pub fn pow2(x: impl Into<NodeRef>) -> NodeRef {
    let x = x.into();
    x.mul(x.clone())
}

/// `pow3( x )` — `MathNode.js`: `mul( x, x, x )`.
pub fn pow3(x: impl Into<NodeRef>) -> NodeRef {
    x.into().pow3()
}

/// `pow4( x )` — `MathNode.js`: `mul( x, x, x, x )`.
pub fn pow4(x: impl Into<NodeRef>) -> NodeRef {
    let x = x.into();
    x.mul(x.clone()).mul(x.clone()).mul(x)
}

/// `difference( a, b )` — `MathNode.DIFFERENCE`, `abs( a - b )`.
pub fn difference(a: impl Into<NodeRef>, b: impl Into<NodeRef>) -> NodeRef {
    let d = a.into().sub(b);
    unary("abs", d)
}

/// `cbrt( a )` — `MathNode.js`: `mul( sign( a ), pow( abs( a ), 1.0 / 3.0 ) )`.
pub fn cbrt(a: impl Into<NodeRef>) -> NodeRef {
    let a = a.into();
    unary("sign", a.clone()).mul(pow_of(unary("abs", a), float(1.0 / 3.0)))
}

/// `lengthSq( a )` — `MathNode.js`: `dot( a, a )`.
pub fn length_sq(a: impl Into<NodeRef>) -> NodeRef {
    let a = a.into();
    super::dot(a.clone(), a)
}

/// `faceForward( N, I, Nref )` — `MathNode.FACEFORWARD`, WGSL's builtin.
pub fn face_forward(
    n: impl Into<NodeRef>,
    i: impl Into<NodeRef>,
    nref: impl Into<NodeRef>,
) -> NodeRef {
    let (n, i, nref) = (n.into(), i.into(), nref.into());
    let ty = input_type(&[&n, &i, &nref]);
    math("faceForward", vec![n, i, nref], ty)
}

/// `parabola( x, k )` — `MathUtils.js`:
/// `pow( mul( 4.0, x.mul( sub( 1.0, x ) ) ), k )`.
pub fn parabola(x: impl Into<NodeRef>, k: impl Into<NodeRef>) -> NodeRef {
    let x = x.into();
    pow_of(float(4.0).mul(x.mul(float(1.0).sub(x.clone()))), k.into())
}

/// `gain( x, k )` — `MathUtils.js`: `select( x.lessThan( 0.5 ), pow( mul(
/// 2.0, x ), k ).mul( 0.5 ), sub( 1.0, pow( mul( 2.0, sub( 1.0, x ) ), k
/// ).mul( 0.5 ) ) )`, which generates as an `if`/`else` over a result var.
pub fn gain(x: impl Into<NodeRef>, k: impl Into<NodeRef>) -> NodeRef {
    let (x, k) = (x.into(), k.into());
    let low = pow_of(float(2.0).mul(x.clone()), k.clone()).mul(0.5);
    let high = float(1.0).sub(pow_of(float(2.0).mul(float(1.0).sub(x.clone())), k).mul(0.5));
    x.less_than(0.5).select(low, high)
}

/// `pcurve( x, a, b )` — `MathUtils.js`: `pow( div( pow( x, a ), add( pow(
/// x, a ), pow( sub( 1.0, x ), b ) ) ), div( 1.0, a ) )`. The two `pow( x, a
/// )` are two nodes in three, so they are two expressions here too.
pub fn pcurve(x: impl Into<NodeRef>, a: impl Into<NodeRef>, b: impl Into<NodeRef>) -> NodeRef {
    let (x, a, b) = (x.into(), a.into(), b.into());
    let num = pow_of(x.clone(), a.clone());
    let den = pow_of(x.clone(), a.clone()).add(pow_of(float(1.0).sub(x), b));
    pow_of(num.div(den), float(1.0).div(a))
}

/// `sinc( x, k )` — `MathUtils.js`: `arg = abs( PI.mul( k.mul( x ).sub( 1.0
/// ) ) ).max( 1e-6 ).toConst()`, then `sin( arg ).div( arg )`. Three calls
/// `k.mul()`, so `k` must be a node there; here anything `Into<NodeRef>` is.
pub fn sinc(x: impl Into<NodeRef>, k: impl Into<NodeRef>) -> NodeRef {
    let (x, k) = (x.into(), k.into());
    let arg = unary("abs", pi().mul(k.mul(x).sub(1.0))).max(1e-6);
    let arg = to_const(None, arg);
    arg.sin().div(arg)
}

/// `faceforward( N, I, Nref )` — `MathNode.js`' `faceforward = faceForward`,
/// the deprecated GLSL spelling of [`face_forward`]. Three keeps it as a
/// plain alias with no warning, and so does this.
pub fn faceforward(
    n: impl Into<NodeRef>,
    i: impl Into<NodeRef>,
    nref: impl Into<NodeRef>,
) -> NodeRef {
    face_forward(n, i, nref)
}

/// `inversesqrt( x )` — `MathNode.js`' `inversesqrt = inverseSqrt`, the
/// deprecated GLSL spelling of [`super::inverse_sqrt`]. A plain alias, as in
/// three.
pub fn inversesqrt(x: impl Into<NodeRef>) -> NodeRef {
    super::inverse_sqrt(x)
}

/// `any( x )` — `MathNode.ANY`: whether any component of a boolean vector is
/// true. The `all` counterpart is [`super::all`].
pub fn any(x: impl Into<NodeRef>) -> NodeRef {
    math("any", vec![x.into()], Type::Bool)
}

/// `transformNormalByViewMatrix( normal, viewMatrix )` — `MathNode.js`:
/// `normalize( mul( viewMatrix, vec4( vec3( normal ), 0.0 ) ).xyz )`. The
/// view matrix's upper 3×3 is taken as orthonormal, so no normal matrix is
/// involved.
pub fn transform_normal_by_view_matrix(
    normal: impl Into<NodeRef>,
    view_matrix: impl Into<NodeRef>,
) -> NodeRef {
    let n = join(Type::Vec4, vec![normal.into().to_vec3(), float(0.0)]);
    view_matrix.into().mul(n).xyz().normalize()
}

/// `transformNormal( normal, matrix )` — `Normal.js`: `mat3( matrix )
/// .inverse().transpose().mul( normal ).normalize()`, the normal through the
/// inverse transpose of `matrix`'s upper 3×3.
///
/// Three's `matrix` defaults to `modelWorldMatrix`. Rust has no default
/// arguments, so three's one-argument `transformNormal( normal )` is
/// `transform_normal( normal, model_world_matrix() )` here.
pub fn transform_normal(normal: impl Into<NodeRef>, matrix: impl Into<NodeRef>) -> NodeRef {
    matrix
        .into()
        .to_mat3()
        .inverse()
        .transpose()
        .mul(normal.into())
        .normalize()
}

/// `transformNormalToView( normal )` — `Normal.js`: `normal` from local to
/// view space, `modelNormalMatrix.mul( normal ).transformNormalByViewMatrix(
/// cameraViewMatrix )`.
///
/// Three takes `normal.transformNormalByViewMatrix( modelNormalViewMatrix )`
/// instead when `builder.context.modelNormalViewMatrix` is set, which only
/// `renderer.highPrecision` does. The port reads the same key from the build
/// context in force when this is called — nothing in the crate sets it.
pub fn transform_normal_to_view(normal: impl Into<NodeRef>) -> NodeRef {
    let normal = normal.into();
    let matrix =
        crate::nodes::builder::current_context(|cx| cx.extra.get("modelNormalViewMatrix").cloned());
    match matrix {
        Some(matrix) => transform_normal_by_view_matrix(normal, matrix),
        None => transform_normal_by_view_matrix(
            super::model_normal_matrix().mul(normal),
            camera_view_matrix(),
        ),
    }
}

/// `transformNormalByInverseViewMatrix( normal, viewMatrix )` —
/// `MathNode.js`: `normalize( vec4( vec3( normal ), 0.0 ).mul( viewMatrix
/// ).xyz )`. Post-multiplying by an orthonormal view matrix is
/// pre-multiplying by its inverse.
pub fn transform_normal_by_inverse_view_matrix(
    normal: impl Into<NodeRef>,
    view_matrix: impl Into<NodeRef>,
) -> NodeRef {
    let n = join(Type::Vec4, vec![normal.into().to_vec3(), float(0.0)]);
    n.mul(view_matrix.into()).xyz().normalize()
}

// ---------------------------------------------------------------------------
// packing (`math/PackFloatNode.js`, `math/UnpackFloatNode.js`,
// `math/Packed4x8IntegerNode.js`)
// ---------------------------------------------------------------------------

/// `PackFloatNode` / `UnpackFloatNode`: one call to the WGSL builtin
/// `WGSLNodeBuilder.getFloatPackingMethod()` names, its operand built at its
/// own type and written with no padding inside the parentheses, as three's
/// template does (`pack2x16snorm(v)`).
fn float_packing(builtin: &'static str, x: impl Into<NodeRef>, ty: Type) -> NodeRef {
    math(builtin, vec![x.into()], ty)
}

/// `packSnorm2x16( v )` — `PackFloatNode( 'snorm' )`: a `vec2` in `[ -1, 1 ]`
/// to a `u32`, WGSL's `pack2x16snorm`.
pub fn pack_snorm_2x16(v: impl Into<NodeRef>) -> NodeRef {
    float_packing("pack2x16snorm", v, Type::U32)
}

/// `packUnorm2x16( v )` — `PackFloatNode( 'unorm' )`, WGSL's `pack2x16unorm`.
pub fn pack_unorm_2x16(v: impl Into<NodeRef>) -> NodeRef {
    float_packing("pack2x16unorm", v, Type::U32)
}

/// `packHalf2x16( v )` — `PackFloatNode( 'float16' )`, WGSL's
/// `pack2x16float`.
pub fn pack_half_2x16(v: impl Into<NodeRef>) -> NodeRef {
    float_packing("pack2x16float", v, Type::U32)
}

/// `packSnorm4x8( v )` — `PackFloatNode( 'snorm', { layout: '4x8' } )`, WGSL's
/// `pack4x8snorm`.
pub fn pack_snorm_4x8(v: impl Into<NodeRef>) -> NodeRef {
    float_packing("pack4x8snorm", v, Type::U32)
}

/// `packUnorm4x8( v )` — `PackFloatNode( 'unorm', { layout: '4x8' } )`, WGSL's
/// `pack4x8unorm`.
pub fn pack_unorm_4x8(v: impl Into<NodeRef>) -> NodeRef {
    float_packing("pack4x8unorm", v, Type::U32)
}

/// `unpackSnorm2x16( u )` — `UnpackFloatNode( 'snorm' )`, a `vec2`; WGSL's
/// `unpack2x16snorm`.
pub fn unpack_snorm_2x16(u: impl Into<NodeRef>) -> NodeRef {
    float_packing("unpack2x16snorm", u, Type::Vec2)
}

/// `unpackUnorm2x16( u )` — `UnpackFloatNode( 'unorm' )`, WGSL's
/// `unpack2x16unorm`.
pub fn unpack_unorm_2x16(u: impl Into<NodeRef>) -> NodeRef {
    float_packing("unpack2x16unorm", u, Type::Vec2)
}

/// `unpackHalf2x16( u )` — `UnpackFloatNode( 'float16' )`, WGSL's
/// `unpack2x16float`.
pub fn unpack_half_2x16(u: impl Into<NodeRef>) -> NodeRef {
    float_packing("unpack2x16float", u, Type::Vec2)
}

/// `unpackSnorm4x8( u )` — `UnpackFloatNode( 'snorm', { layout: '4x8' } )`, a
/// `vec4`; WGSL's `unpack4x8snorm`.
pub fn unpack_snorm_4x8(u: impl Into<NodeRef>) -> NodeRef {
    float_packing("unpack4x8snorm", u, Type::Vec4)
}

/// `unpackUnorm4x8( u )` — `UnpackFloatNode( 'unorm', { layout: '4x8' } )`,
/// WGSL's `unpack4x8unorm`.
pub fn unpack_unorm_4x8(u: impl Into<NodeRef>) -> NodeRef {
    float_packing("unpack4x8unorm", u, Type::Vec4)
}

/// `Packed4x8IntegerNode`: the WGSL builtin of the same name, operands built
/// at `getInputType()` — `ivec4` for the signed packs, `uvec4` for the
/// unsigned ones, `uint` for everything that reads a packed word.
///
/// Three emulates these with `tsl_packed4x8_*` functions when
/// `navigator.gpu.wgslLanguageFeatures` lacks `packed_4x8_integer_dot_product`;
/// the Chrome the fixtures are dumped from has it, so three's WGSL — and this
/// — is the native builtin, which naga also implements. The emulation is not
/// ported: there is no browser to ask.
fn packed_4x8(method: &'static str, args: Vec<NodeRef>, ty: Type) -> NodeRef {
    math(method, args, ty)
}

/// `dot4U8Packed( a, b )` — the dot product of two words' four unsigned
/// bytes, a `u32`.
pub fn dot_4u8_packed(a: impl Into<NodeRef>, b: impl Into<NodeRef>) -> NodeRef {
    packed_4x8("dot4U8Packed", vec![a.into(), b.into()], Type::U32)
}

/// `dot4I8Packed( a, b )` — the dot product of two words' four signed bytes,
/// an `i32`.
pub fn dot_4i8_packed(a: impl Into<NodeRef>, b: impl Into<NodeRef>) -> NodeRef {
    packed_4x8("dot4I8Packed", vec![a.into(), b.into()], Type::I32)
}

/// `pack4xI8( v )` — the low byte of each `ivec4` component into one `u32`.
pub fn pack_4x_i8(v: impl Into<NodeRef>) -> NodeRef {
    packed_4x8("pack4xI8", vec![v.into()], Type::U32)
}

/// `pack4xU8( v )` — the low byte of each `uvec4` component into one `u32`.
pub fn pack_4x_u8(v: impl Into<NodeRef>) -> NodeRef {
    packed_4x8("pack4xU8", vec![v.into()], Type::U32)
}

/// `pack4xI8Clamp( v )` — [`pack_4x_i8`] after clamping each component to
/// `[ -128, 127 ]`.
pub fn pack_4x_i8_clamp(v: impl Into<NodeRef>) -> NodeRef {
    packed_4x8("pack4xI8Clamp", vec![v.into()], Type::U32)
}

/// `pack4xU8Clamp( v )` — [`pack_4x_u8`] after clamping each component to
/// `[ 0, 255 ]`.
pub fn pack_4x_u8_clamp(v: impl Into<NodeRef>) -> NodeRef {
    packed_4x8("pack4xU8Clamp", vec![v.into()], Type::U32)
}

/// `unpack4xI8( u )` — a word's four bytes, sign-extended, as an `ivec4`.
pub fn unpack_4x_i8(u: impl Into<NodeRef>) -> NodeRef {
    packed_4x8("unpack4xI8", vec![u.into()], Type::IVec4)
}

/// `unpack4xU8( u )` — a word's four bytes, zero-extended, as a `uvec4`.
pub fn unpack_4x_u8(u: impl Into<NodeRef>) -> NodeRef {
    packed_4x8("unpack4xU8", vec![u.into()], Type::UVec4)
}

// ---------------------------------------------------------------------------
// bits and integers (`math/OperatorNode.js`, `math/BitcastNode.js`)
// ---------------------------------------------------------------------------

/// `xor( a, b )` — `OperatorNode( '^^' )`, the logical exclusive or. WGSL has
/// no `^^`, so it lowers to `WGSLNodeBuilder`'s `tsl_xor` polyfill.
pub fn xor(a: impl Into<NodeRef>, b: impl Into<NodeRef>) -> NodeRef {
    math("tsl_xor", vec![a.into(), b.into()], Type::Bool)
}

/// `bitNot( a )` — `OperatorNode( '~' )`, typed `getIntegerType( typeA )`.
pub fn bit_not(a: impl Into<NodeRef>) -> NodeRef {
    let node = a.into();
    let ty = integer_type(node.ty());
    NodeRef::new(Node::BitNot { node, ty })
}

/// A `BitcastNode` over `x` into the vector of `component` of `x`'s width:
/// `bitcast<T>( x )`.
fn bitcast(x: NodeRef, component: Type) -> NodeRef {
    let ty = Type::vector_of(component, x.ty().components());
    let name = match ty {
        Type::F32 => "bitcast<f32>",
        Type::I32 => "bitcast<i32>",
        Type::U32 => "bitcast<u32>",
        Type::Vec2 => "bitcast<vec2<f32>>",
        Type::Vec3 => "bitcast<vec3<f32>>",
        Type::Vec4 => "bitcast<vec4<f32>>",
        Type::IVec2 => "bitcast<vec2<i32>>",
        Type::IVec3 => "bitcast<vec3<i32>>",
        Type::IVec4 => "bitcast<vec4<i32>>",
        Type::UVec2 => "bitcast<vec2<u32>>",
        Type::UVec3 => "bitcast<vec3<u32>>",
        Type::UVec4 => "bitcast<vec4<u32>>",
        other => panic!("three-rs: bitcast to {other:?}"),
    };
    math(name, vec![x], ty)
}

/// `floatBitsToInt( x )` — `BitcastNode.js`: `new BitcastNode( x, 'int' )`.
pub fn float_bits_to_int(x: impl Into<NodeRef>) -> NodeRef {
    bitcast(x.into(), Type::I32)
}

/// `floatBitsToUint( x )` — `BitcastNode.js`: `new BitcastNode( x, 'uint' )`.
pub fn float_bits_to_uint(x: impl Into<NodeRef>) -> NodeRef {
    bitcast(x.into(), Type::U32)
}

/// `intBitsToFloat( x )` — `BitcastNode.js`: `new BitcastNode( x, 'float' )`.
pub fn int_bits_to_float(x: impl Into<NodeRef>) -> NodeRef {
    bitcast(x.into(), Type::F32)
}

/// `uintBitsToFloat( x )` — `BitcastNode.js`: `new BitcastNode( x, 'float' )`.
pub fn uint_bits_to_float(x: impl Into<NodeRef>) -> NodeRef {
    bitcast(x.into(), Type::F32)
}

/// `increment( a )` — `OperatorNode.js`: `temp = int( a ).toConst();
/// a.addAssign( 1 ); return temp;`. `a` is a var; the value is `a` as it was.
pub fn increment(a: &NodeRef) -> NodeRef {
    let temp = to_const(None, a.to(Type::I32));
    block(vec![temp.clone(), a.add_assign(1)], temp)
}

/// `decrement( a )` — `OperatorNode.js`: the post-decrement twin of
/// [`increment`].
pub fn decrement(a: &NodeRef) -> NodeRef {
    let temp = to_const(None, a.to(Type::I32));
    block(vec![temp.clone(), a.sub_assign(1)], temp)
}

/// `incrementBefore( a )` — `OperatorNode.js`: `a.addAssign( 1 ); return a;`.
pub fn increment_before(a: &NodeRef) -> NodeRef {
    block(vec![a.add_assign(1)], a.clone())
}

/// `decrementBefore( a )` — `OperatorNode.js`: `a.subAssign( 1 ); return a;`.
pub fn decrement_before(a: &NodeRef) -> NodeRef {
    block(vec![a.sub_assign(1)], a.clone())
}

/// `mod( a, b )` — `MathNode.MOD`. On integers it is the `%` operator; on
/// floats WGSL's `%` truncates where GLSL's `mod` floors, so
/// `WGSLNodeBuilder` lowers it to `tsl_mod_float` / `tsl_mod_vec2/3/4`, with
/// `b` widened to the input type (`uv.mod( 0.5 )` is `tsl_mod_vec2( uv,
/// vec2<f32>( 0.5 ) )`). `mod` is a Rust keyword, hence the trailing `_`.
pub fn mod_(a: impl Into<NodeRef>, b: impl Into<NodeRef>) -> NodeRef {
    let (a, b) = (a.into(), b.into());
    let ty = input_type(&[&a, &b]);
    if matches!(ty.component_type(), Type::I32 | Type::U32) {
        return binary("%", a, b);
    }
    let name = match ty {
        Type::F32 => "tsl_mod_float",
        Type::Vec2 => "tsl_mod_vec2",
        Type::Vec3 => "tsl_mod_vec3",
        Type::Vec4 => "tsl_mod_vec4",
        other => panic!("three-rs: mod() of a {other:?}"),
    };
    math(name, vec![a, b], ty)
}

// ---------------------------------------------------------------------------
// noise and hashing (`math/Hash.js`, `math/MathNode.js`, `math/TriNoise3D.js`)
// ---------------------------------------------------------------------------

/// `hash( seed )` — `Hash.js`, the PCG hash from shadertoy XlGcRh, inlined:
///
/// ```ignore
/// state = seed.toUint().mul( 747796405 ).add( 2891336453 )
/// word = state.shiftRight( state.shiftRight( 28 ).add( 4 ) ).bitXor( state ).mul( 277803737 )
/// return word.shiftRight( 22 ).bitXor( word ).toFloat().mul( 1 / 2 ** 32 )
/// ```
pub fn hash(seed: impl Into<NodeRef>) -> NodeRef {
    let state = seed.into().to_uint().mul(747796405u32).add(2891336453u32);
    let word = state
        .shift_right(state.shift_right(28u32).add(4u32))
        .bit_xor(state.clone())
        .mul(277803737u32);
    word.shift_right(22u32)
        .bit_xor(word)
        .to_float()
        .mul(1.0 / 4294967296.0)
}

/// `rand( uv )` — `MathNode.js`: `fract( sin( mod( dot( uv.xy, vec2(
/// 12.9898, 78.233 ) ), PI ) ).mul( 43758.5453 ) )`, inlined.
pub fn rand(uv: impl Into<NodeRef>) -> NodeRef {
    let uv = uv.into();
    // `uv.xy` of a `vec2` is `SplitNode` over the whole vector, which three
    // generates as the vector itself.
    let uv = if uv.ty() == Type::Vec2 { uv } else { uv.xy() };
    let dt = super::dot(uv, constant(Type::Vec2, vec![12.9898, 78.233]));
    let sn = mod_(dt, pi());
    unary("fract", sn.sin().mul(43758.5453))
}

/// `tri( x )` — `TriNoise3D.js`, a layout `Fn` (`fn tri`).
fn tri_def() -> Rc<FnDef> {
    thread_local! { static CELL: Lazy<Rc<FnDef>> = const { Lazy::new() }; }
    CELL.with(|c| {
        c.get(|| {
            shader_fn(Some("tri"), vec![("x", Type::F32)], Type::F32, |args| {
                args[0].fract().sub(0.5).abs()
            })
        })
    })
}

fn tri(x: NodeRef) -> NodeRef {
    call(&tri_def(), vec![x])
}

/// `tri3( p )` — `TriNoise3D.js`, a layout `Fn` (`fn tri3`).
fn tri3(p: NodeRef) -> NodeRef {
    thread_local! { static CELL: Lazy<Rc<FnDef>> = const { Lazy::new() }; }
    let def = CELL.with(|c| {
        c.get(|| {
            shader_fn(Some("tri3"), vec![("p", Type::Vec3)], Type::Vec3, |args| {
                let p = &args[0];
                join(
                    Type::Vec3,
                    vec![
                        tri(p.z().add(tri(p.y().mul(1.0)))),
                        tri(p.z().add(tri(p.x().mul(1.0)))),
                        tri(p.y().add(tri(p.x().mul(1.0)))),
                    ],
                )
            })
        })
    });
    call(&def, vec![p])
}

/// `triNoise3D( position, speed, time )` — `TriNoise3D.js`
/// (cabbibo's glsl-tri-noise-3d), a layout `Fn` emitted as `fn triNoise3D`
/// with its float `Loop( { start: 0, end: 3, type: 'float', condition: '<='
/// } )`.
pub fn tri_noise_3d(
    position: impl Into<NodeRef>,
    speed: impl Into<NodeRef>,
    time: impl Into<NodeRef>,
) -> NodeRef {
    thread_local! { static CELL: Lazy<Rc<FnDef>> = const { Lazy::new() }; }
    let def = CELL.with(|c| {
        c.get(|| {
            shader_fn(
                Some("triNoise3D"),
                vec![
                    ("position", Type::Vec3),
                    ("speed", Type::F32),
                    ("time", Type::F32),
                ],
                Type::F32,
                |args| {
                    let (position, speed, time) = (&args[0], &args[1], &args[2]);
                    let p = to_var(None, position.clone());
                    let z = to_var(None, float(1.4));
                    let rz = to_var(None, float(0.0));
                    let bp = to_var(None, p.clone());
                    let index = NodeRef::new(Node::Param {
                        name: "i",
                        ty: Type::F32,
                    });
                    let dg = to_var(None, tri3(bp.mul(2.0)));
                    let t = to_var(None, tri(p.z().add(tri(p.x().add(tri(p.y()))))));
                    let body = vec![
                        dg.clone(),
                        p.add_assign(dg.add(time.mul(float(0.1).mul(speed.clone())))),
                        bp.mul_assign(1.8),
                        z.mul_assign(1.5),
                        p.mul_assign(1.2),
                        t.clone(),
                        rz.add_assign(t.div(z.clone())),
                        bp.add_assign(0.14),
                    ];
                    let looped = NodeRef::new(Node::Loop {
                        start: Some(float(0.0)),
                        count: float(3.0),
                        index,
                        condition: "<=",
                        update: None,
                        body,
                    });
                    block(vec![p, z, rz.clone(), bp, looped], rz)
                },
            )
        })
    });
    call(
        &def,
        vec![position.into().to(Type::Vec3), speed.into(), time.into()],
    )
}

// ---------------------------------------------------------------------------
// oscillators and uv utils (`utils/Oscillators.js`, `utils/UVUtils.js`)
// ---------------------------------------------------------------------------

/// `oscTriangle( t )` — `Oscillators.js`:
/// `t.add( 0.5 ).fract().mul( 2 ).sub( 1 ).abs()`. Three defaults `t` to
/// `time`; pass [`super::time`] for that.
pub fn osc_triangle(t: impl Into<NodeRef>) -> NodeRef {
    t.into().add(0.5).fract().mul(2.0).sub(1.0).abs()
}

/// `oscSquare( t )` — `Oscillators.js`: `t.fract().round()`.
pub fn osc_square(t: impl Into<NodeRef>) -> NodeRef {
    unary("round", t.into().fract())
}

/// `oscSawtooth( t )` — `Oscillators.js`: `t.fract()`.
pub fn osc_sawtooth(t: impl Into<NodeRef>) -> NodeRef {
    t.into().fract()
}

/// `rotateUV( uv, rotation, center = vec2( 0.5 ) )` — `UVUtils.js`:
/// `rotate( uv.sub( center ), rotation ).add( center )`.
pub fn rotate_uv(uv: impl Into<NodeRef>, rotation: impl Into<NodeRef>) -> NodeRef {
    rotate_uv_about(uv, rotation, constant(Type::Vec2, vec![0.5, 0.5]))
}

/// [`rotate_uv`] with an explicit `center`.
pub fn rotate_uv_about(
    uv: impl Into<NodeRef>,
    rotation: impl Into<NodeRef>,
    center: impl Into<NodeRef>,
) -> NodeRef {
    let center = center.into();
    rotate(uv.into().sub(center.clone()), rotation).add(center)
}

/// `spherizeUV( uv, strength, center = vec2( 0.5 ) )` — `UVUtils.js`:
/// `delta = uv - center; delta4 = dot( delta, delta )²; uv + delta *
/// delta4 * strength`.
pub fn spherize_uv(uv: impl Into<NodeRef>, strength: impl Into<NodeRef>) -> NodeRef {
    spherize_uv_about(uv, strength, constant(Type::Vec2, vec![0.5, 0.5]))
}

/// [`spherize_uv`] with an explicit `center`.
pub fn spherize_uv_about(
    uv: impl Into<NodeRef>,
    strength: impl Into<NodeRef>,
    center: impl Into<NodeRef>,
) -> NodeRef {
    let uv = uv.into();
    let delta = uv.sub(center);
    let delta2 = delta.dot(delta.clone());
    let delta4 = delta2.mul(delta2.clone());
    let offset = delta4.mul(strength);
    uv.add(delta.mul(offset))
}

/// `remap( node, inLow, inHigh, outLow = 0, outHigh = 1 )` — `Remap.js`:
/// `node.sub( inLow ).div( inHigh.sub( inLow ) ).mul( outHigh.sub( outLow )
/// ).add( outLow )`. Rust has no default arguments; pass `0.0, 1.0` for
/// three's defaults, which is also what three emits for them.
pub fn remap(
    node: impl Into<NodeRef>,
    in_low: impl Into<NodeRef>,
    in_high: impl Into<NodeRef>,
    out_low: impl Into<NodeRef>,
    out_high: impl Into<NodeRef>,
) -> NodeRef {
    remap_impl(
        node.into(),
        in_low.into(),
        in_high.into(),
        out_low.into(),
        out_high.into(),
        false,
    )
}

/// `remapClamp( … )` — `Remap.js`: [`remap`] with `t.clamp()` before the
/// output range is applied.
pub fn remap_clamp(
    node: impl Into<NodeRef>,
    in_low: impl Into<NodeRef>,
    in_high: impl Into<NodeRef>,
    out_low: impl Into<NodeRef>,
    out_high: impl Into<NodeRef>,
) -> NodeRef {
    remap_impl(
        node.into(),
        in_low.into(),
        in_high.into(),
        out_low.into(),
        out_high.into(),
        true,
    )
}

fn remap_impl(
    node: NodeRef,
    in_low: NodeRef,
    in_high: NodeRef,
    out_low: NodeRef,
    out_high: NodeRef,
    clamp: bool,
) -> NodeRef {
    let mut t = node.sub(in_low.clone()).div(in_high.sub(in_low));
    if clamp {
        t = t.clamp(0.0, 1.0);
    }
    t.mul(out_high.sub(out_low.clone())).add(out_low)
}

// ---------------------------------------------------------------------------
// timers (`utils/Timer.js`)
// ---------------------------------------------------------------------------

/// `deltaTime` — `Timer.js`' `uniform( 0 ).setGroup( renderGroup ).onRenderUpdate(
/// ( frame ) => frame.deltaTime )`: seconds since the previous frame.
pub fn delta_time() -> NodeRef {
    thread_local! { static CELL: Lazy<NodeRef> = const { Lazy::new() }; }
    CELL.with(|c| {
        c.get(|| {
            uniform(
                UniformSource::DeltaTime,
                Type::F32,
                UniformGroup::Render,
                None,
            )
        })
    })
}

/// `frameId` — `Timer.js`' `uniform( 0, 'uint' ).setGroup( renderGroup
/// ).onRenderUpdate( ( frame ) => frame.frameId )`.
pub fn frame_id() -> NodeRef {
    thread_local! { static CELL: Lazy<NodeRef> = const { Lazy::new() }; }
    CELL.with(|c| {
        c.get(|| {
            uniform(
                UniformSource::FrameId,
                Type::U32,
                UniformGroup::Render,
                None,
            )
        })
    })
}

// ---------------------------------------------------------------------------
// billboarding (`utils/SpriteUtils.js`)
// ---------------------------------------------------------------------------

/// The options object `billboarding( { … } )` takes. `Default` is three's
/// defaults: `position: null, horizontal: true, vertical: false,
/// horizontalRotation: false`.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Billboarding {
    /// The billboard's center; `None` derives it from `positionWorld`.
    pub position: Option<NodeRef>,
    /// Whether to follow the camera rotation horizontally.
    pub horizontal: bool,
    /// Whether to follow the camera rotation vertically.
    pub vertical: bool,
    /// Whether to rotate around the Y axis to face the camera.
    pub horizontal_rotation: bool,
}

impl Default for Billboarding {
    fn default() -> Self {
        Self {
            position: None,
            horizontal: true,
            vertical: false,
            horizontal_rotation: false,
        }
    }
}

/// `billboarding( { position, horizontal, vertical, horizontalRotation } )` —
/// `SpriteUtils.js`: a clip-space position for a `vertexNode` that turns a
/// mesh's local axes toward the camera.
///
/// **Divergence, cosmetic** (`docs/nodes.md` §8): three assigns into the
/// `modelViewMatrix` operator node, which its builder turns into an inline
/// `var nodeVarN : mat4x4<f32> = …`; the port asks for the var explicitly,
/// so it is a `var<private>` assigned in the flow. Same value.
pub fn billboarding(options: Billboarding) -> NodeRef {
    let mw = model_world_matrix();
    let center = match options.position {
        Some(p) => p,
        None => position_world().sub(
            mw.mul(join(Type::Vec4, vec![position_geometry(), float(0.0)]))
                .xyz(),
        ),
    };

    let mut statements = Vec::new();
    let world = to_var(None, mw.clone());
    let column = |m: &NodeRef, c: usize, v: &NodeRef, out: &mut Vec<NodeRef>| {
        out.push(m.element(c).element(0).assign(v.x()));
        out.push(m.element(c).element(1).assign(v.y()));
        out.push(m.element(c).element(2).assign(v.z()));
    };
    column(&world, 3, &center, &mut statements);

    let model_view = to_var(None, camera_view_matrix().mul(world.clone()));
    let scale_x = mw.element(0).length();
    let scale_y = mw.element(1).length();
    let scale_z = mw.element(2).length();

    let (right, up, forward);
    if options.horizontal_rotation {
        let look = camera_position().sub(world.element(3).xyz());
        let look_xz = join(Type::Vec3, vec![look.x(), float(0.0), look.z()]).normalize();
        let right_w = join(
            Type::Vec3,
            vec![look_xz.z(), float(0.0), look_xz.x().negate()],
        );
        right = Some(
            camera_view_matrix()
                .mul(join(Type::Vec4, vec![right_w, float(0.0)]))
                .xyz()
                .mul(scale_x),
        );
        up = Some(camera_view_matrix().element(1).xyz().mul(scale_y));
        forward = camera_view_matrix()
            .mul(join(Type::Vec4, vec![look_xz, float(0.0)]))
            .xyz()
            .mul(scale_z);
    } else {
        right = options
            .horizontal
            .then(|| join(Type::Vec3, vec![scale_x, float(0.0), float(0.0)]));
        up = options
            .vertical
            .then(|| join(Type::Vec3, vec![float(0.0), scale_y, float(0.0)]));
        forward = constant(Type::Vec3, vec![0.0, 0.0, 1.0]);
    }
    if let Some(right) = right {
        column(&model_view, 0, &right, &mut statements);
    }
    if let Some(up) = up {
        column(&model_view, 1, &up, &mut statements);
    }
    column(&model_view, 2, &forward, &mut statements);

    let result = camera_projection_matrix()
        .mul(model_view)
        .mul(position_geometry());
    block(statements, result)
}

// ---------------------------------------------------------------------------
// methods
// ---------------------------------------------------------------------------

impl NodeRef {
    /// `x.toFloat()` — `TSLCore.js`' `addMethodChaining( 'toFloat', float )`.
    pub fn to_float(&self) -> NodeRef {
        self.to(Type::vector_of(Type::F32, 1))
    }
    /// `x.toInt()`.
    pub fn to_int(&self) -> NodeRef {
        self.to(Type::I32)
    }
    /// `x.toUint()`.
    pub fn to_uint(&self) -> NodeRef {
        self.to(Type::U32)
    }
    /// `x.toBool()`.
    pub fn to_bool(&self) -> NodeRef {
        self.to(Type::Bool)
    }
    /// `x.toVec2()`.
    pub fn to_vec2(&self) -> NodeRef {
        self.to(Type::Vec2)
    }
    /// `x.toVec3()`.
    pub fn to_vec3(&self) -> NodeRef {
        self.to(Type::Vec3)
    }
    /// `x.toVec4()`.
    pub fn to_vec4(&self) -> NodeRef {
        self.to(Type::Vec4)
    }
    /// `x.toColor()` — a `vec3`.
    pub fn to_color(&self) -> NodeRef {
        self.to(Type::Vec3)
    }
    /// `x.toIVec2()`.
    pub fn to_ivec2(&self) -> NodeRef {
        self.to(Type::IVec2)
    }
    /// `x.toIVec3()`.
    pub fn to_ivec3(&self) -> NodeRef {
        self.to(Type::IVec3)
    }
    /// `x.toIVec4()`.
    pub fn to_ivec4(&self) -> NodeRef {
        self.to(Type::IVec4)
    }
    /// `x.toUVec2()`.
    pub fn to_uvec2(&self) -> NodeRef {
        self.to(Type::UVec2)
    }
    /// `x.toUVec3()`.
    pub fn to_uvec3(&self) -> NodeRef {
        self.to(Type::UVec3)
    }
    /// `x.toUVec4()`.
    pub fn to_uvec4(&self) -> NodeRef {
        self.to(Type::UVec4)
    }
    /// `x.toBVec2()`.
    pub fn to_bvec2(&self) -> NodeRef {
        self.to(Type::BVec2)
    }
    /// `x.toBVec3()`.
    pub fn to_bvec3(&self) -> NodeRef {
        self.to(Type::BVec3)
    }
    /// `x.toBVec4()`.
    pub fn to_bvec4(&self) -> NodeRef {
        self.to(Type::BVec4)
    }
    /// `x.toMat2()`.
    pub fn to_mat2(&self) -> NodeRef {
        self.to(Type::Mat2)
    }
    /// `x.toMat3()` — from a `mat4`, `NodeBuilder.format()`'s column
    /// narrowing.
    pub fn to_mat3(&self) -> NodeRef {
        self.to(Type::Mat3)
    }
    /// `x.toMat4()`.
    pub fn to_mat4(&self) -> NodeRef {
        self.to(Type::Mat4)
    }

    /// `x.atan()` — one-argument `MathNode.ATAN`.
    pub fn atan(&self) -> NodeRef {
        atan(self)
    }
    /// `x.acos()`.
    pub fn acos(&self) -> NodeRef {
        acos(self)
    }
    /// `x.tan()`.
    pub fn tan(&self) -> NodeRef {
        tan(self)
    }
    /// `x.sinh()`.
    pub fn sinh(&self) -> NodeRef {
        sinh(self)
    }
    /// `x.cosh()`.
    pub fn cosh(&self) -> NodeRef {
        cosh(self)
    }
    /// `x.tanh()`.
    pub fn tanh(&self) -> NodeRef {
        tanh(self)
    }
    /// `x.asinh()`.
    pub fn asinh(&self) -> NodeRef {
        asinh(self)
    }
    /// `x.acosh()`.
    pub fn acosh(&self) -> NodeRef {
        acosh(self)
    }
    /// `x.atanh()`.
    pub fn atanh(&self) -> NodeRef {
        atanh(self)
    }
    /// `x.round()`.
    pub fn round(&self) -> NodeRef {
        round(self)
    }
    /// `x.trunc()`.
    pub fn trunc(&self) -> NodeRef {
        trunc(self)
    }
    /// `x.degrees()`.
    pub fn degrees(&self) -> NodeRef {
        degrees(self)
    }
    /// `x.radians()`.
    pub fn radians(&self) -> NodeRef {
        radians(self)
    }
    /// `m.determinant()`.
    pub fn determinant(&self) -> NodeRef {
        determinant(self)
    }
    /// `m.inverse()`.
    pub fn inverse(&self) -> NodeRef {
        inverse(self)
    }
    /// `m.transpose()`.
    pub fn transpose(&self) -> NodeRef {
        transpose(self)
    }
    /// `x.pow2()`.
    pub fn pow2(&self) -> NodeRef {
        pow2(self)
    }
    /// `x.pow4()`.
    pub fn pow4(&self) -> NodeRef {
        pow4(self)
    }
    /// `a.difference( b )`.
    pub fn difference(&self, b: impl Into<NodeRef>) -> NodeRef {
        difference(self, b)
    }
    /// `x.cbrt()`.
    pub fn cbrt(&self) -> NodeRef {
        cbrt(self)
    }
    /// `a.lengthSq()`.
    pub fn length_sq(&self) -> NodeRef {
        length_sq(self)
    }
    /// `v.length()` — `MathNode.LENGTH`.
    pub fn length(&self) -> NodeRef {
        super::length(self)
    }
    /// `n.faceForward( i, nref )`.
    pub fn face_forward(&self, i: impl Into<NodeRef>, nref: impl Into<NodeRef>) -> NodeRef {
        face_forward(self, i, nref)
    }
    /// `b.all()` — `MathNode.ALL`, see [`super::all`].
    pub fn all(&self) -> NodeRef {
        super::all(self)
    }
    /// `b.any()` — `MathNode.ANY`, see [`any`].
    pub fn any(&self) -> NodeRef {
        any(self)
    }
    /// `n.transformNormalByViewMatrix( viewMatrix )`.
    pub fn transform_normal_by_view_matrix(&self, view_matrix: impl Into<NodeRef>) -> NodeRef {
        transform_normal_by_view_matrix(self, view_matrix)
    }
    /// `n.transformNormal( matrix )` — see [`transform_normal`]. Three's
    /// `matrix` defaults to `modelWorldMatrix`; pass `model_world_matrix()`
    /// for its no-argument form.
    pub fn transform_normal(&self, matrix: impl Into<NodeRef>) -> NodeRef {
        transform_normal(self, matrix)
    }
    /// `n.transformNormalByInverseViewMatrix( viewMatrix )`.
    pub fn transform_normal_by_inverse_view_matrix(
        &self,
        view_matrix: impl Into<NodeRef>,
    ) -> NodeRef {
        transform_normal_by_inverse_view_matrix(self, view_matrix)
    }
    /// `a.xor( b )`.
    pub fn xor(&self, b: impl Into<NodeRef>) -> NodeRef {
        xor(self, b)
    }
    /// `a.bitNot()`.
    pub fn bit_not(&self) -> NodeRef {
        bit_not(self)
    }
    /// `x.countOneBits()`.
    pub fn count_one_bits(&self) -> NodeRef {
        count_one_bits(self)
    }
    /// `x.countTrailingZeros()`.
    pub fn count_trailing_zeros(&self) -> NodeRef {
        count_trailing_zeros(self)
    }
    /// `x.countLeadingZeros()`.
    pub fn count_leading_zeros(&self) -> NodeRef {
        count_leading_zeros(self)
    }
    /// `a.mod( b )` — see [`mod_`].
    pub fn mod_(&self, b: impl Into<NodeRef>) -> NodeRef {
        mod_(self, b)
    }
    /// `x.hash()`.
    pub fn hash(&self) -> NodeRef {
        hash(self)
    }
    /// `x.remap( inLow, inHigh, outLow, outHigh )` — `Remap.js`'
    /// `addMethodChaining( 'remap', remap )`.
    pub fn remap(
        &self,
        in_low: impl Into<NodeRef>,
        in_high: impl Into<NodeRef>,
        out_low: impl Into<NodeRef>,
        out_high: impl Into<NodeRef>,
    ) -> NodeRef {
        remap(self, in_low, in_high, out_low, out_high)
    }
    /// `x.remapClamp( inLow, inHigh, outLow, outHigh )`.
    pub fn remap_clamp(
        &self,
        in_low: impl Into<NodeRef>,
        in_high: impl Into<NodeRef>,
        out_low: impl Into<NodeRef>,
        out_high: impl Into<NodeRef>,
    ) -> NodeRef {
        remap_clamp(self, in_low, in_high, out_low, out_high)
    }
    /// `x.smoothstep( low, high )` — `MathNode.js`' `smoothstepElement`:
    /// `smoothstep( low, high, x )`.
    pub fn smoothstep(&self, low: impl Into<NodeRef>, high: impl Into<NodeRef>) -> NodeRef {
        super::smoothstep(low, high, self)
    }
    /// `x.step( edge )` — `MathNode.js`' `stepElement`: `step( edge, x )`.
    pub fn step(&self, edge: impl Into<NodeRef>) -> NodeRef {
        super::step(edge, self)
    }
}

/// `MathNode.getInputType()` for callers elsewhere in `tsl`.
pub(super) fn math_input_type(args: &[&NodeRef]) -> Type {
    input_type(args)
}