274 lines
10 KiB
JavaScript
274 lines
10 KiB
JavaScript
/* StemEngine Spirit Render Graph (Muon fork overlay)
|
|
* Atmospheric layered glow visualization with audio reactivity.
|
|
*/
|
|
|
|
;(function stemengineSpiritRenderGraph() {
|
|
'use strict'
|
|
|
|
var QUALITY_SCALE = { low: 0.8, balanced: 1.0, high: 1.25 }
|
|
|
|
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 compileProgram(gl, vsSrc, fsSrc) {
|
|
function c(type, src) {
|
|
var s = gl.createShader(type)
|
|
gl.shaderSource(s, src)
|
|
gl.compileShader(s)
|
|
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) {
|
|
var e = gl.getShaderInfoLog(s) || 'shader compile failed'
|
|
window.__stemengineEmitError__('SPIRIT_SHADER', e)
|
|
gl.deleteShader(s)
|
|
return null
|
|
}
|
|
return s
|
|
}
|
|
var vs = c(gl.VERTEX_SHADER, vsSrc)
|
|
var fs = c(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 e2 = gl.getProgramInfoLog(p) || 'program link failed'
|
|
window.__stemengineEmitError__('SPIRIT_PROGRAM', e2)
|
|
gl.deleteProgram(p)
|
|
return null
|
|
}
|
|
return p
|
|
}
|
|
|
|
function mkTarget(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 SpiritSimulator(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][Spirit] 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.running = false
|
|
this.rafId = null
|
|
|
|
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 uFade;',
|
|
'varying vec2 vUv;',
|
|
'float hash(vec2 p){ return fract(sin(dot(p, vec2(12.9898,78.233))) * 43758.5453); }',
|
|
'float fbm(vec2 p){',
|
|
' float v=0.0; float a=0.5;',
|
|
' for(int i=0;i<4;i++){',
|
|
' v += (hash(floor(p)) * 2.0 - 1.0) * a;',
|
|
' p = p*2.03 + 13.1;',
|
|
' a *= 0.5;',
|
|
' }',
|
|
' return v*0.5 + 0.5;',
|
|
'}',
|
|
'void main(){',
|
|
' vec2 uv=vUv;',
|
|
' vec2 c=uv-0.5;',
|
|
' float r=length(c);',
|
|
' float a=atan(c.y,c.x);',
|
|
' vec2 drift = vec2(cos(a + uTime*(0.3+uBass*1.8)), sin(a - uTime*(0.22+uTreble*1.4))) * (0.001 + 0.003*uBass);',
|
|
' vec3 prev=texture2D(uPrev, clamp(uv-drift,0.0,1.0)).rgb * uFade;',
|
|
' float n = fbm(uv*5.0 + vec2(uTime*0.12, -uTime*0.08));',
|
|
' float halo = exp(-r*(3.0 + uTreble*3.0));',
|
|
' float strand = smoothstep(0.0, 0.08 + uTreble*0.06, abs(sin(a*10.0 + n*9.0 + uTime*(1.8+uRms*4.0))) * (0.55-r));',
|
|
' float pulse = uBeat * exp(-r*2.8) + uRms*0.08;',
|
|
' vec3 cool = vec3(0.2 + uTreble*0.6, 0.35 + uRms*0.45, 0.8);',
|
|
' vec3 warm = vec3(0.95, 0.45 + uBass*0.35, 0.2 + uRms*0.25);',
|
|
' vec3 glow = mix(cool, warm, clamp(n + uBass*0.35, 0.0, 1.0)) * (halo*0.35 + strand*0.65 + pulse*0.5);',
|
|
' gl_FragColor = vec4(clamp(prev + glow, 0.0, 1.0), 1.0);',
|
|
'}'
|
|
].join('\n')
|
|
var blitFs = 'precision mediump float; uniform sampler2D uTex; varying vec2 vUv; void main(){ vec3 c=texture2D(uTex,vUv).rgb; gl_FragColor=vec4(c*c*0.15 + c,1.0);} '
|
|
|
|
this.updateProgram = compileProgram(gl, vs, updateFs)
|
|
this.blitProgram = compileProgram(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()
|
|
}
|
|
|
|
SpiritSimulator.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 = mkTarget(gl, w, h)
|
|
this.write = mkTarget(gl, w, h)
|
|
}
|
|
|
|
SpiritSimulator.prototype.setQuality = function (q) {
|
|
var next = String(q || 'balanced')
|
|
if (next === this.quality) return
|
|
this.quality = next
|
|
this.allocateTargets()
|
|
}
|
|
|
|
SpiritSimulator.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()
|
|
}
|
|
|
|
SpiritSimulator.prototype.draw = 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)
|
|
}
|
|
|
|
SpiritSimulator.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.5 * getBand(audio, 'sub', audio.sub || 0) + 0.5 * getBand(audio, 'bass', audio.bass || 0), 0.64)
|
|
var treble = shaped(0.55 * getBand(audio, 'highMid', 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.02 : 0
|
|
var dropBoost = audio.isBeatDrop ? 0.02 : 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, 'uFade'),
|
|
(this.quality === 'high' ? 0.965 : this.quality === 'low' ? 0.92 : 0.945) - buildBoost + dropBoost,
|
|
)
|
|
this.draw(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.draw(this.blitProgram)
|
|
|
|
var tmp = this.read
|
|
this.read = this.write
|
|
this.write = tmp
|
|
}
|
|
|
|
SpiritSimulator.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() })
|
|
}
|
|
|
|
SpiritSimulator.prototype.start = function () {
|
|
if (this.running) return
|
|
this.running = true
|
|
this.tick()
|
|
}
|
|
|
|
SpiritSimulator.prototype.stop = function () {
|
|
this.running = false
|
|
if (this.rafId != null) {
|
|
cancelAnimationFrame(this.rafId)
|
|
this.rafId = null
|
|
}
|
|
}
|
|
|
|
SpiritSimulator.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.__StemEngineSpiritRenderGraph__ = SpiritSimulator
|
|
console.log('[StemEngine][Spirit] Render graph module loaded')
|
|
})()
|