@hackbuild/motion-views 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -94,6 +94,12 @@ The stage writes a `.hb-lay-stage`, the card is `.hb-avcard`, the strip `.hb-cli
94
94
  | [`clipStrip`](../../docs/api/motion-views/vanilla.md#clipstrip) | mount the clip strip |
95
95
  | [`defineMotionViews`](../../docs/api/motion-views/vanilla.md#definemotionviews) | define the three custom elements |
96
96
  | [`gestureClip`](../../docs/api/motion-views/vanilla.md#gestureclip) | a gesture written in code as a clip |
97
+ | [`threeStage`](../../docs/api/motion-views/vanilla.md#threestage) | a lit stage that fills an element, ticks and frees itself |
98
+ | [`meshTurntable`](../../docs/api/motion-views/vanilla.md#meshturntable) | one object on a stage you drag to turn, for a relief or a surface |
99
+ | [`stageOrbit`](../../docs/api/motion-views/vanilla.md#stageorbit) | drag to turn a camera round a target, the wheel moves in and out |
100
+ | [`boxDog`](../../docs/api/motion-views/vanilla.md#boxdog) | a dog of bones and boxes with the bone names in `BOX_DOG_BONES` |
101
+ | [`walkBvh`](../../docs/api/motion-views/vanilla.md#walkbvh) | a walk loop as bvh text for another rig, to retarget |
102
+ | [`freeMeshes`](../../docs/api/motion-views/vanilla.md#freemeshes) | free the geometry and materials under an object |
97
103
  | [`CharacterStage`](../../docs/api/motion-views/vue.md#characterstage) | the stage as a Vue component |
98
104
  | [`AvatarDrop`](../../docs/api/motion-views/vue.md#avatardrop) | the drop card as a Vue component |
99
105
  | [`ClipStrip`](../../docs/api/motion-views/vue.md#clipstrip) | the strip as a Vue component |
@@ -15320,10 +15320,579 @@ var HackbuildMotionViews = (function(exports, three) {
15320
15320
  customElements.define("hb-clip-strip", Strip);
15321
15321
  }
15322
15322
  }
15323
+ // src/stage.ts
15324
+ /** A drag orbit on `el` for `camera`: horizontal drag turns, vertical drag tilts, the wheel moves in and out. */
15325
+ function stageOrbit(camera, el) {
15326
+ const target = new three.Vector3();
15327
+ const s = new three.Spherical();
15328
+ let drag = null;
15329
+ const turn = (dTheta, dPhi, scale = 1) => {
15330
+ s.setFromVector3(camera.position.clone().sub(target));
15331
+ s.theta += dTheta;
15332
+ s.phi = Math.min(Math.PI * .49, Math.max(.08, s.phi + dPhi));
15333
+ s.radius = Math.min(200, Math.max(.3, s.radius * scale));
15334
+ camera.position.setFromSpherical(s).add(target);
15335
+ };
15336
+ const down = (e) => {
15337
+ if (!orbit.enabled) return;
15338
+ drag = {
15339
+ x: e.clientX,
15340
+ y: e.clientY,
15341
+ id: e.pointerId
15342
+ };
15343
+ el.setPointerCapture?.(e.pointerId);
15344
+ };
15345
+ const move = (e) => {
15346
+ if (!drag || e.pointerId !== drag.id || !orbit.enabled) return;
15347
+ turn(-(e.clientX - drag.x) * .008, -(e.clientY - drag.y) * .008);
15348
+ drag.x = e.clientX;
15349
+ drag.y = e.clientY;
15350
+ };
15351
+ const up = () => drag = null;
15352
+ const wheel = (e) => {
15353
+ if (!orbit.enabled) return;
15354
+ e.preventDefault();
15355
+ turn(0, 0, Math.exp(e.deltaY * .001));
15356
+ };
15357
+ el.addEventListener("pointerdown", down);
15358
+ el.addEventListener("pointermove", move);
15359
+ el.addEventListener("pointerup", up);
15360
+ el.addEventListener("pointercancel", up);
15361
+ el.addEventListener("wheel", wheel, { passive: false });
15362
+ const orbit = {
15363
+ target,
15364
+ enabled: true,
15365
+ update: () => {
15366
+ if (orbit.enabled) camera.lookAt(target);
15367
+ },
15368
+ dispose() {
15369
+ el.removeEventListener("pointerdown", down);
15370
+ el.removeEventListener("pointermove", move);
15371
+ el.removeEventListener("pointerup", up);
15372
+ el.removeEventListener("pointercancel", up);
15373
+ el.removeEventListener("wheel", wheel);
15374
+ }
15375
+ };
15376
+ return orbit;
15377
+ }
15378
+ /** Frees the geometry and materials of every mesh under `root`. */
15379
+ function freeMeshes(root) {
15380
+ root.traverse((o) => {
15381
+ if (o instanceof three.Mesh) {
15382
+ o.geometry.dispose();
15383
+ for (const m of [o.material].flat()) m.dispose();
15384
+ }
15385
+ });
15386
+ }
15387
+ /**
15388
+ * A stage lit in paper light that fills `host`: it sizes with the host,
15389
+ * calls `tick(dt, t)` each frame (dt capped at 50 ms), draws, and on
15390
+ * dispose stops the loop and frees the renderer, the orbit and every mesh.
15391
+ */
15392
+ function threeStage(host, tick, opts = {}) {
15393
+ const renderer = opts.renderer ?? new three.WebGLRenderer({ antialias: true });
15394
+ renderer.setPixelRatio(Math.min(2, globalThis.devicePixelRatio || 1));
15395
+ renderer.domElement.style.cssText = "display:block;width:100%;height:100%;touch-action:none";
15396
+ host.append(renderer.domElement);
15397
+ const scene = new three.Scene();
15398
+ scene.background = new three.Color("#efe9db");
15399
+ const camera = new three.PerspectiveCamera(40, 1, .05, 200);
15400
+ camera.position.set(2.4, 1.6, 3.2);
15401
+ const orbit = stageOrbit(camera, renderer.domElement);
15402
+ orbit.target.set(0, .5, 0);
15403
+ scene.add(new three.AmbientLight("#ffffff", 1.1));
15404
+ const sun = new three.DirectionalLight("#ffffff", 1.6);
15405
+ sun.position.set(3, 6, 4);
15406
+ scene.add(sun);
15407
+ if (opts.ground !== false) {
15408
+ const g = new three.PlaneGeometry(40, 40);
15409
+ g.rotateX(-Math.PI / 2);
15410
+ scene.add(new three.Mesh(g, new three.MeshStandardMaterial({
15411
+ color: "#d9d0bd",
15412
+ roughness: 1
15413
+ })));
15414
+ }
15415
+ const fit = () => {
15416
+ const w = Math.max(1, host.clientWidth);
15417
+ const h = Math.max(1, host.clientHeight);
15418
+ renderer.setSize(w, h, false);
15419
+ camera.aspect = w / h;
15420
+ camera.updateProjectionMatrix();
15421
+ };
15422
+ const ro = typeof ResizeObserver === "function" ? new ResizeObserver(fit) : null;
15423
+ ro?.observe(host);
15424
+ fit();
15425
+ let raf = 0;
15426
+ let last = performance.now();
15427
+ let t = 0;
15428
+ const frame = (now) => {
15429
+ const dt = Math.min(.05, Math.max(0, (now - last) / 1e3));
15430
+ last = now;
15431
+ t += dt;
15432
+ tick(dt, t);
15433
+ orbit.update();
15434
+ renderer.render(scene, camera);
15435
+ raf = requestAnimationFrame(frame);
15436
+ };
15437
+ raf = requestAnimationFrame(frame);
15438
+ return {
15439
+ scene,
15440
+ camera,
15441
+ renderer,
15442
+ orbit,
15443
+ dispose() {
15444
+ cancelAnimationFrame(raf);
15445
+ ro?.disconnect();
15446
+ orbit.dispose();
15447
+ freeMeshes(scene);
15448
+ renderer.dispose();
15449
+ renderer.domElement.remove();
15450
+ }
15451
+ };
15452
+ }
15453
+ /**
15454
+ * A turntable that fills `host`: hemisphere light, drag to turn, one
15455
+ * object at a time, sized for a relief or a depth surface tens of units
15456
+ * across (the camera starts 80 units out, looking down at 30 degrees).
15457
+ */
15458
+ function meshTurntable(host, opts = {}) {
15459
+ const stage = threeStage(host, () => {}, {
15460
+ ground: false,
15461
+ renderer: opts.renderer ?? new three.WebGLRenderer({
15462
+ antialias: true,
15463
+ preserveDrawingBuffer: true
15464
+ })
15465
+ });
15466
+ stage.camera.position.set(0, -40, 70);
15467
+ stage.camera.near = .1;
15468
+ stage.camera.far = 1e3;
15469
+ stage.camera.updateProjectionMatrix();
15470
+ stage.orbit.target.set(0, 0, 0);
15471
+ stage.scene.add(new three.HemisphereLight(16777215, 9077104, 1.2));
15472
+ let shown = null;
15473
+ return {
15474
+ stage,
15475
+ show(o) {
15476
+ if (shown) {
15477
+ stage.scene.remove(shown);
15478
+ freeMeshes(shown);
15479
+ }
15480
+ shown = o;
15481
+ stage.scene.add(o);
15482
+ },
15483
+ dispose: () => stage.dispose()
15484
+ };
15485
+ }
15486
+ /** The bone names of `boxDog`, in the shape `@hackbuild/gait` reads (hips, spine, tail, four legs). */
15487
+ const BOX_DOG_BONES = {
15488
+ hips: "hips",
15489
+ spine: ["spine2", "chest"],
15490
+ tail: [
15491
+ "tail1",
15492
+ "tail3",
15493
+ "tail_03"
15494
+ ],
15495
+ legs: {
15496
+ LF: {
15497
+ up: "frontUpr.L",
15498
+ mid: "frontLeg.L_03",
15499
+ end: "frontHorselink.L",
15500
+ toes: ["frontToe.L"]
15501
+ },
15502
+ RF: {
15503
+ up: "frontUpr.R",
15504
+ mid: "frontLeg.R_03",
15505
+ end: "frontHorselink.R",
15506
+ toes: ["frontToe.R"]
15507
+ },
15508
+ LH: {
15509
+ up: "rearUpr.L",
15510
+ mid: "rearLwr.L",
15511
+ end: "rearHorselink.L",
15512
+ toes: ["rearFoot.L", "rearToe.L"]
15513
+ },
15514
+ RH: {
15515
+ up: "rearUpr.R",
15516
+ mid: "rearLwr.R",
15517
+ end: "rearHorselink.R",
15518
+ toes: ["rearFoot.R", "rearToe.R"]
15519
+ }
15520
+ }
15521
+ };
15522
+ /**
15523
+ * A dog of bones and boxes facing +z, about 0.78 m nose to tail with the
15524
+ * hips 0.38 m up; bone names are `BOX_DOG_BONES`. Each bone carries a box
15525
+ * to its child, so the legs show what a gait does to them.
15526
+ */
15527
+ function boxDog(color = "#c9a36b") {
15528
+ const root = new three.Group();
15529
+ const bone = (name, parent, p) => {
15530
+ const b = new three.Bone();
15531
+ b.name = name;
15532
+ b.position.set(...p);
15533
+ parent.add(b);
15534
+ return b;
15535
+ };
15536
+ const hips = bone("hips", root, [
15537
+ 0,
15538
+ .38,
15539
+ 0
15540
+ ]);
15541
+ const chest = bone("chest", bone("spine2", hips, [
15542
+ 0,
15543
+ 0,
15544
+ .1
15545
+ ]), [
15546
+ 0,
15547
+ 0,
15548
+ .12
15549
+ ]);
15550
+ const head = bone("head", bone("neck1", chest, [
15551
+ 0,
15552
+ .08,
15553
+ .05
15554
+ ]), [
15555
+ 0,
15556
+ .08,
15557
+ .06
15558
+ ]);
15559
+ for (const [s, x] of [["L", .09], ["R", -.09]]) {
15560
+ const fe = bone(`frontHorselink.${s}`, bone(`frontLeg.${s}_03`, bone(`frontUpr.${s}`, chest, [
15561
+ x,
15562
+ 0,
15563
+ 0
15564
+ ]), [
15565
+ 0,
15566
+ -.18,
15567
+ -.08
15568
+ ]), [
15569
+ 0,
15570
+ -.16,
15571
+ .08
15572
+ ]);
15573
+ bone(`frontToe.${s}`, fe, [
15574
+ 0,
15575
+ -.02,
15576
+ .03
15577
+ ]);
15578
+ const re = bone(`rearHorselink.${s}`, bone(`rearLwr.${s}`, bone(`rearUpr.${s}`, hips, [
15579
+ x,
15580
+ 0,
15581
+ -.2
15582
+ ]), [
15583
+ 0,
15584
+ -.17,
15585
+ .07
15586
+ ]), [
15587
+ 0,
15588
+ -.15,
15589
+ -.08
15590
+ ]);
15591
+ bone(`rearToe.${s}`, bone(`rearFoot.${s}`, re, [
15592
+ 0,
15593
+ -.02,
15594
+ .01
15595
+ ]), [
15596
+ 0,
15597
+ -.02,
15598
+ .03
15599
+ ]);
15600
+ }
15601
+ bone("tail_03", bone("tail3", bone("tail1", hips, [
15602
+ 0,
15603
+ .02,
15604
+ -.25
15605
+ ]), [
15606
+ 0,
15607
+ 0,
15608
+ -.08
15609
+ ]), [
15610
+ 0,
15611
+ 0,
15612
+ -.08
15613
+ ]);
15614
+ const mat = new three.MeshStandardMaterial({
15615
+ color,
15616
+ roughness: .7
15617
+ });
15618
+ const Z = new three.Vector3(0, 0, 1);
15619
+ const parts = [];
15620
+ root.traverse((o) => {
15621
+ if (!(o instanceof three.Bone)) return;
15622
+ for (const c of o.children) {
15623
+ if (!(c instanceof three.Bone) || c.position.length() < 1e-4) continue;
15624
+ const thick = c.name === "spine2" || c.name === "chest" ? .17 : /Upr/.test(c.name) ? .07 : /tail/.test(c.name) ? .03 : .045;
15625
+ const m = new three.Mesh(new three.BoxGeometry(thick, thick, c.position.length()), mat);
15626
+ m.position.copy(c.position).multiplyScalar(.5);
15627
+ m.quaternion.setFromUnitVectors(Z, c.position.clone().normalize());
15628
+ parts.push([o, m]);
15629
+ }
15630
+ });
15631
+ for (const [o, m] of parts) o.add(m);
15632
+ const skull = new three.Mesh(new three.BoxGeometry(.11, .1, .18), mat);
15633
+ skull.position.set(0, .02, .05);
15634
+ head.add(skull);
15635
+ return root;
15636
+ }
15637
+ /**
15638
+ * A two step walk loop as bvh text for a humanoid that is not ours: other
15639
+ * bone names (Hips, LeftUpLeg, RightForeArm and so on), centimetres, arms
15640
+ * down, ZXY channels. A retarget has real work to do on it.
15641
+ */
15642
+ function walkBvh(o = {}) {
15643
+ const { seconds = 2.4, fps = 30, swing = 1 } = o;
15644
+ const J = [
15645
+ [
15646
+ "Hips",
15647
+ -1,
15648
+ [
15649
+ 0,
15650
+ 0,
15651
+ 0
15652
+ ]
15653
+ ],
15654
+ [
15655
+ "Spine",
15656
+ 0,
15657
+ [
15658
+ 0,
15659
+ 12,
15660
+ 0
15661
+ ]
15662
+ ],
15663
+ [
15664
+ "Spine1",
15665
+ 1,
15666
+ [
15667
+ 0,
15668
+ 16,
15669
+ 0
15670
+ ]
15671
+ ],
15672
+ [
15673
+ "Neck",
15674
+ 2,
15675
+ [
15676
+ 0,
15677
+ 26,
15678
+ 0
15679
+ ]
15680
+ ],
15681
+ [
15682
+ "Head",
15683
+ 3,
15684
+ [
15685
+ 0,
15686
+ 11,
15687
+ 0
15688
+ ]
15689
+ ],
15690
+ [
15691
+ "LeftArm",
15692
+ 2,
15693
+ [
15694
+ 18,
15695
+ 22,
15696
+ 0
15697
+ ]
15698
+ ],
15699
+ [
15700
+ "LeftForeArm",
15701
+ 5,
15702
+ [
15703
+ 29,
15704
+ 0,
15705
+ 0
15706
+ ]
15707
+ ],
15708
+ [
15709
+ "LeftHand",
15710
+ 6,
15711
+ [
15712
+ 26,
15713
+ 0,
15714
+ 0
15715
+ ]
15716
+ ],
15717
+ [
15718
+ "RightArm",
15719
+ 2,
15720
+ [
15721
+ -18,
15722
+ 22,
15723
+ 0
15724
+ ]
15725
+ ],
15726
+ [
15727
+ "RightForeArm",
15728
+ 8,
15729
+ [
15730
+ -29,
15731
+ 0,
15732
+ 0
15733
+ ]
15734
+ ],
15735
+ [
15736
+ "RightHand",
15737
+ 9,
15738
+ [
15739
+ -26,
15740
+ 0,
15741
+ 0
15742
+ ]
15743
+ ],
15744
+ [
15745
+ "LeftUpLeg",
15746
+ 0,
15747
+ [
15748
+ 10,
15749
+ -6,
15750
+ 0
15751
+ ]
15752
+ ],
15753
+ [
15754
+ "LeftLeg",
15755
+ 11,
15756
+ [
15757
+ 0,
15758
+ -47,
15759
+ 0
15760
+ ]
15761
+ ],
15762
+ [
15763
+ "LeftFoot",
15764
+ 12,
15765
+ [
15766
+ 0,
15767
+ -45,
15768
+ 0
15769
+ ]
15770
+ ],
15771
+ [
15772
+ "LeftToeBase",
15773
+ 13,
15774
+ [
15775
+ 0,
15776
+ -8,
15777
+ 14
15778
+ ]
15779
+ ],
15780
+ [
15781
+ "RightUpLeg",
15782
+ 0,
15783
+ [
15784
+ -10,
15785
+ -6,
15786
+ 0
15787
+ ]
15788
+ ],
15789
+ [
15790
+ "RightLeg",
15791
+ 15,
15792
+ [
15793
+ 0,
15794
+ -47,
15795
+ 0
15796
+ ]
15797
+ ],
15798
+ [
15799
+ "RightFoot",
15800
+ 16,
15801
+ [
15802
+ 0,
15803
+ -45,
15804
+ 0
15805
+ ]
15806
+ ],
15807
+ [
15808
+ "RightToeBase",
15809
+ 17,
15810
+ [
15811
+ 0,
15812
+ -8,
15813
+ 14
15814
+ ]
15815
+ ]
15816
+ ];
15817
+ const kids = (i) => J.map((j, k) => [j, k]).filter(([j]) => j[1] === i).map(([, k]) => k);
15818
+ const order = [];
15819
+ const write = (i, pad) => {
15820
+ order.push(i);
15821
+ const [name, parent, off] = J[i];
15822
+ const ch = parent < 0 ? "CHANNELS 6 Xposition Yposition Zposition Zrotation Xrotation Yrotation" : "CHANNELS 3 Zrotation Xrotation Yrotation";
15823
+ const c = kids(i);
15824
+ const body = c.length ? c.map((k) => write(k, pad + " ")).join("") : `${pad} End Site\n${pad} {\n${pad} OFFSET 0 6 0\n${pad} }\n`;
15825
+ return `${pad}${parent < 0 ? "ROOT" : "JOINT"} ${name}\n${pad}{\n${pad} OFFSET ${off.join(" ")}\n${pad} ${ch}\n${body}${pad}}\n`;
15826
+ };
15827
+ const tree = write(0, "");
15828
+ const n = Math.round(seconds * fps);
15829
+ const lines = [];
15830
+ for (let f = 0; f < n; f++) {
15831
+ const ph = f / fps * 2 * Math.PI * (1 / seconds) * 2;
15832
+ const s = Math.sin(ph) * swing;
15833
+ const rot = {
15834
+ LeftUpLeg: [
15835
+ 0,
15836
+ -28 * s,
15837
+ 0
15838
+ ],
15839
+ RightUpLeg: [
15840
+ 0,
15841
+ 28 * s,
15842
+ 0
15843
+ ],
15844
+ LeftLeg: [
15845
+ 0,
15846
+ 45 * Math.max(0, Math.sin(ph + 1.4)) * swing,
15847
+ 0
15848
+ ],
15849
+ RightLeg: [
15850
+ 0,
15851
+ 45 * Math.max(0, Math.sin(ph + 1.4 + Math.PI)) * swing,
15852
+ 0
15853
+ ],
15854
+ LeftArm: [
15855
+ -70,
15856
+ 22 * s,
15857
+ 0
15858
+ ],
15859
+ RightArm: [
15860
+ 70,
15861
+ -22 * s,
15862
+ 0
15863
+ ],
15864
+ LeftForeArm: [
15865
+ 0,
15866
+ 0,
15867
+ -15
15868
+ ],
15869
+ RightForeArm: [
15870
+ 0,
15871
+ 0,
15872
+ 15
15873
+ ]
15874
+ };
15875
+ lines.push(order.map((i) => (i === 0 ? [
15876
+ 0,
15877
+ 106 + 2.5 * Math.cos(2 * ph),
15878
+ 0,
15879
+ 2 * s,
15880
+ 0,
15881
+ 3 * s
15882
+ ] : rot[J[i][0]] ?? [
15883
+ 0,
15884
+ 0,
15885
+ 0
15886
+ ]).map((v) => v.toFixed(3)).join(" ")).join(" "));
15887
+ }
15888
+ return `HIERARCHY\n${tree}MOTION\nFrames: ${n}\nFrame Time: ${(1 / fps).toFixed(6)}\n${lines.join("\n")}\n`;
15889
+ }
15890
+ exports.BOX_DOG_BONES = BOX_DOG_BONES;
15323
15891
  exports.DEFAULT_VERBS = DEFAULT_VERBS;
15324
15892
  exports.GESTURES = GESTURES;
15325
15893
  exports.animationClipOf = animationClipOf;
15326
15894
  exports.avatarDrop = avatarDrop;
15895
+ exports.boxDog = boxDog;
15327
15896
  exports.bytesWord = bytesWord;
15328
15897
  exports.characterStage = characterStage;
15329
15898
  exports.clipFromFindings = clipFromFindings;
@@ -15333,12 +15902,17 @@ var HackbuildMotionViews = (function(exports, three) {
15333
15902
  exports.clipsFromPack = clipsFromPack;
15334
15903
  exports.defineMotionViews = defineMotionViews;
15335
15904
  exports.estimateSeconds = estimateSeconds;
15905
+ exports.freeMeshes = freeMeshes;
15336
15906
  exports.gestureClip = gestureClip;
15337
15907
  exports.licenceLine = licenceLine;
15908
+ exports.meshTurntable = meshTurntable;
15338
15909
  exports.nextClip = nextClip;
15339
15910
  exports.rigCheck = rigCheck;
15340
15911
  exports.secondsWord = secondsWord;
15341
15912
  exports.sizeLine = sizeLine;
15342
15913
  exports.speechEvents = speechEvents;
15914
+ exports.stageOrbit = stageOrbit;
15915
+ exports.threeStage = threeStage;
15916
+ exports.walkBvh = walkBvh;
15343
15917
  return exports;
15344
15918
  })({}, THREE);
@@ -0,0 +1,104 @@
1
+ import { Group, PerspectiveCamera, Scene, Vector3, WebGLRenderer, type Object3D } from "three";
2
+ /** Drag to turn the camera round `target`, wheel or pinch to move in and out. */
3
+ export interface StageOrbit {
4
+ /** The point the camera looks at and turns round. */
5
+ readonly target: Vector3;
6
+ /** Off, the pointer and update leave the camera alone (a page that drives it itself). */
7
+ enabled: boolean;
8
+ /** Points the camera at the target while enabled; the stage calls it each frame. */
9
+ update(): void;
10
+ dispose(): void;
11
+ }
12
+ /** A drag orbit on `el` for `camera`: horizontal drag turns, vertical drag tilts, the wheel moves in and out. */
13
+ export declare function stageOrbit(camera: PerspectiveCamera, el: HTMLElement): StageOrbit;
14
+ /** Frees the geometry and materials of every mesh under `root`. */
15
+ export declare function freeMeshes(root: Object3D): void;
16
+ /** A running stage: its scene, camera, renderer and orbit, and how to stop it. */
17
+ export interface ThreeStage {
18
+ scene: Scene;
19
+ camera: PerspectiveCamera;
20
+ renderer: WebGLRenderer;
21
+ /** The drag orbit; set `orbit.enabled = false` to drive the camera from the page. */
22
+ orbit: StageOrbit;
23
+ dispose(): void;
24
+ }
25
+ /** Options of `threeStage`. */
26
+ export interface ThreeStageOptions {
27
+ /** A paper coloured ground plane, 40 m square. Default on. */
28
+ ground?: boolean;
29
+ /** Pass a renderer the page already made; the stage still sizes and frees it. */
30
+ renderer?: WebGLRenderer;
31
+ }
32
+ /**
33
+ * A stage lit in paper light that fills `host`: it sizes with the host,
34
+ * calls `tick(dt, t)` each frame (dt capped at 50 ms), draws, and on
35
+ * dispose stops the loop and frees the renderer, the orbit and every mesh.
36
+ */
37
+ export declare function threeStage(host: HTMLElement, tick: (dt: number, t: number) => void, opts?: ThreeStageOptions): ThreeStage;
38
+ /** A running turntable: its stage, what it shows, and how to stop it. */
39
+ export interface MeshTurntable {
40
+ stage: ThreeStage;
41
+ /** Shows `o` in place of what was shown; the old one's meshes are freed. */
42
+ show(o: Object3D): void;
43
+ dispose(): void;
44
+ }
45
+ /**
46
+ * A turntable that fills `host`: hemisphere light, drag to turn, one
47
+ * object at a time, sized for a relief or a depth surface tens of units
48
+ * across (the camera starts 80 units out, looking down at 30 degrees).
49
+ */
50
+ export declare function meshTurntable(host: HTMLElement, opts?: {
51
+ renderer?: WebGLRenderer;
52
+ }): MeshTurntable;
53
+ /** The bone names of `boxDog`, in the shape `@hackbuild/gait` reads (hips, spine, tail, four legs). */
54
+ export declare const BOX_DOG_BONES: {
55
+ readonly hips: "hips";
56
+ readonly spine: readonly ["spine2", "chest"];
57
+ readonly tail: readonly ["tail1", "tail3", "tail_03"];
58
+ readonly legs: {
59
+ readonly LF: {
60
+ readonly up: "frontUpr.L";
61
+ readonly mid: "frontLeg.L_03";
62
+ readonly end: "frontHorselink.L";
63
+ readonly toes: readonly ["frontToe.L"];
64
+ };
65
+ readonly RF: {
66
+ readonly up: "frontUpr.R";
67
+ readonly mid: "frontLeg.R_03";
68
+ readonly end: "frontHorselink.R";
69
+ readonly toes: readonly ["frontToe.R"];
70
+ };
71
+ readonly LH: {
72
+ readonly up: "rearUpr.L";
73
+ readonly mid: "rearLwr.L";
74
+ readonly end: "rearHorselink.L";
75
+ readonly toes: readonly ["rearFoot.L", "rearToe.L"];
76
+ };
77
+ readonly RH: {
78
+ readonly up: "rearUpr.R";
79
+ readonly mid: "rearLwr.R";
80
+ readonly end: "rearHorselink.R";
81
+ readonly toes: readonly ["rearFoot.R", "rearToe.R"];
82
+ };
83
+ };
84
+ };
85
+ /**
86
+ * A dog of bones and boxes facing +z, about 0.78 m nose to tail with the
87
+ * hips 0.38 m up; bone names are `BOX_DOG_BONES`. Each bone carries a box
88
+ * to its child, so the legs show what a gait does to them.
89
+ */
90
+ export declare function boxDog(color?: string): Group;
91
+ /** Options of `walkBvh`. */
92
+ export interface WalkBvhOptions {
93
+ /** Loop length in seconds. Default 2.4. */
94
+ seconds?: number;
95
+ fps?: number;
96
+ /** Stride scale: 0.6 strolls, 1.5 marches. Default 1. */
97
+ swing?: number;
98
+ }
99
+ /**
100
+ * A two step walk loop as bvh text for a humanoid that is not ours: other
101
+ * bone names (Hips, LeftUpLeg, RightForeArm and so on), centimetres, arms
102
+ * down, ZXY channels. A retarget has real work to do on it.
103
+ */
104
+ export declare function walkBvh(o?: WalkBvhOptions): string;
package/dist/stage.js ADDED
@@ -0,0 +1,256 @@
1
+ // Small three.js stages for pages and examples: a lit stage that fills a
2
+ // host element and ticks, a turntable that shows one object at a time, a
3
+ // drag orbit both share, a dog of bones and boxes, and a walk written as
4
+ // bvh text for another rig. Everything is built in code; no model, clip or
5
+ // weight file. Each stage frees its renderer, geometry and materials on
6
+ // dispose.
7
+ import { AmbientLight, Bone, BoxGeometry, Color, DirectionalLight, Group, HemisphereLight, Mesh, MeshStandardMaterial, PerspectiveCamera, PlaneGeometry, Scene, Spherical, Vector3, WebGLRenderer, } from "three";
8
+ /** A drag orbit on `el` for `camera`: horizontal drag turns, vertical drag tilts, the wheel moves in and out. */
9
+ export function stageOrbit(camera, el) {
10
+ const target = new Vector3();
11
+ const s = new Spherical();
12
+ let drag = null;
13
+ const turn = (dTheta, dPhi, scale = 1) => {
14
+ s.setFromVector3(camera.position.clone().sub(target));
15
+ s.theta += dTheta;
16
+ s.phi = Math.min(Math.PI * 0.49, Math.max(0.08, s.phi + dPhi));
17
+ s.radius = Math.min(200, Math.max(0.3, s.radius * scale));
18
+ camera.position.setFromSpherical(s).add(target);
19
+ };
20
+ const down = (e) => {
21
+ if (!orbit.enabled)
22
+ return;
23
+ drag = { x: e.clientX, y: e.clientY, id: e.pointerId };
24
+ el.setPointerCapture?.(e.pointerId);
25
+ };
26
+ const move = (e) => {
27
+ if (!drag || e.pointerId !== drag.id || !orbit.enabled)
28
+ return;
29
+ turn(-(e.clientX - drag.x) * 0.008, -(e.clientY - drag.y) * 0.008);
30
+ drag.x = e.clientX;
31
+ drag.y = e.clientY;
32
+ };
33
+ const up = () => (drag = null);
34
+ const wheel = (e) => {
35
+ if (!orbit.enabled)
36
+ return;
37
+ e.preventDefault();
38
+ turn(0, 0, Math.exp(e.deltaY * 0.001));
39
+ };
40
+ el.addEventListener("pointerdown", down);
41
+ el.addEventListener("pointermove", move);
42
+ el.addEventListener("pointerup", up);
43
+ el.addEventListener("pointercancel", up);
44
+ el.addEventListener("wheel", wheel, { passive: false });
45
+ const orbit = {
46
+ target,
47
+ enabled: true,
48
+ update: () => { if (orbit.enabled)
49
+ camera.lookAt(target); },
50
+ dispose() {
51
+ el.removeEventListener("pointerdown", down);
52
+ el.removeEventListener("pointermove", move);
53
+ el.removeEventListener("pointerup", up);
54
+ el.removeEventListener("pointercancel", up);
55
+ el.removeEventListener("wheel", wheel);
56
+ },
57
+ };
58
+ return orbit;
59
+ }
60
+ /** Frees the geometry and materials of every mesh under `root`. */
61
+ export function freeMeshes(root) {
62
+ root.traverse((o) => {
63
+ if (o instanceof Mesh) {
64
+ o.geometry.dispose();
65
+ for (const m of [o.material].flat())
66
+ m.dispose();
67
+ }
68
+ });
69
+ }
70
+ /**
71
+ * A stage lit in paper light that fills `host`: it sizes with the host,
72
+ * calls `tick(dt, t)` each frame (dt capped at 50 ms), draws, and on
73
+ * dispose stops the loop and frees the renderer, the orbit and every mesh.
74
+ */
75
+ export function threeStage(host, tick, opts = {}) {
76
+ const renderer = opts.renderer ?? new WebGLRenderer({ antialias: true });
77
+ renderer.setPixelRatio(Math.min(2, globalThis.devicePixelRatio || 1));
78
+ renderer.domElement.style.cssText = "display:block;width:100%;height:100%;touch-action:none";
79
+ host.append(renderer.domElement);
80
+ const scene = new Scene();
81
+ scene.background = new Color("#efe9db");
82
+ const camera = new PerspectiveCamera(40, 1, 0.05, 200);
83
+ camera.position.set(2.4, 1.6, 3.2);
84
+ const orbit = stageOrbit(camera, renderer.domElement);
85
+ orbit.target.set(0, 0.5, 0);
86
+ scene.add(new AmbientLight("#ffffff", 1.1));
87
+ const sun = new DirectionalLight("#ffffff", 1.6);
88
+ sun.position.set(3, 6, 4);
89
+ scene.add(sun);
90
+ if (opts.ground !== false) {
91
+ const g = new PlaneGeometry(40, 40);
92
+ g.rotateX(-Math.PI / 2);
93
+ scene.add(new Mesh(g, new MeshStandardMaterial({ color: "#d9d0bd", roughness: 1 })));
94
+ }
95
+ const fit = () => {
96
+ const w = Math.max(1, host.clientWidth);
97
+ const h = Math.max(1, host.clientHeight);
98
+ renderer.setSize(w, h, false);
99
+ camera.aspect = w / h;
100
+ camera.updateProjectionMatrix();
101
+ };
102
+ const ro = typeof ResizeObserver === "function" ? new ResizeObserver(fit) : null;
103
+ ro?.observe(host);
104
+ fit();
105
+ let raf = 0;
106
+ let last = performance.now();
107
+ let t = 0;
108
+ const frame = (now) => {
109
+ const dt = Math.min(0.05, Math.max(0, (now - last) / 1000));
110
+ last = now;
111
+ t += dt;
112
+ tick(dt, t);
113
+ orbit.update();
114
+ renderer.render(scene, camera);
115
+ raf = requestAnimationFrame(frame);
116
+ };
117
+ raf = requestAnimationFrame(frame);
118
+ return {
119
+ scene, camera, renderer, orbit,
120
+ dispose() {
121
+ cancelAnimationFrame(raf);
122
+ ro?.disconnect();
123
+ orbit.dispose();
124
+ freeMeshes(scene);
125
+ renderer.dispose();
126
+ renderer.domElement.remove();
127
+ },
128
+ };
129
+ }
130
+ /**
131
+ * A turntable that fills `host`: hemisphere light, drag to turn, one
132
+ * object at a time, sized for a relief or a depth surface tens of units
133
+ * across (the camera starts 80 units out, looking down at 30 degrees).
134
+ */
135
+ export function meshTurntable(host, opts = {}) {
136
+ const renderer = opts.renderer ?? new WebGLRenderer({ antialias: true, preserveDrawingBuffer: true });
137
+ const stage = threeStage(host, () => { }, { ground: false, renderer });
138
+ stage.camera.position.set(0, -40, 70);
139
+ stage.camera.near = 0.1;
140
+ stage.camera.far = 1000;
141
+ stage.camera.updateProjectionMatrix();
142
+ stage.orbit.target.set(0, 0, 0);
143
+ stage.scene.add(new HemisphereLight(0xffffff, 0x8a8170, 1.2));
144
+ let shown = null;
145
+ return {
146
+ stage,
147
+ show(o) {
148
+ if (shown) {
149
+ stage.scene.remove(shown);
150
+ freeMeshes(shown);
151
+ }
152
+ shown = o;
153
+ stage.scene.add(o);
154
+ },
155
+ dispose: () => stage.dispose(),
156
+ };
157
+ }
158
+ /** The bone names of `boxDog`, in the shape `@hackbuild/gait` reads (hips, spine, tail, four legs). */
159
+ export const BOX_DOG_BONES = {
160
+ hips: "hips",
161
+ spine: ["spine2", "chest"],
162
+ tail: ["tail1", "tail3", "tail_03"],
163
+ legs: {
164
+ LF: { up: "frontUpr.L", mid: "frontLeg.L_03", end: "frontHorselink.L", toes: ["frontToe.L"] },
165
+ RF: { up: "frontUpr.R", mid: "frontLeg.R_03", end: "frontHorselink.R", toes: ["frontToe.R"] },
166
+ LH: { up: "rearUpr.L", mid: "rearLwr.L", end: "rearHorselink.L", toes: ["rearFoot.L", "rearToe.L"] },
167
+ RH: { up: "rearUpr.R", mid: "rearLwr.R", end: "rearHorselink.R", toes: ["rearFoot.R", "rearToe.R"] },
168
+ },
169
+ };
170
+ /**
171
+ * A dog of bones and boxes facing +z, about 0.78 m nose to tail with the
172
+ * hips 0.38 m up; bone names are `BOX_DOG_BONES`. Each bone carries a box
173
+ * to its child, so the legs show what a gait does to them.
174
+ */
175
+ export function boxDog(color = "#c9a36b") {
176
+ const root = new Group();
177
+ const bone = (name, parent, p) => {
178
+ const b = new Bone();
179
+ b.name = name;
180
+ b.position.set(...p);
181
+ parent.add(b);
182
+ return b;
183
+ };
184
+ const hips = bone("hips", root, [0, 0.38, 0]);
185
+ const spine = bone("spine2", hips, [0, 0, 0.1]);
186
+ const chest = bone("chest", spine, [0, 0, 0.12]);
187
+ const head = bone("head", bone("neck1", chest, [0, 0.08, 0.05]), [0, 0.08, 0.06]);
188
+ for (const [s, x] of [["L", 0.09], ["R", -0.09]]) {
189
+ const fe = bone(`frontHorselink.${s}`, bone(`frontLeg.${s}_03`, bone(`frontUpr.${s}`, chest, [x, 0, 0]), [0, -0.18, -0.08]), [0, -0.16, 0.08]);
190
+ bone(`frontToe.${s}`, fe, [0, -0.02, 0.03]);
191
+ const re = bone(`rearHorselink.${s}`, bone(`rearLwr.${s}`, bone(`rearUpr.${s}`, hips, [x, 0, -0.2]), [0, -0.17, 0.07]), [0, -0.15, -0.08]);
192
+ bone(`rearToe.${s}`, bone(`rearFoot.${s}`, re, [0, -0.02, 0.01]), [0, -0.02, 0.03]);
193
+ }
194
+ bone("tail_03", bone("tail3", bone("tail1", hips, [0, 0.02, -0.25]), [0, 0, -0.08]), [0, 0, -0.08]);
195
+ const mat = new MeshStandardMaterial({ color, roughness: 0.7 });
196
+ const Z = new Vector3(0, 0, 1);
197
+ const parts = [];
198
+ root.traverse((o) => {
199
+ if (!(o instanceof Bone))
200
+ return;
201
+ for (const c of o.children) {
202
+ if (!(c instanceof Bone) || c.position.length() < 1e-4)
203
+ continue;
204
+ const thick = c.name === "spine2" || c.name === "chest" ? 0.17 : /Upr/.test(c.name) ? 0.07 : /tail/.test(c.name) ? 0.03 : 0.045;
205
+ const m = new Mesh(new BoxGeometry(thick, thick, c.position.length()), mat);
206
+ m.position.copy(c.position).multiplyScalar(0.5);
207
+ m.quaternion.setFromUnitVectors(Z, c.position.clone().normalize());
208
+ parts.push([o, m]);
209
+ }
210
+ });
211
+ for (const [o, m] of parts)
212
+ o.add(m);
213
+ const skull = new Mesh(new BoxGeometry(0.11, 0.1, 0.18), mat);
214
+ skull.position.set(0, 0.02, 0.05);
215
+ head.add(skull);
216
+ return root;
217
+ }
218
+ /**
219
+ * A two step walk loop as bvh text for a humanoid that is not ours: other
220
+ * bone names (Hips, LeftUpLeg, RightForeArm and so on), centimetres, arms
221
+ * down, ZXY channels. A retarget has real work to do on it.
222
+ */
223
+ export function walkBvh(o = {}) {
224
+ const { seconds = 2.4, fps = 30, swing = 1 } = o;
225
+ const J = [
226
+ ["Hips", -1, [0, 0, 0]], ["Spine", 0, [0, 12, 0]], ["Spine1", 1, [0, 16, 0]], ["Neck", 2, [0, 26, 0]], ["Head", 3, [0, 11, 0]],
227
+ ["LeftArm", 2, [18, 22, 0]], ["LeftForeArm", 5, [29, 0, 0]], ["LeftHand", 6, [26, 0, 0]],
228
+ ["RightArm", 2, [-18, 22, 0]], ["RightForeArm", 8, [-29, 0, 0]], ["RightHand", 9, [-26, 0, 0]],
229
+ ["LeftUpLeg", 0, [10, -6, 0]], ["LeftLeg", 11, [0, -47, 0]], ["LeftFoot", 12, [0, -45, 0]], ["LeftToeBase", 13, [0, -8, 14]],
230
+ ["RightUpLeg", 0, [-10, -6, 0]], ["RightLeg", 15, [0, -47, 0]], ["RightFoot", 16, [0, -45, 0]], ["RightToeBase", 17, [0, -8, 14]],
231
+ ];
232
+ const kids = (i) => J.map((j, k) => [j, k]).filter(([j]) => j[1] === i).map(([, k]) => k);
233
+ const order = [];
234
+ const write = (i, pad) => {
235
+ order.push(i);
236
+ const [name, parent, off] = J[i];
237
+ const ch = parent < 0 ? "CHANNELS 6 Xposition Yposition Zposition Zrotation Xrotation Yrotation" : "CHANNELS 3 Zrotation Xrotation Yrotation";
238
+ const c = kids(i);
239
+ const body = c.length ? c.map((k) => write(k, pad + " ")).join("") : `${pad} End Site\n${pad} {\n${pad} OFFSET 0 6 0\n${pad} }\n`;
240
+ return `${pad}${parent < 0 ? "ROOT" : "JOINT"} ${name}\n${pad}{\n${pad} OFFSET ${off.join(" ")}\n${pad} ${ch}\n${body}${pad}}\n`;
241
+ };
242
+ const tree = write(0, "");
243
+ const n = Math.round(seconds * fps);
244
+ const lines = [];
245
+ for (let f = 0; f < n; f++) {
246
+ const ph = (f / fps) * 2 * Math.PI * (1 / seconds) * 2;
247
+ const s = Math.sin(ph) * swing;
248
+ const rot = {
249
+ LeftUpLeg: [0, -28 * s, 0], RightUpLeg: [0, 28 * s, 0],
250
+ LeftLeg: [0, 45 * Math.max(0, Math.sin(ph + 1.4)) * swing, 0], RightLeg: [0, 45 * Math.max(0, Math.sin(ph + 1.4 + Math.PI)) * swing, 0],
251
+ LeftArm: [-70, 22 * s, 0], RightArm: [70, -22 * s, 0], LeftForeArm: [0, 0, -15], RightForeArm: [0, 0, 15],
252
+ };
253
+ lines.push(order.map((i) => (i === 0 ? [0, 106 + 2.5 * Math.cos(2 * ph), 0, 2 * s, 0, 3 * s] : rot[J[i][0]] ?? [0, 0, 0]).map((v) => v.toFixed(3)).join(" ")).join(" "));
254
+ }
255
+ return `HIERARCHY\n${tree}MOTION\nFrames: ${n}\nFrame Time: ${(1 / fps).toFixed(6)}\n${lines.join("\n")}\n`;
256
+ }
package/dist/vanilla.d.ts CHANGED
@@ -4,3 +4,4 @@ export * from "./avatar-drop.js";
4
4
  export * from "./clip-strip.js";
5
5
  export * from "./gestures.js";
6
6
  export * from "./elements.js";
7
+ export * from "./stage.js";
package/dist/vanilla.js CHANGED
@@ -1,9 +1,11 @@
1
1
  // @hackbuild/motion-views/vanilla: CharacterStage, AvatarDrop and ClipStrip
2
2
  // as framework free views over hackbuild-ui classes, and as custom
3
- // elements once defineMotionViews() runs. Importing defines nothing.
3
+ // elements once defineMotionViews() runs, and the small stages (threeStage,
4
+ // meshTurntable, boxDog, walkBvh). Importing defines nothing.
4
5
  export * from "./index.js";
5
6
  export * from "./character-stage.js";
6
7
  export * from "./avatar-drop.js";
7
8
  export * from "./clip-strip.js";
8
9
  export * from "./gestures.js";
9
10
  export * from "./elements.js";
11
+ export * from "./stage.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackbuild/motion-views",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "The motion views: CharacterStage, AvatarDrop and ClipStrip, binding the motion packages to hackbuild-ui, each as a Vue component and a custom element.",
5
5
  "type": "module",
6
6
  "license": "MIT",