Repository navigation
Expand file tree
/
Copy pathwireface.js
More file actions
1659 lines (1568 loc) · 73.2 KB
/
Copy pathwireface.js
File metadata and controls
1659 lines (1568 loc) · 73.2 KB
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
/* ============================================================================
* wireface.js — minimal embeddable lipsync face renderer
* ----------------------------------------------------------------------------
* Repository: https://github.com/styk-tv/wireface
* npm: https://www.npmjs.com/package/wireface
* Copyright (c) 2026 Peter Styk
* Released under the MIT License — see LICENSE in the repository root.
* ----------------------------------------------------------------------------
* Each call to createWireface(canvas, options) builds a fully independent
* instance: its own Babylon engine + scene, its own audio context, its own
* channel state, its own materials and meshes. Multiple instances can live on
* the same page side-by-side without sharing state.
*
* Requires: BABYLON.js loaded as a global (e.g. from cdn.babylonjs.com) BEFORE
* this script is loaded.
*
* Public API returned by createWireface:
* loadAudio(file_or_blob) → Promise<void>
* loadPreset(presetObject) → void (the JSON saved by the v010+ UI)
* setRenderConfig(partial) → void (apply one or more renderConfig fields)
* getRenderConfig() → object (snapshot of current config)
* play(fromOffset?) → void
* pause() → void
* stop() → void
* setMood(name) → void ('neutral','happy','sad','angry','fear','surprise','sleep')
* setChannel(name, value) → void (manual override, auto-releases ~1.2s)
* setChannelGain(name, gain) → void (multiplier on channel before render)
* getChannel(name) → number (current smoothed value)
* getChannelTarget(name) → number (latest target before smoothing)
* getChannelGain(name) → number
* getChannelNames() → string[]
* getMoodNames() → string[]
* setLoop(bool) → void
* setView(view) → void ('front','three-q','profile','orbit')
* isPlaying() → bool
* getDuration() → number (seconds, 0 if no audio)
* getPosition() → number (seconds since start)
* getActiveMood() → string
* dispose() → void
* ========================================================================== */
(function (global) {
'use strict';
/* ─── module-level constants ─────────────────────────────────────────── */
const CHANNELS = {
viseme_sil: { range: [0,1], def: 1.0 }, viseme_PP: { range: [0,1], def: 0 },
viseme_FF: { range: [0,1], def: 0 }, viseme_TH: { range: [0,1], def: 0 },
viseme_DD: { range: [0,1], def: 0 }, viseme_kk: { range: [0,1], def: 0 },
viseme_CH: { range: [0,1], def: 0 }, viseme_SS: { range: [0,1], def: 0 },
viseme_nn: { range: [0,1], def: 0 }, viseme_RR: { range: [0,1], def: 0 },
viseme_aa: { range: [0,1], def: 0 }, viseme_E: { range: [0,1], def: 0 },
viseme_I: { range: [0,1], def: 0 }, viseme_O: { range: [0,1], def: 0 },
viseme_U: { range: [0,1], def: 0 },
jawOpen: { range: [0,1], def: 0 },
mouthSmile: { range: [-1,1], def: 0 },
mouthPucker: { range: [0,1], def: 0 },
eyeBlinkLeft: { range: [0,1], def: 0 },
eyeBlinkRight: { range: [0,1], def: 0 },
eyeLookH: { range: [-1,1], def: 0 },
eyeLookV: { range: [-1,1], def: 0 },
eyeSquint: { range: [0,1], def: 0 },
browInnerUp: { range: [0,1], def: 0 },
browOuterUp: { range: [-1,1], def: 0 },
browDown: { range: [0,1], def: 0 },
noseSneer: { range: [0,1], def: 0 },
headRotateX: { range: [-1,1], def: 0 },
headRotateY: { range: [-1,1], def: 0 },
headRotateZ: { range: [-1,1], def: 0 },
};
const CHANNEL_NAMES = Object.keys(CHANNELS);
const VISEME_PARAMS = {
sil: { open: 0.00, width: 0.00, round: 0.00, smile: 0.00, lipPress: 0.30, jaw: 0.00 },
PP: { open: 0.00, width: 0.00, round: 0.20, smile: 0.00, lipPress: 0.95, jaw: 0.00 },
FF: { open: 0.10, width: 0.05, round: 0.00, smile: 0.05, lipPress: 0.55, jaw: 0.10 },
TH: { open: 0.20, width: 0.15, round: 0.00, smile: 0.00, lipPress: 0.10, jaw: 0.18 },
DD: { open: 0.28, width: 0.25, round: 0.00, smile: 0.05, lipPress: 0.05, jaw: 0.25 },
kk: { open: 0.32, width: 0.20, round: 0.00, smile: 0.00, lipPress: 0.05, jaw: 0.30 },
CH: { open: 0.32, width: 0.00, round: 0.55, smile: 0.00, lipPress: 0.10, jaw: 0.25 },
SS: { open: 0.18, width: 0.45, round: 0.00, smile: 0.20, lipPress: 0.20, jaw: 0.10 },
nn: { open: 0.18, width: 0.18, round: 0.00, smile: 0.00, lipPress: 0.30, jaw: 0.18 },
RR: { open: 0.35, width: 0.00, round: 0.50, smile: 0.00, lipPress: 0.05, jaw: 0.25 },
aa: { open: 0.85, width: 0.45, round: 0.00, smile: 0.00, lipPress: 0.00, jaw: 0.85 },
E: { open: 0.50, width: 0.65, round: 0.00, smile: 0.20, lipPress: 0.00, jaw: 0.45 },
I: { open: 0.28, width: 0.85, round: 0.00, smile: 0.30, lipPress: 0.00, jaw: 0.18 },
O: { open: 0.55, width: 0.00, round: 0.75, smile: 0.00, lipPress: 0.00, jaw: 0.45 },
U: { open: 0.35, width: -0.15, round: 0.95, smile: 0.00, lipPress: 0.05, jaw: 0.25 },
};
const MOODS = {
neutral: { browInnerUp: 0, browOuterUp: 0, browDown: 0, mouthSmile: 0, eyeSquint: 0 },
happy: { browOuterUp: 0.4, mouthSmile: 0.7, eyeSquint: 0.25 },
sad: { browInnerUp: 0.7, browDown: 0.2, mouthSmile: -0.5, eyeBlinkLeft: 0.15, eyeBlinkRight: 0.15 },
angry: { browInnerUp: 0, browOuterUp: -0.4, browDown: 0.7, mouthSmile: -0.4, noseSneer: 0.5, eyeSquint: 0.4 },
fear: { browInnerUp: 0.7, browOuterUp: 0.5, mouthSmile: -0.3, eyeBlinkLeft: -0.2, eyeBlinkRight: -0.2 },
surprise:{ browInnerUp: 0.6, browOuterUp: 0.8, jawOpen: 0.5, mouthSmile: 0 },
sleep: { eyeBlinkLeft: 1, eyeBlinkRight: 1, browDown: 0.3, mouthSmile: 0 },
};
const MOOD_NAMES = Object.keys(MOODS);
const MOOD_CHANNELS = ['browInnerUp','browOuterUp','browDown','mouthSmile',
'eyeSquint','noseSneer','eyeBlinkLeft','eyeBlinkRight'];
const SMOOTH = {
default: 0.35,
eyeBlinkLeft: 0.55, eyeBlinkRight: 0.55,
...Object.fromEntries(Object.keys(VISEME_PARAMS).map(v => ['viseme_' + v, 0.45])),
browInnerUp: 0.18, browOuterUp: 0.18, browDown: 0.18,
headRotateX: 0.10, headRotateY: 0.10, headRotateZ: 0.10,
eyeLookH: 0.25, eyeLookV: 0.25,
};
/* SHAPES table — fields use plain arrays for direction so we don't need
BABYLON at module-init time. They're converted to Vector3 inside the
factory. */
const SHAPE_DEFS = {
mouth_jaw_drop: { uv:[0.50, 0.78], d:[ 0.00,-0.20,-0.02], r: 0.28 },
mouth_open_lower: { uv:[0.50, 0.71], d:[ 0.00,-0.10, 0.00], r: 0.16 },
mouth_open_upper: { uv:[0.50, 0.66], d:[ 0.00, 0.04, 0.00], r: 0.13 },
mouth_smile_L: { uv:[0.32, 0.69], d:[-0.06, 0.07,-0.02], r: 0.13 },
mouth_smile_R: { uv:[0.68, 0.69], d:[ 0.06, 0.07,-0.02], r: 0.13 },
mouth_frown_L: { uv:[0.32, 0.69], d:[ 0.02,-0.07, 0.00], r: 0.13 },
mouth_frown_R: { uv:[0.68, 0.69], d:[-0.02,-0.07, 0.00], r: 0.13 },
mouth_round_L: { uv:[0.32, 0.69], d:[ 0.07, 0.00, 0.03], r: 0.13 },
mouth_round_R: { uv:[0.68, 0.69], d:[-0.07, 0.00, 0.03], r: 0.13 },
mouth_round_top: { uv:[0.50, 0.66], d:[ 0.00,-0.02, 0.04], r: 0.12 },
mouth_round_bot: { uv:[0.50, 0.71], d:[ 0.00, 0.02, 0.04], r: 0.12 },
mouth_press_top: { uv:[0.50, 0.66], d:[ 0.00,-0.025,0.00], r: 0.12 },
mouth_press_bot: { uv:[0.50, 0.71], d:[ 0.00, 0.025,0.00], r: 0.12 },
mouth_width_L: { uv:[0.32, 0.69], d:[-0.05, 0.00, 0.00], r: 0.13 },
mouth_width_R: { uv:[0.68, 0.69], d:[ 0.05, 0.00, 0.00], r: 0.13 },
eye_blink_L_up: { uv:[0.30, 0.36], d:[ 0.00,-0.05,-0.02], r: 0.08 },
eye_blink_L_lo: { uv:[0.30, 0.45], d:[ 0.00, 0.03, 0.00], r: 0.08 },
eye_blink_R_up: { uv:[0.70, 0.36], d:[ 0.00,-0.05,-0.02], r: 0.08 },
eye_blink_R_lo: { uv:[0.70, 0.45], d:[ 0.00, 0.03, 0.00], r: 0.08 },
eye_squint_L: { uv:[0.30, 0.42], d:[ 0.00,-0.015,0.00], r: 0.10 },
eye_squint_R: { uv:[0.70, 0.42], d:[ 0.00,-0.015,0.00], r: 0.10 },
brow_inner_L: { uv:[0.42, 0.25], d:[ 0.00, 0.06, 0.02], r: 0.10 },
brow_inner_R: { uv:[0.58, 0.25], d:[ 0.00, 0.06, 0.02], r: 0.10 },
brow_outer_L: { uv:[0.20, 0.27], d:[ 0.00, 0.05, 0.00], r: 0.12 },
brow_outer_R: { uv:[0.80, 0.27], d:[ 0.00, 0.05, 0.00], r: 0.12 },
brow_down_L: { uv:[0.30, 0.27], d:[ 0.00,-0.05, 0.00], r: 0.14 },
brow_down_R: { uv:[0.70, 0.27], d:[ 0.00,-0.05, 0.00], r: 0.14 },
nose_sneer_L: { uv:[0.42, 0.55], d:[ 0.00, 0.03, 0.02], r: 0.08 },
nose_sneer_R: { uv:[0.58, 0.55], d:[ 0.00, 0.03, 0.02], r: 0.08 },
};
const DEFAULT_CONFIG = {
mode: 'wire', // 'wire' | 'edges' | 'shaded'
meshCols: 17,
meshRows: 21,
spread: 1.0,
reactivity: 0.25,
scaleNose: 1.0, scaleEyes: 1.0, scaleMouth: 1.0,
flipFace: true,
glow: false,
pupils: true,
fragment: true,
minimal: true,
meshVisible: true,
loopAudio: true,
mouthHole: true, mouthHoleSize: 1.0,
eyeHoles: false, eyeHoleSize: 1.0, eyeDepth: 1.0,
mouthAnchor: true, eyeAnchor: false,
lineThickness: 1.0,
lipVertAmp: 1.6, lipPressForce: 1.0,
overlayTracksMesh: true,
moodTransitionTime: 0.6,
lineColor: '#ffffff', pupilColor: '#ffffff',
// Per-feature minimal-line overrides. null = inherit lineColor (backward
// compat with v1.0.x presets). Each feature can be hidden individually
// even when the master `minimal` is on. Each has its own thickness
// multiplier on top of the global `lineThickness`.
mouthColor: null, eyeColor: null, noseColor: null,
minimalMouth: true, minimalEyes: true, minimalNose: true, minimalBrows: true,
mouthLineThickness: 1.0, eyeLineThickness: 1.0, noseLineThickness: 1.0, browLineThickness: 1.0,
baseColor: '#ffffff', fadeColor: '#000000', depthFade: 0.0,
// exponent applied to the per-vertex fade weight. >1 darkens the back
// faster (so silhouette edges actually reach fadeColor in wire mode);
// <1 brightens it. 1.0 = linear, original behavior.
depthFadeCurve: 1.0,
irisColor: '#000000', irisSize: 1.6,
browColor: '#ffffff',
jawExtend: 0.0, mouthGrow: 0.0,
// experimental — static z push of upper-mouth and lower-jaw regions for
// animal / crocodile-mouth distortions. Stack on top of jawExtend (which
// is jawOpen-driven). Default 0 = current behavior.
upperJawProtrude: 0.0, lowerJawProtrude: 0.0,
cameraView: 'three-q', // initial view
};
/* ─── module helpers ─────────────────────────────────────────────────── */
function hex2rgb(hex) {
const h = (hex || '#ffffff').replace('#', '');
return [
parseInt(h.slice(0, 2), 16) / 255,
parseInt(h.slice(2, 4), 16) / 255,
parseInt(h.slice(4, 6), 16) / 255,
];
}
/* ──────────────────────────────────────────────────────────────────────
* F A C T O R Y
* ────────────────────────────────────────────────────────────────────── */
function createWireface(canvas, options) {
if (typeof BABYLON === 'undefined') {
throw new Error('wireface: BABYLON.js not loaded — include babylon.js before wireface.js');
}
const config = Object.assign({}, DEFAULT_CONFIG, options || {});
const V = (x, y, z) => new BABYLON.Vector3(x, y, z);
/* convert SHAPE_DEFS → SHAPES with Vector3 directions */
const SHAPES = {};
for (const k of Object.keys(SHAPE_DEFS)) {
const s = SHAPE_DEFS[k];
SHAPES[k] = { uv: s.uv, dir: V(s.d[0], s.d[1], s.d[2]), radius: s.r };
}
/* ── per-instance state ── */
const state = {};
const stateTarget = {};
const manualLock = {};
const channelGains = {};
CHANNEL_NAMES.forEach(n => {
state[n] = CHANNELS[n].def;
stateTarget[n] = CHANNELS[n].def;
channelGains[n] = 1.0;
});
const moodTargetWeights = {};
const moodCurrentWeights = {};
for (const m of MOOD_NAMES) {
moodTargetWeights[m] = 0;
moodCurrentWeights[m] = 0;
}
moodTargetWeights.neutral = 1;
moodCurrentWeights.neutral = 1;
let activeMood = 'neutral';
/* ── Babylon scene ── */
const engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: false, stencil: false, antialias: true });
// v1.1.2: useRightHandedSystem MUST be set via Scene constructor options
// (not by post-construction property assignment) — the post-assign path
// gets silently no-op'd somewhere in Babylon 9.5's scene init pipeline,
// so the scene boots in LH despite our request. The constructor option
// sticks. Verified empirically with playwright: post-assign showed
// `scene.useRightHandedSystem === false` at first frame, but options-form
// shows true.
//
// Why we want RH at all: Babylon's LH default puts +Z into the screen.
// warpToFace builds the face with +Z = nose-forward, so in LH the face
// would render facing AWAY from the default ArcRotateCamera at -Z (user
// sees back of head, profile shows wrong side). RH flips Z so +Z is
// out-of-screen, geometry naturally faces the camera, and the camera
// view alphas (front=-π/2, profile=0, three-q=-π/2-0.6) map to the
// conventionally-correct sides of the face — front shows the front,
// profile shows the face's right, three-quarter is a proper portrait
// angle. No geometry change, no faceRoot.rotation flip, no view-alpha
// rewiring.
const scene = new BABYLON.Scene(engine, { useRightHandedSystem: true });
scene.useRightHandedSystem = true; // belt and braces — also assign post-ctor
scene.clearColor = new BABYLON.Color4(0, 0, 0, 1);
// we never pick on pointer-move; saves a per-frame ray test
scene.skipPointerMovePicking = true;
/* glow layer — bloom on the emissive wire/overlay/iris materials. Gated by
config.glow; intensity 0 disables it. Created once and toggled, not
added/removed per frame. */
const glowLayer = new BABYLON.GlowLayer('glow', scene, { mainTextureFixedSize: 1024, blurKernelSize: 32 });
glowLayer.intensity = 0;
const camera = new BABYLON.ArcRotateCamera('cam', -Math.PI/2 - 0.6, Math.PI/2 - 0.15, 4.0, BABYLON.Vector3.Zero(), scene);
camera.minZ = 0.1; camera.maxZ = 100;
camera.lowerRadiusLimit = 1.5; camera.upperRadiusLimit = 12;
camera.wheelDeltaPercentage = 0.01;
// sandboxed iframes can throw on getGamepads — neutralize
const _origGet = navigator.getGamepads;
try { navigator.getGamepads = () => []; } catch (_) {}
camera.attachControl(canvas, true);
try { navigator.getGamepads = _origGet; } catch (_) {}
const hemi = new BABYLON.HemisphericLight('h', new BABYLON.Vector3(0, 1, 0), scene);
hemi.intensity = 0.9;
hemi.groundColor = new BABYLON.Color3(0.10, 0.10, 0.14);
const faceRoot = new BABYLON.TransformNode('faceRoot', scene);
/* ── materials ── */
const faceMat = new BABYLON.StandardMaterial('faceMat', scene);
faceMat.diffuseColor = new BABYLON.Color3(0.05, 0.05, 0.07);
faceMat.emissiveColor = new BABYLON.Color3(0.012, 0.012, 0.020);
faceMat.specularColor = new BABYLON.Color3(0.20, 0.22, 0.28);
faceMat.specularPower = 48;
faceMat.backFaceCulling = false;
faceMat.useVertexColor = true;
const wireMat = new BABYLON.StandardMaterial('wireMat', scene);
wireMat.emissiveColor = new BABYLON.Color3(1, 1, 1);
wireMat.disableLighting = true;
wireMat.wireframe = true;
wireMat.backFaceCulling = false;
wireMat.alpha = 1;
wireMat.useVertexColor = true;
/* one material per minimal-line feature so each can have its own color
independent of the others. lineColor is the shared fallback; if a
per-feature color (mouthColor/eyeColor/noseColor) is null the feature
inherits lineColor at apply time. */
function makeMinimalMat(name) {
const m = new BABYLON.StandardMaterial(name, scene);
m.emissiveColor = new BABYLON.Color3(1, 1, 1);
m.disableLighting = true;
m.specularColor = new BABYLON.Color3(0, 0, 0);
return m;
}
const mouthMat = makeMinimalMat('mouthMat');
const eyeMat = makeMinimalMat('eyeMat');
const noseMat = makeMinimalMat('noseMat');
const browMat = makeMinimalMat('browMat');
// legacy alias — some downstream code still references minimalMat
const minimalMat = mouthMat;
const minimalGroup = new BABYLON.TransformNode('minimal', scene);
minimalGroup.parent = faceRoot;
/* ─── 4a. parametric warp: grid (u,v) → face surface (x,y,z) ───
Ported verbatim from the v011 editor. Uses an elliptical taper so the
silhouette is oval, not rectangular. Then layers feature bumps (eye
sockets, brow ridge, nose, mouth, cheeks) as gaussian falloffs in
xRel/yRel space (xRel = (u-0.5)*2, yRel = (0.5-v)*2). This shape's
z-range (≈0.10..0.30) is what lets depthFade produce a visible
gradient instead of pinning the silhouette to black. */
function warpToFace(u, v) {
const xRel = (u - 0.5) * 2; // -1..1 left to right
const yRel = (0.5 - v) * 2; // 1..-1 top to bottom
// strong elliptical taper — boundary of the grid maps to an oval
const yNorm = yRel * 0.95;
const ovalT = Math.max(0, 1 - yNorm * yNorm);
const widthAtY = Math.sqrt(ovalT) * 0.80;
let x = xRel * widthAtY;
// y stays linear; chin extends + forehead squashes
let y = yRel * 1.0;
if (yRel < -0.4) y = -0.4 + (yRel + 0.4) * 1.55;
if (yRel > 0.7) y = 0.7 + (yRel - 0.7) * 0.45;
// base z bulge — quadratic falloff from center
let z = (1 - xRel*xRel * 0.55 - yRel*yRel * 0.55) * 0.22;
if (z < -0.05) z = -0.05;
// eye sockets recess (gaussian — broad and bowl-shaped).
// Centered at yRel=0.20 → UV v=0.40 to align with the eye opening
// (eye-hole puncture, minimal-line eyelid overlay, eye_blink shapes
// are all at v=0.40). Original v011 used yRel=0.30 which placed the
// bowl above the eye opening.
const eyeY = 0.20;
const eyeL_d = ((xRel + 0.45) / 0.30) ** 2 + ((yRel - eyeY) / 0.18) ** 2;
const eyeR_d = ((xRel - 0.45) / 0.30) ** 2 + ((yRel - eyeY) / 0.18) ** 2;
const eyeRecess = 0.045 * config.eyeDepth;
z -= eyeRecess * Math.exp(-eyeL_d);
z -= eyeRecess * Math.exp(-eyeR_d);
// brow ridge bumps forward
const brow_d = (xRel / 0.7) ** 2 + ((yRel - 0.55) / 0.10) ** 2;
z += 0.045 * Math.exp(-brow_d);
// nose — the dominant z bump
const nose_d = (xRel / 0.18) ** 2 + ((yRel - 0.05) / 0.40) ** 2;
z += 0.22 * Math.exp(-nose_d);
// mouth area pushed slightly forward
const mouth_d = (xRel / 0.55) ** 2 + ((yRel + 0.45) / 0.20) ** 2;
z += 0.06 * Math.exp(-mouth_d);
// cheekbones
const cheekL_d = ((xRel + 0.55) / 0.30) ** 2 + ((yRel - 0.05) / 0.28) ** 2;
const cheekR_d = ((xRel - 0.55) / 0.30) ** 2 + ((yRel - 0.05) / 0.28) ** 2;
z += 0.04 * Math.exp(-cheekL_d);
z += 0.04 * Math.exp(-cheekR_d);
return V(x, y, z);
}
/* mouth center constants */
const mouthCenterUV = warpToFace(0.50, 0.69);
const mouthBaseHW = 0.27;
/* lip target — SAME formula used by both the bezier overlay and the mesh
anchor pass so they animate identically */
function lipTarget(xRel, isUpper, p) {
const cx = mouthCenterUV.x, cy = mouthCenterUV.y, cz = mouthCenterUV.z;
const hw = mouthBaseHW * (1 + p.width * 0.40) * (1 - p.round * 0.40);
const vAmp = config.lipVertAmp;
const closeF = config.lipPressForce;
const sep = p.open * 0.16 * vAmp;
const compress = p.lipPress * 0.012 * vAmp * closeF;
const jawDrop = p.jaw * 0.09 * vAmp;
const smileLift = p.smile * 0.05;
const roundFwd = p.round * 0.07;
const upperDip = 0.007 * (1 - p.lipPress * 0.55);
const lowerDip = 0.022 * (1 - p.lipPress * 0.55);
let cp;
if (isUpper) {
const upperY = cy + sep / 2 - compress + smileLift * 0.4;
cp = [
[-1.00, cy + smileLift, -roundFwd * 0.5],
[-0.45, upperY + 0.012, 0.012],
[ 0.00, upperY - upperDip, 0.018 + roundFwd],
[ 0.45, upperY + 0.012, 0.012],
[ 1.00, cy + smileLift, -roundFwd * 0.5],
];
} else {
const lowerY = cy - sep / 2 + compress - jawDrop;
cp = [
[-1.00, cy + smileLift, -roundFwd * 0.5],
[-0.50, lowerY - 0.005, 0.014],
[ 0.00, lowerY - lowerDip, 0.022 + roundFwd * 1.1],
[ 0.50, lowerY - 0.005, 0.014],
[ 1.00, cy + smileLift, -roundFwd * 0.5],
];
}
xRel = Math.max(-1, Math.min(1, xRel));
let i = 0;
while (i < cp.length - 1 && xRel > cp[i + 1][0]) i++;
i = Math.min(i, cp.length - 2);
const t = (xRel - cp[i][0]) / (cp[i + 1][0] - cp[i][0]);
const yT = cp[i][1] * (1 - t) + cp[i + 1][1] * t;
const zO = cp[i][2] * (1 - t) + cp[i + 1][2] * t;
return V(cx + xRel * hw, yT, cz + zO);
}
function eyeLidTargetFor(xRel, isUpper, eyeDef, blink, squint) {
const a = ((xRel + 1) / 2) * Math.PI;
const x = eyeDef.center.x + Math.cos(Math.PI - a) * eyeDef.hw;
const baseY = eyeDef.center.y + (isUpper ? 1 : -1) * Math.sin(a) * eyeDef.hh;
const y = baseY * (1 - blink) + eyeDef.center.y * blink;
const squintShift = squint * 0.012;
const yWithSquint = y + (isUpper ? -squintShift : squintShift * 0.5);
const z = eyeDef.center.z + 0.018 + Math.sin(a) * 0.012;
return V(x, yWithSquint, z);
}
/* ── viseme aggregator ── */
function computeMouthParams() {
let totalW = 0;
const out = { open: 0, width: 0, round: 0, smile: 0, lipPress: 0, jaw: 0 };
for (const v of Object.keys(VISEME_PARAMS)) {
const w = state['viseme_' + v] || 0;
if (w <= 0) continue;
totalW += w;
const p = VISEME_PARAMS[v];
out.open += p.open * w; out.width += p.width * w; out.round += p.round * w;
out.smile += p.smile * w; out.lipPress += p.lipPress * w; out.jaw += p.jaw * w;
}
if (totalW > 1) {
const k = 1 / totalW;
out.open *= k; out.width *= k; out.round *= k;
out.smile *= k; out.lipPress *= k; out.jaw *= k;
}
out.smile += state.mouthSmile;
const pucker = state.mouthPucker;
out.round += pucker * 0.7; out.width += -pucker * 0.4;
out.open += state.jawOpen * 0.6;
out.jaw += state.jawOpen * 0.7;
return out;
}
/* ── mesh state (per instance) ── */
let mesh = null;
let baseVertices = [];
let baseUVs = [];
let triIndices = [];
let adjacency = [];
let _dispBuf = null, _posBuf = null, _normBuf = null, _colBuf = null;
let _zMin = 0, _zMax = 0;
let _baseRGB = [1,1,1], _fadeRGB = [0,0,0];
let _dirtyBaseColor = false;
let mouthRingVerts = [];
let eyeRingVerts = [];
const NEUTRAL_MOUTH = { open: 0, width: 0, round: 0, smile: 0, lipPress: 0.30, jaw: 0 };
function computeAdjacency(tris, n) {
const adj = Array.from({ length: n }, () => new Set());
for (let i = 0; i < tris.length; i += 3) {
const a = tris[i], b = tris[i+1], c = tris[i+2];
adj[a].add(b); adj[a].add(c);
adj[b].add(a); adj[b].add(c);
adj[c].add(a); adj[c].add(b);
}
return adj.map(s => Array.from(s));
}
function rebuildMesh() {
if (mesh) { mesh.dispose(); mesh = null; }
const cols = config.meshCols;
const rows = config.meshRows;
baseVertices = []; baseUVs = []; triIndices = [];
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
const u = c / (cols - 1);
const v = r / (rows - 1);
baseUVs.push({ u, v });
baseVertices.push(warpToFace(u, v));
}
}
// optional oval clip
const passes = (i) => {
if (!config.fragment) return true;
const u = baseUVs[i].u, v = baseUVs[i].v;
const xRel = (u - 0.5) * 2;
const yRel = (0.5 - v) * 2;
return (xRel * xRel * 1.2 + yRel * yRel * 0.95) < 1.0;
};
const insideHole = (i, cu, cv, ru, rv) => {
const du = (baseUVs[i].u - cu) / ru;
const dv = (baseUVs[i].v - cv) / rv;
return du*du + dv*dv < 1;
};
const triAllowed = (a, b, c) => {
if (!passes(a) || !passes(b) || !passes(c)) return false;
if (config.mouthHole) {
const cu = 0.50, cv = 0.69;
const ru = 0.18 * config.mouthHoleSize, rv = 0.075 * config.mouthHoleSize;
// skip if centroid is inside the mouth ellipse
const ucen = (baseUVs[a].u + baseUVs[b].u + baseUVs[c].u) / 3;
const vcen = (baseUVs[a].v + baseUVs[b].v + baseUVs[c].v) / 3;
const du = (ucen - cu) / ru, dv = (vcen - cv) / rv;
if (du*du + dv*dv < 1) return false;
}
if (config.eyeHoles) {
// eye-hole ellipse — sized to roughly match the eye feature radius
// so the cut is visibly meaningful at typical mesh densities.
// eyeHoleSize is a multiplier (mirrors mouthHoleSize).
const eRU = 0.105 * config.eyeHoleSize;
const eRV = 0.075 * config.eyeHoleSize;
const ucen = (baseUVs[a].u + baseUVs[b].u + baseUVs[c].u) / 3;
const vcen = (baseUVs[a].v + baseUVs[b].v + baseUVs[c].v) / 3;
const dl = ((ucen-0.30)/eRU)**2 + ((vcen-0.40)/eRV)**2;
const dr = ((ucen-0.70)/eRU)**2 + ((vcen-0.40)/eRV)**2;
if (dl < 1 || dr < 1) return false;
}
return true;
};
const maybeAdd = (a, b, c) => { if (triAllowed(a,b,c)) triIndices.push(a, b, c); };
for (let r = 0; r < rows - 1; r++) {
for (let c = 0; c < cols - 1; c++) {
const i00 = r*cols + c, i10 = r*cols + (c+1);
const i01 = (r+1)*cols + c, i11 = (r+1)*cols + (c+1);
if ((r + c) % 2 === 0) { maybeAdd(i00, i10, i11); maybeAdd(i00, i11, i01); }
else { maybeAdd(i00, i10, i01); maybeAdd(i10, i11, i01); }
}
}
adjacency = computeAdjacency(triIndices, baseVertices.length);
mesh = new BABYLON.Mesh('face', scene);
mesh.parent = faceRoot;
const N = baseVertices.length;
_posBuf = new Float32Array(N * 3);
_normBuf = new Float32Array(N * 3);
_colBuf = new Float32Array(N * 4);
_dispBuf = new Array(N);
_zMin = +Infinity; _zMax = -Infinity;
for (let i = 0; i < N; i++) {
_posBuf[3*i] = baseVertices[i].x;
_posBuf[3*i+1] = baseVertices[i].y;
_posBuf[3*i+2] = baseVertices[i].z;
if (baseVertices[i].z < _zMin) _zMin = baseVertices[i].z;
if (baseVertices[i].z > _zMax) _zMax = baseVertices[i].z;
_dispBuf[i] = V(0, 0, 0);
_colBuf[4*i] = 1; _colBuf[4*i+1] = 1; _colBuf[4*i+2] = 1; _colBuf[4*i+3] = 1;
}
BABYLON.VertexData.ComputeNormals(_posBuf, triIndices, _normBuf);
const vd = new BABYLON.VertexData();
vd.positions = Array.from(_posBuf);
vd.indices = triIndices;
vd.normals = Array.from(_normBuf);
vd.colors = Array.from(_colBuf);
vd.applyToMesh(mesh, true);
applyRenderMode(); applyFaceFlip(); applyMeshVisibility();
identifyAnchorRings();
_dirtyBaseColor = true;
}
function identifyAnchorRings() {
mouthRingVerts = []; eyeRingVerts = [];
const mRU = 0.18 * config.mouthHoleSize;
const mRV = 0.075 * config.mouthHoleSize;
const mCU = 0.50, mCV = 0.69;
for (let i = 0; i < baseUVs.length; i++) {
const du = (baseUVs[i].u - mCU) / mRU;
const dv = (baseUVs[i].v - mCV) / mRV;
const d2 = du*du + dv*dv;
if (d2 < 4) {
const isUpper = baseUVs[i].v < mCV;
const xRel = Math.max(-1, Math.min(1, du));
let weight = (d2 <= 1) ? d2 : (4 - d2) / 3;
weight = Math.max(0, Math.min(1, weight));
mouthRingVerts.push({
idx: i, isUpper, xRel, weight,
baselineTarget: lipTarget(xRel, isUpper, NEUTRAL_MOUTH)
});
}
}
const eyeRingDef = [
{ side: 'L', cu: 0.30, cv: 0.40, ru: 0.085, rv: 0.045, hw: 0.10, hh: 0.040 },
{ side: 'R', cu: 0.70, cv: 0.40, ru: 0.085, rv: 0.045, hw: 0.10, hh: 0.040 },
];
for (const e of eyeRingDef) {
for (let i = 0; i < baseUVs.length; i++) {
const du = (baseUVs[i].u - e.cu) / e.ru;
const dv = (baseUVs[i].v - e.cv) / e.rv;
const d2 = du*du + dv*dv;
if (d2 < 4) {
const isUpper = baseUVs[i].v < e.cv;
const xRel = Math.max(-1, Math.min(1, du));
let weight = (d2 <= 1) ? d2 : (4 - d2) / 3;
weight = Math.max(0, Math.min(1, weight));
const center = warpToFace(e.cu, e.cv);
eyeRingVerts.push({
idx: i, side: e.side, isUpper, xRel, weight,
eyeDef: { center, hw: e.hw, hh: e.hh },
baselineTarget: eyeLidTargetFor(xRel, isUpper, { center, hw: e.hw, hh: e.hh }, 0, 0)
});
}
}
}
}
/* ── displacement passes ── */
function applyShape(disp, shape, weight, spread) {
if (Math.abs(weight) < 0.001) return;
const cu = shape.uv[0], cv = shape.uv[1];
const r = shape.radius * spread;
const r2 = r * r;
const dx = shape.dir.x * weight, dy = shape.dir.y * weight, dz = shape.dir.z * weight;
for (let i = 0; i < baseUVs.length; i++) {
const du = baseUVs[i].u - cu;
const dv = baseUVs[i].v - cv;
const d2 = du*du + dv*dv;
if (d2 > 4 * r2) continue;
const w = Math.exp(-d2 / r2);
disp[i].x += dx * w; disp[i].y += dy * w; disp[i].z += dz * w;
}
}
function computeMorphs(disp) {
const spread = config.spread;
const p = computeMouthParams();
// mouth
applyShape(disp, SHAPES.mouth_jaw_drop, p.jaw, spread);
applyShape(disp, SHAPES.mouth_open_lower, p.open, spread);
applyShape(disp, SHAPES.mouth_open_upper, p.open * 0.4, spread);
const sm = p.smile;
if (sm > 0) { applyShape(disp, SHAPES.mouth_smile_L, sm, spread); applyShape(disp, SHAPES.mouth_smile_R, sm, spread); }
else if (sm < 0) { applyShape(disp, SHAPES.mouth_frown_L, -sm, spread); applyShape(disp, SHAPES.mouth_frown_R, -sm, spread); }
applyShape(disp, SHAPES.mouth_round_L, p.round, spread);
applyShape(disp, SHAPES.mouth_round_R, p.round, spread);
applyShape(disp, SHAPES.mouth_round_top, p.round, spread);
applyShape(disp, SHAPES.mouth_round_bot, p.round, spread);
applyShape(disp, SHAPES.mouth_press_top, p.lipPress, spread);
applyShape(disp, SHAPES.mouth_press_bot, p.lipPress, spread);
if (p.width > 0) {
applyShape(disp, SHAPES.mouth_width_L, p.width, spread);
applyShape(disp, SHAPES.mouth_width_R, p.width, spread);
}
// eyes
applyShape(disp, SHAPES.eye_blink_L_up, state.eyeBlinkLeft, spread);
applyShape(disp, SHAPES.eye_blink_L_lo, state.eyeBlinkLeft, spread);
applyShape(disp, SHAPES.eye_blink_R_up, state.eyeBlinkRight, spread);
applyShape(disp, SHAPES.eye_blink_R_lo, state.eyeBlinkRight, spread);
applyShape(disp, SHAPES.eye_squint_L, state.eyeSquint, spread);
applyShape(disp, SHAPES.eye_squint_R, state.eyeSquint, spread);
// brows
applyShape(disp, SHAPES.brow_inner_L, state.browInnerUp, spread);
applyShape(disp, SHAPES.brow_inner_R, state.browInnerUp, spread);
// v011: browOuterUp >= 0 lifts outer brow; < 0 drives brow_down at 0.6x
if (state.browOuterUp >= 0) {
applyShape(disp, SHAPES.brow_outer_L, state.browOuterUp, spread);
applyShape(disp, SHAPES.brow_outer_R, state.browOuterUp, spread);
} else {
applyShape(disp, SHAPES.brow_down_L, -state.browOuterUp * 0.6, spread);
applyShape(disp, SHAPES.brow_down_R, -state.browOuterUp * 0.6, spread);
}
applyShape(disp, SHAPES.brow_down_L, state.browDown, spread);
applyShape(disp, SHAPES.brow_down_R, state.browDown, spread);
// nose
applyShape(disp, SHAPES.nose_sneer_L, state.noseSneer, spread);
applyShape(disp, SHAPES.nose_sneer_R, state.noseSneer, spread);
}
function couple(disp, factor) {
if (factor <= 0) return;
const N = disp.length;
const out = new Array(N);
for (let i = 0; i < N; i++) {
const adj = adjacency[i];
if (adj.length === 0) { out[i] = V(disp[i].x, disp[i].y, disp[i].z); continue; }
let sx = 0, sy = 0, sz = 0;
for (const j of adj) { sx += disp[j].x; sy += disp[j].y; sz += disp[j].z; }
sx /= adj.length; sy /= adj.length; sz /= adj.length;
out[i] = V(
disp[i].x * (1 - factor) + sx * factor,
disp[i].y * (1 - factor) + sy * factor,
disp[i].z * (1 - factor) + sz * factor,
);
}
for (let i = 0; i < N; i++) { disp[i].x = out[i].x; disp[i].y = out[i].y; disp[i].z = out[i].z; }
}
const FEATURE_CENTERS = [
{ name: 'nose', uv: [0.50, 0.50], radiusUV: 0.16, get scale() { return config.scaleNose; } },
{ name: 'eyeL', uv: [0.30, 0.40], radiusUV: 0.11, get scale() { return config.scaleEyes; } },
{ name: 'eyeR', uv: [0.70, 0.40], radiusUV: 0.11, get scale() { return config.scaleEyes; } },
{ name: 'mouth', uv: [0.50, 0.70], radiusUV: 0.18, get scale() { return config.scaleMouth; } },
];
function applyFeatureScales(disp) {
for (const f of FEATURE_CENTERS) {
const s = f.scale;
if (Math.abs(s - 1) < 0.001) continue;
const center = warpToFace(f.uv[0], f.uv[1]);
const r2 = f.radiusUV * f.radiusUV;
const cutoff = r2 * 4;
for (let i = 0; i < baseUVs.length; i++) {
const du = baseUVs[i].u - f.uv[0];
const dv = baseUVs[i].v - f.uv[1];
const d2 = du*du + dv*dv;
if (d2 > cutoff) continue;
const w = Math.exp(-d2 / r2);
const ox = baseVertices[i].x - center.x;
const oy = baseVertices[i].y - center.y;
const oz = baseVertices[i].z - center.z;
const k = (s - 1) * w;
disp[i].x += ox * k; disp[i].y += oy * k; disp[i].z += oz * k;
}
}
}
function applyJawExtension(disp) {
const amt = config.jawExtend;
if (amt < 0.001) return;
const open = state.jawOpen || 0;
if (open < 0.001) return;
const push = open * amt * 0.10;
for (let i = 0; i < baseUVs.length; i++) {
const v = baseUVs[i].v;
if (v < 0.55) continue;
const w = Math.min(1, (v - 0.55) / 0.45);
disp[i].z += push * w;
}
}
function applyMouthGrow(disp) {
const amt = config.mouthGrow;
if (amt < 0.001) return;
const p = computeMouthParams();
const openness = p.open;
if (openness < 0.001) return;
const scale = openness * amt;
const r = 0.25, r2 = r * r;
const cu = 0.50, cv = 0.69;
for (let i = 0; i < baseUVs.length; i++) {
const du = baseUVs[i].u - cu;
const dv = baseUVs[i].v - cv;
const d2 = du*du + dv*dv;
if (d2 > r2) continue;
const w = 1 - Math.sqrt(d2 / r2);
const base = baseVertices[i];
disp[i].x += (base.x - mouthCenterUV.x) * scale * w * 0.40;
disp[i].y += (base.y - mouthCenterUV.y) * scale * w * 0.40;
disp[i].z += scale * w * 0.05;
}
}
/* Static jaw-protrude passes — push the upper-mouth and lower-jaw
regions forward (z+) independent of jawOpen. Used together they
form the snout / animal / crocodile-mouth distortion. Each is a
smooth tent ramp in v-space:
- upper: peaks at v=0.62 (just above the mouth), tapers off by
v=0.45 (top) and v=0.72 (mouth line)
- lower: peaks at v=0.85 (chin tip), tapers off by v=0.70 (mouth)
and v=1.0 (bottom of grid)
Magnitude is `protrude * 0.18` for both — at amt=3 (slider max),
lower jaw juts forward ~0.54 z-units which is comparable to the
nose tip's z=0.22, giving a clearly elongated jaw silhouette. */
function applyUpperJawProtrude(disp) {
const amt = config.upperJawProtrude;
if (amt < 0.001) return;
const push = amt * 0.18;
for (let i = 0; i < baseUVs.length; i++) {
const v = baseUVs[i].v;
if (v < 0.45 || v > 0.72) continue;
// tent: 0 at edges, 1 at v=0.62
const w = v < 0.62 ? (v - 0.45) / 0.17 : (0.72 - v) / 0.10;
disp[i].z += push * w;
}
}
function applyLowerJawProtrude(disp) {
const amt = config.lowerJawProtrude;
if (amt < 0.001) return;
const push = amt * 0.18;
for (let i = 0; i < baseUVs.length; i++) {
const v = baseUVs[i].v;
if (v < 0.70) continue;
// tent: 0 at v=0.70, 1 at v=0.85, tapers to 0.5 at v=1.0
let w;
if (v < 0.85) w = (v - 0.70) / 0.15;
else w = 1 - (v - 0.85) / 0.30; // ramp down past chin tip
if (w < 0) w = 0;
disp[i].z += push * w;
}
}
function applyMouthAnchors(disp) {
if (!config.mouthAnchor) return;
const p = computeMouthParams();
for (const v of mouthRingVerts) {
const target = lipTarget(v.xRel, v.isUpper, p);
const dx = target.x - v.baselineTarget.x;
const dy = target.y - v.baselineTarget.y;
const dz = target.z - v.baselineTarget.z;
const w = v.weight;
disp[v.idx].x = disp[v.idx].x * (1 - w) + dx * w;
disp[v.idx].y = disp[v.idx].y * (1 - w) + dy * w;
disp[v.idx].z = disp[v.idx].z * (1 - w) + dz * w;
}
}
function applyEyeAnchors(disp) {
if (!config.eyeAnchor) return;
const blinkL = state.eyeBlinkLeft, blinkR = state.eyeBlinkRight;
const squint = state.eyeSquint;
for (const v of eyeRingVerts) {
const blink = v.side === 'L' ? blinkL : blinkR;
const target = eyeLidTargetFor(v.xRel, v.isUpper, v.eyeDef, blink, squint);
const dx = target.x - v.baselineTarget.x;
const dy = target.y - v.baselineTarget.y;
const dz = target.z - v.baselineTarget.z;
const w = v.weight;
disp[v.idx].x = disp[v.idx].x * (1 - w) + dx * w;
disp[v.idx].y = disp[v.idx].y * (1 - w) + dy * w;
disp[v.idx].z = disp[v.idx].z * (1 - w) + dz * w;
}
}
function applyFaceFlip() {
// Original semantic: flipFace=true rotates 180° around Y so face's
// nose ends up at -z, which is closer to the default ArcRotateCamera
// at world z=-radius. RH coord system (set on the scene) doesn't
// change ArcRotateCamera's position, only material/winding behavior,
// so the face still NEEDS this rotation in RH.
if (faceRoot) faceRoot.rotation.y = config.flipFace ? Math.PI : 0;
}
function applyMeshVisibility() {
if (mesh) mesh.isVisible = !!config.meshVisible;
}
function applyMinimalVisibility() {
if (minimalGroup) {
minimalGroup.getChildMeshes().forEach(m => m.isVisible = !!config.minimal);
}
}
function applyRenderMode() {
if (!mesh) return;
if (mesh._edgesRenderer) mesh.disableEdgesRendering();
const w = config.lineThickness;
switch (config.mode) {
case 'wire':
mesh.material = wireMat;
wireMat.wireframe = true;
wireMat.alpha = 1.0;
break;
case 'edges':
mesh.material = faceMat;
faceMat.alpha = 1.0;
faceMat.wireframe = false;
mesh.enableEdgesRendering(0.9999);
mesh.edgesWidth = 4 * w;
mesh.edgesColor = new BABYLON.Color4(_baseRGB[0], _baseRGB[1], _baseRGB[2], 1);
break;
case 'shaded':
mesh.material = faceMat;
faceMat.alpha = 1.0;
faceMat.wireframe = false;
break;
}
}
/* ── pupils + iris ── */
let pupilMat = null, pupilL = null, pupilR = null;
let irisMat = null, irisL = null, irisR = null;
function rebuildPupils() {
if (pupilL) { pupilL.dispose(); pupilL = null; }
if (pupilR) { pupilR.dispose(); pupilR = null; }
if (irisL) { irisL.dispose(); irisL = null; }
if (irisR) { irisR.dispose(); irisR = null; }
if (!config.pupils) return;
if (!pupilMat) {
pupilMat = new BABYLON.StandardMaterial('pupilMat', scene);
pupilMat.emissiveColor = BABYLON.Color3.FromHexString(config.pupilColor);
pupilMat.disableLighting = true;
pupilMat.backFaceCulling = false;
}
if (!irisMat) {
irisMat = new BABYLON.StandardMaterial('irisMat', scene);
irisMat.emissiveColor = BABYLON.Color3.FromHexString(config.irisColor);
irisMat.disableLighting = true;
irisMat.backFaceCulling = false;
}
const irisRsize = 0.035 * config.irisSize;
irisL = BABYLON.MeshBuilder.CreateDisc('irisL', { radius: irisRsize, tessellation: 24 }, scene);
irisR = BABYLON.MeshBuilder.CreateDisc('irisR', { radius: irisRsize, tessellation: 24 }, scene);
irisL.material = irisR.material = irisMat;
irisL.parent = irisR.parent = faceRoot;
pupilL = BABYLON.MeshBuilder.CreateDisc('pupilL', { radius: 0.035, tessellation: 14 }, scene);
pupilR = BABYLON.MeshBuilder.CreateDisc('pupilR', { radius: 0.035, tessellation: 14 }, scene);
pupilL.material = pupilR.material = pupilMat;
pupilL.parent = pupilR.parent = faceRoot;
}
function updatePupils() {
if (!pupilL || !pupilR) return;
const lookH = state.eyeLookH * 0.05;
const lookV = state.eyeLookV * 0.035;
const eyeL = warpToFace(0.30, 0.40);
const eyeR = warpToFace(0.70, 0.40);
if (irisL && irisR) {
irisL.position.set(eyeL.x + lookH * 0.6, eyeL.y + lookV * 0.6, eyeL.z + 0.045);
irisR.position.set(eyeR.x + lookH * 0.6, eyeR.y + lookV * 0.6, eyeR.z + 0.045);
}
pupilL.position.set(eyeL.x + lookH, eyeL.y + lookV, eyeL.z + 0.05);
pupilR.position.set(eyeR.x + lookH, eyeR.y + lookV, eyeR.z + 0.05);
const blink = (state.eyeBlinkLeft + state.eyeBlinkRight) * 0.5;
pupilL.isVisible = pupilR.isVisible = blink < 0.85;
if (irisL && irisR) irisL.isVisible = irisR.isVisible = blink < 0.85;
}
/* ── minimal-line overlay ── */
function sampleDispAtUV(u, v) {
if (!_dispBuf) return V(0, 0, 0);
const r2 = 0.06 * 0.06;
let totalW = 0, sx = 0, sy = 0, sz = 0;
for (let i = 0; i < baseUVs.length; i++) {
const du = baseUVs[i].u - u, dv = baseUVs[i].v - v;
const d2 = du*du + dv*dv;
if (d2 < r2) {
const w = 1 - Math.sqrt(d2 / r2);
totalW += w;
sx += _dispBuf[i].x * w; sy += _dispBuf[i].y * w; sz += _dispBuf[i].z * w;
}
}
if (totalW === 0) return V(0, 0, 0);
return V(sx/totalW, sy/totalW, sz/totalW);
}
function deformedPoint(u, v) {
const base = warpToFace(u, v);
if (!config.overlayTracksMesh) return base;
const d = sampleDispAtUV(u, v);
return V(base.x + d.x, base.y + d.y, base.z + d.z);
}
function smoothPath(controls, samples = 18) {
const out = [];
const N = controls.length;
const get = i => controls[Math.max(0, Math.min(N - 1, i))];
for (let i = 0; i < N - 1; i++) {
const p0 = get(i - 1), p1 = get(i), p2 = get(i + 1), p3 = get(i + 2);
for (let s = 0; s < samples; s++) {
const t = s / samples;
const t2 = t*t, t3 = t*t2;
const x = 0.5 * ((2*p1.x) + (-p0.x + p2.x)*t + (2*p0.x - 5*p1.x + 4*p2.x - p3.x)*t2 + (-p0.x + 3*p1.x - 3*p2.x + p3.x)*t3);
const y = 0.5 * ((2*p1.y) + (-p0.y + p2.y)*t + (2*p0.y - 5*p1.y + 4*p2.y - p3.y)*t2 + (-p0.y + 3*p1.y - 3*p2.y + p3.y)*t3);
const z = 0.5 * ((2*p1.z) + (-p0.z + p2.z)*t + (2*p0.z - 5*p1.z + 4*p2.z - p3.z)*t2 + (-p0.z + 3*p1.z - 3*p2.z + p3.z)*t3);
out.push(V(x, y, z));
}
}
out.push(controls[N - 1]);
return out;
}
/* makeTube creates a new tube OR updates an existing one in-place.
Final radius = baseRadius * featureMul * config.lineThickness — applied
in BOTH new and instance forms. Babylon's CreateTube instance-form
applies opts.radius every call (it does not preserve the radius from
construction), so updateMinimal MUST pass the current featureMul each
frame or the tube collapses to (base × 1 × lineThickness). */
function makeTube(name, points, radius, mat, featureMul, instance) {
const r = radius * (featureMul == null ? 1 : featureMul) * config.lineThickness;
const opts = { path: points, radius: r, tessellation: 6, updatable: true };
if (instance) opts.instance = instance;
const tube = BABYLON.MeshBuilder.CreateTube(name, opts, scene);
if (!instance) { tube.material = mat || mouthMat; tube.parent = minimalGroup; }
return tube;
}