Skip to content

Commit

Permalink
support new VSA urls
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed May 8, 2024
1 parent 5dcbe3f commit c0b56ca
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions editor/base64.js
Original file line number Diff line number Diff line change
@@ -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));
4 changes: 4 additions & 0 deletions editor/compressor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* global LZMA */

const compressor = new LZMA( 'js/lzma_worker.js' );
export default compressor;
5 changes: 1 addition & 4 deletions editor/index.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -66,7 +65,6 @@ let visualTypeElem;
let saveElem;
let compileStatusElem;
let canvas;
let compressor;
let controls;
let doNotSetURL = true;
const g_slow = false;
Expand Down Expand Up @@ -134,7 +132,6 @@ const setVisualizer = ndx => {
};

async function main() {
compressor = new LZMA( 'js/lzma_worker.js' );
canvas = $('visualization');
controls = $('controls');

Expand Down
22 changes: 17 additions & 5 deletions editor/visualizers/effects/VSAEffect.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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]);
Expand Down

0 comments on commit c0b56ca

Please sign in to comment.