From c0b56ca6d06c351566d45da78f7544d290239060 Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Wed, 8 May 2024 18:44:51 +0900 Subject: [PATCH] support new VSA urls --- editor/base64.js | 4 ++++ editor/compressor.js | 4 ++++ editor/index.js | 5 +---- editor/visualizers/effects/VSAEffect.js | 22 +++++++++++++++++----- 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 editor/base64.js create mode 100644 editor/compressor.js diff --git a/editor/base64.js b/editor/base64.js new file mode 100644 index 0000000..82e2281 --- /dev/null +++ b/editor/base64.js @@ -0,0 +1,4 @@ +export const decode = s => Uint8Array.from(atob(s), c => c.charCodeAt(0)); +export const encode = b => btoa(String.fromCharCode(...new Uint8Array(b))); +export const decodeToString = s => new TextDecoder().decode(decode(s)); +export const encodeString = s => encode(new TextEncoder().encode(s)); diff --git a/editor/compressor.js b/editor/compressor.js new file mode 100644 index 0000000..1acc219 --- /dev/null +++ b/editor/compressor.js @@ -0,0 +1,4 @@ +/* global LZMA */ + +const compressor = new LZMA( 'js/lzma_worker.js' ); +export default compressor; \ No newline at end of file diff --git a/editor/index.js b/editor/index.js index 39f3f7e..86dbcaf 100644 --- a/editor/index.js +++ b/editor/index.js @@ -1,8 +1,7 @@ -/* global LZMA */ /* global WavMaker */ import '../js/scrollbars.js'; import * as twgl from '../js/twgl-full.module.js'; - +import compressor from './compressor.js'; import { createElem as el } from './elem.js'; import ByteBeatNode from '../src/ByteBeatNode.js'; @@ -66,7 +65,6 @@ let visualTypeElem; let saveElem; let compileStatusElem; let canvas; -let compressor; let controls; let doNotSetURL = true; const g_slow = false; @@ -134,7 +132,6 @@ const setVisualizer = ndx => { }; async function main() { - compressor = new LZMA( 'js/lzma_worker.js' ); canvas = $('visualization'); controls = $('controls'); diff --git a/editor/visualizers/effects/VSAEffect.js b/editor/visualizers/effects/VSAEffect.js index 2677e76..a473df0 100644 --- a/editor/visualizers/effects/VSAEffect.js +++ b/editor/visualizers/effects/VSAEffect.js @@ -1,5 +1,9 @@ import * as twgl from '../../../js/twgl-full.module.js'; +import { + decode, +} from '../../base64.js'; +import compressor from '../../compressor.js'; const m4 = twgl.m4; @@ -305,11 +309,19 @@ export default class VSAEffect { this.currentUrl = this.pendingUrl; this.pendingUrl = undefined; this.compiling = true; - const mungedUrl = url.includes('vertexshaderart.com') - ? `${url}/art.json` - : url; - const req = await fetch(mungedUrl); - const vsa = await req.json(); + let vsa; + if (u.hash.includes('s=')) { + const q = new URLSearchParams(u.hash.substring(1)); + const bytes = decode(q.get('s')); + const text = await new Promise((resolve, reject) => compressor.decompress(bytes, resolve, () => {}, reject)); + vsa = JSON.parse(text); + } else { + const mungedUrl = url.includes('vertexshaderart.com') + ? `${url}/art.json` + : url; + const req = await fetch(mungedUrl); + vsa = await req.json(); + } const gl = this.gl; const vs = applyTemplateToShader(vsa.settings.shader); const programInfo = await twgl.createProgramInfoAsync(gl, [vs, s_fs]);