3d-spinner 0.9.1 → 0.9.3
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 +96 -11
- package/dist/animation-label.d.ts +8 -0
- package/dist/animation-label.js +47 -0
- package/dist/animation.d.ts +2 -0
- package/dist/animations/charged-orb.d.ts +55 -0
- package/dist/animations/charged-orb.js +229 -0
- package/dist/animations/grid-assembly.d.ts +67 -0
- package/dist/animations/grid-assembly.js +268 -0
- package/dist/animations/object-motion.d.ts +19 -7
- package/dist/animations/object-motion.js +38 -32
- package/dist/animations/particles.d.ts +110 -0
- package/dist/animations/particles.js +209 -0
- package/dist/cjs/animations/charged-orb.cjs +1334 -0
- package/dist/cjs/animations/grid-assembly.cjs +1498 -0
- package/dist/cjs/animations/object-motion.cjs +1449 -0
- package/dist/cjs/animations/particles.cjs +1940 -0
- package/dist/cjs/animations/spin.cjs +1300 -0
- package/dist/cjs/composite-animation.cjs +58 -0
- package/dist/cjs/engines/little-3d-engine/little-3d-engine.cjs +1433 -0
- package/dist/cjs/engines/little-3d-engine/loaders/obj.cjs +87 -0
- package/dist/cjs/engines/little-3d-engine/renderers/canvas2d-textured.cjs +345 -0
- package/dist/cjs/engines/little-3d-engine/renderers/webgl-textured.cjs +528 -0
- package/dist/cjs/engines/little-3d-engine/renderers/webgpu-textured.cjs +712 -0
- package/dist/cjs/engines/little-tween-engine/little-tween-engine.cjs +279 -0
- package/dist/cjs/index.cjs +120 -0
- package/dist/cjs/motion/motion.cjs +140 -0
- package/dist/cjs/motion/transitions.cjs +80 -0
- package/dist/cjs/prefabs/prefabs.cjs +3450 -0
- package/dist/composite-animation.d.ts +18 -0
- package/dist/composite-animation.js +39 -0
- package/dist/engines/little-3d-engine/little-3d-engine.d.ts +17 -12
- package/dist/engines/little-3d-engine/little-3d-engine.js +13 -8
- package/dist/engines/little-3d-engine/loaders/obj.d.ts +12 -4
- package/dist/engines/little-3d-engine/loaders/obj.js +44 -5
- package/dist/engines/little-3d-engine/renderer.d.ts +7 -1
- package/dist/engines/little-3d-engine/renderer.js +2 -0
- package/dist/engines/little-3d-engine/renderers/canvas2d-textured.d.ts +20 -0
- package/dist/engines/little-3d-engine/renderers/canvas2d-textured.js +135 -0
- package/dist/engines/little-3d-engine/renderers/textured-helpers.d.ts +9 -0
- package/dist/engines/little-3d-engine/renderers/textured-helpers.js +35 -0
- package/dist/engines/little-3d-engine/renderers/webgl-textured.d.ts +43 -0
- package/dist/engines/little-3d-engine/renderers/webgl-textured.js +211 -0
- package/dist/engines/little-3d-engine/renderers/webgpu-textured.d.ts +45 -0
- package/dist/engines/little-3d-engine/renderers/webgpu-textured.js +300 -0
- package/dist/engines/little-3d-engine/renderers/webgpu.d.ts +11 -6
- package/dist/engines/little-3d-engine/shapes/complex/plane.d.ts +3 -0
- package/dist/engines/little-3d-engine/shapes/complex/plane.js +33 -0
- package/dist/engines/little-3d-engine/shapes/{cube.d.ts → primitives/cube.d.ts} +1 -1
- package/dist/engines/little-3d-engine/shapes/{octahedron.d.ts → primitives/octahedron.d.ts} +1 -1
- package/dist/engines/little-3d-engine/shapes/{pyramid.d.ts → primitives/pyramid.d.ts} +1 -1
- package/dist/engines/little-3d-engine/shapes/primitives/quad.d.ts +9 -0
- package/dist/engines/little-3d-engine/shapes/primitives/quad.js +18 -0
- package/dist/engines/little-3d-engine/shapes/{cube-sphere.d.ts → primitives/spheres/cube-sphere.d.ts} +1 -1
- package/dist/engines/little-3d-engine/shapes/{icosphere.d.ts → primitives/spheres/icosphere.d.ts} +1 -1
- package/dist/engines/little-3d-engine/shapes/{icosphere.js → primitives/spheres/icosphere.js} +1 -1
- package/dist/engines/little-3d-engine/shapes/{octa-sphere.d.ts → primitives/spheres/octa-sphere.d.ts} +1 -1
- package/dist/engines/little-3d-engine/shapes/{octa-sphere.js → primitives/spheres/octa-sphere.js} +1 -1
- package/dist/engines/little-3d-engine/shapes/{uv-sphere.d.ts → primitives/spheres/uv-sphere.d.ts} +1 -1
- package/dist/engines/little-3d-engine/shapes/{tetrahedron.d.ts → primitives/tetrahedron.d.ts} +1 -1
- package/dist/engines/little-3d-engine/textures/dynamic/canvas-texture.d.ts +1 -0
- package/dist/engines/little-3d-engine/textures/dynamic/canvas-texture.js +8 -0
- package/dist/engines/little-3d-engine/textures/dynamic/shine.d.ts +2 -0
- package/dist/engines/little-3d-engine/textures/dynamic/shine.js +14 -0
- package/dist/engines/little-3d-engine/textures/dynamic/star.d.ts +2 -0
- package/dist/engines/little-3d-engine/textures/dynamic/star.js +16 -0
- package/dist/engines/little-3d-engine/textures/dynamic/streak.d.ts +5 -0
- package/dist/engines/little-3d-engine/textures/dynamic/streak.js +20 -0
- package/dist/index.d.ts +2 -1
- package/dist/prefabs/charged-orb.d.ts +17 -0
- package/dist/prefabs/charged-orb.js +37 -0
- package/dist/prefabs/crystal-comet.d.ts +4 -0
- package/dist/prefabs/crystal-comet.js +41 -0
- package/dist/prefabs/ghost-train.d.ts +9 -0
- package/dist/prefabs/ghost-train.js +44 -0
- package/dist/prefabs/grid-assembly.d.ts +13 -0
- package/dist/prefabs/grid-assembly.js +15 -0
- package/dist/prefabs/monochrome-streak.d.ts +4 -0
- package/dist/prefabs/monochrome-streak.js +25 -0
- package/dist/prefabs/plane-star-trail.d.ts +4 -0
- package/dist/prefabs/plane-star-trail.js +40 -0
- package/dist/prefabs/prefabs.d.ts +10 -0
- package/dist/prefabs/prefabs.js +9 -0
- package/dist/prefabs/pulsing-starfield.d.ts +4 -0
- package/dist/prefabs/pulsing-starfield.js +27 -0
- package/dist/prefabs/rocket-launch.d.ts +8 -0
- package/dist/prefabs/rocket-launch.js +59 -0
- package/dist/prefabs/spinner.d.ts +7 -0
- package/dist/prefabs/spinner.js +17 -0
- package/dist/prefabs/star-swarm.d.ts +4 -0
- package/dist/prefabs/star-swarm.js +27 -0
- package/dist/prefabs/types.d.ts +46 -0
- package/dist/prefabs/types.js +1 -0
- package/dist/umd/spinner.global.js +4212 -0
- package/dist/umd/spinner.global.min.js +113 -0
- package/package.json +67 -11
- /package/dist/engines/little-3d-engine/shapes/{cube.js → primitives/cube.js} +0 -0
- /package/dist/engines/little-3d-engine/shapes/{octahedron.js → primitives/octahedron.js} +0 -0
- /package/dist/engines/little-3d-engine/shapes/{pyramid.js → primitives/pyramid.js} +0 -0
- /package/dist/engines/little-3d-engine/shapes/{cube-sphere.js → primitives/spheres/cube-sphere.js} +0 -0
- /package/dist/engines/little-3d-engine/shapes/{uv-sphere.js → primitives/spheres/uv-sphere.js} +0 -0
- /package/dist/engines/little-3d-engine/shapes/{tetrahedron.js → primitives/tetrahedron.js} +0 -0
|
@@ -0,0 +1,1300 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __esm = (fn, res, err) => function __init() {
|
|
7
|
+
if (err) throw err[0];
|
|
8
|
+
try {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
} catch (e) {
|
|
11
|
+
throw err = [e], e;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
|
|
28
|
+
// src/engines/little-3d-engine/core/math.ts
|
|
29
|
+
function subtract(a, b) {
|
|
30
|
+
return { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
|
|
31
|
+
}
|
|
32
|
+
function cross(a, b) {
|
|
33
|
+
return {
|
|
34
|
+
x: a.y * b.z - a.z * b.y,
|
|
35
|
+
y: a.z * b.x - a.x * b.z,
|
|
36
|
+
z: a.x * b.y - a.y * b.x
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function dot(a, b) {
|
|
40
|
+
return a.x * b.x + a.y * b.y + a.z * b.z;
|
|
41
|
+
}
|
|
42
|
+
function scale(v, s) {
|
|
43
|
+
return { x: v.x * s, y: v.y * s, z: v.z * s };
|
|
44
|
+
}
|
|
45
|
+
function normalize(v) {
|
|
46
|
+
const length = Math.hypot(v.x, v.y, v.z);
|
|
47
|
+
if (length === 0) return { x: 0, y: 0, z: 0 };
|
|
48
|
+
return { x: v.x / length, y: v.y / length, z: v.z / length };
|
|
49
|
+
}
|
|
50
|
+
function multiply(a, b) {
|
|
51
|
+
const out = new Array(16);
|
|
52
|
+
for (let col = 0; col < 4; col++) {
|
|
53
|
+
for (let row = 0; row < 4; row++) {
|
|
54
|
+
let sum = 0;
|
|
55
|
+
for (let k = 0; k < 4; k++) {
|
|
56
|
+
sum += a[k * 4 + row] * b[col * 4 + k];
|
|
57
|
+
}
|
|
58
|
+
out[col * 4 + row] = sum;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
function translation(x, y, z) {
|
|
64
|
+
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1];
|
|
65
|
+
}
|
|
66
|
+
function scaleMatrix(s) {
|
|
67
|
+
return [s, 0, 0, 0, 0, s, 0, 0, 0, 0, s, 0, 0, 0, 0, 1];
|
|
68
|
+
}
|
|
69
|
+
function rotationX(rad) {
|
|
70
|
+
const c = Math.cos(rad);
|
|
71
|
+
const s = Math.sin(rad);
|
|
72
|
+
return [1, 0, 0, 0, 0, c, s, 0, 0, -s, c, 0, 0, 0, 0, 1];
|
|
73
|
+
}
|
|
74
|
+
function rotationY(rad) {
|
|
75
|
+
const c = Math.cos(rad);
|
|
76
|
+
const s = Math.sin(rad);
|
|
77
|
+
return [c, 0, -s, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1];
|
|
78
|
+
}
|
|
79
|
+
function rotationZ(rad) {
|
|
80
|
+
const c = Math.cos(rad);
|
|
81
|
+
const s = Math.sin(rad);
|
|
82
|
+
return [c, s, 0, 0, -s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
83
|
+
}
|
|
84
|
+
function perspective(fovY, aspect, near, far) {
|
|
85
|
+
const f = 1 / Math.tan(fovY / 2);
|
|
86
|
+
const nf = 1 / (near - far);
|
|
87
|
+
return [
|
|
88
|
+
f / aspect,
|
|
89
|
+
0,
|
|
90
|
+
0,
|
|
91
|
+
0,
|
|
92
|
+
0,
|
|
93
|
+
f,
|
|
94
|
+
0,
|
|
95
|
+
0,
|
|
96
|
+
0,
|
|
97
|
+
0,
|
|
98
|
+
(far + near) * nf,
|
|
99
|
+
-1,
|
|
100
|
+
0,
|
|
101
|
+
0,
|
|
102
|
+
2 * far * near * nf,
|
|
103
|
+
0
|
|
104
|
+
];
|
|
105
|
+
}
|
|
106
|
+
function transformPoint(m, p) {
|
|
107
|
+
const x = m[0] * p.x + m[4] * p.y + m[8] * p.z + m[12];
|
|
108
|
+
const y = m[1] * p.x + m[5] * p.y + m[9] * p.z + m[13];
|
|
109
|
+
const z = m[2] * p.x + m[6] * p.y + m[10] * p.z + m[14];
|
|
110
|
+
const w = m[3] * p.x + m[7] * p.y + m[11] * p.z + m[15] || 1;
|
|
111
|
+
return { x: x / w, y: y / w, z: z / w };
|
|
112
|
+
}
|
|
113
|
+
function transformAffine(m, p) {
|
|
114
|
+
return {
|
|
115
|
+
x: m[0] * p.x + m[4] * p.y + m[8] * p.z + m[12],
|
|
116
|
+
y: m[1] * p.x + m[5] * p.y + m[9] * p.z + m[13],
|
|
117
|
+
z: m[2] * p.x + m[6] * p.y + m[10] * p.z + m[14]
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
var init_math = __esm({
|
|
121
|
+
"src/engines/little-3d-engine/core/math.ts"() {
|
|
122
|
+
"use strict";
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
// src/engines/little-3d-engine/core/geometry.ts
|
|
127
|
+
function parseColor(color) {
|
|
128
|
+
const hex = color.trim().replace("#", "");
|
|
129
|
+
const full = hex.length === 3 ? hex.split("").map((c) => c + c).join("") : hex;
|
|
130
|
+
const n = parseInt(full, 16);
|
|
131
|
+
return [n >> 16 & 255, n >> 8 & 255, n & 255];
|
|
132
|
+
}
|
|
133
|
+
function expandToTriangles(mesh) {
|
|
134
|
+
let triangles = 0;
|
|
135
|
+
for (const face of mesh.faces) triangles += Math.max(0, face.indices.length - 2);
|
|
136
|
+
const positions = new Float32Array(triangles * 9);
|
|
137
|
+
const normals = new Float32Array(triangles * 9);
|
|
138
|
+
const colors = new Float32Array(triangles * 9);
|
|
139
|
+
let o = 0;
|
|
140
|
+
for (const face of mesh.faces) {
|
|
141
|
+
const v0 = mesh.vertices[face.indices[0]];
|
|
142
|
+
const v1 = mesh.vertices[face.indices[1]];
|
|
143
|
+
const v2 = mesh.vertices[face.indices[2]];
|
|
144
|
+
const normal = normalize(cross(subtract(v1, v0), subtract(v2, v0)));
|
|
145
|
+
const [r, g, b] = parseColor(face.color);
|
|
146
|
+
const cr = r / 255;
|
|
147
|
+
const cg = g / 255;
|
|
148
|
+
const cb = b / 255;
|
|
149
|
+
for (let k = 1; k < face.indices.length - 1; k++) {
|
|
150
|
+
const tri = [face.indices[0], face.indices[k], face.indices[k + 1]];
|
|
151
|
+
for (const index of tri) {
|
|
152
|
+
const v = mesh.vertices[index];
|
|
153
|
+
positions[o] = v.x;
|
|
154
|
+
positions[o + 1] = v.y;
|
|
155
|
+
positions[o + 2] = v.z;
|
|
156
|
+
normals[o] = normal.x;
|
|
157
|
+
normals[o + 1] = normal.y;
|
|
158
|
+
normals[o + 2] = normal.z;
|
|
159
|
+
colors[o] = cr;
|
|
160
|
+
colors[o + 1] = cg;
|
|
161
|
+
colors[o + 2] = cb;
|
|
162
|
+
o += 3;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { positions, normals, colors, count: positions.length / 3 };
|
|
167
|
+
}
|
|
168
|
+
var init_geometry = __esm({
|
|
169
|
+
"src/engines/little-3d-engine/core/geometry.ts"() {
|
|
170
|
+
"use strict";
|
|
171
|
+
init_math();
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// src/engines/little-3d-engine/core/light.ts
|
|
176
|
+
function clamp01(value) {
|
|
177
|
+
return Math.min(1, Math.max(0, value));
|
|
178
|
+
}
|
|
179
|
+
function shadeColor(normal, color, light) {
|
|
180
|
+
const lambert = Math.max(0, dot(normal, light.toLight));
|
|
181
|
+
const brightness = clamp01(light.ambient + light.intensity * lambert);
|
|
182
|
+
const [r, g, b] = parseColor(color);
|
|
183
|
+
return `rgb(${Math.round(r * brightness)}, ${Math.round(g * brightness)}, ${Math.round(
|
|
184
|
+
b * brightness
|
|
185
|
+
)})`;
|
|
186
|
+
}
|
|
187
|
+
var DEFAULTS2, Light;
|
|
188
|
+
var init_light = __esm({
|
|
189
|
+
"src/engines/little-3d-engine/core/light.ts"() {
|
|
190
|
+
"use strict";
|
|
191
|
+
init_math();
|
|
192
|
+
init_geometry();
|
|
193
|
+
DEFAULTS2 = {
|
|
194
|
+
direction: { x: -0.4, y: -0.7, z: -0.6 },
|
|
195
|
+
intensity: 0.85,
|
|
196
|
+
ambient: 0.25
|
|
197
|
+
};
|
|
198
|
+
Light = class {
|
|
199
|
+
constructor(options) {
|
|
200
|
+
this.options = { ...DEFAULTS2, ...options };
|
|
201
|
+
this.params = {
|
|
202
|
+
toLight: normalize(scale(this.options.direction, -1)),
|
|
203
|
+
intensity: this.options.intensity,
|
|
204
|
+
ambient: this.options.ambient
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
/** Convenience wrapper around {@link shadeColor} using this light. */
|
|
208
|
+
shade(normal, color) {
|
|
209
|
+
return shadeColor(normal, color, this.params);
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// src/engines/little-3d-engine/renderers/webgl.ts
|
|
216
|
+
var webgl_exports = {};
|
|
217
|
+
__export(webgl_exports, {
|
|
218
|
+
WebGLRenderer: () => WebGLRenderer
|
|
219
|
+
});
|
|
220
|
+
function compile(gl, type, source) {
|
|
221
|
+
const shader = gl.createShader(type);
|
|
222
|
+
gl.shaderSource(shader, source);
|
|
223
|
+
gl.compileShader(shader);
|
|
224
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
225
|
+
throw new Error(`3d-spinner: shader compile failed: ${gl.getShaderInfoLog(shader)}`);
|
|
226
|
+
}
|
|
227
|
+
return shader;
|
|
228
|
+
}
|
|
229
|
+
function link(gl) {
|
|
230
|
+
const program = gl.createProgram();
|
|
231
|
+
gl.attachShader(program, compile(gl, gl.VERTEX_SHADER, VERTEX_SHADER));
|
|
232
|
+
gl.attachShader(program, compile(gl, gl.FRAGMENT_SHADER, FRAGMENT_SHADER));
|
|
233
|
+
gl.linkProgram(program);
|
|
234
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
235
|
+
throw new Error(`3d-spinner: program link failed: ${gl.getProgramInfoLog(program)}`);
|
|
236
|
+
}
|
|
237
|
+
return program;
|
|
238
|
+
}
|
|
239
|
+
var VERTEX_SHADER, FRAGMENT_SHADER, WebGLRenderer;
|
|
240
|
+
var init_webgl = __esm({
|
|
241
|
+
"src/engines/little-3d-engine/renderers/webgl.ts"() {
|
|
242
|
+
"use strict";
|
|
243
|
+
init_geometry();
|
|
244
|
+
init_renderer();
|
|
245
|
+
VERTEX_SHADER = `#version 300 es
|
|
246
|
+
in vec3 aPos;
|
|
247
|
+
in vec3 aNormal;
|
|
248
|
+
in vec3 aColor;
|
|
249
|
+
uniform mat4 uViewProj;
|
|
250
|
+
uniform mat4 uModel;
|
|
251
|
+
out vec3 vNormal;
|
|
252
|
+
out vec3 vColor;
|
|
253
|
+
void main() {
|
|
254
|
+
vNormal = mat3(uModel) * aNormal;
|
|
255
|
+
vColor = aColor;
|
|
256
|
+
gl_Position = uViewProj * uModel * vec4(aPos, 1.0);
|
|
257
|
+
}`;
|
|
258
|
+
FRAGMENT_SHADER = `#version 300 es
|
|
259
|
+
precision mediump float;
|
|
260
|
+
in vec3 vNormal;
|
|
261
|
+
in vec3 vColor;
|
|
262
|
+
uniform vec3 uToLight;
|
|
263
|
+
uniform float uIntensity;
|
|
264
|
+
uniform float uAmbient;
|
|
265
|
+
uniform float uOpacity;
|
|
266
|
+
out vec4 fragColor;
|
|
267
|
+
void main() {
|
|
268
|
+
float lambert = max(dot(normalize(vNormal), normalize(uToLight)), 0.0);
|
|
269
|
+
float brightness = clamp(uAmbient + uIntensity * lambert, 0.0, 1.0);
|
|
270
|
+
fragColor = vec4(vColor * brightness, uOpacity);
|
|
271
|
+
}`;
|
|
272
|
+
WebGLRenderer = class {
|
|
273
|
+
constructor(options = {}) {
|
|
274
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
275
|
+
if (options.background) {
|
|
276
|
+
const [r, g, b] = parseColor(options.background);
|
|
277
|
+
this.clearColor = [r / 255, g / 255, b / 255, 1];
|
|
278
|
+
} else {
|
|
279
|
+
this.clearColor = [0, 0, 0, 0];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
init(canvas) {
|
|
283
|
+
const gl = canvas.getContext("webgl2");
|
|
284
|
+
if (!gl) throw new Error("3d-spinner: WebGL2 is not supported in this browser.");
|
|
285
|
+
this.gl = gl;
|
|
286
|
+
this.program = link(gl);
|
|
287
|
+
this.locations = {
|
|
288
|
+
aPos: gl.getAttribLocation(this.program, "aPos"),
|
|
289
|
+
aNormal: gl.getAttribLocation(this.program, "aNormal"),
|
|
290
|
+
aColor: gl.getAttribLocation(this.program, "aColor"),
|
|
291
|
+
uViewProj: gl.getUniformLocation(this.program, "uViewProj"),
|
|
292
|
+
uModel: gl.getUniformLocation(this.program, "uModel"),
|
|
293
|
+
uToLight: gl.getUniformLocation(this.program, "uToLight"),
|
|
294
|
+
uIntensity: gl.getUniformLocation(this.program, "uIntensity"),
|
|
295
|
+
uAmbient: gl.getUniformLocation(this.program, "uAmbient"),
|
|
296
|
+
uOpacity: gl.getUniformLocation(this.program, "uOpacity")
|
|
297
|
+
};
|
|
298
|
+
gl.enable(gl.DEPTH_TEST);
|
|
299
|
+
gl.enable(gl.CULL_FACE);
|
|
300
|
+
gl.cullFace(gl.BACK);
|
|
301
|
+
gl.frontFace(gl.CCW);
|
|
302
|
+
}
|
|
303
|
+
resize() {
|
|
304
|
+
const gl = this.gl;
|
|
305
|
+
if (!gl) return;
|
|
306
|
+
const canvas = gl.canvas;
|
|
307
|
+
gl.viewport(0, 0, canvas.width, canvas.height);
|
|
308
|
+
}
|
|
309
|
+
buffers(mesh) {
|
|
310
|
+
const cached = this.cache.get(mesh);
|
|
311
|
+
if (cached) return cached;
|
|
312
|
+
const gl = this.gl;
|
|
313
|
+
const loc = this.locations;
|
|
314
|
+
const data = expandToTriangles(mesh);
|
|
315
|
+
const vao = gl.createVertexArray();
|
|
316
|
+
gl.bindVertexArray(vao);
|
|
317
|
+
const attribute = (location, array) => {
|
|
318
|
+
if (location < 0) return;
|
|
319
|
+
const buffer = gl.createBuffer();
|
|
320
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
321
|
+
gl.bufferData(gl.ARRAY_BUFFER, array, gl.STATIC_DRAW);
|
|
322
|
+
gl.enableVertexAttribArray(location);
|
|
323
|
+
gl.vertexAttribPointer(location, 3, gl.FLOAT, false, 0, 0);
|
|
324
|
+
};
|
|
325
|
+
attribute(loc.aPos, data.positions);
|
|
326
|
+
attribute(loc.aNormal, data.normals);
|
|
327
|
+
attribute(loc.aColor, data.colors);
|
|
328
|
+
gl.bindVertexArray(null);
|
|
329
|
+
const result = { vao, count: data.count };
|
|
330
|
+
this.cache.set(mesh, result);
|
|
331
|
+
return result;
|
|
332
|
+
}
|
|
333
|
+
render(frame) {
|
|
334
|
+
const gl = this.gl;
|
|
335
|
+
const loc = this.locations;
|
|
336
|
+
if (!gl || !this.program || !loc) return;
|
|
337
|
+
gl.clearColor(...this.clearColor);
|
|
338
|
+
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
339
|
+
gl.useProgram(this.program);
|
|
340
|
+
gl.uniformMatrix4fv(loc.uViewProj, false, new Float32Array(frame.viewProjection));
|
|
341
|
+
gl.uniform3f(loc.uToLight, frame.light.toLight.x, frame.light.toLight.y, frame.light.toLight.z);
|
|
342
|
+
gl.uniform1f(loc.uIntensity, frame.light.intensity);
|
|
343
|
+
gl.uniform1f(loc.uAmbient, frame.light.ambient);
|
|
344
|
+
gl.disable(gl.BLEND);
|
|
345
|
+
gl.depthMask(true);
|
|
346
|
+
gl.cullFace(gl.BACK);
|
|
347
|
+
for (const item of frame.items) {
|
|
348
|
+
if (item.transparency) continue;
|
|
349
|
+
const mesh = this.buffers(item.mesh);
|
|
350
|
+
gl.uniformMatrix4fv(loc.uModel, false, new Float32Array(item.model));
|
|
351
|
+
gl.uniform1f(loc.uOpacity, 1);
|
|
352
|
+
gl.bindVertexArray(mesh.vao);
|
|
353
|
+
gl.drawArrays(gl.TRIANGLES, 0, mesh.count);
|
|
354
|
+
}
|
|
355
|
+
gl.enable(gl.BLEND);
|
|
356
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
357
|
+
gl.depthMask(false);
|
|
358
|
+
for (const item of frame.items) {
|
|
359
|
+
const transparency = item.transparency;
|
|
360
|
+
if (!transparency) continue;
|
|
361
|
+
const mesh = this.buffers(item.mesh);
|
|
362
|
+
gl.uniformMatrix4fv(loc.uModel, false, new Float32Array(item.model));
|
|
363
|
+
gl.bindVertexArray(mesh.vao);
|
|
364
|
+
if (transparency.mode === "two-sided") {
|
|
365
|
+
const resolved = resolveTwoSidedOpacity(transparency);
|
|
366
|
+
gl.cullFace(gl.FRONT);
|
|
367
|
+
gl.uniform1f(loc.uOpacity, resolved.back);
|
|
368
|
+
gl.drawArrays(gl.TRIANGLES, 0, mesh.count);
|
|
369
|
+
gl.cullFace(gl.BACK);
|
|
370
|
+
gl.uniform1f(loc.uOpacity, resolved.front);
|
|
371
|
+
gl.drawArrays(gl.TRIANGLES, 0, mesh.count);
|
|
372
|
+
} else {
|
|
373
|
+
gl.cullFace(gl.BACK);
|
|
374
|
+
gl.uniform1f(loc.uOpacity, opacity(transparency.opacity, DEFAULT_ONE_SIDED_OPACITY));
|
|
375
|
+
gl.drawArrays(gl.TRIANGLES, 0, mesh.count);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
gl.depthMask(true);
|
|
379
|
+
gl.disable(gl.BLEND);
|
|
380
|
+
gl.cullFace(gl.BACK);
|
|
381
|
+
gl.bindVertexArray(null);
|
|
382
|
+
}
|
|
383
|
+
destroy() {
|
|
384
|
+
const gl = this.gl;
|
|
385
|
+
if (gl) {
|
|
386
|
+
for (const mesh of this.cache.values()) gl.deleteVertexArray(mesh.vao);
|
|
387
|
+
if (this.program) gl.deleteProgram(this.program);
|
|
388
|
+
}
|
|
389
|
+
this.cache.clear();
|
|
390
|
+
this.gl = void 0;
|
|
391
|
+
this.program = void 0;
|
|
392
|
+
this.locations = void 0;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
// src/engines/little-3d-engine/renderers/webgpu.ts
|
|
399
|
+
var webgpu_exports = {};
|
|
400
|
+
__export(webgpu_exports, {
|
|
401
|
+
WebGPURenderer: () => WebGPURenderer
|
|
402
|
+
});
|
|
403
|
+
var WGSL, CLIP_Z_FIX, UNIFORM_STRIDE, WebGPURenderer;
|
|
404
|
+
var init_webgpu = __esm({
|
|
405
|
+
"src/engines/little-3d-engine/renderers/webgpu.ts"() {
|
|
406
|
+
"use strict";
|
|
407
|
+
init_geometry();
|
|
408
|
+
init_math();
|
|
409
|
+
init_renderer();
|
|
410
|
+
WGSL = `
|
|
411
|
+
struct Uniforms {
|
|
412
|
+
viewProj: mat4x4<f32>,
|
|
413
|
+
model: mat4x4<f32>,
|
|
414
|
+
toLight: vec4<f32>,
|
|
415
|
+
params: vec4<f32>,
|
|
416
|
+
};
|
|
417
|
+
@group(0) @binding(0) var<uniform> u: Uniforms;
|
|
418
|
+
|
|
419
|
+
struct VSOut {
|
|
420
|
+
@builtin(position) position: vec4<f32>,
|
|
421
|
+
@location(0) normal: vec3<f32>,
|
|
422
|
+
@location(1) color: vec3<f32>,
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
@vertex
|
|
426
|
+
fn vs(@location(0) pos: vec3<f32>, @location(1) normal: vec3<f32>, @location(2) color: vec3<f32>) -> VSOut {
|
|
427
|
+
var out: VSOut;
|
|
428
|
+
let m = mat3x3<f32>(u.model[0].xyz, u.model[1].xyz, u.model[2].xyz);
|
|
429
|
+
out.normal = m * normal;
|
|
430
|
+
out.color = color;
|
|
431
|
+
out.position = u.viewProj * u.model * vec4<f32>(pos, 1.0);
|
|
432
|
+
return out;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
@fragment
|
|
436
|
+
fn fs(in: VSOut) -> @location(0) vec4<f32> {
|
|
437
|
+
let lambert = max(dot(normalize(in.normal), normalize(u.toLight.xyz)), 0.0);
|
|
438
|
+
let brightness = clamp(u.params.y + u.params.x * lambert, 0.0, 1.0);
|
|
439
|
+
return vec4<f32>(in.color * brightness, u.params.z);
|
|
440
|
+
}
|
|
441
|
+
`;
|
|
442
|
+
CLIP_Z_FIX = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 1];
|
|
443
|
+
UNIFORM_STRIDE = 256;
|
|
444
|
+
WebGPURenderer = class {
|
|
445
|
+
constructor(options = {}) {
|
|
446
|
+
this.uniformCapacity = 0;
|
|
447
|
+
this.depthSize = "";
|
|
448
|
+
this.destroyed = false;
|
|
449
|
+
this.cache = /* @__PURE__ */ new Map();
|
|
450
|
+
if (options.background) {
|
|
451
|
+
const [r, g, b] = parseColor(options.background);
|
|
452
|
+
this.clearValue = { r: r / 255, g: g / 255, b: b / 255, a: 1 };
|
|
453
|
+
this.alphaMode = "opaque";
|
|
454
|
+
} else {
|
|
455
|
+
this.clearValue = { r: 0, g: 0, b: 0, a: 0 };
|
|
456
|
+
this.alphaMode = "premultiplied";
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
async init(canvas) {
|
|
460
|
+
const gpu = navigator.gpu;
|
|
461
|
+
if (!gpu) throw new Error("3d-spinner: WebGPU is not supported in this browser.");
|
|
462
|
+
const adapter = await gpu.requestAdapter();
|
|
463
|
+
if (!adapter) throw new Error("3d-spinner: no WebGPU adapter is available.");
|
|
464
|
+
const device = await adapter.requestDevice();
|
|
465
|
+
if (this.destroyed) {
|
|
466
|
+
device.destroy?.();
|
|
467
|
+
return;
|
|
468
|
+
}
|
|
469
|
+
const context = canvas.getContext("webgpu");
|
|
470
|
+
if (!context) throw new Error("3d-spinner: could not get a WebGPU canvas context.");
|
|
471
|
+
const format = gpu.getPreferredCanvasFormat();
|
|
472
|
+
context.configure({ device, format, alphaMode: this.alphaMode });
|
|
473
|
+
const module2 = device.createShaderModule({ code: WGSL });
|
|
474
|
+
const stage = globalThis.GPUShaderStage;
|
|
475
|
+
const layout = device.createBindGroupLayout({
|
|
476
|
+
entries: [
|
|
477
|
+
{
|
|
478
|
+
binding: 0,
|
|
479
|
+
visibility: stage.VERTEX | stage.FRAGMENT,
|
|
480
|
+
buffer: { type: "uniform", hasDynamicOffset: true, minBindingSize: 160 }
|
|
481
|
+
}
|
|
482
|
+
]
|
|
483
|
+
});
|
|
484
|
+
const vertexBuffer = (location) => ({
|
|
485
|
+
arrayStride: 12,
|
|
486
|
+
attributes: [{ shaderLocation: location, offset: 0, format: "float32x3" }]
|
|
487
|
+
});
|
|
488
|
+
const pipelineLayout = device.createPipelineLayout({ bindGroupLayouts: [layout] });
|
|
489
|
+
const blend = {
|
|
490
|
+
color: {
|
|
491
|
+
srcFactor: "src-alpha",
|
|
492
|
+
dstFactor: "one-minus-src-alpha",
|
|
493
|
+
operation: "add"
|
|
494
|
+
},
|
|
495
|
+
alpha: { srcFactor: "one", dstFactor: "one-minus-src-alpha", operation: "add" }
|
|
496
|
+
};
|
|
497
|
+
const pipeline = (cullMode, transparent) => device.createRenderPipeline({
|
|
498
|
+
layout: pipelineLayout,
|
|
499
|
+
vertex: {
|
|
500
|
+
module: module2,
|
|
501
|
+
entryPoint: "vs",
|
|
502
|
+
buffers: [vertexBuffer(0), vertexBuffer(1), vertexBuffer(2)]
|
|
503
|
+
},
|
|
504
|
+
fragment: {
|
|
505
|
+
module: module2,
|
|
506
|
+
entryPoint: "fs",
|
|
507
|
+
targets: [{ format, ...transparent ? { blend } : {} }]
|
|
508
|
+
},
|
|
509
|
+
primitive: { topology: "triangle-list", cullMode, frontFace: "ccw" },
|
|
510
|
+
depthStencil: {
|
|
511
|
+
format: "depth24plus",
|
|
512
|
+
depthWriteEnabled: !transparent,
|
|
513
|
+
depthCompare: "less"
|
|
514
|
+
}
|
|
515
|
+
});
|
|
516
|
+
this.pipeline = pipeline("back", false);
|
|
517
|
+
this.transparentBackPipeline = pipeline("front", true);
|
|
518
|
+
this.transparentFrontPipeline = pipeline("back", true);
|
|
519
|
+
this.canvas = canvas;
|
|
520
|
+
this.device = device;
|
|
521
|
+
this.context = context;
|
|
522
|
+
}
|
|
523
|
+
resize() {
|
|
524
|
+
this.ensureDepth();
|
|
525
|
+
}
|
|
526
|
+
ensureDepth() {
|
|
527
|
+
const canvas = this.canvas;
|
|
528
|
+
if (!this.device || !canvas) return;
|
|
529
|
+
const width = Math.max(1, canvas.width);
|
|
530
|
+
const height = Math.max(1, canvas.height);
|
|
531
|
+
const key = `${width}x${height}`;
|
|
532
|
+
if (key === this.depthSize && this.depthTexture) return;
|
|
533
|
+
this.depthTexture?.destroy?.();
|
|
534
|
+
this.depthTexture = this.device.createTexture({
|
|
535
|
+
size: { width, height },
|
|
536
|
+
format: "depth24plus",
|
|
537
|
+
usage: globalThis.GPUTextureUsage.RENDER_ATTACHMENT
|
|
538
|
+
});
|
|
539
|
+
this.depthSize = key;
|
|
540
|
+
}
|
|
541
|
+
buffers(mesh) {
|
|
542
|
+
const cached = this.cache.get(mesh);
|
|
543
|
+
if (cached) return cached;
|
|
544
|
+
const data = expandToTriangles(mesh);
|
|
545
|
+
const usage = globalThis.GPUBufferUsage.VERTEX | globalThis.GPUBufferUsage.COPY_DST;
|
|
546
|
+
const upload = (array) => {
|
|
547
|
+
const buffer = this.device.createBuffer({ size: array.byteLength, usage });
|
|
548
|
+
this.device.queue.writeBuffer(buffer, 0, array);
|
|
549
|
+
return buffer;
|
|
550
|
+
};
|
|
551
|
+
const result = {
|
|
552
|
+
position: upload(data.positions),
|
|
553
|
+
normal: upload(data.normals),
|
|
554
|
+
color: upload(data.colors),
|
|
555
|
+
count: data.count
|
|
556
|
+
};
|
|
557
|
+
this.cache.set(mesh, result);
|
|
558
|
+
return result;
|
|
559
|
+
}
|
|
560
|
+
ensureUniformCapacity(draws) {
|
|
561
|
+
if (draws <= this.uniformCapacity && this.uniformBuffer) return;
|
|
562
|
+
this.uniformBuffer?.destroy?.();
|
|
563
|
+
this.uniformBuffer = this.device.createBuffer({
|
|
564
|
+
size: Math.max(1, draws) * UNIFORM_STRIDE,
|
|
565
|
+
usage: globalThis.GPUBufferUsage.UNIFORM | globalThis.GPUBufferUsage.COPY_DST
|
|
566
|
+
});
|
|
567
|
+
this.uniformCapacity = draws;
|
|
568
|
+
}
|
|
569
|
+
render(frame) {
|
|
570
|
+
if (this.destroyed || !this.device || !this.context || !this.pipeline) return;
|
|
571
|
+
if (frame.width === 0 || frame.height === 0 || frame.items.length === 0) return;
|
|
572
|
+
this.ensureDepth();
|
|
573
|
+
const draws = [];
|
|
574
|
+
for (const item of frame.items) {
|
|
575
|
+
if (!item.transparency) draws.push({ item, opacity: 1, pipeline: this.pipeline });
|
|
576
|
+
}
|
|
577
|
+
for (const item of frame.items) {
|
|
578
|
+
const transparency = item.transparency;
|
|
579
|
+
if (!transparency) continue;
|
|
580
|
+
if (transparency.mode === "two-sided") {
|
|
581
|
+
const resolved = resolveTwoSidedOpacity(transparency);
|
|
582
|
+
draws.push({
|
|
583
|
+
item,
|
|
584
|
+
opacity: resolved.back,
|
|
585
|
+
pipeline: this.transparentBackPipeline
|
|
586
|
+
});
|
|
587
|
+
draws.push({
|
|
588
|
+
item,
|
|
589
|
+
opacity: resolved.front,
|
|
590
|
+
pipeline: this.transparentFrontPipeline
|
|
591
|
+
});
|
|
592
|
+
} else {
|
|
593
|
+
draws.push({
|
|
594
|
+
item,
|
|
595
|
+
opacity: opacity(transparency.opacity, DEFAULT_ONE_SIDED_OPACITY),
|
|
596
|
+
pipeline: this.transparentFrontPipeline
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
this.ensureUniformCapacity(draws.length);
|
|
601
|
+
const viewProj = multiply(CLIP_Z_FIX, frame.viewProjection);
|
|
602
|
+
const layout = this.pipeline.getBindGroupLayout(0);
|
|
603
|
+
const bindGroup = this.device.createBindGroup({
|
|
604
|
+
layout,
|
|
605
|
+
entries: [
|
|
606
|
+
{ binding: 0, resource: { buffer: this.uniformBuffer, offset: 0, size: 160 } }
|
|
607
|
+
]
|
|
608
|
+
});
|
|
609
|
+
draws.forEach((draw, i) => {
|
|
610
|
+
const data = new Float32Array(UNIFORM_STRIDE / 4);
|
|
611
|
+
data.set(viewProj, 0);
|
|
612
|
+
data.set(draw.item.model, 16);
|
|
613
|
+
data.set([frame.light.toLight.x, frame.light.toLight.y, frame.light.toLight.z, 0], 32);
|
|
614
|
+
data.set([frame.light.intensity, frame.light.ambient, draw.opacity, 0], 36);
|
|
615
|
+
this.device.queue.writeBuffer(this.uniformBuffer, i * UNIFORM_STRIDE, data);
|
|
616
|
+
});
|
|
617
|
+
const encoder = this.device.createCommandEncoder();
|
|
618
|
+
const pass = encoder.beginRenderPass({
|
|
619
|
+
colorAttachments: [
|
|
620
|
+
{
|
|
621
|
+
view: this.context.getCurrentTexture().createView(),
|
|
622
|
+
clearValue: this.clearValue,
|
|
623
|
+
loadOp: "clear",
|
|
624
|
+
storeOp: "store"
|
|
625
|
+
}
|
|
626
|
+
],
|
|
627
|
+
depthStencilAttachment: {
|
|
628
|
+
view: this.depthTexture.createView(),
|
|
629
|
+
depthClearValue: 1,
|
|
630
|
+
depthLoadOp: "clear",
|
|
631
|
+
depthStoreOp: "store"
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
draws.forEach((draw, i) => {
|
|
635
|
+
const mesh = this.buffers(draw.item.mesh);
|
|
636
|
+
pass.setPipeline(draw.pipeline);
|
|
637
|
+
pass.setBindGroup(0, bindGroup, [i * UNIFORM_STRIDE]);
|
|
638
|
+
pass.setVertexBuffer(0, mesh.position);
|
|
639
|
+
pass.setVertexBuffer(1, mesh.normal);
|
|
640
|
+
pass.setVertexBuffer(2, mesh.color);
|
|
641
|
+
pass.draw(mesh.count);
|
|
642
|
+
});
|
|
643
|
+
pass.end();
|
|
644
|
+
this.device.queue.submit([encoder.finish()]);
|
|
645
|
+
}
|
|
646
|
+
destroy() {
|
|
647
|
+
this.destroyed = true;
|
|
648
|
+
for (const mesh of this.cache.values()) {
|
|
649
|
+
mesh.position.destroy?.();
|
|
650
|
+
mesh.normal.destroy?.();
|
|
651
|
+
mesh.color.destroy?.();
|
|
652
|
+
}
|
|
653
|
+
this.cache.clear();
|
|
654
|
+
this.uniformBuffer?.destroy?.();
|
|
655
|
+
this.depthTexture?.destroy?.();
|
|
656
|
+
this.device?.destroy?.();
|
|
657
|
+
this.device = void 0;
|
|
658
|
+
this.context = void 0;
|
|
659
|
+
this.pipeline = void 0;
|
|
660
|
+
this.transparentBackPipeline = void 0;
|
|
661
|
+
this.transparentFrontPipeline = void 0;
|
|
662
|
+
this.uniformBuffer = void 0;
|
|
663
|
+
this.depthTexture = void 0;
|
|
664
|
+
this.canvas = void 0;
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
// src/engines/little-3d-engine/renderers/canvas2d.ts
|
|
671
|
+
var canvas2d_exports = {};
|
|
672
|
+
__export(canvas2d_exports, {
|
|
673
|
+
Canvas2DRenderer: () => Canvas2DRenderer
|
|
674
|
+
});
|
|
675
|
+
var Canvas2DRenderer;
|
|
676
|
+
var init_canvas2d = __esm({
|
|
677
|
+
"src/engines/little-3d-engine/renderers/canvas2d.ts"() {
|
|
678
|
+
"use strict";
|
|
679
|
+
init_light();
|
|
680
|
+
init_math();
|
|
681
|
+
init_renderer();
|
|
682
|
+
Canvas2DRenderer = class {
|
|
683
|
+
constructor(options = {}) {
|
|
684
|
+
this.options = options;
|
|
685
|
+
this.dpr = 1;
|
|
686
|
+
}
|
|
687
|
+
init(canvas) {
|
|
688
|
+
this.ctx = canvas.getContext("2d") ?? void 0;
|
|
689
|
+
}
|
|
690
|
+
resize(_cssWidth, _cssHeight, dpr) {
|
|
691
|
+
this.dpr = dpr;
|
|
692
|
+
this.ctx?.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
693
|
+
}
|
|
694
|
+
render(frame) {
|
|
695
|
+
const ctx = this.ctx;
|
|
696
|
+
if (!ctx) return;
|
|
697
|
+
if (this.options.background) {
|
|
698
|
+
ctx.fillStyle = this.options.background;
|
|
699
|
+
ctx.fillRect(0, 0, frame.width, frame.height);
|
|
700
|
+
} else {
|
|
701
|
+
ctx.clearRect(0, 0, frame.width, frame.height);
|
|
702
|
+
}
|
|
703
|
+
const polygons = [];
|
|
704
|
+
for (const item of frame.items) {
|
|
705
|
+
const world = item.mesh.vertices.map((v) => transformAffine(item.model, v));
|
|
706
|
+
const twoSidedOpacity = item.transparency?.mode === "two-sided" ? resolveTwoSidedOpacity(item.transparency) : void 0;
|
|
707
|
+
for (const face of item.mesh.faces) {
|
|
708
|
+
const a = world[face.indices[0]];
|
|
709
|
+
const b = world[face.indices[1]];
|
|
710
|
+
const c = world[face.indices[2]];
|
|
711
|
+
const normal = normalize(cross(subtract(b, a), subtract(c, a)));
|
|
712
|
+
const frontFacing = dot(normal, subtract(frame.eye, a)) > 0;
|
|
713
|
+
const transparency = item.transparency;
|
|
714
|
+
if (!frontFacing && transparency?.mode !== "two-sided") continue;
|
|
715
|
+
let faceOpacity = 1;
|
|
716
|
+
if (transparency?.mode === "one-sided") {
|
|
717
|
+
faceOpacity = opacity(transparency.opacity, DEFAULT_ONE_SIDED_OPACITY);
|
|
718
|
+
} else if (twoSidedOpacity) {
|
|
719
|
+
faceOpacity = frontFacing ? twoSidedOpacity.front : twoSidedOpacity.back;
|
|
720
|
+
}
|
|
721
|
+
const points = face.indices.map((i) => {
|
|
722
|
+
const ndc = transformPoint(frame.viewProjection, world[i]);
|
|
723
|
+
return {
|
|
724
|
+
x: (ndc.x * 0.5 + 0.5) * frame.width,
|
|
725
|
+
y: (1 - (ndc.y * 0.5 + 0.5)) * frame.height
|
|
726
|
+
};
|
|
727
|
+
});
|
|
728
|
+
let depth = 0;
|
|
729
|
+
for (const i of face.indices) {
|
|
730
|
+
const d = subtract(world[i], frame.eye);
|
|
731
|
+
depth += dot(d, d);
|
|
732
|
+
}
|
|
733
|
+
depth /= face.indices.length;
|
|
734
|
+
polygons.push({
|
|
735
|
+
points,
|
|
736
|
+
color: shadeColor(normal, face.color, frame.light),
|
|
737
|
+
depth,
|
|
738
|
+
opacity: faceOpacity
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
polygons.sort((p, q) => q.depth - p.depth);
|
|
743
|
+
for (const poly of polygons) {
|
|
744
|
+
if (poly.points.length < 3) continue;
|
|
745
|
+
ctx.beginPath();
|
|
746
|
+
ctx.moveTo(poly.points[0].x, poly.points[0].y);
|
|
747
|
+
for (let i = 1; i < poly.points.length; i++) {
|
|
748
|
+
ctx.lineTo(poly.points[i].x, poly.points[i].y);
|
|
749
|
+
}
|
|
750
|
+
ctx.closePath();
|
|
751
|
+
ctx.fillStyle = poly.color;
|
|
752
|
+
ctx.strokeStyle = poly.color;
|
|
753
|
+
ctx.lineWidth = 1;
|
|
754
|
+
ctx.globalAlpha = poly.opacity;
|
|
755
|
+
ctx.fill();
|
|
756
|
+
ctx.stroke();
|
|
757
|
+
}
|
|
758
|
+
ctx.globalAlpha = 1;
|
|
759
|
+
}
|
|
760
|
+
destroy() {
|
|
761
|
+
this.ctx = void 0;
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
});
|
|
766
|
+
|
|
767
|
+
// src/engines/little-3d-engine/renderer.ts
|
|
768
|
+
function opacity(value, fallback) {
|
|
769
|
+
return Math.max(0, Math.min(1, value ?? fallback));
|
|
770
|
+
}
|
|
771
|
+
function resolveTwoSidedOpacity(transparency) {
|
|
772
|
+
const front = opacity(
|
|
773
|
+
transparency.frontOpacity ?? transparency.opacity,
|
|
774
|
+
DEFAULT_FRONT_OPACITY
|
|
775
|
+
);
|
|
776
|
+
const backFallback = transparency.opacity === void 0 ? DEFAULT_BACK_OPACITY : front * (2 / 3);
|
|
777
|
+
return {
|
|
778
|
+
front,
|
|
779
|
+
back: opacity(transparency.backOpacity, backFallback)
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
function orderRenderItems(items, eye) {
|
|
783
|
+
const opaque = [];
|
|
784
|
+
const transparent = [];
|
|
785
|
+
for (const item of items) {
|
|
786
|
+
(item.transparency ? transparent : opaque).push(item);
|
|
787
|
+
}
|
|
788
|
+
transparent.sort((a, b) => {
|
|
789
|
+
const ax = a.model[12] - eye.x;
|
|
790
|
+
const ay = a.model[13] - eye.y;
|
|
791
|
+
const az = a.model[14] - eye.z;
|
|
792
|
+
const bx = b.model[12] - eye.x;
|
|
793
|
+
const by = b.model[13] - eye.y;
|
|
794
|
+
const bz = b.model[14] - eye.z;
|
|
795
|
+
return bx * bx + by * by + bz * bz - (ax * ax + ay * ay + az * az);
|
|
796
|
+
});
|
|
797
|
+
return opaque.concat(transparent);
|
|
798
|
+
}
|
|
799
|
+
async function createRenderer(backend, options = {}) {
|
|
800
|
+
if (typeof backend === "function") return backend(options);
|
|
801
|
+
switch (backend) {
|
|
802
|
+
case "webgl":
|
|
803
|
+
return new (await Promise.resolve().then(() => (init_webgl(), webgl_exports))).WebGLRenderer(options);
|
|
804
|
+
case "webgpu":
|
|
805
|
+
return new (await Promise.resolve().then(() => (init_webgpu(), webgpu_exports))).WebGPURenderer(options);
|
|
806
|
+
case "canvas2d":
|
|
807
|
+
default:
|
|
808
|
+
return new (await Promise.resolve().then(() => (init_canvas2d(), canvas2d_exports))).Canvas2DRenderer(options);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
var DEFAULT_ONE_SIDED_OPACITY, DEFAULT_BACK_OPACITY, DEFAULT_FRONT_OPACITY;
|
|
812
|
+
var init_renderer = __esm({
|
|
813
|
+
"src/engines/little-3d-engine/renderer.ts"() {
|
|
814
|
+
"use strict";
|
|
815
|
+
DEFAULT_ONE_SIDED_OPACITY = 0.35;
|
|
816
|
+
DEFAULT_BACK_OPACITY = 0.84;
|
|
817
|
+
DEFAULT_FRONT_OPACITY = 0.56;
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
// src/animations/spin.ts
|
|
822
|
+
var spin_exports = {};
|
|
823
|
+
__export(spin_exports, {
|
|
824
|
+
SpinAnimation: () => SpinAnimation
|
|
825
|
+
});
|
|
826
|
+
module.exports = __toCommonJS(spin_exports);
|
|
827
|
+
|
|
828
|
+
// src/engines/little-3d-engine/core/camera.ts
|
|
829
|
+
init_math();
|
|
830
|
+
var DEFAULTS = {
|
|
831
|
+
position: { x: 0, y: 0, z: 4 },
|
|
832
|
+
fov: 55 * Math.PI / 180,
|
|
833
|
+
near: 0.1,
|
|
834
|
+
far: 100
|
|
835
|
+
};
|
|
836
|
+
var Camera = class {
|
|
837
|
+
constructor(options) {
|
|
838
|
+
this.options = { ...DEFAULTS, ...options };
|
|
839
|
+
}
|
|
840
|
+
/** Transform a world-space point into view (camera) space. */
|
|
841
|
+
toView(p) {
|
|
842
|
+
const { position } = this.options;
|
|
843
|
+
return transformAffine(translation(-position.x, -position.y, -position.z), p);
|
|
844
|
+
}
|
|
845
|
+
/** Combined view-projection matrix for the given viewport aspect ratio. */
|
|
846
|
+
viewProjection(aspect) {
|
|
847
|
+
const { position, fov, near, far } = this.options;
|
|
848
|
+
const view = translation(-position.x, -position.y, -position.z);
|
|
849
|
+
const projection = perspective(fov, aspect, near, far);
|
|
850
|
+
return multiply(projection, view);
|
|
851
|
+
}
|
|
852
|
+
/** Convert a normalized device coordinate (-1..1) to a pixel position. */
|
|
853
|
+
toScreen(ndc, width, height) {
|
|
854
|
+
return {
|
|
855
|
+
x: (ndc.x * 0.5 + 0.5) * width,
|
|
856
|
+
y: (1 - (ndc.y * 0.5 + 0.5)) * height
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
|
|
861
|
+
// src/engines/little-3d-engine/little-3d-engine.ts
|
|
862
|
+
init_light();
|
|
863
|
+
init_math();
|
|
864
|
+
|
|
865
|
+
// src/engines/little-3d-engine/core/mesh.ts
|
|
866
|
+
function transform(init) {
|
|
867
|
+
return {
|
|
868
|
+
position: init?.position ?? { x: 0, y: 0, z: 0 },
|
|
869
|
+
rotation: init?.rotation ?? { x: 0, y: 0, z: 0 },
|
|
870
|
+
scale: init?.scale ?? 1
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// src/engines/little-3d-engine/little-3d-engine.ts
|
|
875
|
+
init_renderer();
|
|
876
|
+
init_light();
|
|
877
|
+
|
|
878
|
+
// src/engines/little-3d-engine/shapes/primitives/cube.ts
|
|
879
|
+
var DEFAULT_COLORS = ["#3b82f6", "#8b5cf6", "#ec4899", "#f59e0b", "#10b981", "#ef4444"];
|
|
880
|
+
function cube(size = 1, colors = DEFAULT_COLORS) {
|
|
881
|
+
const h = size / 2;
|
|
882
|
+
const vertices = [
|
|
883
|
+
{ x: -h, y: -h, z: h },
|
|
884
|
+
{ x: h, y: -h, z: h },
|
|
885
|
+
{ x: h, y: h, z: h },
|
|
886
|
+
{ x: -h, y: h, z: h },
|
|
887
|
+
{ x: -h, y: -h, z: -h },
|
|
888
|
+
{ x: h, y: -h, z: -h },
|
|
889
|
+
{ x: h, y: h, z: -h },
|
|
890
|
+
{ x: -h, y: h, z: -h }
|
|
891
|
+
];
|
|
892
|
+
const faces = [
|
|
893
|
+
{ indices: [0, 1, 2, 3], color: colors[0 % colors.length] },
|
|
894
|
+
{ indices: [5, 4, 7, 6], color: colors[1 % colors.length] },
|
|
895
|
+
{ indices: [3, 2, 6, 7], color: colors[2 % colors.length] },
|
|
896
|
+
{ indices: [4, 5, 1, 0], color: colors[3 % colors.length] },
|
|
897
|
+
{ indices: [1, 5, 6, 2], color: colors[4 % colors.length] },
|
|
898
|
+
{ indices: [4, 0, 3, 7], color: colors[5 % colors.length] }
|
|
899
|
+
];
|
|
900
|
+
return { vertices, faces };
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
// src/engines/little-3d-engine/shapes/primitives/spheres/icosphere.ts
|
|
904
|
+
init_geometry();
|
|
905
|
+
var T = (1 + Math.sqrt(5)) / 2;
|
|
906
|
+
var SEED_VERTICES = [
|
|
907
|
+
{ x: -1, y: T, z: 0 },
|
|
908
|
+
{ x: 1, y: T, z: 0 },
|
|
909
|
+
{ x: -1, y: -T, z: 0 },
|
|
910
|
+
{ x: 1, y: -T, z: 0 },
|
|
911
|
+
{ x: 0, y: -1, z: T },
|
|
912
|
+
{ x: 0, y: 1, z: T },
|
|
913
|
+
{ x: 0, y: -1, z: -T },
|
|
914
|
+
{ x: 0, y: 1, z: -T },
|
|
915
|
+
{ x: T, y: 0, z: -1 },
|
|
916
|
+
{ x: T, y: 0, z: 1 },
|
|
917
|
+
{ x: -T, y: 0, z: -1 },
|
|
918
|
+
{ x: -T, y: 0, z: 1 }
|
|
919
|
+
];
|
|
920
|
+
|
|
921
|
+
// src/engines/little-3d-engine/shapes/primitives/spheres/octa-sphere.ts
|
|
922
|
+
init_geometry();
|
|
923
|
+
|
|
924
|
+
// src/engines/little-3d-engine/little-3d-engine.ts
|
|
925
|
+
init_geometry();
|
|
926
|
+
init_renderer();
|
|
927
|
+
init_math();
|
|
928
|
+
function modelMatrix(t) {
|
|
929
|
+
const rotation = multiply(
|
|
930
|
+
rotationZ(t.rotation.z),
|
|
931
|
+
multiply(rotationY(t.rotation.y), rotationX(t.rotation.x))
|
|
932
|
+
);
|
|
933
|
+
return multiply(
|
|
934
|
+
translation(t.position.x, t.position.y, t.position.z),
|
|
935
|
+
multiply(rotation, scaleMatrix(t.scale))
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
var Little3dEngine = class {
|
|
939
|
+
constructor(options = {}) {
|
|
940
|
+
this.scene = [];
|
|
941
|
+
this.cssWidth = 0;
|
|
942
|
+
this.cssHeight = 0;
|
|
943
|
+
this.ready = false;
|
|
944
|
+
this.generation = 0;
|
|
945
|
+
this.rafId = 0;
|
|
946
|
+
this.running = false;
|
|
947
|
+
this.camera = new Camera(options.camera);
|
|
948
|
+
this.light = new Light(options.light);
|
|
949
|
+
this.backend = options.backend ?? "canvas2d";
|
|
950
|
+
this.background = options.background;
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* Create the canvas inside `target`, load the selected backend, and start
|
|
954
|
+
* tracking size. Resolves once the renderer is ready; rejects if the backend
|
|
955
|
+
* is unavailable. Drawing is a no-op until it resolves.
|
|
956
|
+
*/
|
|
957
|
+
async mount(target) {
|
|
958
|
+
const canvas = document.createElement("canvas");
|
|
959
|
+
canvas.style.display = "block";
|
|
960
|
+
canvas.style.width = "100%";
|
|
961
|
+
canvas.style.height = "100%";
|
|
962
|
+
target.appendChild(canvas);
|
|
963
|
+
this.canvas = canvas;
|
|
964
|
+
this.observer = new ResizeObserver(() => this.resize());
|
|
965
|
+
this.observer.observe(canvas);
|
|
966
|
+
this.resize();
|
|
967
|
+
const generation = this.generation;
|
|
968
|
+
const dropCanvas = () => {
|
|
969
|
+
if (this.canvas !== canvas) return;
|
|
970
|
+
this.observer?.disconnect();
|
|
971
|
+
this.observer = void 0;
|
|
972
|
+
canvas.remove();
|
|
973
|
+
this.canvas = void 0;
|
|
974
|
+
};
|
|
975
|
+
try {
|
|
976
|
+
const renderer = await createRenderer(this.backend, { background: this.background });
|
|
977
|
+
if (generation !== this.generation) {
|
|
978
|
+
renderer.destroy();
|
|
979
|
+
dropCanvas();
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
await renderer.init(canvas);
|
|
983
|
+
if (generation !== this.generation) {
|
|
984
|
+
renderer.destroy();
|
|
985
|
+
dropCanvas();
|
|
986
|
+
return;
|
|
987
|
+
}
|
|
988
|
+
this.renderer = renderer;
|
|
989
|
+
this.resize();
|
|
990
|
+
this.ready = true;
|
|
991
|
+
} catch (error) {
|
|
992
|
+
dropCanvas();
|
|
993
|
+
throw error;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
/** Add a mesh to the scene and return a handle for animating it. */
|
|
997
|
+
add(mesh, init) {
|
|
998
|
+
const entry = {
|
|
999
|
+
mesh,
|
|
1000
|
+
transform: transform(init),
|
|
1001
|
+
transparency: init?.transparency,
|
|
1002
|
+
remove: () => {
|
|
1003
|
+
const i = this.scene.indexOf(entry);
|
|
1004
|
+
if (i >= 0) this.scene.splice(i, 1);
|
|
1005
|
+
}
|
|
1006
|
+
};
|
|
1007
|
+
this.scene.push(entry);
|
|
1008
|
+
return entry;
|
|
1009
|
+
}
|
|
1010
|
+
resize() {
|
|
1011
|
+
const canvas = this.canvas;
|
|
1012
|
+
if (!canvas) return;
|
|
1013
|
+
const dpr = window.devicePixelRatio || 1;
|
|
1014
|
+
this.cssWidth = canvas.clientWidth || canvas.parentElement?.clientWidth || 0;
|
|
1015
|
+
this.cssHeight = canvas.clientHeight || canvas.parentElement?.clientHeight || 0;
|
|
1016
|
+
canvas.width = Math.max(1, Math.round(this.cssWidth * dpr));
|
|
1017
|
+
canvas.height = Math.max(1, Math.round(this.cssHeight * dpr));
|
|
1018
|
+
this.renderer?.resize(this.cssWidth, this.cssHeight, dpr);
|
|
1019
|
+
}
|
|
1020
|
+
/** Draw a single frame from the current scene state. */
|
|
1021
|
+
render() {
|
|
1022
|
+
if (!this.ready || !this.renderer) return;
|
|
1023
|
+
const width = this.cssWidth;
|
|
1024
|
+
const height = this.cssHeight;
|
|
1025
|
+
if (width === 0 || height === 0) return;
|
|
1026
|
+
const items = this.scene.map((entry) => ({
|
|
1027
|
+
mesh: entry.mesh,
|
|
1028
|
+
model: modelMatrix(entry.transform),
|
|
1029
|
+
transparency: entry.transparency
|
|
1030
|
+
}));
|
|
1031
|
+
const eye = this.camera.options.position;
|
|
1032
|
+
this.renderer.render({
|
|
1033
|
+
items: orderRenderItems(items, eye),
|
|
1034
|
+
viewProjection: this.camera.viewProjection(width / height),
|
|
1035
|
+
eye,
|
|
1036
|
+
light: this.light.params,
|
|
1037
|
+
width,
|
|
1038
|
+
height
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
/** Start an internal animation loop that calls {@link render} each frame. */
|
|
1042
|
+
start() {
|
|
1043
|
+
if (this.running) return;
|
|
1044
|
+
this.running = true;
|
|
1045
|
+
const loop = () => {
|
|
1046
|
+
if (!this.running) return;
|
|
1047
|
+
this.render();
|
|
1048
|
+
this.rafId = requestAnimationFrame(loop);
|
|
1049
|
+
};
|
|
1050
|
+
this.rafId = requestAnimationFrame(loop);
|
|
1051
|
+
}
|
|
1052
|
+
/** Stop the internal animation loop started by {@link start}. */
|
|
1053
|
+
stop() {
|
|
1054
|
+
this.running = false;
|
|
1055
|
+
if (this.rafId) cancelAnimationFrame(this.rafId);
|
|
1056
|
+
this.rafId = 0;
|
|
1057
|
+
}
|
|
1058
|
+
/** Stop animating, release the renderer, and remove the canvas. */
|
|
1059
|
+
destroy() {
|
|
1060
|
+
this.generation++;
|
|
1061
|
+
this.ready = false;
|
|
1062
|
+
this.stop();
|
|
1063
|
+
this.observer?.disconnect();
|
|
1064
|
+
this.observer = void 0;
|
|
1065
|
+
this.renderer?.destroy();
|
|
1066
|
+
this.renderer = void 0;
|
|
1067
|
+
this.canvas?.remove();
|
|
1068
|
+
this.canvas = void 0;
|
|
1069
|
+
}
|
|
1070
|
+
};
|
|
1071
|
+
|
|
1072
|
+
// src/engines/little-tween-engine/core/tweens.ts
|
|
1073
|
+
function input(value, overextend) {
|
|
1074
|
+
if (Number.isNaN(value)) return 0;
|
|
1075
|
+
if (overextend) return value;
|
|
1076
|
+
return Math.min(1, Math.max(0, value));
|
|
1077
|
+
}
|
|
1078
|
+
function easeInQuad(value, overextend = false) {
|
|
1079
|
+
const x = input(value, overextend);
|
|
1080
|
+
return x * x;
|
|
1081
|
+
}
|
|
1082
|
+
function easeOutQuad(value, overextend = false) {
|
|
1083
|
+
const x = input(value, overextend);
|
|
1084
|
+
return 1 - (1 - x) * (1 - x);
|
|
1085
|
+
}
|
|
1086
|
+
function easeOutCubic(value, overextend = false) {
|
|
1087
|
+
const x = input(value, overextend);
|
|
1088
|
+
return 1 - Math.pow(1 - x, 3);
|
|
1089
|
+
}
|
|
1090
|
+
function easeOutExpo(value, overextend = false) {
|
|
1091
|
+
const x = input(value, overextend);
|
|
1092
|
+
return x === 1 ? 1 : 1 - Math.pow(2, -10 * x);
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
// src/progress-animation.ts
|
|
1096
|
+
function resolveOptions(options = {}) {
|
|
1097
|
+
return {
|
|
1098
|
+
popDurationMs: options.popDurationMs ?? 500,
|
|
1099
|
+
overextend: options.overextend ?? 0.2,
|
|
1100
|
+
startSnapRatio: options.startSnapRatio ?? 0.2,
|
|
1101
|
+
loadingText: options.loadingText === void 0 ? "loading" : options.loadingText,
|
|
1102
|
+
doneText: options.doneText ?? "done",
|
|
1103
|
+
doneFadeDurationMs: options.doneFadeDurationMs ?? 2e3,
|
|
1104
|
+
removeOnComplete: options.removeOnComplete ?? false
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
function popPhaseT(now, phaseStart, durationMs) {
|
|
1108
|
+
if (durationMs <= 0) return 1;
|
|
1109
|
+
return Math.min(1, Math.max(0, (now - phaseStart) / durationMs));
|
|
1110
|
+
}
|
|
1111
|
+
var ProgressAnimation = class {
|
|
1112
|
+
constructor(options = {}) {
|
|
1113
|
+
this.phase = "idle";
|
|
1114
|
+
this.phaseStart = 0;
|
|
1115
|
+
this.activeProgress = 0;
|
|
1116
|
+
this.popTarget = 0;
|
|
1117
|
+
this.doneFadeStart = 0;
|
|
1118
|
+
this.options = resolveOptions(options);
|
|
1119
|
+
}
|
|
1120
|
+
/** Begin the intro pop. Ignored unless idle. */
|
|
1121
|
+
enter(now) {
|
|
1122
|
+
if (this.phase !== "idle") return;
|
|
1123
|
+
this.phase = "startPop";
|
|
1124
|
+
this.phaseStart = now;
|
|
1125
|
+
this.activeProgress = 0;
|
|
1126
|
+
this.popTarget = 0;
|
|
1127
|
+
}
|
|
1128
|
+
/** Begin the outro pop. Ignored unless mid-intro or active. */
|
|
1129
|
+
exit(now) {
|
|
1130
|
+
if (this.phase !== "startPop" && this.phase !== "active") return;
|
|
1131
|
+
this.phase = "endPop";
|
|
1132
|
+
this.phaseStart = now;
|
|
1133
|
+
}
|
|
1134
|
+
isFinished() {
|
|
1135
|
+
return this.phase === "finished";
|
|
1136
|
+
}
|
|
1137
|
+
update(now, progress, targetProgress) {
|
|
1138
|
+
const {
|
|
1139
|
+
popDurationMs,
|
|
1140
|
+
overextend,
|
|
1141
|
+
startSnapRatio,
|
|
1142
|
+
loadingText,
|
|
1143
|
+
doneText,
|
|
1144
|
+
doneFadeDurationMs,
|
|
1145
|
+
removeOnComplete
|
|
1146
|
+
} = this.options;
|
|
1147
|
+
const goal = targetProgress ?? progress;
|
|
1148
|
+
if (this.phase === "startPop" || this.phase === "active") {
|
|
1149
|
+
this.activeProgress = progress;
|
|
1150
|
+
if (this.phase === "startPop") this.popTarget = Math.max(this.popTarget, goal, progress);
|
|
1151
|
+
}
|
|
1152
|
+
let scale2 = 0;
|
|
1153
|
+
let text = null;
|
|
1154
|
+
let textOpacity = 0;
|
|
1155
|
+
let hidden = false;
|
|
1156
|
+
if (this.phase === "startPop") {
|
|
1157
|
+
const t = popPhaseT(now, this.phaseStart, popDurationMs);
|
|
1158
|
+
const peak = this.popTarget * (1 + overextend);
|
|
1159
|
+
if (t < startSnapRatio) {
|
|
1160
|
+
const snapT = startSnapRatio > 0 ? t / startSnapRatio : 1;
|
|
1161
|
+
scale2 = peak * easeOutExpo(snapT);
|
|
1162
|
+
} else {
|
|
1163
|
+
const settleT = startSnapRatio < 1 ? (t - startSnapRatio) / (1 - startSnapRatio) : 1;
|
|
1164
|
+
scale2 = peak + (this.activeProgress - peak) * easeOutCubic(settleT);
|
|
1165
|
+
}
|
|
1166
|
+
if (t >= 1) this.phase = "active";
|
|
1167
|
+
} else if (this.phase === "active") {
|
|
1168
|
+
scale2 = this.activeProgress;
|
|
1169
|
+
} else if (this.phase === "endPop") {
|
|
1170
|
+
const t = popPhaseT(now, this.phaseStart, popDurationMs);
|
|
1171
|
+
const peak = 1 + overextend;
|
|
1172
|
+
if (t < 0.5) {
|
|
1173
|
+
scale2 = 1 + (peak - 1) * easeOutQuad(t * 2);
|
|
1174
|
+
} else {
|
|
1175
|
+
scale2 = peak * (1 - easeInQuad((t - 0.5) * 2));
|
|
1176
|
+
}
|
|
1177
|
+
if (t >= 1) {
|
|
1178
|
+
this.phase = "done";
|
|
1179
|
+
this.doneFadeStart = now;
|
|
1180
|
+
scale2 = 0;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
if (this.phase === "startPop" || this.phase === "active") {
|
|
1184
|
+
if (loadingText !== false) {
|
|
1185
|
+
text = loadingText;
|
|
1186
|
+
textOpacity = 0.65;
|
|
1187
|
+
}
|
|
1188
|
+
} else if (this.phase === "endPop") {
|
|
1189
|
+
text = doneText;
|
|
1190
|
+
textOpacity = 0.65;
|
|
1191
|
+
} else if (this.phase === "done") {
|
|
1192
|
+
const fadeT = popPhaseT(now, this.doneFadeStart, doneFadeDurationMs);
|
|
1193
|
+
if (fadeT >= 1) {
|
|
1194
|
+
if (removeOnComplete) hidden = true;
|
|
1195
|
+
this.phase = "finished";
|
|
1196
|
+
} else {
|
|
1197
|
+
text = doneText;
|
|
1198
|
+
textOpacity = 0.65 * (1 - fadeT);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
return { scale: scale2, text, textOpacity, hidden };
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
|
|
1205
|
+
// src/animations/spin.ts
|
|
1206
|
+
var LABEL_STYLE = [
|
|
1207
|
+
"position:absolute",
|
|
1208
|
+
"inset:0",
|
|
1209
|
+
"display:flex",
|
|
1210
|
+
"align-items:center",
|
|
1211
|
+
"justify-content:center",
|
|
1212
|
+
"pointer-events:none",
|
|
1213
|
+
"font:600 1.1rem/1.2 system-ui,sans-serif",
|
|
1214
|
+
"letter-spacing:0.06em",
|
|
1215
|
+
"text-transform:lowercase",
|
|
1216
|
+
"color:rgba(255,255,255,0.65)",
|
|
1217
|
+
"z-index:1"
|
|
1218
|
+
].join(";");
|
|
1219
|
+
function resolveMesh(shape) {
|
|
1220
|
+
if (!shape) return cube();
|
|
1221
|
+
return typeof shape === "function" ? shape() : shape;
|
|
1222
|
+
}
|
|
1223
|
+
function applyColor(mesh, color) {
|
|
1224
|
+
if (color === void 0 || Array.isArray(color) && color.length === 0) return mesh;
|
|
1225
|
+
const pick = Array.isArray(color) ? (i) => color[i % color.length] : () => color;
|
|
1226
|
+
return { vertices: mesh.vertices, faces: mesh.faces.map((f, i) => ({ ...f, color: pick(i) })) };
|
|
1227
|
+
}
|
|
1228
|
+
var SpinAnimation = class {
|
|
1229
|
+
constructor(options = {}) {
|
|
1230
|
+
this.exited = false;
|
|
1231
|
+
this.mesh = applyColor(resolveMesh(options.shape), options.color);
|
|
1232
|
+
this.spinX = options.spinX ?? 7e-4;
|
|
1233
|
+
this.spinY = options.spinY ?? 11e-4;
|
|
1234
|
+
this.backend = options.backend;
|
|
1235
|
+
this.transparency = options.transparency;
|
|
1236
|
+
this.progress = options.progressAnimation ? new ProgressAnimation(options.progressAnimation) : void 0;
|
|
1237
|
+
}
|
|
1238
|
+
mount(target) {
|
|
1239
|
+
target.style.position = "relative";
|
|
1240
|
+
const engine = new Little3dEngine({
|
|
1241
|
+
backend: this.backend,
|
|
1242
|
+
camera: { position: { x: 0, y: 0, z: 2.8 } }
|
|
1243
|
+
});
|
|
1244
|
+
this.handle = engine.add(this.mesh, { transparency: this.transparency });
|
|
1245
|
+
this.engine = engine;
|
|
1246
|
+
engine.mount(target).catch((error) => {
|
|
1247
|
+
target.textContent = error instanceof Error ? error.message : String(error);
|
|
1248
|
+
});
|
|
1249
|
+
if (this.progress) {
|
|
1250
|
+
const label = document.createElement("div");
|
|
1251
|
+
label.style.cssText = LABEL_STYLE;
|
|
1252
|
+
label.setAttribute("aria-hidden", "true");
|
|
1253
|
+
label.hidden = true;
|
|
1254
|
+
target.appendChild(label);
|
|
1255
|
+
this.label = label;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
enter(now) {
|
|
1259
|
+
this.progress?.enter(now);
|
|
1260
|
+
}
|
|
1261
|
+
exit(now) {
|
|
1262
|
+
this.exited = true;
|
|
1263
|
+
this.progress?.exit(now);
|
|
1264
|
+
}
|
|
1265
|
+
isFinished() {
|
|
1266
|
+
return this.progress ? this.progress.isFinished() : this.exited;
|
|
1267
|
+
}
|
|
1268
|
+
render(now, frame) {
|
|
1269
|
+
if (!this.engine || !this.handle) return;
|
|
1270
|
+
const rotation = this.handle.transform.rotation;
|
|
1271
|
+
rotation.x = now * this.spinX;
|
|
1272
|
+
rotation.y = now * this.spinY;
|
|
1273
|
+
if (this.progress) {
|
|
1274
|
+
const visual = this.progress.update(now, frame.progress, frame.targetProgress);
|
|
1275
|
+
this.handle.transform.scale = visual.hidden ? 0 : visual.scale;
|
|
1276
|
+
this.applyLabel(visual);
|
|
1277
|
+
} else {
|
|
1278
|
+
this.handle.transform.scale = 1;
|
|
1279
|
+
}
|
|
1280
|
+
this.engine.render();
|
|
1281
|
+
}
|
|
1282
|
+
destroy() {
|
|
1283
|
+
this.label?.remove();
|
|
1284
|
+
this.label = void 0;
|
|
1285
|
+
this.engine?.destroy();
|
|
1286
|
+
this.engine = void 0;
|
|
1287
|
+
this.handle = void 0;
|
|
1288
|
+
}
|
|
1289
|
+
applyLabel(visual) {
|
|
1290
|
+
if (!this.label) return;
|
|
1291
|
+
if (visual.hidden || visual.text == null) {
|
|
1292
|
+
this.label.hidden = true;
|
|
1293
|
+
this.label.textContent = "";
|
|
1294
|
+
return;
|
|
1295
|
+
}
|
|
1296
|
+
this.label.hidden = false;
|
|
1297
|
+
this.label.textContent = visual.text;
|
|
1298
|
+
this.label.style.opacity = String(visual.textOpacity);
|
|
1299
|
+
}
|
|
1300
|
+
};
|