huashu-motion 0.1.0 → 0.4.0

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.
Files changed (47) hide show
  1. package/README.en.md +166 -5
  2. package/README.md +167 -6
  3. package/THIRD_PARTY.md +1 -0
  4. package/cli/lib/capture.js +1 -1
  5. package/cli/lib/cdp.js +9 -1
  6. package/cli/lib/check.js +13 -0
  7. package/cli/lib/composed.js +2 -2
  8. package/cli/lib/composer.js +33 -12
  9. package/cli/lib/grab.js +239 -0
  10. package/cli/lib/qa.js +53 -1
  11. package/cli/lib/render-om.js +3 -1
  12. package/cli/lib/render.js +6 -4
  13. package/cli/lib/share.js +101 -0
  14. package/cli/lib/sound.js +4 -0
  15. package/cli/lib/soundprint.js +72 -0
  16. package/cli/lib/swatch.js +7 -3
  17. package/cli/om.js +22 -3
  18. package/package.json +2 -2
  19. package/runtime/om-engine.js +129 -1
  20. package/runtime/om.js +180 -1
  21. package/runtime/three/LICENSE +21 -0
  22. package/runtime/three/entry.js +19 -0
  23. package/runtime/three/om-three.js +280 -0
  24. package/runtime/three/three.min.js +4586 -0
  25. package/skills/huashu-motion/SKILL.md +65 -5
  26. package/skills/huashu-motion/references/about-author.md +101 -0
  27. package/skills/huashu-motion/references/colour.md +16 -11
  28. package/skills/huashu-motion/references/contract.md +27 -1
  29. package/skills/huashu-motion/references/depth.md +109 -0
  30. package/skills/huashu-motion/references/director.md +27 -9
  31. package/skills/huashu-motion/references/library/README.md +1 -1
  32. package/skills/huashu-motion/references/library/languages/3d-object.md +5 -3
  33. package/skills/huashu-motion/references/library/techniques.md +4 -2
  34. package/skills/huashu-motion/references/music.md +411 -0
  35. package/skills/huashu-motion/references/punch-up.md +108 -0
  36. package/skills/huashu-motion/references/scores/first-light.abc +1 -1
  37. package/skills/huashu-motion/references/scores/four-rooms.abc +1 -1
  38. package/skills/huashu-motion/references/sound.md +5 -3
  39. package/templates/object-3d/assets/comment.svg +1 -0
  40. package/templates/object-3d/assets/cursor.svg +1 -0
  41. package/templates/object-3d/assets/studio.jpg +0 -0
  42. package/templates/object-3d/index.html +163 -0
  43. package/templates/object-3d/materials.md +6 -0
  44. package/templates/object-3d/template.json +23 -0
  45. package/templates/ui-space/index.html +150 -0
  46. package/templates/ui-space/materials.md +6 -0
  47. package/templates/ui-space/template.json +22 -0
