98 lines
4.9 KiB
JavaScript
98 lines
4.9 KiB
JavaScript
import { createHash } from 'node:crypto';
|
|
import { readFileSync } from 'node:fs';
|
|
import { dirname, resolve, basename } from 'node:path';
|
|
|
|
const hashes = {
|
|
'world.onscreen.js': 'b7b2d38c5121428ef32a8c2eeb03da0d40b7eb7cd3074e7825dc504f129b2fdf',
|
|
'dice-box.es.js': 'd1caccae54bd074c84eee2055dbb77073b6a95d294f973e610a71bad4bc59a89',
|
|
};
|
|
function once(source, before, after) {
|
|
if (source.split(before).length !== 2) throw new Error('Dice-Box patch anchor changed: ' + before.slice(0, 90));
|
|
return source.replace(before, after);
|
|
}
|
|
export function patchDiceBox(name, source, root) {
|
|
if (createHash('sha256').update(source).digest('hex') !== hashes[name]) {
|
|
throw new Error(`Dice-Box ${name} differs from 1.1.4. Review the animation adapter before upgrading.`);
|
|
}
|
|
if (name === 'world.onscreen.js') {
|
|
source = once(source, 'r = new g(t, this.scene);', 'r = createCosmicMaterial(t, this.scene, g, Re);');
|
|
source = once(source, 'this.onInitComplete();', 'this.cosmicMotion = new CosmicMotion(C(this, K), C(this, fe), () => Object.values(C(this, Z))), this.onInitComplete();');
|
|
source = once(source, ' render(e) {', ' render(e) {\n this.cosmicMotion.stopIdle();');
|
|
source = once(source, 'this.onRollComplete()) : C(this, K).render();',
|
|
'this.cosmicMotion.startIdle(), this.onRollComplete()) : this.cosmicMotion.renderRolling();');
|
|
source = once(source, ' clear() {', ' clear() {\n this.cosmicMotion.stopIdle();');
|
|
source = once(source, ' resize(e) {\n const t = C(this, fe).width', ` dispose() {
|
|
if (this.cosmicDisposed) return;
|
|
this.cosmicDisposed = true;
|
|
C(this, Ve).forEach(clearTimeout);
|
|
this.cosmicMotion.dispose();
|
|
C(this, oe).stopRenderLoop();
|
|
if (C(this, ne)) { C(this, ne).onmessage = null; C(this, ne).close(); }
|
|
C(this, K).dispose();
|
|
C(this, oe).dispose();
|
|
}
|
|
setVisualsActive(active) { this.cosmicMotion.setActive(active); }
|
|
getVisualState() {
|
|
return {
|
|
disposed: !!this.cosmicDisposed, active: this.cosmicMotion.active,
|
|
idle: this.cosmicMotion.idle, seconds: this.cosmicMotion.seconds,
|
|
frameId: C(this, K).getFrameId(), engineLoops: C(this, oe).activeRenderLoops.length,
|
|
dice: Object.values(C(this, Z)).filter(d => d.mesh).map(d => ({
|
|
id: d.id, theme: d.config.theme, position: d.mesh.position.asArray(),
|
|
rotation: d.mesh.rotationQuaternion.asArray(),
|
|
shaderReady: !!d.mesh.material?.getEffect()?.isReady(),
|
|
vertices: d.mesh.getTotalVertices()
|
|
}))
|
|
};
|
|
}
|
|
resize(e) {
|
|
if (this.cosmicDisposed) return;
|
|
const t = C(this, fe).width`);
|
|
source = once(source, 'Oe.loadDie(e, C(this, K)).then((t) => {',
|
|
'Oe.loadDie(e, C(this, K)).then((t) => {\n if (this.cosmicDisposed) return;');
|
|
source = once(source, ' requestAnimationFrame(() => {\n C(this, ne).postMessage({',
|
|
' requestAnimationFrame(() => {\n if (this.cosmicDisposed) return;\n C(this, ne).postMessage({');
|
|
return `import { createCosmicMaterial } from ${JSON.stringify(resolve(root, 'src/dice-box/cosmic-material.js'))};
|
|
import { CosmicMotion } from ${JSON.stringify(resolve(root, 'src/dice-box/cosmic-motion.js'))};\n` + source;
|
|
}
|
|
source = once(source, ` resizeWorld() {
|
|
const b = pl(() => {
|
|
W(this, u).resize({ width: this.canvas.clientWidth, height: this.canvas.clientHeight }), W(this, p) && W(this, p).postMessage({ action: "resize", width: this.canvas.clientWidth, height: this.canvas.clientHeight });
|
|
});
|
|
window.addEventListener("resize", b);
|
|
}`, ` resizeWorld() {
|
|
if (this.cosmicDisposed) return;
|
|
if (!this.cosmicResize) {
|
|
this.cosmicResize = pl(() => {
|
|
if (this.cosmicDisposed || !this.cosmicReady) return;
|
|
W(this, u).resize({ width: this.canvas.clientWidth, height: this.canvas.clientHeight });
|
|
if (W(this, p)) W(this, p).postMessage({ action: "resize", width: this.canvas.clientWidth, height: this.canvas.clientHeight });
|
|
});
|
|
window.addEventListener("resize", this.cosmicResize);
|
|
}
|
|
this.cosmicResize();
|
|
}
|
|
setVisualsActive(active) { W(this, u).setVisualsActive?.(active); }
|
|
getVisualState() { return W(this, u).getVisualState?.(); }
|
|
dispose() {
|
|
if (this.cosmicDisposed) return;
|
|
this.cosmicDisposed = true;
|
|
window.removeEventListener("resize", this.cosmicResize);
|
|
W(this, u).dispose?.();
|
|
W(this, p)?.terminate();
|
|
this.canvas?.remove();
|
|
}`);
|
|
source = once(source, 'await Promise.all([W(this, w), W(this, K)]),',
|
|
'await Promise.all([W(this, w), W(this, K)]), this.cosmicReady = true,');
|
|
return source;
|
|
}
|
|
|
|
export function diceBoxAnimationPlugin(root) {
|
|
return { name: 'dice-box-cosmic-animation', setup(build) {
|
|
build.onLoad({ filter: /[\\/]@3d-dice[\\/]dice-box[\\/]dist[\\/](world\.onscreen|dice-box\.es)\.js$/ }, args => ({
|
|
contents: patchDiceBox(basename(args.path), readFileSync(args.path, 'utf8'), root),
|
|
loader: 'js', resolveDir: dirname(args.path),
|
|
}));
|
|
} };
|
|
}
|