266 lines
9.9 KiB
JavaScript
266 lines
9.9 KiB
JavaScript
/* StemEngine Tendrils Render Graph (Muon fork overlay)
|
|
* Particle trail / feedback visualization with audio reactivity.
|
|
*/
|
|
|
|
;(function stemengineTendrilsRenderGraph() {
|
|
'use strict'
|
|
|
|
var QUALITY_SCALE = { low: 0.75, balanced: 1.0, high: 1.35 }
|
|
|
|
function clamp01(v) {
|
|
var n = Number(v)
|
|
if (!isFinite(n)) n = 0
|
|
return Math.max(0, Math.min(1, n))
|
|
}
|
|
|
|
function shaped(v, power) {
|
|
return Math.pow(clamp01(v), power)
|
|
}
|
|
|
|
function getBand(audio, key, fallback) {
|
|
var bands = audio && audio.frequencyBands ? audio.frequencyBands : null
|
|
var value = bands && isFinite(Number(bands[key])) ? Number(bands[key]) : fallback
|
|
return clamp01(value)
|
|
}
|
|
|
|
function createProgram(gl, vsSrc, fsSrc) {
|
|
function compile(type, src) {
|
|
var s = gl.createShader(type)
|
|
gl.shaderSource(s, src)
|
|
gl.compileShader(s)
|
|
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) {
|
|
var err = gl.getShaderInfoLog(s) || 'shader compile failed'
|
|
window.__stemengineEmitError__('TENDRILS_SHADER', err)
|
|
gl.deleteShader(s)
|
|
return null
|
|
}
|
|
return s
|
|
}
|
|
var vs = compile(gl.VERTEX_SHADER, vsSrc)
|
|
var fs = compile(gl.FRAGMENT_SHADER, fsSrc)
|
|
if (!vs || !fs) return null
|
|
var p = gl.createProgram()
|
|
gl.attachShader(p, vs)
|
|
gl.attachShader(p, fs)
|
|
gl.linkProgram(p)
|
|
gl.deleteShader(vs)
|
|
gl.deleteShader(fs)
|
|
if (!gl.getProgramParameter(p, gl.LINK_STATUS)) {
|
|
var err2 = gl.getProgramInfoLog(p) || 'program link failed'
|
|
window.__stemengineEmitError__('TENDRILS_PROGRAM', err2)
|
|
gl.deleteProgram(p)
|
|
return null
|
|
}
|
|
return p
|
|
}
|
|
|
|
function createTarget(gl, w, h) {
|
|
var tex = gl.createTexture()
|
|
gl.bindTexture(gl.TEXTURE_2D, tex)
|
|
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, null)
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
|
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
|
|
|
var fbo = gl.createFramebuffer()
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo)
|
|
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0)
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
|
|
return { tex: tex, fbo: fbo, width: w, height: h }
|
|
}
|
|
|
|
function TendrilsSimulator(canvas, gl, width, height, quality) {
|
|
this.canvas = canvas || document.querySelector('canvas')
|
|
this.gl = gl || (this.canvas && (this.canvas.getContext('webgl2', { alpha: false, antialias: false }) || this.canvas.getContext('webgl', { alpha: false, antialias: false })))
|
|
if (!this.gl) throw new Error('[StemEngine][Tendrils] Unable to acquire WebGL context')
|
|
|
|
this.width = Math.max(1, Number(width) || this.canvas.clientWidth || this.canvas.width || 512)
|
|
this.height = Math.max(1, Number(height) || this.canvas.clientHeight || this.canvas.height || 512)
|
|
this.quality = String(quality || window.__STEMENGINE_MUON_QUALITY__ || 'balanced')
|
|
this.paused = false
|
|
this.rafId = null
|
|
this.running = false
|
|
|
|
var gl = this.gl
|
|
var vs = 'attribute vec2 aPos; varying vec2 vUv; void main(){ vUv=aPos*0.5+0.5; gl_Position=vec4(aPos,0.0,1.0);} '
|
|
var updateFs = [
|
|
'precision highp float;',
|
|
'uniform sampler2D uPrev;',
|
|
'uniform vec2 uRes;',
|
|
'uniform float uTime;',
|
|
'uniform float uBass;',
|
|
'uniform float uTreble;',
|
|
'uniform float uRms;',
|
|
'uniform float uBeat;',
|
|
'uniform float uTrailDecay;',
|
|
'varying vec2 vUv;',
|
|
'float hash(vec2 p){ return fract(sin(dot(p, vec2(127.1,311.7))) * 43758.5453); }',
|
|
'void main(){',
|
|
' vec2 uv=vUv;',
|
|
' vec2 c=uv-0.5;',
|
|
' float r=max(length(c), 1e-4);',
|
|
' float a=atan(c.y,c.x);',
|
|
' float flow=sin(a*8.0 + uTime*(1.6+uBass*4.0)) + cos(r*45.0 + uTime*(1.1+uTreble*5.0));',
|
|
' vec2 dir=normalize(vec2(cos(a+flow*0.45), sin(a+flow*0.45)));',
|
|
' vec2 offset=dir*(0.0015 + 0.0035*uBass) + vec2(hash(uv+uTime)-0.5, hash(uv+uTime*1.7)-0.5)*(0.002*uTreble);',
|
|
' vec3 prev=texture2D(uPrev, clamp(uv-offset, 0.0, 1.0)).rgb * uTrailDecay;',
|
|
' float line=exp(-abs(sin((a*12.0 + r*120.0) + uTime*6.0*(1.0+uTreble)))*26.0);',
|
|
' float beatBurst=uBeat * exp(-r*4.0);',
|
|
' vec3 colorA=vec3(0.2+uBass*0.8, 0.2+uRms*0.7, 0.4+uTreble*0.6);',
|
|
' vec3 colorB=vec3(0.9,0.6+uTreble*0.35,0.2+uBass*0.4);',
|
|
' vec3 inject=mix(colorA, colorB, clamp(r*1.5, 0.0, 1.0))*(line*0.3 + beatBurst*0.8 + uRms*0.05);',
|
|
' gl_FragColor=vec4(clamp(prev + inject, 0.0, 1.0), 1.0);',
|
|
'}'
|
|
].join('\n')
|
|
var blitFs = 'precision mediump float; uniform sampler2D uTex; varying vec2 vUv; void main(){ gl_FragColor = texture2D(uTex, vUv); }'
|
|
|
|
this.updateProgram = createProgram(gl, vs, updateFs)
|
|
this.blitProgram = createProgram(gl, vs, blitFs)
|
|
|
|
var quad = new Float32Array([-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1])
|
|
this.quad = gl.createBuffer()
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, this.quad)
|
|
gl.bufferData(gl.ARRAY_BUFFER, quad, gl.STATIC_DRAW)
|
|
|
|
this.read = null
|
|
this.write = null
|
|
this.allocateTargets()
|
|
this.start()
|
|
}
|
|
|
|
TendrilsSimulator.prototype.allocateTargets = function () {
|
|
var gl = this.gl
|
|
if (this.read) {
|
|
gl.deleteTexture(this.read.tex)
|
|
gl.deleteFramebuffer(this.read.fbo)
|
|
}
|
|
if (this.write) {
|
|
gl.deleteTexture(this.write.tex)
|
|
gl.deleteFramebuffer(this.write.fbo)
|
|
}
|
|
var scale = QUALITY_SCALE[this.quality] || QUALITY_SCALE.balanced
|
|
var w = Math.max(64, Math.floor(this.width * scale))
|
|
var h = Math.max(64, Math.floor(this.height * scale))
|
|
this.read = createTarget(gl, w, h)
|
|
this.write = createTarget(gl, w, h)
|
|
}
|
|
|
|
TendrilsSimulator.prototype.setQuality = function (q) {
|
|
var next = String(q || 'balanced')
|
|
if (next === this.quality) return
|
|
this.quality = next
|
|
this.allocateTargets()
|
|
}
|
|
|
|
TendrilsSimulator.prototype.setViewport = function (w, h) {
|
|
var nw = Math.max(1, Number(w) || this.width)
|
|
var nh = Math.max(1, Number(h) || this.height)
|
|
if (nw === this.width && nh === this.height) return
|
|
this.width = nw
|
|
this.height = nh
|
|
if (this.canvas) {
|
|
this.canvas.width = nw
|
|
this.canvas.height = nh
|
|
}
|
|
this.allocateTargets()
|
|
}
|
|
|
|
TendrilsSimulator.prototype.drawQuad = function (program) {
|
|
var gl = this.gl
|
|
gl.useProgram(program)
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, this.quad)
|
|
var loc = gl.getAttribLocation(program, 'aPos')
|
|
gl.enableVertexAttribArray(loc)
|
|
gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0)
|
|
gl.drawArrays(gl.TRIANGLES, 0, 6)
|
|
}
|
|
|
|
TendrilsSimulator.prototype.step = function () {
|
|
if (this.paused || !this.read || !this.write) return
|
|
var gl = this.gl
|
|
var t = performance.now() * 0.001
|
|
var audio = window.__STEMENGINE_MUON_AUDIO__ || {}
|
|
var bass = shaped(0.6 * getBand(audio, 'bass', audio.bass || 0) + 0.4 * getBand(audio, 'sub', audio.sub || 0), 0.62)
|
|
var treble = shaped(0.55 * getBand(audio, 'presence', audio.treble || 0) + 0.45 * getBand(audio, 'brilliance', audio.treble || 0), 0.66)
|
|
var rms = shaped(clamp01(audio.rms || 0), 0.8)
|
|
var beat = audio.beat ? 1 : 0
|
|
var buildBoost = audio.isBuildUp ? 0.12 : 0
|
|
var dropBoost = audio.isBeatDrop ? 0.2 : 0
|
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, this.write.fbo)
|
|
gl.viewport(0, 0, this.write.width, this.write.height)
|
|
gl.useProgram(this.updateProgram)
|
|
gl.activeTexture(gl.TEXTURE0)
|
|
gl.bindTexture(gl.TEXTURE_2D, this.read.tex)
|
|
gl.uniform1i(gl.getUniformLocation(this.updateProgram, 'uPrev'), 0)
|
|
gl.uniform2f(gl.getUniformLocation(this.updateProgram, 'uRes'), this.write.width, this.write.height)
|
|
gl.uniform1f(gl.getUniformLocation(this.updateProgram, 'uTime'), t)
|
|
gl.uniform1f(gl.getUniformLocation(this.updateProgram, 'uBass'), bass)
|
|
gl.uniform1f(gl.getUniformLocation(this.updateProgram, 'uTreble'), treble)
|
|
gl.uniform1f(gl.getUniformLocation(this.updateProgram, 'uRms'), rms)
|
|
gl.uniform1f(gl.getUniformLocation(this.updateProgram, 'uBeat'), beat)
|
|
gl.uniform1f(
|
|
gl.getUniformLocation(this.updateProgram, 'uTrailDecay'),
|
|
(0.90 + (this.quality === 'high' ? 0.08 : this.quality === 'low' ? 0.04 : 0.06)) - buildBoost + dropBoost,
|
|
)
|
|
this.drawQuad(this.updateProgram)
|
|
|
|
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
|
|
gl.viewport(0, 0, this.width, this.height)
|
|
gl.useProgram(this.blitProgram)
|
|
gl.activeTexture(gl.TEXTURE0)
|
|
gl.bindTexture(gl.TEXTURE_2D, this.write.tex)
|
|
gl.uniform1i(gl.getUniformLocation(this.blitProgram, 'uTex'), 0)
|
|
this.drawQuad(this.blitProgram)
|
|
|
|
var tmp = this.read
|
|
this.read = this.write
|
|
this.write = tmp
|
|
}
|
|
|
|
TendrilsSimulator.prototype.tick = function () {
|
|
if (!this.running) return
|
|
this.paused = Boolean(window.__STEMENGINE_MUON_PAUSED__)
|
|
this.setQuality(window.__STEMENGINE_MUON_QUALITY__)
|
|
var vp = window.__STEMENGINE_MUON_VIEWPORT__
|
|
if (vp) this.setViewport(vp.width, vp.height)
|
|
this.step()
|
|
var self = this
|
|
this.rafId = requestAnimationFrame(function () { self.tick() })
|
|
}
|
|
|
|
TendrilsSimulator.prototype.start = function () {
|
|
if (this.running) return
|
|
this.running = true
|
|
this.tick()
|
|
}
|
|
|
|
TendrilsSimulator.prototype.stop = function () {
|
|
this.running = false
|
|
if (this.rafId != null) {
|
|
cancelAnimationFrame(this.rafId)
|
|
this.rafId = null
|
|
}
|
|
}
|
|
|
|
TendrilsSimulator.prototype.dispose = function () {
|
|
var gl = this.gl
|
|
this.stop()
|
|
if (this.quad) gl.deleteBuffer(this.quad)
|
|
if (this.updateProgram) gl.deleteProgram(this.updateProgram)
|
|
if (this.blitProgram) gl.deleteProgram(this.blitProgram)
|
|
if (this.read) {
|
|
gl.deleteTexture(this.read.tex)
|
|
gl.deleteFramebuffer(this.read.fbo)
|
|
}
|
|
if (this.write) {
|
|
gl.deleteTexture(this.write.tex)
|
|
gl.deleteFramebuffer(this.write.fbo)
|
|
}
|
|
}
|
|
|
|
window.__StemEngineTendrilsRenderGraph__ = TendrilsSimulator
|
|
console.log('[StemEngine][Tendrils] Render graph module loaded')
|
|
})()
|