@hackbuild/motion-views 0.0.0-stage → 0.1.1

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/dist/dom.js ADDED
@@ -0,0 +1,52 @@
1
+ // Small DOM helpers the views share. Every class comes from hackbuild-ui;
2
+ // the views carry no styles of their own.
3
+ import { strokeIcon } from "@virgilvox/hackbuild-ui/vanilla";
4
+ /** An element with classes, attributes and children. A false or null attribute is left off. */
5
+ export function el(doc, tag, cls = "", attrs = {}, kids = []) {
6
+ const e = doc.createElement(tag);
7
+ if (cls)
8
+ e.className = cls;
9
+ for (const [k, v] of Object.entries(attrs))
10
+ if (v !== false && v != null)
11
+ e.setAttribute(k, v === true ? "" : String(v));
12
+ e.append(...kids);
13
+ return e;
14
+ }
15
+ /** A child of `host` with this class, made and appended when missing, so a Vue shell or a page can supply it. */
16
+ export function part(host, cls, make) {
17
+ const found = [...host.children].find((c) => c.classList.contains(cls));
18
+ if (found)
19
+ return found;
20
+ const e = make();
21
+ host.append(e);
22
+ return e;
23
+ }
24
+ /** A 44 px icon button from the hackbuild-ui stroke set. */
25
+ export function iconButton(doc, icon, label, attrs = {}) {
26
+ const b = el(doc, "button", "hb-iconbtn", { type: "button", "aria-label": label, title: label, ...attrs });
27
+ b.innerHTML = strokeIcon(icon, { size: 18 });
28
+ return b;
29
+ }
30
+ /** A light button, or the one pink action when `go`. */
31
+ export function button(doc, label, go = false, attrs = {}) {
32
+ return el(doc, "button", go ? "hb-btn hb-btn-go" : "hb-btn", { type: "button", ...attrs }, [label]);
33
+ }
34
+ /** Text with a tone for hackbuild-ui's status line. */
35
+ export function statusLine(doc, text, tone) {
36
+ return el(doc, "p", `hb-status is-${tone === "bad" ? "err" : tone}`, {}, [text]);
37
+ }
38
+ /** An object URL that is revoked on the next call, so one thumbnail slot never leaks. */
39
+ export function urlSlot() {
40
+ let last = null;
41
+ return (b) => {
42
+ if (last)
43
+ URL.revokeObjectURL(last);
44
+ last = null;
45
+ if (!b)
46
+ return "";
47
+ if (typeof b === "string")
48
+ return b;
49
+ last = URL.createObjectURL(b);
50
+ return last;
51
+ };
52
+ }
@@ -0,0 +1,26 @@
1
+ import { type CharacterStageHandle, type CharacterStageOptions } from "./character-stage.js";
2
+ import { type AvatarDropHandle, type AvatarDropOptions } from "./avatar-drop.js";
3
+ import { type ClipStripHandle, type StripItem } from "./clip-strip.js";
4
+ /** Options `defineMotionViews` shares with every element it defines. */
5
+ export interface MotionViewsOptions {
6
+ /** Options every hb-character-stage shares: the vrm hook, lights, follow. */
7
+ stage?: CharacterStageOptions;
8
+ /** Options every hb-avatar-drop shares: the vrm hook. */
9
+ drop?: AvatarDropOptions;
10
+ }
11
+ /** The hb-character-stage element, with its view handle once connected. */
12
+ export interface CharacterStageElement extends HTMLElement {
13
+ readonly handle: CharacterStageHandle | null;
14
+ }
15
+ /** The hb-avatar-drop element, with its view handle once connected. */
16
+ export interface AvatarDropElement extends HTMLElement {
17
+ readonly handle: AvatarDropHandle | null;
18
+ }
19
+ /** The hb-clip-strip element, with its view handle and the items and picked clip as properties. */
20
+ export interface ClipStripElement extends HTMLElement {
21
+ readonly handle: ClipStripHandle | null;
22
+ items: readonly StripItem[];
23
+ current: string | null;
24
+ }
25
+ /** Defines hb-character-stage, hb-avatar-drop and hb-clip-strip once. */
26
+ export declare function defineMotionViews(opts?: MotionViewsOptions): void;
@@ -0,0 +1,120 @@
1
+ // The views as custom elements, for a plain HTML page as the originals are.
2
+ // Nothing is defined on import: call defineMotionViews() once. Each element
3
+ // dispatches its view's events as DOM events of the same type with the
4
+ // event as detail, and keeps the view's handle on `.handle`.
5
+ //
6
+ // <hb-character-stage name="moheeb" framing="bust"><div slot="inset">..</div></hb-character-stage>
7
+ // <hb-avatar-drop go></hb-avatar-drop>
8
+ // <hb-clip-strip></hb-clip-strip> (set .items from script)
9
+ import { characterStage } from "./character-stage.js";
10
+ import { avatarDrop } from "./avatar-drop.js";
11
+ import { clipStrip } from "./clip-strip.js";
12
+ const relay = (target) => (e) => target.dispatchEvent(new CustomEvent(e.type, { detail: e }));
13
+ /** Defines hb-character-stage, hb-avatar-drop and hb-clip-strip once. */
14
+ export function defineMotionViews(opts = {}) {
15
+ if (!customElements.get("hb-character-stage")) {
16
+ class Stage extends HTMLElement {
17
+ static observedAttributes = ["src", "framing"];
18
+ handle = null;
19
+ off = null;
20
+ connectedCallback() {
21
+ if (this.handle)
22
+ return;
23
+ const slotted = [...this.children].filter((c) => c.getAttribute("slot") === "inset");
24
+ const framing = this.getAttribute("framing");
25
+ const o = { ...opts.stage, drop: !this.hasAttribute("no-drop") };
26
+ const name = this.getAttribute("name");
27
+ const src = this.getAttribute("src");
28
+ if (name)
29
+ o.name = name;
30
+ if (src)
31
+ o.src = src;
32
+ if (framing === "bust" || framing === "full")
33
+ o.framing = framing;
34
+ this.handle = characterStage(this, o);
35
+ this.handle.inset.append(...slotted);
36
+ this.off = this.handle.on(relay(this));
37
+ }
38
+ disconnectedCallback() {
39
+ this.off?.();
40
+ this.handle?.dispose();
41
+ this.handle = null;
42
+ }
43
+ attributeChangedCallback(name, was, now) {
44
+ if (!this.handle || was === now)
45
+ return;
46
+ if (name === "src") {
47
+ if (now)
48
+ void this.handle.show(now);
49
+ else
50
+ this.handle.showPlaceholder();
51
+ }
52
+ else if (now === "bust" || now === "full")
53
+ this.handle.frame(now);
54
+ }
55
+ }
56
+ customElements.define("hb-character-stage", Stage);
57
+ }
58
+ if (!customElements.get("hb-avatar-drop")) {
59
+ class Drop extends HTMLElement {
60
+ handle = null;
61
+ off = null;
62
+ connectedCallback() {
63
+ if (this.handle)
64
+ return;
65
+ if (!this.style.display)
66
+ this.style.display = "block";
67
+ const o = { ...opts.drop, go: this.hasAttribute("go") };
68
+ const label = this.getAttribute("label");
69
+ if (label)
70
+ o.label = label;
71
+ this.handle = avatarDrop(this, o);
72
+ this.off = this.handle.on(relay(this));
73
+ }
74
+ disconnectedCallback() {
75
+ this.off?.();
76
+ this.handle?.dispose();
77
+ this.handle = null;
78
+ }
79
+ }
80
+ customElements.define("hb-avatar-drop", Drop);
81
+ }
82
+ if (!customElements.get("hb-clip-strip")) {
83
+ class Strip extends HTMLElement {
84
+ handle = null;
85
+ off = null;
86
+ pending = [];
87
+ picked = null;
88
+ get items() {
89
+ return this.pending;
90
+ }
91
+ set items(v) {
92
+ this.pending = v;
93
+ this.handle?.setItems(v, this.picked);
94
+ }
95
+ get current() {
96
+ return this.handle?.current?.id ?? this.picked;
97
+ }
98
+ set current(id) {
99
+ this.picked = id;
100
+ if (id)
101
+ this.handle?.pick(id);
102
+ }
103
+ connectedCallback() {
104
+ if (this.handle)
105
+ return;
106
+ // the strip and its play button join the parent row, as the verb row lays them out
107
+ if (!this.style.display)
108
+ this.style.display = "contents";
109
+ this.handle = clipStrip(this, { items: this.pending, current: this.picked, go: !this.hasAttribute("no-go") });
110
+ this.off = this.handle.on(relay(this));
111
+ }
112
+ disconnectedCallback() {
113
+ this.off?.();
114
+ this.handle?.dispose();
115
+ this.handle = null;
116
+ }
117
+ }
118
+ customElements.define("hb-clip-strip", Strip);
119
+ }
120
+ }
@@ -0,0 +1,14 @@
1
+ import { AnimationClip } from "three";
2
+ import type { Clip } from "@hackbuild/presence";
3
+ /** A gesture written in code over the body's own bones. */
4
+ export type Gesture = "wave" | "nod" | "shake" | "bow";
5
+ /** Every gesture `gestureClip` can make. */
6
+ export declare const GESTURES: readonly Gesture[];
7
+ /**
8
+ * A gesture as a presence clip over `names`, starting from `base` (bones
9
+ * times 4 local rotations, the pose when it was built). Bones the body
10
+ * lacks are skipped; returns null when it has none of the gesture's bones.
11
+ */
12
+ export declare function gestureClip(id: Gesture, names: readonly string[], base: ArrayLike<number>, fps?: number): Clip | null;
13
+ /** A presence clip as a three.js AnimationClip with one quaternion track per bone object name. */
14
+ export declare function animationClipOf(clip: Clip, names: readonly string[]): AnimationClip;
@@ -0,0 +1,73 @@
1
+ // Gestures written in code over a body's own bone order, for the verb row
2
+ // before any clip file arrives, and the step from a presence clip to a
3
+ // three.js AnimationClip that the clip thumbnails need. Bones a gesture
4
+ // does not touch keep the pose they had when it was built, so an upper
5
+ // layer wave leaves the relaxed left arm where it was.
6
+ import { AnimationClip, Quaternion, QuaternionKeyframeTrack, Vector3 } from "three";
7
+ /** Every gesture `gestureClip` can make. */
8
+ export const GESTURES = ["wave", "nod", "shake", "bow"];
9
+ // shapes in the normalized humanoid space: identity rest, +Z forward, left on +X
10
+ const MOVES = {
11
+ wave: {
12
+ seconds: 2,
13
+ bones: {
14
+ rightUpperArm: (t, q, a) => (q.setFromAxisAngle(a.Z, -1.2 * Math.sin(Math.PI * t)), "set"),
15
+ rightLowerArm: (t, q, a) => (q.setFromAxisAngle(a.Y, Math.sin(Math.PI * t) * (0.9 + 0.35 * Math.sin(t * Math.PI * 8))), "set"),
16
+ },
17
+ },
18
+ nod: { seconds: 1.2, bones: { head: (t, q, a) => (q.setFromAxisAngle(a.X, 0.22 * Math.sin(t * Math.PI * 2) ** 2), "mul") } },
19
+ shake: { seconds: 1.4, bones: { head: (t, q, a) => (q.setFromAxisAngle(a.Y, 0.3 * Math.sin(t * Math.PI * 4) * Math.sin(Math.PI * t)), "mul") } },
20
+ bow: {
21
+ seconds: 1.8,
22
+ bones: {
23
+ spine: (t, q, a) => (q.setFromAxisAngle(a.X, 0.3 * Math.sin(Math.PI * t)), "mul"),
24
+ chest: (t, q, a) => (q.setFromAxisAngle(a.X, 0.2 * Math.sin(Math.PI * t)), "mul"),
25
+ head: (t, q, a) => (q.setFromAxisAngle(a.X, 0.15 * Math.sin(Math.PI * t)), "mul"),
26
+ },
27
+ },
28
+ };
29
+ /**
30
+ * A gesture as a presence clip over `names`, starting from `base` (bones
31
+ * times 4 local rotations, the pose when it was built). Bones the body
32
+ * lacks are skipped; returns null when it has none of the gesture's bones.
33
+ */
34
+ export function gestureClip(id, names, base, fps = 30) {
35
+ const move = MOVES[id];
36
+ const n = names.length;
37
+ const used = Object.keys(move.bones).map((b) => names.indexOf(b));
38
+ if (used.every((i) => i < 0))
39
+ return null;
40
+ const frames = Math.round(move.seconds * fps) + 1;
41
+ const rotations = new Float32Array(frames * n * 4);
42
+ const q = new Quaternion();
43
+ const b = new Quaternion();
44
+ const axes = { X: new Vector3(1, 0, 0), Y: new Vector3(0, 1, 0), Z: new Vector3(0, 0, 1) };
45
+ for (let f = 0; f < frames; f++) {
46
+ const t = f / (frames - 1);
47
+ for (let i = 0; i < n; i++) {
48
+ const at = (f * n + i) * 4;
49
+ b.set(base[i * 4], base[i * 4 + 1], base[i * 4 + 2], base[i * 4 + 3]);
50
+ const fn = move.bones[names[i]];
51
+ if (fn) {
52
+ const how = fn(t, q, axes);
53
+ if (how === "mul")
54
+ b.multiply(q);
55
+ else
56
+ b.copy(q);
57
+ }
58
+ b.toArray(rotations, at);
59
+ }
60
+ }
61
+ return { name: id, fps, frames, bones: n, loop: false, rotations };
62
+ }
63
+ /** A presence clip as a three.js AnimationClip with one quaternion track per bone object name. */
64
+ export function animationClipOf(clip, names) {
65
+ const times = Array.from({ length: clip.frames }, (_, f) => f / clip.fps);
66
+ const tracks = names.map((name, i) => {
67
+ const values = new Float32Array(clip.frames * 4);
68
+ for (let f = 0; f < clip.frames; f++)
69
+ values.set(clip.rotations.subarray((f * clip.bones + i) * 4, (f * clip.bones + i) * 4 + 4), f * 4);
70
+ return new QuaternionKeyframeTrack(`${name}.quaternion`, times, values);
71
+ });
72
+ return new AnimationClip(clip.name, (clip.frames - 1) / clip.fps, tracks);
73
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./words.js";
2
+ export * from "./clips.js";
3
+ export * from "./speech.js";
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // @hackbuild/motion-views: the motion views of the rebuild, binding the
2
+ // motion packages to hackbuild-ui. This entry holds the words, the clip
3
+ // items and the speech timings, with no three.js and no DOM. `/vanilla` has the views and their
4
+ // custom elements, `/vue` the same views as Vue components.
5
+ export * from "./words.js";
6
+ export * from "./clips.js";
7
+ export * from "./speech.js";