motionary 10.4.0 → 10.5.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.
- package/CHANGELOG.md +18 -0
- package/README.md +37 -0
- package/dist/chunks/{anim-image-DDdb9q3X.js → anim-image-BlNPv4JG.js} +2 -2
- package/dist/chunks/{anim-image-DDdb9q3X.js.map → anim-image-BlNPv4JG.js.map} +1 -1
- package/dist/chunks/{anim-image-blQgPFFS.cjs → anim-image-DuxvYhq6.cjs} +2 -2
- package/dist/chunks/{anim-image-blQgPFFS.cjs.map → anim-image-DuxvYhq6.cjs.map} +1 -1
- package/dist/chunks/{keyframes-B5PWe5bU.js → keyframes-B8ITIAVa.js} +2 -2
- package/dist/chunks/{keyframes-B5PWe5bU.js.map → keyframes-B8ITIAVa.js.map} +1 -1
- package/dist/chunks/{keyframes-tC3I7anQ.cjs → keyframes-Ce1s05n3.cjs} +2 -2
- package/dist/chunks/{keyframes-tC3I7anQ.cjs.map → keyframes-Ce1s05n3.cjs.map} +1 -1
- package/dist/chunks/{registry-CT-TT_bx.js → registry-DXdFb2fJ.js} +2 -2
- package/dist/chunks/{registry-CT-TT_bx.js.map → registry-DXdFb2fJ.js.map} +1 -1
- package/dist/chunks/{registry-tzwKkldE.cjs → registry-VhfPSI0v.cjs} +2 -2
- package/dist/chunks/{registry-tzwKkldE.cjs.map → registry-VhfPSI0v.cjs.map} +1 -1
- package/dist/chunks/{ticker-C8lSGml9.js → ticker-C5RMHMW7.js} +2 -2
- package/dist/chunks/{ticker-C8lSGml9.js.map → ticker-C5RMHMW7.js.map} +1 -1
- package/dist/chunks/{ticker-Un0bvTWR.cjs → ticker-DvmC6WLS.cjs} +2 -2
- package/dist/chunks/{ticker-Un0bvTWR.cjs.map → ticker-DvmC6WLS.cjs.map} +1 -1
- package/dist/chunks/{tween-BcejKpsx.js → tween-CPtm-EdT.js} +2 -2
- package/dist/chunks/{tween-BcejKpsx.js.map → tween-CPtm-EdT.js.map} +1 -1
- package/dist/chunks/{tween-CwLLC3Ko.cjs → tween-_BGIl6hK.cjs} +2 -2
- package/dist/chunks/{tween-CwLLC3Ko.cjs.map → tween-_BGIl6hK.cjs.map} +1 -1
- package/dist/components/widgets.cjs +745 -212
- package/dist/components/widgets.cjs.map +1 -1
- package/dist/components/widgets.d.cts +202 -2
- package/dist/components/widgets.d.ts +202 -2
- package/dist/components/widgets.js +738 -213
- package/dist/components/widgets.js.map +1 -1
- package/dist/llms-full.txt +150 -1
- package/dist/llms.txt +9 -2
- package/dist/manifest.json +394 -1
- package/dist/runtime/format-apng.cjs +4 -4
- package/dist/runtime/format-apng.js +4 -4
- package/dist/runtime/format-css.cjs +4 -4
- package/dist/runtime/format-css.js +4 -4
- package/dist/runtime/format-gif.cjs +4 -4
- package/dist/runtime/format-gif.js +4 -4
- package/dist/runtime/format-gltf.cjs +207 -0
- package/dist/runtime/format-gltf.cjs.map +1 -0
- package/dist/runtime/format-gltf.d.cts +251 -0
- package/dist/runtime/format-gltf.d.ts +251 -0
- package/dist/runtime/format-gltf.iife.js +2 -0
- package/dist/runtime/format-gltf.iife.js.map +1 -0
- package/dist/runtime/format-gltf.js +200 -0
- package/dist/runtime/format-gltf.js.map +1 -0
- package/dist/runtime/format-motion.cjs +4 -4
- package/dist/runtime/format-motion.js +4 -4
- package/dist/runtime/format-obj.cjs +181 -0
- package/dist/runtime/format-obj.cjs.map +1 -0
- package/dist/runtime/format-obj.d.cts +176 -0
- package/dist/runtime/format-obj.d.ts +176 -0
- package/dist/runtime/format-obj.iife.js +2 -0
- package/dist/runtime/format-obj.iife.js.map +1 -0
- package/dist/runtime/format-obj.js +174 -0
- package/dist/runtime/format-obj.js.map +1 -0
- package/dist/runtime/format-sprite.cjs +3 -3
- package/dist/runtime/format-sprite.js +3 -3
- package/dist/runtime/format-svg.cjs +3 -3
- package/dist/runtime/format-svg.js +3 -3
- package/dist/runtime/format-webp.cjs +4 -4
- package/dist/runtime/format-webp.js +4 -4
- package/dist/runtime/gl.cjs +763 -0
- package/dist/runtime/gl.cjs.map +1 -0
- package/dist/runtime/gl.d.cts +260 -0
- package/dist/runtime/gl.d.ts +260 -0
- package/dist/runtime/gl.iife.js +2 -0
- package/dist/runtime/gl.iife.js.map +1 -0
- package/dist/runtime/gl.js +739 -0
- package/dist/runtime/gl.js.map +1 -0
- package/dist/runtime/scroll.cjs +2 -2
- package/dist/runtime/scroll.js +2 -2
- package/dist/runtime/smooth.cjs +1 -1
- package/dist/runtime/smooth.js +1 -1
- package/dist/runtime/text.cjs +1 -1
- package/dist/runtime/text.js +1 -1
- package/dist/runtime.cjs +3 -3
- package/dist/runtime.d.cts +1 -1
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.iife.js +1 -1
- package/dist/runtime.iife.js.map +1 -1
- package/dist/runtime.js +5 -5
- package/dist/widgets.umd.js +1 -1
- package/dist/widgets.umd.js.map +1 -1
- package/docs/ROADMAP.md +1 -1
- package/docs/components/README.md +8 -1
- package/docs/components/usa-gl-scene.md +66 -0
- package/docs/components/usa-gpu-particles.md +32 -0
- package/docs/components/usa-shader-backdrop.md +36 -0
- package/docs/components/usa-three-scene.md +20 -0
- package/docs/components.md +48 -0
- package/docs/runtime/format-gltf.md +63 -0
- package/docs/runtime/format-obj.md +59 -0
- package/docs/runtime/gl.md +62 -0
- package/package.json +32 -2
|
@@ -0,0 +1,739 @@
|
|
|
1
|
+
import { R as RUNTIME_VERSION } from '../chunks/registry-DXdFb2fJ.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `motionary/runtime/gl` (10.5) — a small WebGL2 scene renderer written for
|
|
5
|
+
* Motionary (not a Three.js clone; own API, own shaders):
|
|
6
|
+
*
|
|
7
|
+
* - **math** (`mat4`, quaternions — column-major `Float32Array`s), pure;
|
|
8
|
+
* - **scene graph**: `GlNode` (position / rotation quaternion / scale,
|
|
9
|
+
* children, optional mesh), `Camera` (perspective, `lookAt`), lights
|
|
10
|
+
* (`ambient`, up to 4 directional / point), `bounds()` + `frameNode()`;
|
|
11
|
+
* - **geometry** builders (`box`, `plane`, `sphere`, `torus`) and custom
|
|
12
|
+
* geometry from typed arrays (indexed 16 / 32-bit or not);
|
|
13
|
+
* - **materials**: `standard` (metallic-roughness PBR approximation, base
|
|
14
|
+
* colour texture, emissive, ACES-free Reinhard tone mapping, sRGB),
|
|
15
|
+
* `unlit`, and `shader` (your GLSL ES 3.00 fragment with the standard
|
|
16
|
+
* varyings + your uniforms);
|
|
17
|
+
* - **textures** from images, canvases, bitmaps and **videos** (updated per
|
|
18
|
+
* decoded frame with `requestVideoFrameCallback`; `scrubVideo()` seeks a
|
|
19
|
+
* video from a 0–1 progress, e.g. a scroll scene);
|
|
20
|
+
* - `orbitControls()` — drag / wheel / pinch / arrow keys, damping, optional
|
|
21
|
+
* auto-rotate (off under reduced motion).
|
|
22
|
+
*
|
|
23
|
+
* Math, geometry and the scene graph are pure (SSR / workers); rendering
|
|
24
|
+
* needs WebGL2 (`createRenderer()` throws a clear error without it). Model
|
|
25
|
+
* loaders live in `motionary/runtime/format-gltf` and `format-obj`.
|
|
26
|
+
*/
|
|
27
|
+
const mat4 = {
|
|
28
|
+
identity: () => new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]),
|
|
29
|
+
multiply(a, b, out = new Float32Array(16)) {
|
|
30
|
+
for (let c = 0; c < 4; c++)
|
|
31
|
+
for (let r = 0; r < 4; r++)
|
|
32
|
+
out[c * 4 + r] = a[r] * b[c * 4] + a[4 + r] * b[c * 4 + 1] + a[8 + r] * b[c * 4 + 2] + a[12 + r] * b[c * 4 + 3];
|
|
33
|
+
return out;
|
|
34
|
+
},
|
|
35
|
+
perspective(fovy, aspect, near, far) {
|
|
36
|
+
const f = 1 / Math.tan(fovy / 2), nf = 1 / (near - far);
|
|
37
|
+
return new Float32Array([f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, (far + near) * nf, -1, 0, 0, 2 * far * near * nf, 0]);
|
|
38
|
+
},
|
|
39
|
+
lookAt(eye, target, up = [0, 1, 0]) {
|
|
40
|
+
let z = sub(eye, target);
|
|
41
|
+
z = norm(z.some((v) => v) ? z : [0, 0, 1]);
|
|
42
|
+
let x = cross(up, z);
|
|
43
|
+
if (!x.some((v) => Math.abs(v) > 1e-9))
|
|
44
|
+
x = cross([0, 0, 1], z);
|
|
45
|
+
x = norm(x);
|
|
46
|
+
const y = cross(z, x);
|
|
47
|
+
return new Float32Array([x[0], y[0], z[0], 0, x[1], y[1], z[1], 0, x[2], y[2], z[2], 0, -dot(x, eye), -dot(y, eye), -dot(z, eye), 1]);
|
|
48
|
+
},
|
|
49
|
+
/** Translation · rotation (quaternion) · scale. */
|
|
50
|
+
compose(t, q, s) {
|
|
51
|
+
const [x, y, z, w] = q, x2 = x + x, y2 = y + y, z2 = z + z;
|
|
52
|
+
const xx = x * x2, xy = x * y2, xz = x * z2, yy = y * y2, yz = y * z2, zz = z * z2, wx = w * x2, wy = w * y2, wz = w * z2;
|
|
53
|
+
return new Float32Array([(1 - (yy + zz)) * s[0], (xy + wz) * s[0], (xz - wy) * s[0], 0, (xy - wz) * s[1], (1 - (xx + zz)) * s[1], (yz + wx) * s[1], 0, (xz + wy) * s[2], (yz - wx) * s[2], (1 - (xx + yy)) * s[2], 0, t[0], t[1], t[2], 1]);
|
|
54
|
+
},
|
|
55
|
+
invert(m) {
|
|
56
|
+
const o = new Float32Array(16);
|
|
57
|
+
const [a00, a01, a02, a03, a10, a11, a12, a13, a20, a21, a22, a23, a30, a31, a32, a33] = m;
|
|
58
|
+
const b00 = a00 * a11 - a01 * a10, b01 = a00 * a12 - a02 * a10, b02 = a00 * a13 - a03 * a10, b03 = a01 * a12 - a02 * a11, b04 = a01 * a13 - a03 * a11, b05 = a02 * a13 - a03 * a12;
|
|
59
|
+
const b06 = a20 * a31 - a21 * a30, b07 = a20 * a32 - a22 * a30, b08 = a20 * a33 - a23 * a30, b09 = a21 * a32 - a22 * a31, b10 = a21 * a33 - a23 * a31, b11 = a22 * a33 - a23 * a32;
|
|
60
|
+
let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
|
|
61
|
+
if (!det)
|
|
62
|
+
return null;
|
|
63
|
+
det = 1 / det;
|
|
64
|
+
o[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
|
|
65
|
+
o[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
|
|
66
|
+
o[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
|
|
67
|
+
o[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
|
|
68
|
+
o[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
|
|
69
|
+
o[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
|
|
70
|
+
o[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
|
|
71
|
+
o[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
|
|
72
|
+
o[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
|
|
73
|
+
o[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
|
|
74
|
+
o[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
|
|
75
|
+
o[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
|
|
76
|
+
o[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
|
|
77
|
+
o[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
|
|
78
|
+
o[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
|
|
79
|
+
o[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
|
|
80
|
+
return o;
|
|
81
|
+
},
|
|
82
|
+
/** Inverse-transpose of the upper 3×3 (normal matrix), as a mat3. */
|
|
83
|
+
normal(m) {
|
|
84
|
+
const i = mat4.invert(m) || mat4.identity();
|
|
85
|
+
return new Float32Array([i[0], i[4], i[8], i[1], i[5], i[9], i[2], i[6], i[10]]);
|
|
86
|
+
},
|
|
87
|
+
transformPoint(m, p) {
|
|
88
|
+
const w = m[3] * p[0] + m[7] * p[1] + m[11] * p[2] + m[15] || 1;
|
|
89
|
+
return [(m[0] * p[0] + m[4] * p[1] + m[8] * p[2] + m[12]) / w, (m[1] * p[0] + m[5] * p[1] + m[9] * p[2] + m[13]) / w, (m[2] * p[0] + m[6] * p[1] + m[10] * p[2] + m[14]) / w];
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
93
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
94
|
+
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
95
|
+
const norm = (a) => {
|
|
96
|
+
const l = Math.hypot(a[0], a[1], a[2]) || 1;
|
|
97
|
+
return [a[0] / l, a[1] / l, a[2] / l];
|
|
98
|
+
};
|
|
99
|
+
/** Quaternion from Euler angles (radians, XYZ order). */
|
|
100
|
+
function quatFromEuler(x, y, z) {
|
|
101
|
+
const cx = Math.cos(x / 2), sx = Math.sin(x / 2), cy = Math.cos(y / 2), sy = Math.sin(y / 2), cz = Math.cos(z / 2), sz = Math.sin(z / 2);
|
|
102
|
+
return [sx * cy * cz + cx * sy * sz, cx * sy * cz - sx * cy * sz, cx * cy * sz + sx * sy * cz, cx * cy * cz - sx * sy * sz];
|
|
103
|
+
}
|
|
104
|
+
/** Hamilton product a·b. */
|
|
105
|
+
function quatMultiply(a, b) {
|
|
106
|
+
return [a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1], a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0], a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3], a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]];
|
|
107
|
+
}
|
|
108
|
+
/** Spherical interpolation. */
|
|
109
|
+
function quatSlerp(a, b, t) {
|
|
110
|
+
let d = a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
|
|
111
|
+
const bb = d < 0 ? b.map((v) => -v) : b;
|
|
112
|
+
d = Math.abs(d);
|
|
113
|
+
if (d > 0.9995) {
|
|
114
|
+
const r = a.map((v, i) => v + (bb[i] - v) * t);
|
|
115
|
+
const l = Math.hypot(...r) || 1;
|
|
116
|
+
return r.map((v) => v / l);
|
|
117
|
+
}
|
|
118
|
+
const th = Math.acos(d), s = Math.sin(th), wa = Math.sin((1 - t) * th) / s, wb = Math.sin(t * th) / s;
|
|
119
|
+
return a.map((v, i) => v * wa + bb[i] * wb);
|
|
120
|
+
}
|
|
121
|
+
const geo = (p, n, uv, idx) => ({ positions: new Float32Array(p), normals: new Float32Array(n), uvs: new Float32Array(uv), indices: p.length / 3 > 65535 ? new Uint32Array(idx) : new Uint16Array(idx) });
|
|
122
|
+
/** Axis-aligned box centred on the origin. */
|
|
123
|
+
function box(w = 1, h = 1, d = 1) {
|
|
124
|
+
const p = [], n = [], uv = [], idx = [];
|
|
125
|
+
const faces = [[[1, 0, 0], [0, 0, -1], [0, 1, 0]], [[-1, 0, 0], [0, 0, 1], [0, 1, 0]], [[0, 1, 0], [1, 0, 0], [0, 0, -1]], [[0, -1, 0], [1, 0, 0], [0, 0, 1]], [[0, 0, 1], [1, 0, 0], [0, 1, 0]], [[0, 0, -1], [-1, 0, 0], [0, 1, 0]]];
|
|
126
|
+
const half = [w / 2, h / 2, d / 2];
|
|
127
|
+
for (const [nn, u, v] of faces) {
|
|
128
|
+
const b = p.length / 3;
|
|
129
|
+
for (const [su, sv] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) {
|
|
130
|
+
for (let k = 0; k < 3; k++)
|
|
131
|
+
p.push((nn[k] + u[k] * su + v[k] * sv) * half[k]);
|
|
132
|
+
n.push(...nn);
|
|
133
|
+
uv.push((su + 1) / 2, (1 - sv) / 2);
|
|
134
|
+
}
|
|
135
|
+
idx.push(b, b + 1, b + 2, b, b + 2, b + 3);
|
|
136
|
+
}
|
|
137
|
+
return geo(p, n, uv, idx);
|
|
138
|
+
}
|
|
139
|
+
/** XY plane facing +Z. */
|
|
140
|
+
function plane(w = 1, h = 1, sx = 1, sy = 1) {
|
|
141
|
+
const p = [], n = [], uv = [], idx = [];
|
|
142
|
+
for (let j = 0; j <= sy; j++)
|
|
143
|
+
for (let i = 0; i <= sx; i++) {
|
|
144
|
+
p.push((i / sx - 0.5) * w, (j / sy - 0.5) * h, 0);
|
|
145
|
+
n.push(0, 0, 1);
|
|
146
|
+
uv.push(i / sx, 1 - j / sy);
|
|
147
|
+
}
|
|
148
|
+
for (let j = 0; j < sy; j++)
|
|
149
|
+
for (let i = 0; i < sx; i++) {
|
|
150
|
+
const a = j * (sx + 1) + i, b = a + 1, c = a + sx + 1, d = c + 1;
|
|
151
|
+
idx.push(a, b, d, a, d, c);
|
|
152
|
+
}
|
|
153
|
+
return geo(p, n, uv, idx);
|
|
154
|
+
}
|
|
155
|
+
/** UV sphere. */
|
|
156
|
+
function sphere(r = 0.5, ws = 32, hs = 16) {
|
|
157
|
+
const p = [], n = [], uv = [], idx = [];
|
|
158
|
+
for (let j = 0; j <= hs; j++) {
|
|
159
|
+
const v = j / hs, th = v * Math.PI;
|
|
160
|
+
for (let i = 0; i <= ws; i++) {
|
|
161
|
+
const u = i / ws, ph = u * Math.PI * 2;
|
|
162
|
+
const x = -Math.cos(ph) * Math.sin(th), y = Math.cos(th), z = Math.sin(ph) * Math.sin(th);
|
|
163
|
+
p.push(x * r, y * r, z * r);
|
|
164
|
+
n.push(x, y, z);
|
|
165
|
+
uv.push(u, v);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
for (let j = 0; j < hs; j++)
|
|
169
|
+
for (let i = 0; i < ws; i++) {
|
|
170
|
+
const a = j * (ws + 1) + i, b = a + ws + 1;
|
|
171
|
+
if (j)
|
|
172
|
+
idx.push(a, b, a + 1);
|
|
173
|
+
if (j < hs - 1)
|
|
174
|
+
idx.push(a + 1, b, b + 1);
|
|
175
|
+
}
|
|
176
|
+
return geo(p, n, uv, idx);
|
|
177
|
+
}
|
|
178
|
+
/** Torus in the XY plane. */
|
|
179
|
+
function torus(R = 0.5, r = 0.18, rs = 48, ts = 16) {
|
|
180
|
+
const p = [], n = [], uv = [], idx = [];
|
|
181
|
+
for (let j = 0; j <= ts; j++)
|
|
182
|
+
for (let i = 0; i <= rs; i++) {
|
|
183
|
+
const u = (i / rs) * Math.PI * 2, v = (j / ts) * Math.PI * 2;
|
|
184
|
+
const cx = R * Math.cos(u), cy = R * Math.sin(u);
|
|
185
|
+
const x = (R + r * Math.cos(v)) * Math.cos(u), y = (R + r * Math.cos(v)) * Math.sin(u), z = r * Math.sin(v);
|
|
186
|
+
p.push(x, y, z);
|
|
187
|
+
const nn = norm([x - cx, y - cy, z]);
|
|
188
|
+
n.push(...nn);
|
|
189
|
+
uv.push(i / rs, j / ts);
|
|
190
|
+
}
|
|
191
|
+
for (let j = 1; j <= ts; j++)
|
|
192
|
+
for (let i = 1; i <= rs; i++) {
|
|
193
|
+
const a = (rs + 1) * j + i - 1, b = (rs + 1) * (j - 1) + i - 1, c = (rs + 1) * (j - 1) + i, d = (rs + 1) * j + i;
|
|
194
|
+
idx.push(a, b, d, b, c, d);
|
|
195
|
+
}
|
|
196
|
+
return geo(p, n, uv, idx);
|
|
197
|
+
}
|
|
198
|
+
/** Flat normals for a geometry without them (per triangle; un-indexes the mesh). */
|
|
199
|
+
function computeNormals(g) {
|
|
200
|
+
const P = g.positions, I = g.indices;
|
|
201
|
+
const count = I ? I.length : P.length / 3;
|
|
202
|
+
const pos = new Float32Array(count * 3), nrm = new Float32Array(count * 3), uvs = g.uvs ? new Float32Array(count * 2) : undefined;
|
|
203
|
+
for (let t = 0; t < count; t += 3) {
|
|
204
|
+
const v = [0, 1, 2].map((k) => (I ? I[t + k] : t + k));
|
|
205
|
+
const a = [P[v[0] * 3], P[v[0] * 3 + 1], P[v[0] * 3 + 2]], b = [P[v[1] * 3], P[v[1] * 3 + 1], P[v[1] * 3 + 2]], c = [P[v[2] * 3], P[v[2] * 3 + 1], P[v[2] * 3 + 2]];
|
|
206
|
+
const nn = norm(cross(sub(b, a), sub(c, a)));
|
|
207
|
+
v.forEach((vi, k) => {
|
|
208
|
+
pos.set(P.subarray(vi * 3, vi * 3 + 3), (t + k) * 3);
|
|
209
|
+
nrm.set(nn, (t + k) * 3);
|
|
210
|
+
if (uvs && g.uvs)
|
|
211
|
+
uvs.set(g.uvs.subarray(vi * 2, vi * 2 + 2), (t + k) * 2);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return { positions: pos, normals: nrm, uvs, mode: g.mode };
|
|
215
|
+
}
|
|
216
|
+
function standardMaterial(o = {}) {
|
|
217
|
+
return { type: 'standard', color: [1, 1, 1, 1], metallic: 0, roughness: 0.6, emissive: [0, 0, 0], ...o };
|
|
218
|
+
}
|
|
219
|
+
function unlitMaterial(o = {}) {
|
|
220
|
+
return { ...standardMaterial(o), type: 'unlit' };
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Custom fragment shader: `fragment` is GLSL ES 3.00 code defining
|
|
224
|
+
* `vec4 shade()`; available: `v_pos` (world), `v_nrm`, `v_uv`, `u_time`,
|
|
225
|
+
* `u_camPos`, `u_res` and your `uniforms` (float, vec2–4, sampler2D).
|
|
226
|
+
*/
|
|
227
|
+
function shaderMaterial(fragment, uniforms = {}, o = {}) {
|
|
228
|
+
return { ...standardMaterial(o), type: 'shader', fragment, uniforms };
|
|
229
|
+
}
|
|
230
|
+
/** '#ff8800' / 'rgb(…)' / [r, g, b] → linear-ready [r, g, b, a] (sRGB values 0–1; the shader linearises). */
|
|
231
|
+
function color(c, a = 1) {
|
|
232
|
+
if (Array.isArray(c))
|
|
233
|
+
return [c[0], c[1], c[2], c[3] ?? a];
|
|
234
|
+
const h = /^#([0-9a-f]{3,8})$/i.exec(c.trim());
|
|
235
|
+
if (h) {
|
|
236
|
+
let s = h[1];
|
|
237
|
+
if (s.length <= 4)
|
|
238
|
+
s = s.split('').map((x) => x + x).join('');
|
|
239
|
+
const v = parseInt(s.slice(0, 6), 16);
|
|
240
|
+
return [((v >> 16) & 255) / 255, ((v >> 8) & 255) / 255, (v & 255) / 255, s.length === 8 ? parseInt(s.slice(6), 16) / 255 : a];
|
|
241
|
+
}
|
|
242
|
+
const m = /rgba?\(([^)]+)\)/.exec(c);
|
|
243
|
+
if (m) {
|
|
244
|
+
const p = m[1].split(/[\s,/]+/).filter(Boolean).map(Number);
|
|
245
|
+
return [p[0] / 255, p[1] / 255, p[2] / 255, p[3] ?? a];
|
|
246
|
+
}
|
|
247
|
+
return [1, 1, 1, a];
|
|
248
|
+
}
|
|
249
|
+
function texture(source, o = {}) {
|
|
250
|
+
const isVideo = typeof HTMLVideoElement !== 'undefined' && source instanceof HTMLVideoElement;
|
|
251
|
+
return { source, srgb: true, repeat: true, flipY: false, video: isVideo, needsUpdate: true, ...o };
|
|
252
|
+
}
|
|
253
|
+
class GlNode {
|
|
254
|
+
constructor(name = '', mesh = null) {
|
|
255
|
+
this.name = '';
|
|
256
|
+
this.position = [0, 0, 0];
|
|
257
|
+
this.rotation = [0, 0, 0, 1];
|
|
258
|
+
this.scale = [1, 1, 1];
|
|
259
|
+
/** Set to use a fixed local matrix instead of position / rotation / scale (glTF `matrix`). */
|
|
260
|
+
this.matrix = null;
|
|
261
|
+
this.children = [];
|
|
262
|
+
this.parent = null;
|
|
263
|
+
this.mesh = null;
|
|
264
|
+
this.visible = true;
|
|
265
|
+
/** Free-form data (loaders put source info here). */
|
|
266
|
+
this.extras = {};
|
|
267
|
+
this.world = mat4.identity();
|
|
268
|
+
this.name = name;
|
|
269
|
+
this.mesh = mesh;
|
|
270
|
+
}
|
|
271
|
+
add(...nodes) {
|
|
272
|
+
for (const n of nodes) {
|
|
273
|
+
n.parent?.remove(n);
|
|
274
|
+
n.parent = this;
|
|
275
|
+
this.children.push(n);
|
|
276
|
+
}
|
|
277
|
+
return this;
|
|
278
|
+
}
|
|
279
|
+
remove(n) {
|
|
280
|
+
const i = this.children.indexOf(n);
|
|
281
|
+
if (i >= 0)
|
|
282
|
+
this.children.splice(i, 1);
|
|
283
|
+
n.parent = null;
|
|
284
|
+
}
|
|
285
|
+
setEuler(x, y, z) {
|
|
286
|
+
this.rotation = quatFromEuler(x, y, z);
|
|
287
|
+
return this;
|
|
288
|
+
}
|
|
289
|
+
local() {
|
|
290
|
+
return this.matrix || mat4.compose(this.position, this.rotation, this.scale);
|
|
291
|
+
}
|
|
292
|
+
/** Recompute world matrices of this subtree. */
|
|
293
|
+
updateWorld(parent) {
|
|
294
|
+
const m = parent ? mat4.multiply(parent, this.local()) : this.local();
|
|
295
|
+
this.world.set(m);
|
|
296
|
+
for (const c of this.children)
|
|
297
|
+
c.updateWorld(this.world);
|
|
298
|
+
}
|
|
299
|
+
traverse(fn) {
|
|
300
|
+
fn(this);
|
|
301
|
+
for (const c of this.children)
|
|
302
|
+
c.traverse(fn);
|
|
303
|
+
}
|
|
304
|
+
find(name) {
|
|
305
|
+
let hit = null;
|
|
306
|
+
this.traverse((n) => {
|
|
307
|
+
if (!hit && n.name === name)
|
|
308
|
+
hit = n;
|
|
309
|
+
});
|
|
310
|
+
return hit;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
class Camera extends GlNode {
|
|
314
|
+
constructor(o = {}) {
|
|
315
|
+
super('camera');
|
|
316
|
+
this.fov = (45 * Math.PI) / 180;
|
|
317
|
+
this.near = 0.05;
|
|
318
|
+
this.far = 500;
|
|
319
|
+
this.aspect = 1;
|
|
320
|
+
this.target = [0, 0, 0];
|
|
321
|
+
this.up = [0, 1, 0];
|
|
322
|
+
if (o.fov)
|
|
323
|
+
this.fov = (o.fov * Math.PI) / 180;
|
|
324
|
+
if (o.near)
|
|
325
|
+
this.near = o.near;
|
|
326
|
+
if (o.far)
|
|
327
|
+
this.far = o.far;
|
|
328
|
+
if (o.position)
|
|
329
|
+
this.position = [...o.position];
|
|
330
|
+
if (o.target)
|
|
331
|
+
this.target = [...o.target];
|
|
332
|
+
}
|
|
333
|
+
view() {
|
|
334
|
+
return mat4.lookAt(this.position, this.target, this.up);
|
|
335
|
+
}
|
|
336
|
+
projection() {
|
|
337
|
+
return mat4.perspective(this.fov, this.aspect, this.near, this.far);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
class Scene {
|
|
341
|
+
constructor() {
|
|
342
|
+
this.root = new GlNode('scene');
|
|
343
|
+
this.ambient = [0.35, 0.35, 0.4];
|
|
344
|
+
this.lights = [{ type: 'directional', vector: [-0.4, -1, -0.6], color: [1, 1, 1], intensity: 2.2 }];
|
|
345
|
+
/** Clear colour (sRGB, 0–1) or null for transparent. */
|
|
346
|
+
this.background = null;
|
|
347
|
+
this.exposure = 1;
|
|
348
|
+
}
|
|
349
|
+
add(...n) {
|
|
350
|
+
this.root.add(...n);
|
|
351
|
+
return this;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/** World-space bounding box of a subtree's meshes. */
|
|
355
|
+
function bounds(node) {
|
|
356
|
+
node.updateWorld(node.parent ? node.parent.world : undefined);
|
|
357
|
+
const min = [Infinity, Infinity, Infinity], max = [-Infinity, -Infinity, -Infinity];
|
|
358
|
+
node.traverse((n) => {
|
|
359
|
+
const meshes = n.mesh ? (Array.isArray(n.mesh) ? n.mesh : [n.mesh]) : [];
|
|
360
|
+
for (const m of meshes) {
|
|
361
|
+
const P = m.geometry.positions;
|
|
362
|
+
for (let i = 0; i < P.length; i += 3) {
|
|
363
|
+
const q = mat4.transformPoint(n.world, [P[i], P[i + 1], P[i + 2]]);
|
|
364
|
+
for (let k = 0; k < 3; k++) {
|
|
365
|
+
if (q[k] < min[k])
|
|
366
|
+
min[k] = q[k];
|
|
367
|
+
if (q[k] > max[k])
|
|
368
|
+
max[k] = q[k];
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
if (min[0] === Infinity)
|
|
374
|
+
return { min: [0, 0, 0], max: [0, 0, 0], center: [0, 0, 0], radius: 0 };
|
|
375
|
+
const center = [(min[0] + max[0]) / 2, (min[1] + max[1]) / 2, (min[2] + max[2]) / 2];
|
|
376
|
+
return { min, max, center, radius: Math.hypot(max[0] - center[0], max[1] - center[1], max[2] - center[2]) };
|
|
377
|
+
}
|
|
378
|
+
/** Point the camera at a subtree so it fills the view. */
|
|
379
|
+
function frameNode(camera, node, padding = 1.25) {
|
|
380
|
+
const b = bounds(node);
|
|
381
|
+
const dist = ((b.radius || 1) * padding) / Math.sin(camera.fov / 2);
|
|
382
|
+
const dir = norm(sub(camera.position, camera.target).some((v) => v) ? sub(camera.position, camera.target) : [0.6, 0.4, 1]);
|
|
383
|
+
camera.target = b.center;
|
|
384
|
+
camera.position = [b.center[0] + dir[0] * dist, b.center[1] + dir[1] * dist, b.center[2] + dir[2] * dist];
|
|
385
|
+
camera.near = Math.max(0.01, dist / 100);
|
|
386
|
+
camera.far = dist * 10 + b.radius * 2;
|
|
387
|
+
}
|
|
388
|
+
// ------------------------------------------------------------------ video
|
|
389
|
+
/** Seek a video from a 0–1 progress (scroll-scrubbed video). Pauses it. */
|
|
390
|
+
function scrubVideo(video, progress) {
|
|
391
|
+
if (!video.duration || !isFinite(video.duration))
|
|
392
|
+
return;
|
|
393
|
+
video.pause();
|
|
394
|
+
const t = Math.min(video.duration - 0.001, Math.max(0, progress * video.duration));
|
|
395
|
+
if ('fastSeek' in video && Math.abs(video.currentTime - t) > 1)
|
|
396
|
+
video.fastSeek(t);
|
|
397
|
+
else
|
|
398
|
+
video.currentTime = t;
|
|
399
|
+
}
|
|
400
|
+
// ------------------------------------------------------------------ renderer
|
|
401
|
+
const VS = `#version 300 es
|
|
402
|
+
layout(location=0) in vec3 a_pos;layout(location=1) in vec3 a_nrm;layout(location=2) in vec2 a_uv;
|
|
403
|
+
uniform mat4 u_model,u_view,u_proj;uniform mat3 u_nmat;out vec3 v_pos,v_nrm;out vec2 v_uv;
|
|
404
|
+
void main(){vec4 w=u_model*vec4(a_pos,1.0);v_pos=w.xyz;v_nrm=normalize(u_nmat*a_nrm);v_uv=a_uv;gl_Position=u_proj*u_view*w;gl_PointSize=2.0;}`;
|
|
405
|
+
const FS_HEAD = `#version 300 es
|
|
406
|
+
precision highp float;in vec3 v_pos,v_nrm;in vec2 v_uv;out vec4 o;
|
|
407
|
+
uniform vec4 u_color;uniform vec3 u_emissive,u_camPos,u_ambient;uniform float u_metal,u_rough,u_exposure,u_time;uniform vec2 u_res;
|
|
408
|
+
uniform sampler2D u_map;uniform int u_hasMap,u_nl,u_unlit;uniform vec4 u_lvec[4];uniform vec3 u_lcol[4];
|
|
409
|
+
vec3 lin(vec3 c){return pow(c,vec3(2.2));}vec3 srgb(vec3 c){return pow(c,vec3(1.0/2.2));}
|
|
410
|
+
`;
|
|
411
|
+
const FS_STD = `${FS_HEAD}void main(){vec4 base=vec4(lin(u_color.rgb),u_color.a);if(u_hasMap==1){vec4 t=texture(u_map,v_uv);base*=vec4(lin(t.rgb),t.a);}
|
|
412
|
+
if(u_unlit==1){o=vec4(srgb(base.rgb),base.a);return;}
|
|
413
|
+
vec3 N=normalize(v_nrm);if(!gl_FrontFacing)N=-N;vec3 V=normalize(u_camPos-v_pos);float r=clamp(u_rough,0.04,1.0),m=clamp(u_metal,0.0,1.0);
|
|
414
|
+
vec3 F0=mix(vec3(0.04),base.rgb,m);vec3 c=u_ambient*base.rgb*(1.0-0.6*m)+u_ambient*F0*0.4;
|
|
415
|
+
for(int i=0;i<4;i++){if(i>=u_nl)break;vec3 L;float at=1.0;if(u_lvec[i].w>0.5){vec3 d=u_lvec[i].xyz-v_pos;float l=length(d);L=d/l;at=1.0/(1.0+0.09*l*l);}else L=normalize(-u_lvec[i].xyz);
|
|
416
|
+
vec3 H=normalize(L+V);float NL=max(dot(N,L),0.0),NH=max(dot(N,H),0.0),NV=max(dot(N,V),1e-3),a=r*r,a2=a*a,dd=NH*NH*(a2-1.0)+1.0,D=a2/(3.14159*dd*dd),k=(r+1.0)*(r+1.0)/8.0;
|
|
417
|
+
float G=NL/(NL*(1.0-k)+k)*NV/(NV*(1.0-k)+k);vec3 F=F0+(1.0-F0)*pow(1.0-max(dot(H,V),0.0),5.0);vec3 S=D*G*F/(4.0*NL*NV+1e-3);
|
|
418
|
+
c+=((1.0-F)*(1.0-m)*base.rgb/3.14159+S)*u_lcol[i]*NL*at;}
|
|
419
|
+
c=(c+u_emissive)*u_exposure;c=c/(c+1.0);o=vec4(srgb(c),base.a);}`;
|
|
420
|
+
/** Create a WebGL2 renderer on a canvas (throws a clear error when WebGL2 is unavailable). */
|
|
421
|
+
function createRenderer(canvas, o = {}) {
|
|
422
|
+
const gl = canvas.getContext('webgl2', { alpha: o.alpha ?? true, antialias: o.antialias ?? true, premultipliedAlpha: false, preserveDrawingBuffer: !!o.preserveDrawingBuffer });
|
|
423
|
+
if (!gl)
|
|
424
|
+
throw new Error('[motionary] runtime/gl: WebGL2 is not available in this browser / context');
|
|
425
|
+
const programs = new Map();
|
|
426
|
+
const owned = [];
|
|
427
|
+
let draws = 0;
|
|
428
|
+
const compile = (key, fs) => {
|
|
429
|
+
let pr = programs.get(key);
|
|
430
|
+
if (pr)
|
|
431
|
+
return pr;
|
|
432
|
+
const sh = (type, src) => {
|
|
433
|
+
const s = gl.createShader(type);
|
|
434
|
+
gl.shaderSource(s, src);
|
|
435
|
+
gl.compileShader(s);
|
|
436
|
+
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS))
|
|
437
|
+
throw new Error('[motionary] runtime/gl shader: ' + gl.getShaderInfoLog(s));
|
|
438
|
+
return s;
|
|
439
|
+
};
|
|
440
|
+
const p = gl.createProgram();
|
|
441
|
+
gl.attachShader(p, sh(gl.VERTEX_SHADER, VS));
|
|
442
|
+
gl.attachShader(p, sh(gl.FRAGMENT_SHADER, fs));
|
|
443
|
+
gl.linkProgram(p);
|
|
444
|
+
if (!gl.getProgramParameter(p, gl.LINK_STATUS))
|
|
445
|
+
throw new Error('[motionary] runtime/gl link: ' + gl.getProgramInfoLog(p));
|
|
446
|
+
pr = { p, u: new Map() };
|
|
447
|
+
programs.set(key, pr);
|
|
448
|
+
owned.push({ del: () => gl.deleteProgram(p) });
|
|
449
|
+
return pr;
|
|
450
|
+
};
|
|
451
|
+
const loc = (pr, n) => {
|
|
452
|
+
if (!pr.u.has(n))
|
|
453
|
+
pr.u.set(n, gl.getUniformLocation(pr.p, n));
|
|
454
|
+
return pr.u.get(n);
|
|
455
|
+
};
|
|
456
|
+
const uploadGeo = (g) => {
|
|
457
|
+
if (g._gpu)
|
|
458
|
+
return g._gpu;
|
|
459
|
+
const vao = gl.createVertexArray();
|
|
460
|
+
gl.bindVertexArray(vao);
|
|
461
|
+
const attr = (i, data, size, fallback) => {
|
|
462
|
+
if (!data) {
|
|
463
|
+
gl.disableVertexAttribArray(i);
|
|
464
|
+
if (size === 3)
|
|
465
|
+
gl.vertexAttrib3f(i, fallback[0], fallback[1], fallback[2]);
|
|
466
|
+
else
|
|
467
|
+
gl.vertexAttrib2f(i, fallback[0], fallback[1]);
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const b = gl.createBuffer();
|
|
471
|
+
owned.push({ del: () => gl.deleteBuffer(b) });
|
|
472
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, b);
|
|
473
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
|
|
474
|
+
gl.enableVertexAttribArray(i);
|
|
475
|
+
gl.vertexAttribPointer(i, size, gl.FLOAT, false, 0, 0);
|
|
476
|
+
};
|
|
477
|
+
attr(0, g.positions, 3, [0, 0, 0]);
|
|
478
|
+
attr(1, g.normals, 3, [0, 0, 1]);
|
|
479
|
+
attr(2, g.uvs, 2, [0, 0]);
|
|
480
|
+
let count = g.positions.length / 3, type = 0;
|
|
481
|
+
if (g.indices) {
|
|
482
|
+
const b = gl.createBuffer();
|
|
483
|
+
owned.push({ del: () => gl.deleteBuffer(b) });
|
|
484
|
+
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, b);
|
|
485
|
+
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, g.indices, gl.STATIC_DRAW);
|
|
486
|
+
count = g.indices.length;
|
|
487
|
+
type = g.indices instanceof Uint32Array ? gl.UNSIGNED_INT : gl.UNSIGNED_SHORT;
|
|
488
|
+
}
|
|
489
|
+
gl.bindVertexArray(null);
|
|
490
|
+
owned.push({ del: () => gl.deleteVertexArray(vao) });
|
|
491
|
+
return (g._gpu = { vao, count, type, indexed: !!g.indices });
|
|
492
|
+
};
|
|
493
|
+
const uploadTex = (t, unit) => {
|
|
494
|
+
let tex = t._gpu;
|
|
495
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
496
|
+
if (!tex) {
|
|
497
|
+
tex = gl.createTexture();
|
|
498
|
+
t._gpu = tex;
|
|
499
|
+
owned.push({ del: () => gl.deleteTexture(tex) });
|
|
500
|
+
t.needsUpdate = true;
|
|
501
|
+
if (t.video) {
|
|
502
|
+
const v = t.source;
|
|
503
|
+
const mark = () => {
|
|
504
|
+
t.needsUpdate = true;
|
|
505
|
+
v.requestVideoFrameCallback?.(mark);
|
|
506
|
+
};
|
|
507
|
+
if (v.requestVideoFrameCallback)
|
|
508
|
+
v.requestVideoFrameCallback(mark);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
gl.bindTexture(gl.TEXTURE_2D, tex);
|
|
512
|
+
const v = t.video ? t.source : null;
|
|
513
|
+
const videoReady = !v || v.readyState >= 2;
|
|
514
|
+
if ((t.needsUpdate || (v && !v.requestVideoFrameCallback && !v.paused)) && videoReady) {
|
|
515
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, !!t.flipY);
|
|
516
|
+
const s = t.source;
|
|
517
|
+
if (s && s.data && !(s instanceof ImageData))
|
|
518
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, s.width, s.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array(s.data.buffer, s.data.byteOffset, s.data.byteLength));
|
|
519
|
+
else
|
|
520
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, s);
|
|
521
|
+
const pot = (n) => (n & (n - 1)) === 0;
|
|
522
|
+
const w = s.videoWidth || s.naturalWidth || s.width, h = s.videoHeight || s.naturalHeight || s.height;
|
|
523
|
+
const mip = !v && pot(w) && pot(h);
|
|
524
|
+
if (mip)
|
|
525
|
+
gl.generateMipmap(gl.TEXTURE_2D);
|
|
526
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, mip ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
|
|
527
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
528
|
+
const wrap = t.repeat && pot(w) && pot(h) ? gl.REPEAT : gl.CLAMP_TO_EDGE;
|
|
529
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap);
|
|
530
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrap);
|
|
531
|
+
t.needsUpdate = false;
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
const r = {
|
|
535
|
+
gl,
|
|
536
|
+
canvas,
|
|
537
|
+
get drawCalls() {
|
|
538
|
+
return draws;
|
|
539
|
+
},
|
|
540
|
+
resize(w, h) {
|
|
541
|
+
const c = canvas;
|
|
542
|
+
const dpr = Math.min(o.maxDpr ?? 2, (typeof devicePixelRatio === 'number' && devicePixelRatio) || 1);
|
|
543
|
+
const W = Math.max(1, Math.round((w ?? c.clientWidth ?? c.width) * dpr)), H = Math.max(1, Math.round((h ?? c.clientHeight ?? c.height) * dpr));
|
|
544
|
+
if (c.width === W && c.height === H)
|
|
545
|
+
return false;
|
|
546
|
+
c.width = W;
|
|
547
|
+
c.height = H;
|
|
548
|
+
return true;
|
|
549
|
+
},
|
|
550
|
+
render(scene, camera, time = 0) {
|
|
551
|
+
draws = 0;
|
|
552
|
+
const W = gl.drawingBufferWidth, H = gl.drawingBufferHeight;
|
|
553
|
+
gl.viewport(0, 0, W, H);
|
|
554
|
+
camera.aspect = W / H || 1;
|
|
555
|
+
const bg = scene.background;
|
|
556
|
+
gl.clearColor(bg ? bg[0] : 0, bg ? bg[1] : 0, bg ? bg[2] : 0, bg ? bg[3] : 0);
|
|
557
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
558
|
+
gl.enable(gl.DEPTH_TEST);
|
|
559
|
+
scene.root.updateWorld();
|
|
560
|
+
const view = camera.view(), proj = camera.projection();
|
|
561
|
+
const lv = new Float32Array(16), lc = new Float32Array(12);
|
|
562
|
+
scene.lights.slice(0, 4).forEach((l, i) => {
|
|
563
|
+
lv.set([...l.vector, l.type === 'point' ? 1 : 0], i * 4);
|
|
564
|
+
lc.set(l.color.map((c) => c * l.intensity), i * 3);
|
|
565
|
+
});
|
|
566
|
+
const opaque = [], blend = [];
|
|
567
|
+
scene.root.traverse((n) => {
|
|
568
|
+
if (!n.visible || !n.mesh)
|
|
569
|
+
return;
|
|
570
|
+
for (const m of Array.isArray(n.mesh) ? n.mesh : [n.mesh])
|
|
571
|
+
(m.material.transparent || m.material.color[3] < 1 ? blend : opaque).push([n, m]);
|
|
572
|
+
});
|
|
573
|
+
for (const [n, m] of [...opaque, ...blend]) {
|
|
574
|
+
const mat = m.material;
|
|
575
|
+
const key = mat.type === 'shader' ? 'shader:' + mat.fragment : 'std';
|
|
576
|
+
const pr = compile(key, mat.type === 'shader' ? `${FS_HEAD}${Object.entries(mat.uniforms || {}).map(([k, v]) => `uniform ${typeof v === 'number' ? 'float' : v.source ? 'sampler2D' : `vec${v.length}`} ${k};`).join('')}\n${mat.fragment}\nvoid main(){o=shade();}` : FS_STD);
|
|
577
|
+
gl.useProgram(pr.p);
|
|
578
|
+
const U = (nm) => loc(pr, nm);
|
|
579
|
+
gl.uniformMatrix4fv(U('u_model'), false, n.world);
|
|
580
|
+
gl.uniformMatrix4fv(U('u_view'), false, view);
|
|
581
|
+
gl.uniformMatrix4fv(U('u_proj'), false, proj);
|
|
582
|
+
gl.uniformMatrix3fv(U('u_nmat'), false, mat4.normal(n.world));
|
|
583
|
+
gl.uniform4fv(U('u_color'), mat.color);
|
|
584
|
+
gl.uniform3fv(U('u_emissive'), mat.emissive);
|
|
585
|
+
gl.uniform3fv(U('u_camPos'), camera.position);
|
|
586
|
+
gl.uniform3fv(U('u_ambient'), scene.ambient);
|
|
587
|
+
gl.uniform1f(U('u_metal'), mat.metallic);
|
|
588
|
+
gl.uniform1f(U('u_rough'), mat.roughness);
|
|
589
|
+
gl.uniform1f(U('u_exposure'), scene.exposure);
|
|
590
|
+
gl.uniform1f(U('u_time'), time / 1000);
|
|
591
|
+
gl.uniform2f(U('u_res'), W, H);
|
|
592
|
+
gl.uniform1i(U('u_unlit'), mat.type === 'unlit' ? 1 : 0);
|
|
593
|
+
gl.uniform1i(U('u_nl'), Math.min(4, scene.lights.length));
|
|
594
|
+
gl.uniform4fv(U('u_lvec'), lv);
|
|
595
|
+
gl.uniform3fv(U('u_lcol'), lc);
|
|
596
|
+
let unit = 0;
|
|
597
|
+
if (mat.map) {
|
|
598
|
+
uploadTex(mat.map, unit);
|
|
599
|
+
gl.uniform1i(U('u_map'), unit++);
|
|
600
|
+
}
|
|
601
|
+
gl.uniform1i(U('u_hasMap'), mat.map ? 1 : 0);
|
|
602
|
+
for (const [k, v] of Object.entries(mat.uniforms || {})) {
|
|
603
|
+
if (typeof v === 'number')
|
|
604
|
+
gl.uniform1f(U(k), v);
|
|
605
|
+
else if (v.source) {
|
|
606
|
+
uploadTex(v, unit);
|
|
607
|
+
gl.uniform1i(U(k), unit++);
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
const a = v;
|
|
611
|
+
(a.length === 2 ? gl.uniform2fv : a.length === 3 ? gl.uniform3fv : gl.uniform4fv).call(gl, U(k), a);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (mat.doubleSided)
|
|
615
|
+
gl.disable(gl.CULL_FACE);
|
|
616
|
+
else
|
|
617
|
+
gl.enable(gl.CULL_FACE);
|
|
618
|
+
if (mat.transparent || mat.color[3] < 1) {
|
|
619
|
+
gl.enable(gl.BLEND);
|
|
620
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
621
|
+
gl.depthMask(false);
|
|
622
|
+
}
|
|
623
|
+
else {
|
|
624
|
+
gl.disable(gl.BLEND);
|
|
625
|
+
gl.depthMask(true);
|
|
626
|
+
}
|
|
627
|
+
const g = uploadGeo(m.geometry);
|
|
628
|
+
gl.bindVertexArray(g.vao);
|
|
629
|
+
const mode = m.geometry.mode === 'lines' || mat.wireframe ? gl.LINES : m.geometry.mode === 'points' ? gl.POINTS : gl.TRIANGLES;
|
|
630
|
+
if (g.indexed)
|
|
631
|
+
gl.drawElements(mode, g.count, g.type, 0);
|
|
632
|
+
else
|
|
633
|
+
gl.drawArrays(mode, 0, g.count);
|
|
634
|
+
draws++;
|
|
635
|
+
}
|
|
636
|
+
gl.bindVertexArray(null);
|
|
637
|
+
gl.depthMask(true);
|
|
638
|
+
},
|
|
639
|
+
dispose() {
|
|
640
|
+
owned.splice(0).forEach((x) => x.del());
|
|
641
|
+
programs.clear();
|
|
642
|
+
},
|
|
643
|
+
};
|
|
644
|
+
return r;
|
|
645
|
+
}
|
|
646
|
+
/** Drag to orbit, wheel / pinch to zoom, arrow keys (when the element has focus). Call `update(dt)` per frame. */
|
|
647
|
+
function orbitControls(camera, el, o = {}) {
|
|
648
|
+
const off = sub(camera.position, camera.target);
|
|
649
|
+
let dist = Math.hypot(...off), theta = Math.atan2(off[0], off[2]), phi = Math.acos(Math.min(1, Math.max(-1, off[1] / (dist || 1))));
|
|
650
|
+
let vt = 0, vp = 0, vz = 0, drag = false, lx = 0, ly = 0;
|
|
651
|
+
const pts = new Map();
|
|
652
|
+
let pinch = 0;
|
|
653
|
+
const damp = o.damping ?? 0.12;
|
|
654
|
+
const reduce = typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
655
|
+
const ctl = { autoRotate: reduce ? 0 : o.autoRotate ?? 0, update, dispose };
|
|
656
|
+
const on = (t, ty, f, opt) => {
|
|
657
|
+
t.addEventListener(ty, f, opt);
|
|
658
|
+
return () => t.removeEventListener(ty, f, opt);
|
|
659
|
+
};
|
|
660
|
+
const offs = [
|
|
661
|
+
on(el, 'pointerdown', (e) => {
|
|
662
|
+
pts.set(e.pointerId, [e.clientX, e.clientY]);
|
|
663
|
+
drag = true;
|
|
664
|
+
lx = e.clientX;
|
|
665
|
+
ly = e.clientY;
|
|
666
|
+
el.setPointerCapture?.(e.pointerId);
|
|
667
|
+
}),
|
|
668
|
+
on(el, 'pointermove', (e) => {
|
|
669
|
+
if (!pts.has(e.pointerId))
|
|
670
|
+
return;
|
|
671
|
+
pts.set(e.pointerId, [e.clientX, e.clientY]);
|
|
672
|
+
if (pts.size === 2 && o.zoom !== false) {
|
|
673
|
+
const [a, b] = Array.from(pts.values());
|
|
674
|
+
const d = Math.hypot(a[0] - b[0], a[1] - b[1]);
|
|
675
|
+
if (pinch)
|
|
676
|
+
vz += (pinch - d) * 0.004;
|
|
677
|
+
pinch = d;
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
if (!drag)
|
|
681
|
+
return;
|
|
682
|
+
vt -= (e.clientX - lx) * 0.005;
|
|
683
|
+
vp -= (e.clientY - ly) * 0.005;
|
|
684
|
+
lx = e.clientX;
|
|
685
|
+
ly = e.clientY;
|
|
686
|
+
}),
|
|
687
|
+
on(el, 'pointerup', (e) => {
|
|
688
|
+
pts.delete(e.pointerId);
|
|
689
|
+
pinch = 0;
|
|
690
|
+
drag = pts.size > 0;
|
|
691
|
+
}),
|
|
692
|
+
on(el, 'pointercancel', (e) => {
|
|
693
|
+
pts.delete(e.pointerId);
|
|
694
|
+
drag = false;
|
|
695
|
+
}),
|
|
696
|
+
on(el, 'wheel', (e) => {
|
|
697
|
+
if (o.zoom === false)
|
|
698
|
+
return;
|
|
699
|
+
e.preventDefault();
|
|
700
|
+
vz += Math.sign(e.deltaY) * 0.08;
|
|
701
|
+
}, { passive: false }),
|
|
702
|
+
on(el, 'keydown', (e) => {
|
|
703
|
+
const k = { ArrowLeft: [0.1, 0, 0], ArrowRight: [-0.1, 0, 0], ArrowUp: [0, 0.1, 0], ArrowDown: [0, -0.1, 0], '+': [0, 0, -0.1], '-': [0, 0, 0.1] }[e.key];
|
|
704
|
+
if (!k)
|
|
705
|
+
return;
|
|
706
|
+
e.preventDefault();
|
|
707
|
+
vt += k[0];
|
|
708
|
+
vp += k[1];
|
|
709
|
+
vz += k[2];
|
|
710
|
+
}),
|
|
711
|
+
];
|
|
712
|
+
function update(dt) {
|
|
713
|
+
const moving = Math.abs(vt) + Math.abs(vp) + Math.abs(vz) > 1e-5 || ctl.autoRotate;
|
|
714
|
+
theta += vt + (ctl.autoRotate && !drag ? (ctl.autoRotate * dt) / 1000 : 0);
|
|
715
|
+
phi = Math.min(Math.PI - 0.05, Math.max(0.05, phi + vp));
|
|
716
|
+
dist = Math.min(o.maxDistance ?? Infinity, Math.max(o.minDistance ?? 0.01, dist * (1 + vz)));
|
|
717
|
+
const k = Math.pow(1 - damp, dt / 16.7);
|
|
718
|
+
vt *= k;
|
|
719
|
+
vp *= k;
|
|
720
|
+
vz *= k;
|
|
721
|
+
const t = camera.target;
|
|
722
|
+
camera.position = [t[0] + dist * Math.sin(phi) * Math.sin(theta), t[1] + dist * Math.cos(phi), t[2] + dist * Math.sin(phi) * Math.cos(theta)];
|
|
723
|
+
return !!moving;
|
|
724
|
+
}
|
|
725
|
+
function dispose() {
|
|
726
|
+
offs.forEach((f) => f());
|
|
727
|
+
}
|
|
728
|
+
return ctl;
|
|
729
|
+
}
|
|
730
|
+
/** The module object for `use(gl)`. */
|
|
731
|
+
const gl = {
|
|
732
|
+
id: 'gl',
|
|
733
|
+
version: RUNTIME_VERSION,
|
|
734
|
+
requires: ['core'],
|
|
735
|
+
api: { mat4, quatFromEuler, quatMultiply, quatSlerp, box, plane, sphere, torus, computeNormals, standardMaterial, unlitMaterial, shaderMaterial, color, texture, GlNode, Camera, Scene, bounds, frameNode, scrubVideo, createRenderer, orbitControls },
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
export { Camera, GlNode, Scene, bounds, box, color, computeNormals, createRenderer, frameNode, gl, mat4, orbitControls, plane, quatFromEuler, quatMultiply, quatSlerp, scrubVideo, shaderMaterial, sphere, standardMaterial, texture, torus, unlitMaterial };
|
|
739
|
+
//# sourceMappingURL=gl.js.map
|