@@ -0,0 +1,280 @@
1
+ /* huashu-motion · Three.js helpers (window.OM3). Needs vendor/three.min.js (window.THREE) and vendor/om.js. MIT.
2
+ * A lit studio, the product's real things in it (its logo SVG extruded, its screenshots on a device or a
3
+ * card, its GLB), and a render that follows OM.space. Everything waits for its files through OM.ready, so
4
+ * no frame is taken before a texture or a model has arrived. */
5
+ (function () {
6
+ const OM3 = (window.OM3 = {});
7
+ const T = () => window.THREE;
8
+ const size = () => {
9
+ const r = document.querySelector('[data-composition-id]');
10
+ return [Number(r?.getAttribute('data-width')) || 1920, Number(r?.getAttribute('data-height')) || 1080];
11
+ };
12
+ // Every load the films wait for also redraws them when it lands: a frame rendered before a texture or
13
+ // a font arrived is never the one that gets captured.
14
+ const films = [];
15
+ const ready = (p) => {
16
+ const q = Promise.resolve(p).then((v) => { films.forEach((f) => f()); return v; });
17
+ return window.OM && OM.ready ? OM.ready(q) : q;
18
+ };
19
+ OM3.ready = ready;
20
+
21
+ // Films render from file://, where fetch() cannot read local files; XMLHttpRequest can. Loaders that
22
+ // fetch (GLTF, HDR) get a blob: URL of the bytes instead.
23
+ OM3.read = (url, type = 'arraybuffer') => new Promise((ok, no) => {
24
+ const x = new XMLHttpRequest();
25
+ x.open('GET', url);
26
+ x.responseType = type;
27
+ const fail = () => no(new Error(`huashu-motion: could not read ${url}`));
28
+ x.onload = () => ((x.status === 0 || x.status < 300) && x.response != null ? ok(x.response) : fail());
29
+ x.onerror = fail;
30
+ x.send();
31
+ });
32
+ const viaBlob = (Loader, url) => ready(OM3.read(url).then((buf) => {
33
+ const blob = URL.createObjectURL(new Blob([buf]));
34
+ return new Loader().loadAsync(blob).finally(() => URL.revokeObjectURL(blob));
35
+ }));
36
+
37
+ // ---------- stage ----------
38
+ // OM3.stage(canvas, { background, env: 'studio'|'room'|'<file>.hdr', exposure, fov, dof: { aperture, maxblur },
39
+ // bloom: { strength, radius, threshold }, fog: [near, far], shadows, units: 'px' })
40
+ OM3.stage = function (canvas, o = {}) {
41
+ const THREE = T(), [W, H] = size();
42
+ const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: !o.background, preserveDrawingBuffer: true });
43
+ renderer.setPixelRatio(1);
44
+ renderer.setSize(W, H, false);
45
+ renderer.outputColorSpace = THREE.SRGBColorSpace;
46
+ renderer.toneMapping = THREE.ACESFilmicToneMapping;
47
+ renderer.toneMappingExposure = o.exposure ?? 1;
48
+ renderer.shadowMap.enabled = o.shadows !== false;
49
+ renderer.shadowMap.type = THREE.PCFSoftShadowMap;
50
+ const scene = new THREE.Scene();
51
+ if (o.background) scene.background = new THREE.Color(o.background);
52
+ if (o.fog) scene.fog = new THREE.Fog(o.background || '#000', o.fog[0], o.fog[1]);
53
+ const px = o.units === 'px';
54
+ const camera = new THREE.PerspectiveCamera(o.fov || 30, W / H, px ? 10 : 0.05, px ? 40000 : 400);
55
+ const pmrem = new THREE.PMREMGenerator(renderer);
56
+ const env = o.env || 'studio';
57
+ if (env === 'room') scene.environment = pmrem.fromScene(new THREE.RoomEnvironment(), 0.04).texture;
58
+ else if (/\.hdr$/i.test(env)) {
59
+ scene.environment = pmrem.fromScene(new THREE.RoomEnvironment(), 0.04).texture;
60
+ viaBlob(THREE.HDRLoader, env).then((tex) => { tex.mapping = THREE.EquirectangularReflectionMapping; scene.environment = pmrem.fromEquirectangular(tex).texture; });
61
+ } else scene.environment = pmrem.fromEquirectangular(studioSky(o.envColors)).texture;
62
+ if (o.envIntensity != null) scene.environmentIntensity = o.envIntensity;
63
+
64
+ let composer = null, bokeh = null;
65
+ if (o.dof || o.bloom) {
66
+ composer = new THREE.EffectComposer(renderer);
67
+ composer.setPixelRatio(1);
68
+ composer.setSize(W, H);
69
+ composer.addPass(new THREE.RenderPass(scene, camera));
70
+ if (o.bloom) composer.addPass(new THREE.UnrealBloomPass(new THREE.Vector2(W, H), o.bloom.strength ?? 0.35, o.bloom.radius ?? 0.4, o.bloom.threshold ?? 0.85));
71
+ if (o.dof) { bokeh = new THREE.BokehPass(scene, camera, { focus: 10, aperture: o.dof.aperture ?? 0.0025, maxblur: o.dof.maxblur ?? 0.008 }); composer.addPass(bokeh); }
72
+ composer.addPass(new THREE.OutputPass());
73
+ }
74
+ const stage = {
75
+ THREE, renderer, scene, camera, W, H, bokeh,
76
+ // focus: distance from the camera to the sharp plane (OM3.film passes the camera's distance to its target)
77
+ render(opts = {}) {
78
+ if (bokeh && opts.focus != null) bokeh.uniforms.focus.value = opts.focus;
79
+ if (composer) composer.render(); else renderer.render(scene, camera);
80
+ },
81
+ add(...objs) { scene.add(...objs); return stage; },
82
+ };
83
+ return stage;
84
+ };
85
+
86
+ // An equirect sky with two soft boxes and a dark floor: metals read as metal (an all-bright environment
87
+ // turns them white and weightless, none at all turns them black).
88
+ function studioSky(c = {}) {
89
+ const THREE = T(), cv = document.createElement('canvas');
90
+ cv.width = 1024; cv.height = 512;
91
+ const g = cv.getContext('2d');
92
+ const sky = g.createLinearGradient(0, 0, 0, 512);
93
+ sky.addColorStop(0, c.top || '#9a9a9a'); sky.addColorStop(0.48, c.horizon || '#5a5a5a'); sky.addColorStop(0.52, c.floor || '#1b1b1b'); sky.addColorStop(1, '#050505');
94
+ g.fillStyle = sky; g.fillRect(0, 0, 1024, 512);
95
+ const box = (x, y, w, h, a) => { const r = g.createRadialGradient(x + w / 2, y + h / 2, 0, x + w / 2, y + h / 2, Math.max(w, h) / 1.4); r.addColorStop(0, `rgba(255,255,255,${a})`); r.addColorStop(1, 'rgba(255,255,255,0)'); g.fillStyle = r; g.fillRect(x - w, y - h, w * 3, h * 3); };
96
+ box(180, 90, 220, 120, 1); box(640, 120, 160, 90, 0.75); box(470, 20, 120, 40, 0.5);
97
+ const tex = new THREE.CanvasTexture(cv);
98
+ tex.mapping = THREE.EquirectangularReflectionMapping;
99
+ tex.colorSpace = THREE.SRGBColorSpace;
100
+ return tex;
101
+ }
102
+
103
+ // ---------- light ----------
104
+ // A key that casts soft shadows over `span` units around the origin, and a rim from behind.
105
+ OM3.lights = function (stage, o = {}) {
106
+ const THREE = T(), span = o.span || 8;
107
+ const key = new THREE.DirectionalLight(o.key || '#ffffff', o.keyIntensity ?? 2.2);
108
+ key.position.set(...(o.from || [span * 0.6, span, span * 0.7]));
109
+ key.castShadow = true;
110
+ key.shadow.mapSize.set(2048, 2048);
111
+ Object.assign(key.shadow.camera, { left: -span, right: span, top: span, bottom: -span, near: 0.1, far: span * 6 });
112
+ key.shadow.radius = 6; key.shadow.bias = -0.0004; key.shadow.normalBias = 0.02;
113
+ const rim = new THREE.DirectionalLight(o.rim || '#ffffff', o.rimIntensity ?? 1.2);
114
+ rim.position.set(-span * 0.8, span * 0.5, -span);
115
+ stage.scene.add(key, rim);
116
+ return { key, rim };
117
+ };
118
+
119
+ // ---------- space ----------
120
+ // A photo-studio sweep (floor curving into a wall): the frame never ends in a hard horizon line.
121
+ OM3.cyc = function (stage, o = {}) {
122
+ const THREE = T(), w = o.width || 60, depth = o.depth || 30, r = o.radius || 8, wall = o.height || 30, y0 = o.y ?? 0;
123
+ const len = depth + (Math.PI * r) / 2 + wall, geo = new THREE.PlaneGeometry(w, len, 1, 120), p = geo.attributes.position;
124
+ for (let i = 0; i < p.count; i++) {
125
+ const s = p.getY(i) + len / 2; // 0 at the front edge
126
+ let y, z;
127
+ if (s <= depth) { y = 0; z = depth / 2 - s; }
128
+ else if (s <= depth + (Math.PI * r) / 2) { const a = (s - depth) / r; y = r - r * Math.cos(a); z = -depth / 2 - r * Math.sin(a); }
129
+ else { y = r + (s - depth - (Math.PI * r) / 2); z = -depth / 2 - r; }
130
+ p.setXYZ(i, p.getX(i), y + y0, z + (o.z || 0));
131
+ }
132
+ geo.computeVertexNormals();
133
+ const mesh = new THREE.Mesh(geo, new THREE.MeshStandardMaterial({ color: o.color || '#d8d4cc', roughness: o.roughness ?? 0.85, side: THREE.DoubleSide }));
134
+ mesh.receiveShadow = true;
135
+ stage.scene.add(mesh);
136
+ return mesh;
137
+ };
138
+
139
+ // ---------- real things ----------
140
+ OM3.texture = function (url) {
141
+ const THREE = T();
142
+ let done;
143
+ const p = new Promise((ok) => (done = ok));
144
+ const tex = new THREE.TextureLoader().load(url, (t) => done(t), undefined, () => done(null));
145
+ tex.colorSpace = THREE.SRGBColorSpace;
146
+ tex.anisotropy = 8;
147
+ ready(p.then((t) => { if (!t) throw new Error(`huashu-motion: could not load ${url}`); }));
148
+ tex.loaded = p;
149
+ return tex;
150
+ };
151
+
152
+ // The product's logo as an object: every filled path of the SVG extruded, in the SVG's own colours
153
+ // (or `color`), centred, `size` units tall. Returns a Group at once; the geometry arrives before frame one.
154
+ // const mark = OM3.logo('assets/logo.svg', { size: 2, depth: 0.25, bevel: 0.03, metalness: 0.2 })
155
+ OM3.logo = function (svg, o = {}) {
156
+ const THREE = T(), group = new THREE.Group();
157
+ const text = /<svg[\s>]/i.test(svg) ? Promise.resolve(svg) : OM3.read(svg, 'text');
158
+ ready(text.then((src) => {
159
+ const data = new THREE.SVGLoader().parse(src), inner = new THREE.Group(), parts = [];
160
+ let y0 = Infinity, y1 = -Infinity;
161
+ for (const path of data.paths) {
162
+ const style = path.userData?.style || {};
163
+ if (style.fill === 'none' || style.fillOpacity === 0) continue;
164
+ const shapes = THREE.SVGLoader.createShapes(path);
165
+ for (const sh of shapes) for (const pt of sh.getPoints()) { y0 = Math.min(y0, pt.y); y1 = Math.max(y1, pt.y); }
166
+ if (shapes.length) parts.push([shapes, path.color]);
167
+ }
168
+ if (!parts.length) throw new Error('huashu-motion: OM3.logo — the SVG has no filled paths');
169
+ // depth and bevel are in scene units: build in SVG units divided by the scale, then scale evenly
170
+ const s = (o.size || 2) / Math.max(1e-6, y1 - y0), bevel = o.bevel ?? 0.03;
171
+ for (const [shapes, color] of parts) {
172
+ const geo = new THREE.ExtrudeGeometry(shapes, { depth: (o.depth ?? 0.25) / s, curveSegments: 24, bevelEnabled: bevel > 0, bevelThickness: bevel / s, bevelSize: (bevel * 0.8) / s, bevelSegments: 6 });
173
+ const mat = new THREE.MeshPhysicalMaterial({ color: o.color || color, roughness: o.roughness ?? 0.32, metalness: o.metalness ?? 0.1, clearcoat: o.clearcoat ?? 0.6, clearcoatRoughness: 0.2 });
174
+ const mesh = new THREE.Mesh(geo, mat);
175
+ mesh.castShadow = mesh.receiveShadow = true;
176
+ inner.add(mesh);
177
+ }
178
+ inner.scale.set(s, -s, s); // SVG y grows down
179
+ inner.updateMatrixWorld(true);
180
+ const c = new THREE.Box3().setFromObject(inner).getCenter(new THREE.Vector3());
181
+ inner.position.sub(c);
182
+ group.add(inner);
183
+ }));
184
+ return group;
185
+ };
186
+
187
+ // A screenshot on a device: a rounded slab with a lit-from-inside screen (the interface keeps its true
188
+ // colours: no tone mapping on the pixels) under a faint glass that catches the studio's reflections.
189
+ // OM3.screen('assets/app.png', { width: 4, bezel: 0.08, depth: 0.12, radius: 0.12, body: '#1d1d1f' })
190
+ // bezel: 0, depth: 0.02 gives a printed card or a floating panel.
191
+ // crop: [left, top, right, bottom] (0–1 of the image) shows one part of a screenshot — a panel lifted
192
+ // out of the interface, the same capture, no new file.
193
+ OM3.screen = function (image, o = {}) {
194
+ const THREE = T(), group = new THREE.Group(), w = o.width || 4;
195
+ let tex = typeof image === 'string' ? OM3.texture(image) : image;
196
+ const cr = o.crop || [0, 0, 1, 1];
197
+ if (o.crop) {
198
+ const src = tex;
199
+ tex = src.clone();
200
+ tex.loaded = src.loaded;
201
+ tex.repeat.set(cr[2] - cr[0], cr[3] - cr[1]);
202
+ tex.offset.set(cr[0], 1 - cr[3]);
203
+ if (src.loaded) src.loaded.then(() => { tex.needsUpdate = true; });
204
+ }
205
+ const build = (aspect) => {
206
+ const h = w / aspect, b = o.bezel ?? 0.08, d = o.depth ?? 0.12, rad = Math.min(o.radius ?? 0.12, (Math.min(w, h) + 2 * b) / 2 - 1e-3);
207
+ if (d > 0) {
208
+ const body = new THREE.Mesh(new THREE.RoundedBoxGeometry(w + 2 * b, h + 2 * b, d, 6, Math.min(rad, d / 2 - 1e-3)), new THREE.MeshPhysicalMaterial({ color: o.body || '#1d1d1f', roughness: 0.35, metalness: 0.7, clearcoat: 1, clearcoatRoughness: 0.15 }));
209
+ body.castShadow = body.receiveShadow = true;
210
+ group.add(body);
211
+ }
212
+ const screen = new THREE.Mesh(roundedPlane(w, h, Math.max(0, rad - b)), new THREE.MeshBasicMaterial({ map: tex, toneMapped: false }));
213
+ screen.position.z = d / 2 + 0.002;
214
+ group.add(screen);
215
+ if (o.glass !== false) {
216
+ const glass = new THREE.Mesh(roundedPlane(w, h, Math.max(0, rad - b)), new THREE.MeshPhysicalMaterial({ color: '#000', roughness: 0.04, metalness: 0, transparent: true, opacity: o.glass ?? 0.18, envMapIntensity: 1.4, depthWrite: false }));
217
+ glass.position.z = d / 2 + 0.004;
218
+ group.add(glass);
219
+ }
220
+ group.userData.size = [w + 2 * b, h + 2 * b, d];
221
+ };
222
+ if (o.aspect) build(o.aspect);
223
+ else ready((tex.loaded || Promise.resolve(tex)).then((t) => build(((t?.image?.width || 16) * (cr[2] - cr[0])) / ((t?.image?.height || 9) * (cr[3] - cr[1])))));
224
+ group.userData.screen = tex;
225
+ return group;
226
+ };
227
+
228
+ function roundedPlane(w, h, r) {
229
+ const THREE = T(), s = new THREE.Shape(), x = -w / 2, y = -h / 2;
230
+ s.moveTo(x + r, y); s.lineTo(x + w - r, y); s.quadraticCurveTo(x + w, y, x + w, y + r); s.lineTo(x + w, y + h - r);
231
+ s.quadraticCurveTo(x + w, y + h, x + w - r, y + h); s.lineTo(x + r, y + h); s.quadraticCurveTo(x, y + h, x, y + h - r);
232
+ s.lineTo(x, y + r); s.quadraticCurveTo(x, y, x + r, y);
233
+ const g = new THREE.ShapeGeometry(s, 16), p = g.attributes.position, uv = g.attributes.uv;
234
+ for (let i = 0; i < p.count; i++) uv.setXY(i, (p.getX(i) - x) / w, (p.getY(i) - y) / h);
235
+ return g;
236
+ }
237
+
238
+ // A .glb (one file: textures inside). Resolves to the glTF scene, shadows on.
239
+ OM3.gltf = function (url) {
240
+ return viaBlob(T().GLTFLoader, url).then((g) => { g.scene.traverse((m) => { if (m.isMesh) m.castShadow = m.receiveShadow = true; }); return g; });
241
+ };
242
+
243
+ // ---------- time ----------
244
+ // One clock for the whole 3D layer: the camera is posed for t, update(t) moves the objects, then one
245
+ // render, in focus on whatever the camera looks at.
246
+ // OM3.film(stage, tl, D, cam, (t) => { mark.rotation.y = t * 0.4; });
247
+ // The render waits for the end of the seek (a microtask): the timeline updates its tweens in start
248
+ // order, so drawing inside the clock tween (which starts at 0) would show the values tweens had
249
+ // before this frame. Every frame — a jump to 3.6 s included — draws what its tweens just set.
250
+ OM3.film = function (stage, tl, duration, cam, update) {
251
+ let at = 0, queued = false;
252
+ const draw = () => {
253
+ queued = false;
254
+ if (cam) cam.apply(at);
255
+ if (update) update(at);
256
+ stage.render({ focus: cam ? cam.pose(at).distance : undefined });
257
+ };
258
+ const queue = () => { if (!queued) { queued = true; queueMicrotask(draw); } };
259
+ films.push(queue);
260
+ return OM.clock(tl, (t) => { at = t; queue(); }, duration);
261
+ };
262
+
263
+ // Clickable in the studio: the object's projected box, so comments arrive with its id.
264
+ OM3.region = function (id, object, stage, label) {
265
+ const THREE = T(), box = new THREE.Box3(), v = new THREE.Vector3();
266
+ OM.region(id, () => {
267
+ box.setFromObject(object);
268
+ if (box.isEmpty()) return null;
269
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
270
+ for (let i = 0; i < 8; i++) {
271
+ v.set(i & 1 ? box.max.x : box.min.x, i & 2 ? box.max.y : box.min.y, i & 4 ? box.max.z : box.min.z).project(stage.camera);
272
+ if (v.z > 1) return null;
273
+ const x = ((v.x + 1) / 2) * stage.W, y = ((1 - v.y) / 2) * stage.H;
274
+ x0 = Math.min(x0, x); y0 = Math.min(y0, y); x1 = Math.max(x1, x); y1 = Math.max(y1, y);
275
+ }
276
+ return [x0, y0, x1 - x0, y1 - y0];
277
+ }, label || id);
278
+ return OM3;
279
+ };
280
+ })();