3d-spinner 0.9.2 → 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 +66 -4
- 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 +124 -43
- package/dist/cjs/animations/particles.cjs +1940 -0
- package/dist/cjs/animations/spin.cjs +4 -3
- package/dist/cjs/composite-animation.cjs +58 -0
- package/dist/cjs/engines/little-3d-engine/little-3d-engine.cjs +131 -22
- package/dist/cjs/engines/little-3d-engine/loaders/obj.cjs +30 -1
- 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/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 +3055 -1251
- package/dist/umd/spinner.global.min.js +60 -5
- package/package.json +42 -1
- /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,1940 @@
|
|
|
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/engines/little-3d-engine/renderers/textured-helpers.ts
|
|
822
|
+
function planarUVs(mesh) {
|
|
823
|
+
let minX = Infinity;
|
|
824
|
+
let minY = Infinity;
|
|
825
|
+
let maxX = -Infinity;
|
|
826
|
+
let maxY = -Infinity;
|
|
827
|
+
for (const v of mesh.vertices) {
|
|
828
|
+
minX = Math.min(minX, v.x);
|
|
829
|
+
minY = Math.min(minY, v.y);
|
|
830
|
+
maxX = Math.max(maxX, v.x);
|
|
831
|
+
maxY = Math.max(maxY, v.y);
|
|
832
|
+
}
|
|
833
|
+
const width = maxX - minX || 1;
|
|
834
|
+
const height = maxY - minY || 1;
|
|
835
|
+
let triangles = 0;
|
|
836
|
+
for (const face of mesh.faces) triangles += Math.max(0, face.indices.length - 2);
|
|
837
|
+
const uvs = new Float32Array(triangles * 6);
|
|
838
|
+
let o = 0;
|
|
839
|
+
for (const face of mesh.faces) {
|
|
840
|
+
for (let k = 1; k < face.indices.length - 1; k++) {
|
|
841
|
+
for (const index of [face.indices[0], face.indices[k], face.indices[k + 1]]) {
|
|
842
|
+
const v = mesh.vertices[index];
|
|
843
|
+
uvs[o] = (v.x - minX) / width;
|
|
844
|
+
uvs[o + 1] = 1 - (v.y - minY) / height;
|
|
845
|
+
o += 2;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
return uvs;
|
|
850
|
+
}
|
|
851
|
+
var init_textured_helpers = __esm({
|
|
852
|
+
"src/engines/little-3d-engine/renderers/textured-helpers.ts"() {
|
|
853
|
+
"use strict";
|
|
854
|
+
}
|
|
855
|
+
});
|
|
856
|
+
|
|
857
|
+
// src/engines/little-3d-engine/renderers/webgpu-textured.ts
|
|
858
|
+
var webgpu_textured_exports = {};
|
|
859
|
+
__export(webgpu_textured_exports, {
|
|
860
|
+
WebGPUTexturedRenderer: () => WebGPUTexturedRenderer
|
|
861
|
+
});
|
|
862
|
+
function itemOpacity(transparency) {
|
|
863
|
+
if (!transparency) return 1;
|
|
864
|
+
if (transparency.mode === "two-sided") return resolveTwoSidedOpacity(transparency).front;
|
|
865
|
+
return opacity(transparency.opacity, DEFAULT_ONE_SIDED_OPACITY);
|
|
866
|
+
}
|
|
867
|
+
var WGSL2, CLIP_Z_FIX2, UNIFORM_STRIDE2, WebGPUTexturedRenderer;
|
|
868
|
+
var init_webgpu_textured = __esm({
|
|
869
|
+
"src/engines/little-3d-engine/renderers/webgpu-textured.ts"() {
|
|
870
|
+
"use strict";
|
|
871
|
+
init_geometry();
|
|
872
|
+
init_math();
|
|
873
|
+
init_renderer();
|
|
874
|
+
init_textured_helpers();
|
|
875
|
+
init_webgpu();
|
|
876
|
+
WGSL2 = `
|
|
877
|
+
struct Uniforms {
|
|
878
|
+
viewProj: mat4x4<f32>,
|
|
879
|
+
model: mat4x4<f32>,
|
|
880
|
+
params: vec4<f32>,
|
|
881
|
+
};
|
|
882
|
+
@group(0) @binding(0) var<uniform> u: Uniforms;
|
|
883
|
+
@group(0) @binding(1) var tex: texture_2d<f32>;
|
|
884
|
+
@group(0) @binding(2) var samp: sampler;
|
|
885
|
+
|
|
886
|
+
struct VSOut {
|
|
887
|
+
@builtin(position) position: vec4<f32>,
|
|
888
|
+
@location(0) uv: vec2<f32>,
|
|
889
|
+
@location(1) color: vec3<f32>,
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
@vertex
|
|
893
|
+
fn vs(@location(0) pos: vec3<f32>, @location(1) uv: vec2<f32>, @location(2) color: vec3<f32>) -> VSOut {
|
|
894
|
+
var out: VSOut;
|
|
895
|
+
out.uv = uv;
|
|
896
|
+
out.color = color;
|
|
897
|
+
out.position = u.viewProj * u.model * vec4<f32>(pos, 1.0);
|
|
898
|
+
return out;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
@fragment
|
|
902
|
+
fn fs(in: VSOut) -> @location(0) vec4<f32> {
|
|
903
|
+
let t = textureSample(tex, samp, in.uv);
|
|
904
|
+
return vec4<f32>(t.rgb * in.color, t.a * u.params.x);
|
|
905
|
+
}
|
|
906
|
+
`;
|
|
907
|
+
CLIP_Z_FIX2 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 1];
|
|
908
|
+
UNIFORM_STRIDE2 = 256;
|
|
909
|
+
WebGPUTexturedRenderer = class extends WebGPURenderer {
|
|
910
|
+
constructor() {
|
|
911
|
+
super(...arguments);
|
|
912
|
+
this.texturedCapacity = 0;
|
|
913
|
+
this.sources = /* @__PURE__ */ new Map();
|
|
914
|
+
this.textures = /* @__PURE__ */ new Map();
|
|
915
|
+
this.retired = [];
|
|
916
|
+
this.texturedBuffers = /* @__PURE__ */ new Map();
|
|
917
|
+
this.bindGroups = /* @__PURE__ */ new Map();
|
|
918
|
+
}
|
|
919
|
+
/** Texture every instance of `mesh` with `source`. Call any time, also before init. */
|
|
920
|
+
setTexture(mesh, source) {
|
|
921
|
+
this.sources.set(mesh, source);
|
|
922
|
+
}
|
|
923
|
+
async init(canvas) {
|
|
924
|
+
await super.init(canvas);
|
|
925
|
+
const device = this.device;
|
|
926
|
+
if (!device) return;
|
|
927
|
+
const format = navigator.gpu.getPreferredCanvasFormat();
|
|
928
|
+
const module2 = device.createShaderModule({ code: WGSL2 });
|
|
929
|
+
const stage = globalThis.GPUShaderStage;
|
|
930
|
+
const layout = device.createBindGroupLayout({
|
|
931
|
+
entries: [
|
|
932
|
+
{
|
|
933
|
+
binding: 0,
|
|
934
|
+
visibility: stage.VERTEX | stage.FRAGMENT,
|
|
935
|
+
buffer: { type: "uniform", hasDynamicOffset: true, minBindingSize: 144 }
|
|
936
|
+
},
|
|
937
|
+
{ binding: 1, visibility: stage.FRAGMENT, texture: {} },
|
|
938
|
+
{ binding: 2, visibility: stage.FRAGMENT, sampler: {} }
|
|
939
|
+
]
|
|
940
|
+
});
|
|
941
|
+
const vertexBuffer = (location, components) => ({
|
|
942
|
+
arrayStride: components * 4,
|
|
943
|
+
attributes: [{ shaderLocation: location, offset: 0, format: `float32x${components}` }]
|
|
944
|
+
});
|
|
945
|
+
this.texturedPipeline = device.createRenderPipeline({
|
|
946
|
+
layout: device.createPipelineLayout({ bindGroupLayouts: [layout] }),
|
|
947
|
+
vertex: {
|
|
948
|
+
module: module2,
|
|
949
|
+
entryPoint: "vs",
|
|
950
|
+
buffers: [vertexBuffer(0, 3), vertexBuffer(1, 2), vertexBuffer(2, 3)]
|
|
951
|
+
},
|
|
952
|
+
fragment: {
|
|
953
|
+
module: module2,
|
|
954
|
+
entryPoint: "fs",
|
|
955
|
+
targets: [
|
|
956
|
+
{
|
|
957
|
+
format,
|
|
958
|
+
blend: {
|
|
959
|
+
color: {
|
|
960
|
+
srcFactor: "src-alpha",
|
|
961
|
+
dstFactor: "one-minus-src-alpha",
|
|
962
|
+
operation: "add"
|
|
963
|
+
},
|
|
964
|
+
alpha: { srcFactor: "one", dstFactor: "one-minus-src-alpha", operation: "add" }
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
]
|
|
968
|
+
},
|
|
969
|
+
primitive: { topology: "triangle-list", cullMode: "back", frontFace: "ccw" },
|
|
970
|
+
depthStencil: {
|
|
971
|
+
format: "depth24plus",
|
|
972
|
+
depthWriteEnabled: false,
|
|
973
|
+
depthCompare: "less"
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
this.sampler = device.createSampler({ magFilter: "linear", minFilter: "linear" });
|
|
977
|
+
}
|
|
978
|
+
textureFor(mesh) {
|
|
979
|
+
const cached = this.textures.get(mesh);
|
|
980
|
+
if (cached) return cached;
|
|
981
|
+
const device = this.device;
|
|
982
|
+
const usage = globalThis.GPUTextureUsage;
|
|
983
|
+
const white = device.createTexture({
|
|
984
|
+
size: { width: 1, height: 1 },
|
|
985
|
+
format: "rgba8unorm",
|
|
986
|
+
usage: usage.TEXTURE_BINDING | usage.COPY_DST
|
|
987
|
+
});
|
|
988
|
+
device.queue.writeTexture(
|
|
989
|
+
{ texture: white },
|
|
990
|
+
new Uint8Array([255, 255, 255, 255]),
|
|
991
|
+
{},
|
|
992
|
+
{ width: 1, height: 1 }
|
|
993
|
+
);
|
|
994
|
+
this.textures.set(mesh, white);
|
|
995
|
+
const upload = async (source2) => {
|
|
996
|
+
const image = source2 instanceof HTMLImageElement ? await createImageBitmap(source2) : source2;
|
|
997
|
+
if (this.destroyed || !this.device || this.textures.get(mesh) !== white) return;
|
|
998
|
+
const width = image.width || 1;
|
|
999
|
+
const height = image.height || 1;
|
|
1000
|
+
const texture = this.device.createTexture({
|
|
1001
|
+
size: { width, height },
|
|
1002
|
+
format: "rgba8unorm",
|
|
1003
|
+
usage: usage.TEXTURE_BINDING | usage.COPY_DST | usage.RENDER_ATTACHMENT
|
|
1004
|
+
});
|
|
1005
|
+
this.device.queue.copyExternalImageToTexture(
|
|
1006
|
+
{ source: image },
|
|
1007
|
+
{ texture },
|
|
1008
|
+
{ width, height }
|
|
1009
|
+
);
|
|
1010
|
+
this.retired.push(white);
|
|
1011
|
+
this.textures.set(mesh, texture);
|
|
1012
|
+
this.bindGroups.delete(mesh);
|
|
1013
|
+
};
|
|
1014
|
+
const source = this.sources.get(mesh);
|
|
1015
|
+
if (typeof source === "string") {
|
|
1016
|
+
const image = new Image();
|
|
1017
|
+
image.onload = () => void upload(image);
|
|
1018
|
+
image.src = source;
|
|
1019
|
+
} else {
|
|
1020
|
+
void upload(source);
|
|
1021
|
+
}
|
|
1022
|
+
return this.textures.get(mesh);
|
|
1023
|
+
}
|
|
1024
|
+
buffersFor(mesh) {
|
|
1025
|
+
const cached = this.texturedBuffers.get(mesh);
|
|
1026
|
+
if (cached) return cached;
|
|
1027
|
+
const data = expandToTriangles(mesh);
|
|
1028
|
+
const usage = globalThis.GPUBufferUsage.VERTEX | globalThis.GPUBufferUsage.COPY_DST;
|
|
1029
|
+
const upload = (array) => {
|
|
1030
|
+
const buffer = this.device.createBuffer({ size: array.byteLength, usage });
|
|
1031
|
+
this.device.queue.writeBuffer(buffer, 0, array);
|
|
1032
|
+
return buffer;
|
|
1033
|
+
};
|
|
1034
|
+
const result = {
|
|
1035
|
+
position: upload(data.positions),
|
|
1036
|
+
uv: upload(planarUVs(mesh)),
|
|
1037
|
+
color: upload(data.colors),
|
|
1038
|
+
count: data.count
|
|
1039
|
+
};
|
|
1040
|
+
this.texturedBuffers.set(mesh, result);
|
|
1041
|
+
return result;
|
|
1042
|
+
}
|
|
1043
|
+
bindGroupFor(mesh) {
|
|
1044
|
+
const texture = this.textureFor(mesh);
|
|
1045
|
+
const cached = this.bindGroups.get(mesh);
|
|
1046
|
+
if (cached && cached.buffer === this.texturedUniforms && cached.texture === texture) {
|
|
1047
|
+
return cached.group;
|
|
1048
|
+
}
|
|
1049
|
+
const group = this.device.createBindGroup({
|
|
1050
|
+
layout: this.texturedPipeline.getBindGroupLayout(0),
|
|
1051
|
+
entries: [
|
|
1052
|
+
{ binding: 0, resource: { buffer: this.texturedUniforms, offset: 0, size: 144 } },
|
|
1053
|
+
{ binding: 1, resource: texture.createView() },
|
|
1054
|
+
{ binding: 2, resource: this.sampler }
|
|
1055
|
+
]
|
|
1056
|
+
});
|
|
1057
|
+
this.bindGroups.set(mesh, { group, buffer: this.texturedUniforms, texture });
|
|
1058
|
+
return group;
|
|
1059
|
+
}
|
|
1060
|
+
render(frame) {
|
|
1061
|
+
const plain = [];
|
|
1062
|
+
const textured = [];
|
|
1063
|
+
for (const item of frame.items) {
|
|
1064
|
+
(this.sources.has(item.mesh) ? textured : plain).push(item);
|
|
1065
|
+
}
|
|
1066
|
+
super.render(textured.length ? { ...frame, items: plain } : frame);
|
|
1067
|
+
if (!textured.length) return;
|
|
1068
|
+
if (this.destroyed || !this.device || !this.context || !this.texturedPipeline) return;
|
|
1069
|
+
if (frame.width === 0 || frame.height === 0) return;
|
|
1070
|
+
this.ensureDepth();
|
|
1071
|
+
if (textured.length > this.texturedCapacity || !this.texturedUniforms) {
|
|
1072
|
+
this.texturedUniforms?.destroy?.();
|
|
1073
|
+
this.texturedUniforms = this.device.createBuffer({
|
|
1074
|
+
size: textured.length * UNIFORM_STRIDE2,
|
|
1075
|
+
usage: globalThis.GPUBufferUsage.UNIFORM | globalThis.GPUBufferUsage.COPY_DST
|
|
1076
|
+
});
|
|
1077
|
+
this.texturedCapacity = textured.length;
|
|
1078
|
+
}
|
|
1079
|
+
const viewProj = multiply(CLIP_Z_FIX2, frame.viewProjection);
|
|
1080
|
+
textured.forEach((item, i) => {
|
|
1081
|
+
const data = new Float32Array(UNIFORM_STRIDE2 / 4);
|
|
1082
|
+
data.set(viewProj, 0);
|
|
1083
|
+
data.set(item.model, 16);
|
|
1084
|
+
data.set([itemOpacity(item.transparency), 0, 0, 0], 32);
|
|
1085
|
+
this.device.queue.writeBuffer(this.texturedUniforms, i * UNIFORM_STRIDE2, data);
|
|
1086
|
+
});
|
|
1087
|
+
const cleared = plain.length > 0;
|
|
1088
|
+
const encoder = this.device.createCommandEncoder();
|
|
1089
|
+
const pass = encoder.beginRenderPass({
|
|
1090
|
+
colorAttachments: [
|
|
1091
|
+
{
|
|
1092
|
+
view: this.context.getCurrentTexture().createView(),
|
|
1093
|
+
clearValue: this.clearValue,
|
|
1094
|
+
loadOp: cleared ? "load" : "clear",
|
|
1095
|
+
storeOp: "store"
|
|
1096
|
+
}
|
|
1097
|
+
],
|
|
1098
|
+
depthStencilAttachment: {
|
|
1099
|
+
view: this.depthTexture.createView(),
|
|
1100
|
+
depthClearValue: 1,
|
|
1101
|
+
depthLoadOp: cleared ? "load" : "clear",
|
|
1102
|
+
depthStoreOp: "store"
|
|
1103
|
+
}
|
|
1104
|
+
});
|
|
1105
|
+
pass.setPipeline(this.texturedPipeline);
|
|
1106
|
+
textured.forEach((item, i) => {
|
|
1107
|
+
const mesh = this.buffersFor(item.mesh);
|
|
1108
|
+
pass.setBindGroup(0, this.bindGroupFor(item.mesh), [i * UNIFORM_STRIDE2]);
|
|
1109
|
+
pass.setVertexBuffer(0, mesh.position);
|
|
1110
|
+
pass.setVertexBuffer(1, mesh.uv);
|
|
1111
|
+
pass.setVertexBuffer(2, mesh.color);
|
|
1112
|
+
pass.draw(mesh.count);
|
|
1113
|
+
});
|
|
1114
|
+
pass.end();
|
|
1115
|
+
this.device.queue.submit([encoder.finish()]);
|
|
1116
|
+
}
|
|
1117
|
+
destroy() {
|
|
1118
|
+
for (const texture of this.textures.values()) texture.destroy?.();
|
|
1119
|
+
for (const texture of this.retired.splice(0)) texture.destroy?.();
|
|
1120
|
+
for (const buffers of this.texturedBuffers.values()) {
|
|
1121
|
+
buffers.position.destroy?.();
|
|
1122
|
+
buffers.uv.destroy?.();
|
|
1123
|
+
buffers.color.destroy?.();
|
|
1124
|
+
}
|
|
1125
|
+
this.textures.clear();
|
|
1126
|
+
this.texturedBuffers.clear();
|
|
1127
|
+
this.bindGroups.clear();
|
|
1128
|
+
this.sources.clear();
|
|
1129
|
+
this.texturedUniforms?.destroy?.();
|
|
1130
|
+
this.texturedUniforms = void 0;
|
|
1131
|
+
this.texturedPipeline = void 0;
|
|
1132
|
+
this.sampler = void 0;
|
|
1133
|
+
super.destroy();
|
|
1134
|
+
}
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
});
|
|
1138
|
+
|
|
1139
|
+
// src/engines/little-3d-engine/renderers/webgl-textured.ts
|
|
1140
|
+
var webgl_textured_exports = {};
|
|
1141
|
+
__export(webgl_textured_exports, {
|
|
1142
|
+
WebGLTexturedRenderer: () => WebGLTexturedRenderer
|
|
1143
|
+
});
|
|
1144
|
+
function compile2(gl, type, source) {
|
|
1145
|
+
const shader = gl.createShader(type);
|
|
1146
|
+
gl.shaderSource(shader, source);
|
|
1147
|
+
gl.compileShader(shader);
|
|
1148
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
1149
|
+
throw new Error(`3d-spinner: shader compile failed: ${gl.getShaderInfoLog(shader)}`);
|
|
1150
|
+
}
|
|
1151
|
+
return shader;
|
|
1152
|
+
}
|
|
1153
|
+
function link2(gl) {
|
|
1154
|
+
const program = gl.createProgram();
|
|
1155
|
+
gl.attachShader(program, compile2(gl, gl.VERTEX_SHADER, VERTEX_SHADER2));
|
|
1156
|
+
gl.attachShader(program, compile2(gl, gl.FRAGMENT_SHADER, FRAGMENT_SHADER2));
|
|
1157
|
+
gl.linkProgram(program);
|
|
1158
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
1159
|
+
throw new Error(`3d-spinner: program link failed: ${gl.getProgramInfoLog(program)}`);
|
|
1160
|
+
}
|
|
1161
|
+
return program;
|
|
1162
|
+
}
|
|
1163
|
+
function itemOpacity2(transparency) {
|
|
1164
|
+
if (!transparency) return 1;
|
|
1165
|
+
if (transparency.mode === "two-sided") return resolveTwoSidedOpacity(transparency).front;
|
|
1166
|
+
return opacity(transparency.opacity, DEFAULT_ONE_SIDED_OPACITY);
|
|
1167
|
+
}
|
|
1168
|
+
var VERTEX_SHADER2, FRAGMENT_SHADER2, WebGLTexturedRenderer;
|
|
1169
|
+
var init_webgl_textured = __esm({
|
|
1170
|
+
"src/engines/little-3d-engine/renderers/webgl-textured.ts"() {
|
|
1171
|
+
"use strict";
|
|
1172
|
+
init_geometry();
|
|
1173
|
+
init_renderer();
|
|
1174
|
+
init_textured_helpers();
|
|
1175
|
+
init_webgl();
|
|
1176
|
+
VERTEX_SHADER2 = `#version 300 es
|
|
1177
|
+
in vec3 aPos;
|
|
1178
|
+
in vec2 aUV;
|
|
1179
|
+
in vec3 aColor;
|
|
1180
|
+
uniform mat4 uViewProj;
|
|
1181
|
+
uniform mat4 uModel;
|
|
1182
|
+
out vec2 vUV;
|
|
1183
|
+
out vec3 vColor;
|
|
1184
|
+
void main() {
|
|
1185
|
+
vUV = aUV;
|
|
1186
|
+
vColor = aColor;
|
|
1187
|
+
gl_Position = uViewProj * uModel * vec4(aPos, 1.0);
|
|
1188
|
+
}`;
|
|
1189
|
+
FRAGMENT_SHADER2 = `#version 300 es
|
|
1190
|
+
precision mediump float;
|
|
1191
|
+
in vec2 vUV;
|
|
1192
|
+
in vec3 vColor;
|
|
1193
|
+
uniform sampler2D uTexture;
|
|
1194
|
+
uniform float uOpacity;
|
|
1195
|
+
out vec4 fragColor;
|
|
1196
|
+
void main() {
|
|
1197
|
+
vec4 t = texture(uTexture, vUV);
|
|
1198
|
+
fragColor = vec4(t.rgb * vColor, t.a * uOpacity);
|
|
1199
|
+
}`;
|
|
1200
|
+
WebGLTexturedRenderer = class {
|
|
1201
|
+
constructor(options = {}) {
|
|
1202
|
+
this.sources = /* @__PURE__ */ new Map();
|
|
1203
|
+
this.textures = /* @__PURE__ */ new Map();
|
|
1204
|
+
this.buffers = /* @__PURE__ */ new Map();
|
|
1205
|
+
this.inner = new WebGLRenderer(options);
|
|
1206
|
+
}
|
|
1207
|
+
/** Texture every instance of `mesh` with `source`. Call any time, also before init. */
|
|
1208
|
+
setTexture(mesh, source) {
|
|
1209
|
+
this.sources.set(mesh, source);
|
|
1210
|
+
}
|
|
1211
|
+
init(canvas) {
|
|
1212
|
+
this.inner.init(canvas);
|
|
1213
|
+
const gl = canvas.getContext("webgl2");
|
|
1214
|
+
this.gl = gl;
|
|
1215
|
+
this.program = link2(gl);
|
|
1216
|
+
this.locations = {
|
|
1217
|
+
aPos: gl.getAttribLocation(this.program, "aPos"),
|
|
1218
|
+
aUV: gl.getAttribLocation(this.program, "aUV"),
|
|
1219
|
+
aColor: gl.getAttribLocation(this.program, "aColor"),
|
|
1220
|
+
uViewProj: gl.getUniformLocation(this.program, "uViewProj"),
|
|
1221
|
+
uModel: gl.getUniformLocation(this.program, "uModel"),
|
|
1222
|
+
uTexture: gl.getUniformLocation(this.program, "uTexture"),
|
|
1223
|
+
uOpacity: gl.getUniformLocation(this.program, "uOpacity")
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
resize() {
|
|
1227
|
+
this.inner.resize();
|
|
1228
|
+
}
|
|
1229
|
+
textureFor(mesh) {
|
|
1230
|
+
const cached = this.textures.get(mesh);
|
|
1231
|
+
if (cached) return cached;
|
|
1232
|
+
const gl = this.gl;
|
|
1233
|
+
const texture = gl.createTexture();
|
|
1234
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
1235
|
+
gl.texImage2D(
|
|
1236
|
+
gl.TEXTURE_2D,
|
|
1237
|
+
0,
|
|
1238
|
+
gl.RGBA,
|
|
1239
|
+
1,
|
|
1240
|
+
1,
|
|
1241
|
+
0,
|
|
1242
|
+
gl.RGBA,
|
|
1243
|
+
gl.UNSIGNED_BYTE,
|
|
1244
|
+
new Uint8Array([255, 255, 255, 255])
|
|
1245
|
+
);
|
|
1246
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
1247
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
1248
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
1249
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
1250
|
+
this.textures.set(mesh, texture);
|
|
1251
|
+
const upload = (image) => {
|
|
1252
|
+
if (!this.gl || this.textures.get(mesh) !== texture) return;
|
|
1253
|
+
this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
|
|
1254
|
+
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, image);
|
|
1255
|
+
};
|
|
1256
|
+
const source = this.sources.get(mesh);
|
|
1257
|
+
if (typeof source === "string") {
|
|
1258
|
+
const image = new Image();
|
|
1259
|
+
image.onload = () => upload(image);
|
|
1260
|
+
image.src = source;
|
|
1261
|
+
} else {
|
|
1262
|
+
upload(source);
|
|
1263
|
+
}
|
|
1264
|
+
return texture;
|
|
1265
|
+
}
|
|
1266
|
+
buffersFor(mesh) {
|
|
1267
|
+
const cached = this.buffers.get(mesh);
|
|
1268
|
+
if (cached) return cached;
|
|
1269
|
+
const gl = this.gl;
|
|
1270
|
+
const loc = this.locations;
|
|
1271
|
+
const data = expandToTriangles(mesh);
|
|
1272
|
+
const vao = gl.createVertexArray();
|
|
1273
|
+
gl.bindVertexArray(vao);
|
|
1274
|
+
const attribute = (location, array, size) => {
|
|
1275
|
+
if (location < 0) return;
|
|
1276
|
+
const buffer = gl.createBuffer();
|
|
1277
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
|
|
1278
|
+
gl.bufferData(gl.ARRAY_BUFFER, array, gl.STATIC_DRAW);
|
|
1279
|
+
gl.enableVertexAttribArray(location);
|
|
1280
|
+
gl.vertexAttribPointer(location, size, gl.FLOAT, false, 0, 0);
|
|
1281
|
+
};
|
|
1282
|
+
attribute(loc.aPos, data.positions, 3);
|
|
1283
|
+
attribute(loc.aColor, data.colors, 3);
|
|
1284
|
+
attribute(loc.aUV, planarUVs(mesh), 2);
|
|
1285
|
+
gl.bindVertexArray(null);
|
|
1286
|
+
const result = { vao, count: data.count };
|
|
1287
|
+
this.buffers.set(mesh, result);
|
|
1288
|
+
return result;
|
|
1289
|
+
}
|
|
1290
|
+
render(frame) {
|
|
1291
|
+
const plain = [];
|
|
1292
|
+
const textured = [];
|
|
1293
|
+
for (const item of frame.items) {
|
|
1294
|
+
(this.sources.has(item.mesh) ? textured : plain).push(item);
|
|
1295
|
+
}
|
|
1296
|
+
this.inner.render(textured.length ? { ...frame, items: plain } : frame);
|
|
1297
|
+
if (!textured.length) return;
|
|
1298
|
+
const gl = this.gl;
|
|
1299
|
+
const loc = this.locations;
|
|
1300
|
+
if (!gl || !this.program || !loc) return;
|
|
1301
|
+
gl.useProgram(this.program);
|
|
1302
|
+
gl.uniformMatrix4fv(loc.uViewProj, false, new Float32Array(frame.viewProjection));
|
|
1303
|
+
gl.uniform1i(loc.uTexture, 0);
|
|
1304
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
1305
|
+
gl.enable(gl.BLEND);
|
|
1306
|
+
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
|
|
1307
|
+
gl.depthMask(false);
|
|
1308
|
+
for (const item of textured) {
|
|
1309
|
+
const buffers = this.buffersFor(item.mesh);
|
|
1310
|
+
gl.bindTexture(gl.TEXTURE_2D, this.textureFor(item.mesh));
|
|
1311
|
+
gl.uniformMatrix4fv(loc.uModel, false, new Float32Array(item.model));
|
|
1312
|
+
gl.uniform1f(loc.uOpacity, itemOpacity2(item.transparency));
|
|
1313
|
+
gl.bindVertexArray(buffers.vao);
|
|
1314
|
+
gl.drawArrays(gl.TRIANGLES, 0, buffers.count);
|
|
1315
|
+
}
|
|
1316
|
+
gl.depthMask(true);
|
|
1317
|
+
gl.disable(gl.BLEND);
|
|
1318
|
+
gl.bindVertexArray(null);
|
|
1319
|
+
}
|
|
1320
|
+
destroy() {
|
|
1321
|
+
const gl = this.gl;
|
|
1322
|
+
if (gl) {
|
|
1323
|
+
for (const texture of this.textures.values()) gl.deleteTexture(texture);
|
|
1324
|
+
for (const buffers of this.buffers.values()) gl.deleteVertexArray(buffers.vao);
|
|
1325
|
+
if (this.program) gl.deleteProgram(this.program);
|
|
1326
|
+
}
|
|
1327
|
+
this.textures.clear();
|
|
1328
|
+
this.buffers.clear();
|
|
1329
|
+
this.sources.clear();
|
|
1330
|
+
this.gl = void 0;
|
|
1331
|
+
this.program = void 0;
|
|
1332
|
+
this.locations = void 0;
|
|
1333
|
+
this.inner.destroy();
|
|
1334
|
+
}
|
|
1335
|
+
};
|
|
1336
|
+
}
|
|
1337
|
+
});
|
|
1338
|
+
|
|
1339
|
+
// src/engines/little-3d-engine/renderers/canvas2d-textured.ts
|
|
1340
|
+
var canvas2d_textured_exports = {};
|
|
1341
|
+
__export(canvas2d_textured_exports, {
|
|
1342
|
+
Canvas2DTexturedRenderer: () => Canvas2DTexturedRenderer
|
|
1343
|
+
});
|
|
1344
|
+
function imageSize(source) {
|
|
1345
|
+
if (source instanceof HTMLImageElement) {
|
|
1346
|
+
return source.complete && source.naturalWidth > 0 ? { width: source.naturalWidth, height: source.naturalHeight } : void 0;
|
|
1347
|
+
}
|
|
1348
|
+
if (source instanceof HTMLVideoElement) {
|
|
1349
|
+
return source.readyState >= 2 ? { width: source.videoWidth, height: source.videoHeight } : void 0;
|
|
1350
|
+
}
|
|
1351
|
+
if (source instanceof SVGImageElement) {
|
|
1352
|
+
const width = source.width.baseVal.value;
|
|
1353
|
+
const height = source.height.baseVal.value;
|
|
1354
|
+
return width > 0 && height > 0 ? { width, height } : void 0;
|
|
1355
|
+
}
|
|
1356
|
+
if (typeof VideoFrame !== "undefined" && source instanceof VideoFrame) {
|
|
1357
|
+
return { width: source.displayWidth, height: source.displayHeight };
|
|
1358
|
+
}
|
|
1359
|
+
const sized = source;
|
|
1360
|
+
return sized.width > 0 && sized.height > 0 ? { width: sized.width, height: sized.height } : void 0;
|
|
1361
|
+
}
|
|
1362
|
+
function drawMappedTriangle(ctx, image, source, target) {
|
|
1363
|
+
const [s0, s1, s2] = source;
|
|
1364
|
+
const [d0, d1, d2] = target;
|
|
1365
|
+
const determinant = s0.x * (s1.y - s2.y) + s1.x * (s2.y - s0.y) + s2.x * (s0.y - s1.y);
|
|
1366
|
+
if (Math.abs(determinant) < 1e-8) return;
|
|
1367
|
+
const a = (d0.x * (s1.y - s2.y) + d1.x * (s2.y - s0.y) + d2.x * (s0.y - s1.y)) / determinant;
|
|
1368
|
+
const c = (d0.x * (s2.x - s1.x) + d1.x * (s0.x - s2.x) + d2.x * (s1.x - s0.x)) / determinant;
|
|
1369
|
+
const e = (d0.x * (s1.x * s2.y - s2.x * s1.y) + d1.x * (s2.x * s0.y - s0.x * s2.y) + d2.x * (s0.x * s1.y - s1.x * s0.y)) / determinant;
|
|
1370
|
+
const b = (d0.y * (s1.y - s2.y) + d1.y * (s2.y - s0.y) + d2.y * (s0.y - s1.y)) / determinant;
|
|
1371
|
+
const d = (d0.y * (s2.x - s1.x) + d1.y * (s0.x - s2.x) + d2.y * (s1.x - s0.x)) / determinant;
|
|
1372
|
+
const f = (d0.y * (s1.x * s2.y - s2.x * s1.y) + d1.y * (s2.x * s0.y - s0.x * s2.y) + d2.y * (s0.x * s1.y - s1.x * s0.y)) / determinant;
|
|
1373
|
+
ctx.save();
|
|
1374
|
+
ctx.beginPath();
|
|
1375
|
+
ctx.moveTo(d0.x, d0.y);
|
|
1376
|
+
ctx.lineTo(d1.x, d1.y);
|
|
1377
|
+
ctx.lineTo(d2.x, d2.y);
|
|
1378
|
+
ctx.closePath();
|
|
1379
|
+
ctx.clip();
|
|
1380
|
+
ctx.transform(a, b, c, d, e, f);
|
|
1381
|
+
ctx.drawImage(image, 0, 0);
|
|
1382
|
+
ctx.restore();
|
|
1383
|
+
}
|
|
1384
|
+
var Canvas2DTexturedRenderer;
|
|
1385
|
+
var init_canvas2d_textured = __esm({
|
|
1386
|
+
"src/engines/little-3d-engine/renderers/canvas2d-textured.ts"() {
|
|
1387
|
+
"use strict";
|
|
1388
|
+
init_math();
|
|
1389
|
+
init_renderer();
|
|
1390
|
+
init_canvas2d();
|
|
1391
|
+
Canvas2DTexturedRenderer = class {
|
|
1392
|
+
constructor(options = {}) {
|
|
1393
|
+
this.sources = /* @__PURE__ */ new Map();
|
|
1394
|
+
this.loaded = /* @__PURE__ */ new Map();
|
|
1395
|
+
this.dpr = 1;
|
|
1396
|
+
this.inner = new Canvas2DRenderer(options);
|
|
1397
|
+
}
|
|
1398
|
+
/** Texture every instance of `mesh` with `source`. Call any time, also before init. */
|
|
1399
|
+
setTexture(mesh, source) {
|
|
1400
|
+
this.sources.set(mesh, source);
|
|
1401
|
+
if (typeof source === "string" && !this.loaded.has(source)) {
|
|
1402
|
+
const image = new Image();
|
|
1403
|
+
image.src = source;
|
|
1404
|
+
this.loaded.set(source, image);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
init(canvas) {
|
|
1408
|
+
this.inner.init(canvas);
|
|
1409
|
+
this.ctx = canvas.getContext("2d") ?? void 0;
|
|
1410
|
+
}
|
|
1411
|
+
resize(cssWidth, cssHeight, dpr) {
|
|
1412
|
+
this.dpr = dpr;
|
|
1413
|
+
this.inner.resize(cssWidth, cssHeight, dpr);
|
|
1414
|
+
}
|
|
1415
|
+
drawable(mesh) {
|
|
1416
|
+
const source = this.sources.get(mesh);
|
|
1417
|
+
return typeof source === "string" ? this.loaded.get(source) : source;
|
|
1418
|
+
}
|
|
1419
|
+
render(frame) {
|
|
1420
|
+
const plain = frame.items.filter((item) => {
|
|
1421
|
+
if (!this.sources.has(item.mesh)) return true;
|
|
1422
|
+
const source = this.drawable(item.mesh);
|
|
1423
|
+
return !source || !imageSize(source);
|
|
1424
|
+
});
|
|
1425
|
+
this.inner.render({ ...frame, items: plain });
|
|
1426
|
+
const ctx = this.ctx;
|
|
1427
|
+
if (!ctx) return;
|
|
1428
|
+
ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0);
|
|
1429
|
+
const tinted = /* @__PURE__ */ new Map();
|
|
1430
|
+
for (const item of frame.items) {
|
|
1431
|
+
const source = this.drawable(item.mesh);
|
|
1432
|
+
if (!source) continue;
|
|
1433
|
+
const size = imageSize(source);
|
|
1434
|
+
if (!size) continue;
|
|
1435
|
+
let image = tinted.get(item.mesh);
|
|
1436
|
+
if (!image) {
|
|
1437
|
+
image = document.createElement("canvas");
|
|
1438
|
+
image.width = size.width;
|
|
1439
|
+
image.height = size.height;
|
|
1440
|
+
const tint = image.getContext("2d");
|
|
1441
|
+
if (!tint) continue;
|
|
1442
|
+
tint.drawImage(source, 0, 0, size.width, size.height);
|
|
1443
|
+
tint.globalCompositeOperation = "source-in";
|
|
1444
|
+
tint.fillStyle = item.mesh.faces[0]?.color ?? "#ffffff";
|
|
1445
|
+
tint.fillRect(0, 0, size.width, size.height);
|
|
1446
|
+
tinted.set(item.mesh, image);
|
|
1447
|
+
}
|
|
1448
|
+
const world = item.mesh.vertices.map((vertex) => transformAffine(item.model, vertex));
|
|
1449
|
+
const projected = world.map((vertex) => {
|
|
1450
|
+
const ndc = transformPoint(frame.viewProjection, vertex);
|
|
1451
|
+
return { x: (ndc.x * 0.5 + 0.5) * frame.width, y: (1 - (ndc.y * 0.5 + 0.5)) * frame.height };
|
|
1452
|
+
});
|
|
1453
|
+
const face = item.mesh.faces[0];
|
|
1454
|
+
if (!face || face.indices.length !== 4) continue;
|
|
1455
|
+
const [a, b, c, d] = face.indices.map((index) => projected[index]);
|
|
1456
|
+
ctx.globalAlpha = item.transparency?.mode === "one-sided" ? opacity(item.transparency.opacity, DEFAULT_ONE_SIDED_OPACITY) : 1;
|
|
1457
|
+
drawMappedTriangle(ctx, image, [{ x: 0, y: size.height }, { x: size.width, y: size.height }, { x: size.width, y: 0 }], [a, b, c]);
|
|
1458
|
+
drawMappedTriangle(ctx, image, [{ x: 0, y: size.height }, { x: size.width, y: 0 }, { x: 0, y: 0 }], [a, c, d]);
|
|
1459
|
+
}
|
|
1460
|
+
ctx.globalAlpha = 1;
|
|
1461
|
+
}
|
|
1462
|
+
destroy() {
|
|
1463
|
+
this.inner.destroy();
|
|
1464
|
+
this.ctx = void 0;
|
|
1465
|
+
this.sources.clear();
|
|
1466
|
+
this.loaded.clear();
|
|
1467
|
+
}
|
|
1468
|
+
};
|
|
1469
|
+
}
|
|
1470
|
+
});
|
|
1471
|
+
|
|
1472
|
+
// src/animations/particles.ts
|
|
1473
|
+
var particles_exports = {};
|
|
1474
|
+
__export(particles_exports, {
|
|
1475
|
+
ParticlesAnimation: () => ParticlesAnimation,
|
|
1476
|
+
particleField: () => particleField
|
|
1477
|
+
});
|
|
1478
|
+
module.exports = __toCommonJS(particles_exports);
|
|
1479
|
+
|
|
1480
|
+
// src/animation-label.ts
|
|
1481
|
+
var LABEL_STYLE = [
|
|
1482
|
+
"position:absolute",
|
|
1483
|
+
"inset:0",
|
|
1484
|
+
"display:flex",
|
|
1485
|
+
"align-items:center",
|
|
1486
|
+
"justify-content:center",
|
|
1487
|
+
"pointer-events:none",
|
|
1488
|
+
"font:700 1.6rem/1 system-ui,sans-serif",
|
|
1489
|
+
"letter-spacing:0.02em",
|
|
1490
|
+
"color:rgba(255,255,255,0.9)",
|
|
1491
|
+
"text-shadow:0 1px 10px rgba(0,0,0,0.6)",
|
|
1492
|
+
"z-index:1"
|
|
1493
|
+
].join(";");
|
|
1494
|
+
function animationLabelOpacity(now, enterAt, introDurationMs, exitAt, outroDurationMs) {
|
|
1495
|
+
if (enterAt === Infinity) return 0;
|
|
1496
|
+
const intro = introDurationMs <= 0 ? 1 : Math.max(0, Math.min(1, (now - enterAt) / introDurationMs));
|
|
1497
|
+
const outro = exitAt === Infinity ? 1 : outroDurationMs <= 0 ? 0 : Math.max(0, Math.min(1, 1 - (now - exitAt) / outroDurationMs));
|
|
1498
|
+
return Math.min(intro, outro);
|
|
1499
|
+
}
|
|
1500
|
+
function mountAnimationLabel(target, content) {
|
|
1501
|
+
var _a;
|
|
1502
|
+
const container = document.createElement("div");
|
|
1503
|
+
container.style.cssText = LABEL_STYLE;
|
|
1504
|
+
container.setAttribute("role", "status");
|
|
1505
|
+
if (typeof content === "string") container.textContent = content;
|
|
1506
|
+
else if (content) {
|
|
1507
|
+
(_a = content.style).pointerEvents || (_a.pointerEvents = "auto");
|
|
1508
|
+
container.appendChild(content);
|
|
1509
|
+
}
|
|
1510
|
+
target.appendChild(container);
|
|
1511
|
+
return {
|
|
1512
|
+
container,
|
|
1513
|
+
setText(value) {
|
|
1514
|
+
if (typeof content !== "object") container.textContent = value;
|
|
1515
|
+
},
|
|
1516
|
+
setOpacity(value) {
|
|
1517
|
+
container.style.opacity = String(value);
|
|
1518
|
+
}
|
|
1519
|
+
};
|
|
1520
|
+
}
|
|
1521
|
+
|
|
1522
|
+
// src/engines/little-3d-engine/core/camera.ts
|
|
1523
|
+
init_math();
|
|
1524
|
+
var DEFAULTS = {
|
|
1525
|
+
position: { x: 0, y: 0, z: 4 },
|
|
1526
|
+
fov: 55 * Math.PI / 180,
|
|
1527
|
+
near: 0.1,
|
|
1528
|
+
far: 100
|
|
1529
|
+
};
|
|
1530
|
+
var Camera = class {
|
|
1531
|
+
constructor(options) {
|
|
1532
|
+
this.options = { ...DEFAULTS, ...options };
|
|
1533
|
+
}
|
|
1534
|
+
/** Transform a world-space point into view (camera) space. */
|
|
1535
|
+
toView(p) {
|
|
1536
|
+
const { position } = this.options;
|
|
1537
|
+
return transformAffine(translation(-position.x, -position.y, -position.z), p);
|
|
1538
|
+
}
|
|
1539
|
+
/** Combined view-projection matrix for the given viewport aspect ratio. */
|
|
1540
|
+
viewProjection(aspect) {
|
|
1541
|
+
const { position, fov, near, far } = this.options;
|
|
1542
|
+
const view = translation(-position.x, -position.y, -position.z);
|
|
1543
|
+
const projection = perspective(fov, aspect, near, far);
|
|
1544
|
+
return multiply(projection, view);
|
|
1545
|
+
}
|
|
1546
|
+
/** Convert a normalized device coordinate (-1..1) to a pixel position. */
|
|
1547
|
+
toScreen(ndc, width, height) {
|
|
1548
|
+
return {
|
|
1549
|
+
x: (ndc.x * 0.5 + 0.5) * width,
|
|
1550
|
+
y: (1 - (ndc.y * 0.5 + 0.5)) * height
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
};
|
|
1554
|
+
|
|
1555
|
+
// src/engines/little-3d-engine/little-3d-engine.ts
|
|
1556
|
+
init_light();
|
|
1557
|
+
init_math();
|
|
1558
|
+
|
|
1559
|
+
// src/engines/little-3d-engine/core/mesh.ts
|
|
1560
|
+
function transform(init) {
|
|
1561
|
+
return {
|
|
1562
|
+
position: init?.position ?? { x: 0, y: 0, z: 0 },
|
|
1563
|
+
rotation: init?.rotation ?? { x: 0, y: 0, z: 0 },
|
|
1564
|
+
scale: init?.scale ?? 1
|
|
1565
|
+
};
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
// src/engines/little-3d-engine/little-3d-engine.ts
|
|
1569
|
+
init_renderer();
|
|
1570
|
+
init_light();
|
|
1571
|
+
|
|
1572
|
+
// src/engines/little-3d-engine/shapes/primitives/quad.ts
|
|
1573
|
+
var DEFAULT_COLORS = ["#3b82f6"];
|
|
1574
|
+
function quad(size = 1, colors = DEFAULT_COLORS) {
|
|
1575
|
+
const s = size / 2;
|
|
1576
|
+
const vertices = [
|
|
1577
|
+
{ x: -s, y: -s, z: 0 },
|
|
1578
|
+
{ x: s, y: -s, z: 0 },
|
|
1579
|
+
{ x: s, y: s, z: 0 },
|
|
1580
|
+
{ x: -s, y: s, z: 0 }
|
|
1581
|
+
];
|
|
1582
|
+
return { vertices, faces: [{ indices: [0, 1, 2, 3], color: colors[0] }] };
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
// src/engines/little-3d-engine/shapes/primitives/spheres/icosphere.ts
|
|
1586
|
+
init_geometry();
|
|
1587
|
+
var T = (1 + Math.sqrt(5)) / 2;
|
|
1588
|
+
var SEED_VERTICES = [
|
|
1589
|
+
{ x: -1, y: T, z: 0 },
|
|
1590
|
+
{ x: 1, y: T, z: 0 },
|
|
1591
|
+
{ x: -1, y: -T, z: 0 },
|
|
1592
|
+
{ x: 1, y: -T, z: 0 },
|
|
1593
|
+
{ x: 0, y: -1, z: T },
|
|
1594
|
+
{ x: 0, y: 1, z: T },
|
|
1595
|
+
{ x: 0, y: -1, z: -T },
|
|
1596
|
+
{ x: 0, y: 1, z: -T },
|
|
1597
|
+
{ x: T, y: 0, z: -1 },
|
|
1598
|
+
{ x: T, y: 0, z: 1 },
|
|
1599
|
+
{ x: -T, y: 0, z: -1 },
|
|
1600
|
+
{ x: -T, y: 0, z: 1 }
|
|
1601
|
+
];
|
|
1602
|
+
|
|
1603
|
+
// src/engines/little-3d-engine/shapes/primitives/spheres/octa-sphere.ts
|
|
1604
|
+
init_geometry();
|
|
1605
|
+
|
|
1606
|
+
// src/engines/little-3d-engine/little-3d-engine.ts
|
|
1607
|
+
init_geometry();
|
|
1608
|
+
init_renderer();
|
|
1609
|
+
init_math();
|
|
1610
|
+
function modelMatrix(t) {
|
|
1611
|
+
const rotation = multiply(
|
|
1612
|
+
rotationZ(t.rotation.z),
|
|
1613
|
+
multiply(rotationY(t.rotation.y), rotationX(t.rotation.x))
|
|
1614
|
+
);
|
|
1615
|
+
return multiply(
|
|
1616
|
+
translation(t.position.x, t.position.y, t.position.z),
|
|
1617
|
+
multiply(rotation, scaleMatrix(t.scale))
|
|
1618
|
+
);
|
|
1619
|
+
}
|
|
1620
|
+
var Little3dEngine = class {
|
|
1621
|
+
constructor(options = {}) {
|
|
1622
|
+
this.scene = [];
|
|
1623
|
+
this.cssWidth = 0;
|
|
1624
|
+
this.cssHeight = 0;
|
|
1625
|
+
this.ready = false;
|
|
1626
|
+
this.generation = 0;
|
|
1627
|
+
this.rafId = 0;
|
|
1628
|
+
this.running = false;
|
|
1629
|
+
this.camera = new Camera(options.camera);
|
|
1630
|
+
this.light = new Light(options.light);
|
|
1631
|
+
this.backend = options.backend ?? "canvas2d";
|
|
1632
|
+
this.background = options.background;
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Create the canvas inside `target`, load the selected backend, and start
|
|
1636
|
+
* tracking size. Resolves once the renderer is ready; rejects if the backend
|
|
1637
|
+
* is unavailable. Drawing is a no-op until it resolves.
|
|
1638
|
+
*/
|
|
1639
|
+
async mount(target) {
|
|
1640
|
+
const canvas = document.createElement("canvas");
|
|
1641
|
+
canvas.style.display = "block";
|
|
1642
|
+
canvas.style.width = "100%";
|
|
1643
|
+
canvas.style.height = "100%";
|
|
1644
|
+
target.appendChild(canvas);
|
|
1645
|
+
this.canvas = canvas;
|
|
1646
|
+
this.observer = new ResizeObserver(() => this.resize());
|
|
1647
|
+
this.observer.observe(canvas);
|
|
1648
|
+
this.resize();
|
|
1649
|
+
const generation = this.generation;
|
|
1650
|
+
const dropCanvas = () => {
|
|
1651
|
+
if (this.canvas !== canvas) return;
|
|
1652
|
+
this.observer?.disconnect();
|
|
1653
|
+
this.observer = void 0;
|
|
1654
|
+
canvas.remove();
|
|
1655
|
+
this.canvas = void 0;
|
|
1656
|
+
};
|
|
1657
|
+
try {
|
|
1658
|
+
const renderer = await createRenderer(this.backend, { background: this.background });
|
|
1659
|
+
if (generation !== this.generation) {
|
|
1660
|
+
renderer.destroy();
|
|
1661
|
+
dropCanvas();
|
|
1662
|
+
return;
|
|
1663
|
+
}
|
|
1664
|
+
await renderer.init(canvas);
|
|
1665
|
+
if (generation !== this.generation) {
|
|
1666
|
+
renderer.destroy();
|
|
1667
|
+
dropCanvas();
|
|
1668
|
+
return;
|
|
1669
|
+
}
|
|
1670
|
+
this.renderer = renderer;
|
|
1671
|
+
this.resize();
|
|
1672
|
+
this.ready = true;
|
|
1673
|
+
} catch (error) {
|
|
1674
|
+
dropCanvas();
|
|
1675
|
+
throw error;
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
/** Add a mesh to the scene and return a handle for animating it. */
|
|
1679
|
+
add(mesh, init) {
|
|
1680
|
+
const entry = {
|
|
1681
|
+
mesh,
|
|
1682
|
+
transform: transform(init),
|
|
1683
|
+
transparency: init?.transparency,
|
|
1684
|
+
remove: () => {
|
|
1685
|
+
const i = this.scene.indexOf(entry);
|
|
1686
|
+
if (i >= 0) this.scene.splice(i, 1);
|
|
1687
|
+
}
|
|
1688
|
+
};
|
|
1689
|
+
this.scene.push(entry);
|
|
1690
|
+
return entry;
|
|
1691
|
+
}
|
|
1692
|
+
resize() {
|
|
1693
|
+
const canvas = this.canvas;
|
|
1694
|
+
if (!canvas) return;
|
|
1695
|
+
const dpr = window.devicePixelRatio || 1;
|
|
1696
|
+
this.cssWidth = canvas.clientWidth || canvas.parentElement?.clientWidth || 0;
|
|
1697
|
+
this.cssHeight = canvas.clientHeight || canvas.parentElement?.clientHeight || 0;
|
|
1698
|
+
canvas.width = Math.max(1, Math.round(this.cssWidth * dpr));
|
|
1699
|
+
canvas.height = Math.max(1, Math.round(this.cssHeight * dpr));
|
|
1700
|
+
this.renderer?.resize(this.cssWidth, this.cssHeight, dpr);
|
|
1701
|
+
}
|
|
1702
|
+
/** Draw a single frame from the current scene state. */
|
|
1703
|
+
render() {
|
|
1704
|
+
if (!this.ready || !this.renderer) return;
|
|
1705
|
+
const width = this.cssWidth;
|
|
1706
|
+
const height = this.cssHeight;
|
|
1707
|
+
if (width === 0 || height === 0) return;
|
|
1708
|
+
const items = this.scene.map((entry) => ({
|
|
1709
|
+
mesh: entry.mesh,
|
|
1710
|
+
model: modelMatrix(entry.transform),
|
|
1711
|
+
transparency: entry.transparency
|
|
1712
|
+
}));
|
|
1713
|
+
const eye = this.camera.options.position;
|
|
1714
|
+
this.renderer.render({
|
|
1715
|
+
items: orderRenderItems(items, eye),
|
|
1716
|
+
viewProjection: this.camera.viewProjection(width / height),
|
|
1717
|
+
eye,
|
|
1718
|
+
light: this.light.params,
|
|
1719
|
+
width,
|
|
1720
|
+
height
|
|
1721
|
+
});
|
|
1722
|
+
}
|
|
1723
|
+
/** Start an internal animation loop that calls {@link render} each frame. */
|
|
1724
|
+
start() {
|
|
1725
|
+
if (this.running) return;
|
|
1726
|
+
this.running = true;
|
|
1727
|
+
const loop = () => {
|
|
1728
|
+
if (!this.running) return;
|
|
1729
|
+
this.render();
|
|
1730
|
+
this.rafId = requestAnimationFrame(loop);
|
|
1731
|
+
};
|
|
1732
|
+
this.rafId = requestAnimationFrame(loop);
|
|
1733
|
+
}
|
|
1734
|
+
/** Stop the internal animation loop started by {@link start}. */
|
|
1735
|
+
stop() {
|
|
1736
|
+
this.running = false;
|
|
1737
|
+
if (this.rafId) cancelAnimationFrame(this.rafId);
|
|
1738
|
+
this.rafId = 0;
|
|
1739
|
+
}
|
|
1740
|
+
/** Stop animating, release the renderer, and remove the canvas. */
|
|
1741
|
+
destroy() {
|
|
1742
|
+
this.generation++;
|
|
1743
|
+
this.ready = false;
|
|
1744
|
+
this.stop();
|
|
1745
|
+
this.observer?.disconnect();
|
|
1746
|
+
this.observer = void 0;
|
|
1747
|
+
this.renderer?.destroy();
|
|
1748
|
+
this.renderer = void 0;
|
|
1749
|
+
this.canvas?.remove();
|
|
1750
|
+
this.canvas = void 0;
|
|
1751
|
+
}
|
|
1752
|
+
};
|
|
1753
|
+
|
|
1754
|
+
// src/animations/particles.ts
|
|
1755
|
+
var DEFAULT_COLORS2 = ["#fde047", "#fb923c", "#f472b6", "#60a5fa"];
|
|
1756
|
+
var FADE_IN_END = 0.15;
|
|
1757
|
+
var FADE_OUT_START = 0.6;
|
|
1758
|
+
function rand01(seed, index, salt) {
|
|
1759
|
+
let h = (seed ^ Math.imul(index + 1, 2654435769) ^ Math.imul(salt + 1, 2246822507)) >>> 0;
|
|
1760
|
+
h = Math.imul(h ^ h >>> 16, 73244475);
|
|
1761
|
+
h = Math.imul(h ^ h >>> 16, 73244475);
|
|
1762
|
+
h ^= h >>> 16;
|
|
1763
|
+
return (h >>> 0) / 4294967296;
|
|
1764
|
+
}
|
|
1765
|
+
function smoothstep(edge0, edge1, value) {
|
|
1766
|
+
const x = Math.max(0, Math.min(1, (value - edge0) / (edge1 - edge0)));
|
|
1767
|
+
return x * x * (3 - 2 * x);
|
|
1768
|
+
}
|
|
1769
|
+
function positiveFinite(value, name) {
|
|
1770
|
+
if (!Number.isFinite(value) || value <= 0) {
|
|
1771
|
+
throw new RangeError(`3d-spinner: ${name} must be a finite number greater than zero.`);
|
|
1772
|
+
}
|
|
1773
|
+
return value;
|
|
1774
|
+
}
|
|
1775
|
+
function emitBasis(direction) {
|
|
1776
|
+
const d = normalize(direction);
|
|
1777
|
+
const helper = Math.abs(d.y) < 0.99 ? { x: 0, y: 1, z: 0 } : { x: 1, y: 0, z: 0 };
|
|
1778
|
+
const right = normalize(cross(helper, d));
|
|
1779
|
+
return { d, right, up: cross(d, right) };
|
|
1780
|
+
}
|
|
1781
|
+
function particleField(options = {}) {
|
|
1782
|
+
const rate = positiveFinite(options.rate ?? 20, "rate");
|
|
1783
|
+
const lifeMs = positiveFinite(options.lifeMs ?? 1800, "lifeMs");
|
|
1784
|
+
const size = options.size ?? 0.16;
|
|
1785
|
+
const speed = options.speed ?? 0.6;
|
|
1786
|
+
const gravity = options.gravity;
|
|
1787
|
+
const spread = options.spread ?? 0.5;
|
|
1788
|
+
const peak = Math.max(0, Math.min(1, options.opacity ?? 0.9));
|
|
1789
|
+
const spin = options.spin ?? 2e-3;
|
|
1790
|
+
const alignToMotion = options.alignToMotion ?? false;
|
|
1791
|
+
const seed = options.seed ?? 1;
|
|
1792
|
+
const basis = options.direction && emitBasis(options.direction);
|
|
1793
|
+
const spawnGapMs = 1e3 / rate;
|
|
1794
|
+
const directionOf = (index) => {
|
|
1795
|
+
const u = rand01(seed, index, 0);
|
|
1796
|
+
const phi = 2 * Math.PI * rand01(seed, index, 1);
|
|
1797
|
+
if (!basis) {
|
|
1798
|
+
const z = 2 * u - 1;
|
|
1799
|
+
const r = Math.sqrt(Math.max(0, 1 - z * z));
|
|
1800
|
+
return { x: r * Math.cos(phi), y: r * Math.sin(phi), z };
|
|
1801
|
+
}
|
|
1802
|
+
const cos = 1 - u * (1 - Math.cos(spread));
|
|
1803
|
+
const sin = Math.sqrt(Math.max(0, 1 - cos * cos));
|
|
1804
|
+
const { d, right, up } = basis;
|
|
1805
|
+
return {
|
|
1806
|
+
x: d.x * cos + (right.x * Math.cos(phi) + up.x * Math.sin(phi)) * sin,
|
|
1807
|
+
y: d.y * cos + (right.y * Math.cos(phi) + up.y * Math.sin(phi)) * sin,
|
|
1808
|
+
z: d.z * cos + (right.z * Math.cos(phi) + up.z * Math.sin(phi)) * sin
|
|
1809
|
+
};
|
|
1810
|
+
};
|
|
1811
|
+
return {
|
|
1812
|
+
maxLive: Math.ceil(lifeMs * rate / 1e3) + 1,
|
|
1813
|
+
spawnGapMs,
|
|
1814
|
+
lifeMs,
|
|
1815
|
+
sample(index, t) {
|
|
1816
|
+
if (index < 0) return void 0;
|
|
1817
|
+
const age = t - index * spawnGapMs;
|
|
1818
|
+
if (age < 0 || age >= lifeMs) return void 0;
|
|
1819
|
+
const seconds = age / 1e3;
|
|
1820
|
+
const dir = directionOf(index);
|
|
1821
|
+
const particleSpeed = speed * (0.6 + 0.8 * rand01(seed, index, 2));
|
|
1822
|
+
const travel = particleSpeed * seconds;
|
|
1823
|
+
const pull = gravity ? 0.5 * seconds * seconds : 0;
|
|
1824
|
+
const life = age / lifeMs;
|
|
1825
|
+
const roll = alignToMotion ? Math.atan2(
|
|
1826
|
+
dir.y * particleSpeed + (gravity?.y ?? 0) * seconds,
|
|
1827
|
+
dir.x * particleSpeed + (gravity?.x ?? 0) * seconds
|
|
1828
|
+
) : 2 * Math.PI * rand01(seed, index, 3) + (2 * rand01(seed, index, 4) - 1) * spin * age;
|
|
1829
|
+
return {
|
|
1830
|
+
position: {
|
|
1831
|
+
x: dir.x * travel + (gravity ? gravity.x * pull : 0),
|
|
1832
|
+
y: dir.y * travel + (gravity ? gravity.y * pull : 0),
|
|
1833
|
+
z: dir.z * travel + (gravity ? gravity.z * pull : 0)
|
|
1834
|
+
},
|
|
1835
|
+
roll,
|
|
1836
|
+
size: size * (0.7 + 0.6 * rand01(seed, index, 5)),
|
|
1837
|
+
opacity: peak * smoothstep(0, FADE_IN_END, life) * (1 - smoothstep(FADE_OUT_START, 1, life))
|
|
1838
|
+
};
|
|
1839
|
+
}
|
|
1840
|
+
};
|
|
1841
|
+
}
|
|
1842
|
+
var ParticlesAnimation = class {
|
|
1843
|
+
constructor(options = {}) {
|
|
1844
|
+
this.handles = [];
|
|
1845
|
+
this.fades = [];
|
|
1846
|
+
this.enterAt = Infinity;
|
|
1847
|
+
this.exitAt = Infinity;
|
|
1848
|
+
this.finished = false;
|
|
1849
|
+
this.field = particleField(options);
|
|
1850
|
+
this.colors = options.colors ?? DEFAULT_COLORS2;
|
|
1851
|
+
this.backend = options.backend;
|
|
1852
|
+
this.texture = options.texture;
|
|
1853
|
+
this.labelContent = options.label;
|
|
1854
|
+
this.fadeLabel = options.fadeLabel ?? true;
|
|
1855
|
+
this.emitter = options.emitter;
|
|
1856
|
+
this.outroMs = Math.max(0, options.outroMs ?? 0);
|
|
1857
|
+
}
|
|
1858
|
+
mount(target) {
|
|
1859
|
+
if (!target.style.position) target.style.position = "relative";
|
|
1860
|
+
const meshes = this.colors.map((color) => quad(1, [color]));
|
|
1861
|
+
const texture = this.texture;
|
|
1862
|
+
const backend = texture ? async (rendererOptions) => {
|
|
1863
|
+
const renderer = this.backend === "webgpu" ? new (await Promise.resolve().then(() => (init_webgpu_textured(), webgpu_textured_exports))).WebGPUTexturedRenderer(rendererOptions) : this.backend === "webgl" ? new (await Promise.resolve().then(() => (init_webgl_textured(), webgl_textured_exports))).WebGLTexturedRenderer(rendererOptions) : new (await Promise.resolve().then(() => (init_canvas2d_textured(), canvas2d_textured_exports))).Canvas2DTexturedRenderer(rendererOptions);
|
|
1864
|
+
for (const mesh of meshes) renderer.setTexture(mesh, texture);
|
|
1865
|
+
return renderer;
|
|
1866
|
+
} : this.backend;
|
|
1867
|
+
const engine = new Little3dEngine({
|
|
1868
|
+
backend,
|
|
1869
|
+
camera: { position: { x: 0, y: 0, z: 3 } },
|
|
1870
|
+
light: { intensity: 0, ambient: 1 }
|
|
1871
|
+
});
|
|
1872
|
+
for (let slot = 0; slot < this.field.maxLive; slot++) {
|
|
1873
|
+
const fade = { mode: "one-sided", opacity: 0 };
|
|
1874
|
+
this.fades.push(fade);
|
|
1875
|
+
this.handles.push(engine.add(meshes[slot % meshes.length], { scale: 0, transparency: fade }));
|
|
1876
|
+
}
|
|
1877
|
+
this.engine = engine;
|
|
1878
|
+
engine.mount(target).catch((error) => {
|
|
1879
|
+
target.textContent = error instanceof Error ? error.message : String(error);
|
|
1880
|
+
});
|
|
1881
|
+
this.label = mountAnimationLabel(target, this.labelContent);
|
|
1882
|
+
if (this.fadeLabel) this.label.setOpacity(0);
|
|
1883
|
+
}
|
|
1884
|
+
enter(now) {
|
|
1885
|
+
if (this.enterAt === Infinity) this.enterAt = now;
|
|
1886
|
+
}
|
|
1887
|
+
exit(now) {
|
|
1888
|
+
if (this.exitAt === Infinity) this.exitAt = now;
|
|
1889
|
+
}
|
|
1890
|
+
isFinished() {
|
|
1891
|
+
return this.finished;
|
|
1892
|
+
}
|
|
1893
|
+
render(now, frame) {
|
|
1894
|
+
if (!this.engine || !this.label) return;
|
|
1895
|
+
if (this.exitAt !== Infinity && now >= this.exitAt + this.outroMs + this.field.lifeMs) this.finished = true;
|
|
1896
|
+
for (const handle of this.handles) handle.transform.scale = 0;
|
|
1897
|
+
if (this.enterAt !== Infinity) {
|
|
1898
|
+
const t = now - this.enterAt;
|
|
1899
|
+
const gap = this.field.spawnGapMs;
|
|
1900
|
+
let first = Math.max(0, Math.ceil((t - this.field.lifeMs) / gap));
|
|
1901
|
+
let last = Math.floor(t / gap);
|
|
1902
|
+
if (this.exitAt !== Infinity) {
|
|
1903
|
+
last = Math.min(last, Math.floor((this.exitAt - this.enterAt + this.outroMs) / gap));
|
|
1904
|
+
}
|
|
1905
|
+
first = Math.max(first, last - this.field.maxLive + 1);
|
|
1906
|
+
for (let index = first; index <= last; index++) {
|
|
1907
|
+
const sample = this.field.sample(index, t);
|
|
1908
|
+
if (!sample) continue;
|
|
1909
|
+
const slot = index % this.handles.length;
|
|
1910
|
+
const transform2 = this.handles[slot].transform;
|
|
1911
|
+
const origin = this.emitter?.positionAt(this.enterAt + index * gap);
|
|
1912
|
+
transform2.position.x = sample.position.x + (origin?.x ?? 0);
|
|
1913
|
+
transform2.position.y = sample.position.y + (origin?.y ?? 0);
|
|
1914
|
+
transform2.position.z = sample.position.z + (origin?.z ?? 0);
|
|
1915
|
+
transform2.rotation.z = sample.roll;
|
|
1916
|
+
transform2.scale = sample.size;
|
|
1917
|
+
this.fades[slot].opacity = sample.opacity;
|
|
1918
|
+
}
|
|
1919
|
+
}
|
|
1920
|
+
this.label.setText(frame.indeterminate ? typeof this.labelContent === "string" ? this.labelContent : "" : `${Math.round(frame.progress * 100)}%`);
|
|
1921
|
+
if (this.fadeLabel) {
|
|
1922
|
+
this.label.setOpacity(animationLabelOpacity(
|
|
1923
|
+
now,
|
|
1924
|
+
this.enterAt,
|
|
1925
|
+
this.field.lifeMs * FADE_IN_END,
|
|
1926
|
+
this.exitAt,
|
|
1927
|
+
this.field.lifeMs
|
|
1928
|
+
));
|
|
1929
|
+
}
|
|
1930
|
+
this.engine.render();
|
|
1931
|
+
}
|
|
1932
|
+
destroy() {
|
|
1933
|
+
this.label?.container.remove();
|
|
1934
|
+
this.label = void 0;
|
|
1935
|
+
this.engine?.destroy();
|
|
1936
|
+
this.engine = void 0;
|
|
1937
|
+
this.handles.length = 0;
|
|
1938
|
+
this.fades.length = 0;
|
|
1939
|
+
}
|
|
1940
|
+
};
|