Files
stemengine_old/muon-iframe/fluid-render-graph.js
T
2026-04-27 17:56:54 +09:00

1119 lines
36 KiB
JavaScript

/* StemEngine Fluid Render Graph (Muon fork overlay)
*
* Phase 2 implementation: WebGL fluid simulation with audio reactivity.
* Injected at build time; compatible with existing Muon visualizer.
*
* Features:
* - Ping-pong FBO infrastructure (velocity, dye, divergence, pressure, curl)
* - Dynamic resize handling
* - Bloom chain (balanced + high tiers)
* - Audio-reactive splatting (bass → force, beat → impulse, treble → turbulence)
* - Three quality tiers (low, balanced, high)
*/
;(function stemengineFluidRenderGraph() {
'use strict'
// =========================================================================
// CONSTANTS & CONFIGURATION
// =========================================================================
/** Quality tier iteration counts */
var QUALITY_CONFIG = {
low: { pressure_iterations: 10, bloom: false },
balanced: { pressure_iterations: 20, bloom: true },
high: { pressure_iterations: 40, bloom: true },
}
/** Default texture format for float data */
var TEXTURE_FORMAT = 0x1908 // gl.RGBA
var TEXTURE_TYPE = 0x1406 // gl.UNSIGNED_BYTE
var FLOAT_TEXTURE_TYPE = 0x1406 // Will be overridden if EXT_color_buffer_float available
/** Splat radius (in pixels) for audio-reactive splatting */
var SPLAT_RADIUS_BASE = 15
var SPLAT_RADIUS_MAX = 50
function clamp01(v) {
var n = Number(v)
if (!isFinite(n)) n = 0
return Math.max(0, Math.min(1, n))
}
// Match Muon's snappier feel by accenting peaks while keeping low-end detail.
function shaped(v, power) {
var c = clamp01(v)
return Math.pow(c, power)
}
function getBand(audio, key, fallback) {
var bands = audio && audio.frequencyBands ? audio.frequencyBands : null
var candidate = bands && isFinite(Number(bands[key])) ? Number(bands[key]) : fallback
return clamp01(candidate)
}
// =========================================================================
// UTILITY: WebGL Extensions & Capabilities
// =========================================================================
function detectCapabilities(gl) {
var caps = {
floatTextures: false,
floatLinear: false,
drawBuffers: false,
}
var extColorBuffer = gl.getExtension('EXT_color_buffer_float')
var extTextureFloat = gl.getExtension('OES_texture_float')
var extTextureHalfFloat = gl.getExtension('OES_texture_half_float')
var extFloatLinear = gl.getExtension('OES_texture_float_linear')
var extDrawBuffers = gl.getExtension('WEBGL_draw_buffers')
if (extColorBuffer && extTextureFloat && extFloatLinear) {
caps.floatTextures = true
caps.floatLinear = true
FLOAT_TEXTURE_TYPE = 0x1406 // gl.FLOAT
TEXTURE_TYPE = 0x1406 // gl.FLOAT
} else if (extTextureHalfFloat && extColorBuffer) {
caps.floatTextures = true
FLOAT_TEXTURE_TYPE = 0x140b // gl.HALF_FLOAT_OES
TEXTURE_TYPE = 0x140b
}
if (extDrawBuffers) {
caps.drawBuffers = true
}
return caps
}
// =========================================================================
// SHADER COMPILATION & LINKING
// =========================================================================
function compileShader(gl, source, type) {
var shader = gl.createShader(type)
gl.shaderSource(shader, source)
gl.compileShader(shader)
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
var info = gl.getShaderInfoLog(shader)
console.error('[StemEngine][Fluid] Shader compile error:', info)
window.__stemengineEmitError__('SHADER_COMPILE', info)
gl.deleteShader(shader)
return null
}
return shader
}
function linkProgram(gl, vsSrc, fsSrc) {
var vs = compileShader(gl, vsSrc, gl.VERTEX_SHADER)
var fs = compileShader(gl, fsSrc, gl.FRAGMENT_SHADER)
if (!vs || !fs) return null
var prog = gl.createProgram()
gl.attachShader(prog, vs)
gl.attachShader(prog, fs)
gl.linkProgram(prog)
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) {
var info = gl.getProgramInfoLog(prog)
console.error('[StemEngine][Fluid] Program link error:', info)
window.__stemengineEmitError__('PROGRAM_LINK', info)
gl.deleteProgram(prog)
return null
}
gl.deleteShader(vs)
gl.deleteShader(fs)
return prog
}
// =========================================================================
// FULLSCREEN QUAD VERTEX SHADER (shared by all passes)
// =========================================================================
var QUAD_VS = `
attribute vec2 aPos;
varying vec2 vUv;
void main() {
vUv = aPos * 0.5 + 0.5;
gl_Position = vec4(aPos, 0.0, 1.0);
}
`
// =========================================================================
// SHADER SOURCE: SPLAT
// =========================================================================
var SPLAT_FS = `
precision highp float;
uniform sampler2D uVelocity;
uniform vec2 uSplatPos;
uniform float uSplatRadius;
uniform float uSplatForce;
uniform bool uSplatIsBeat;
uniform float uTurbulenceAmplitude;
varying vec2 vUv;
void main() {
vec2 pos = gl_FragCoord.xy;
float dist = length(pos - uSplatPos);
// Gaussian kernel
float kernel = exp(-dist * dist / (uSplatRadius * uSplatRadius));
vec2 splatVel;
if (uSplatIsBeat) {
// Concentrated impulse for beat
splatVel = normalize(pos - uSplatPos) * uSplatForce * kernel;
} else {
// Diffuse spread for bass
splatVel = (pos - uSplatPos) * uSplatForce * 0.1 * kernel;
}
// Apply turbulence rotation (treble-driven vorticity)
float angle = uTurbulenceAmplitude * sin(length(pos) * 0.01) * 3.14159;
splatVel = vec2(
splatVel.x * cos(angle) - splatVel.y * sin(angle),
splatVel.x * sin(angle) + splatVel.y * cos(angle)
);
vec2 vel = texture2D(uVelocity, vUv).xy;
gl_FragColor = vec4(vel + splatVel, 0.0, 1.0);
}
`
// =========================================================================
// SHADER SOURCE: CURL (vorticity)
// =========================================================================
var CURL_FS = `
precision highp float;
uniform sampler2D uVelocity;
uniform vec2 uInvSize;
varying vec2 vUv;
void main() {
float n = texture2D(uVelocity, vUv + vec2(0.0, uInvSize.y)).y;
float s = texture2D(uVelocity, vUv - vec2(0.0, uInvSize.y)).y;
float e = texture2D(uVelocity, vUv + vec2(uInvSize.x, 0.0)).x;
float w = texture2D(uVelocity, vUv - vec2(uInvSize.x, 0.0)).x;
float curl = (e - w) - (n - s);
gl_FragColor = vec4(curl, 0.0, 0.0, 1.0);
}
`
// =========================================================================
// SHADER SOURCE: DIVERGENCE
// =========================================================================
var DIVERGENCE_FS = `
precision highp float;
uniform sampler2D uVelocity;
uniform vec2 uInvSize;
varying vec2 vUv;
void main() {
float n = texture2D(uVelocity, vUv + vec2(0.0, uInvSize.y)).y;
float s = texture2D(uVelocity, vUv - vec2(0.0, uInvSize.y)).y;
float e = texture2D(uVelocity, vUv + vec2(uInvSize.x, 0.0)).x;
float w = texture2D(uVelocity, vUv - vec2(uInvSize.x, 0.0)).x;
float div = 0.5 * ((e - w) + (n - s));
gl_FragColor = vec4(div, 0.0, 0.0, 1.0);
}
`
// =========================================================================
// SHADER SOURCE: PRESSURE JACOBI
// =========================================================================
var PRESSURE_JACOBI_FS = `
precision highp float;
uniform sampler2D uPressure;
uniform sampler2D uDivergence;
uniform float uAlpha;
uniform float uRBeta;
uniform vec2 uInvSize;
varying vec2 vUv;
void main() {
float n = texture2D(uPressure, vUv + vec2(0.0, uInvSize.y)).x;
float s = texture2D(uPressure, vUv - vec2(0.0, uInvSize.y)).x;
float e = texture2D(uPressure, vUv + vec2(uInvSize.x, 0.0)).x;
float w = texture2D(uPressure, vUv - vec2(uInvSize.x, 0.0)).x;
float div = texture2D(uDivergence, vUv).x;
float p = (n + s + e + w + uAlpha * div) * uRBeta;
gl_FragColor = vec4(p, 0.0, 0.0, 1.0);
}
`
// =========================================================================
// SHADER SOURCE: GRADIENT SUBTRACT
// =========================================================================
var GRADIENT_SUBTRACT_FS = `
precision highp float;
uniform sampler2D uVelocity;
uniform sampler2D uPressure;
uniform vec2 uInvSize;
uniform float uGradientScale;
varying vec2 vUv;
void main() {
float n = texture2D(uPressure, vUv + vec2(0.0, uInvSize.y)).x;
float s = texture2D(uPressure, vUv - vec2(0.0, uInvSize.y)).x;
float e = texture2D(uPressure, vUv + vec2(uInvSize.x, 0.0)).x;
float w = texture2D(uPressure, vUv - vec2(uInvSize.x, 0.0)).x;
vec2 grad = uGradientScale * vec2(e - w, n - s);
vec2 vel = texture2D(uVelocity, vUv).xy;
gl_FragColor = vec4(vel - grad, 0.0, 1.0);
}
`
// =========================================================================
// SHADER SOURCE: ADVECT VELOCITY
// =========================================================================
var ADVECT_VELOCITY_FS = `
precision highp float;
uniform sampler2D uVelocity;
uniform vec2 uInvSize;
uniform float uDt;
uniform float uDecay;
varying vec2 vUv;
void main() {
vec2 vel = texture2D(uVelocity, vUv).xy;
vec2 backPos = vUv - uDt * vel * uInvSize;
// Clamp to valid range
backPos = clamp(backPos, uInvSize, 1.0 - uInvSize);
vec2 advectedVel = texture2D(uVelocity, backPos).xy * uDecay;
gl_FragColor = vec4(advectedVel, 0.0, 1.0);
}
`
// =========================================================================
// SHADER SOURCE: ADVECT DYE
// =========================================================================
var ADVECT_DYE_FS = `
precision highp float;
uniform sampler2D uDye;
uniform sampler2D uVelocity;
uniform vec2 uInvSize;
uniform float uDt;
uniform float uDecay;
uniform vec3 uAudioColor;
varying vec2 vUv;
void main() {
vec2 vel = texture2D(uVelocity, vUv).xy;
vec2 backPos = vUv - uDt * vel * uInvSize;
backPos = clamp(backPos, uInvSize, 1.0 - uInvSize);
vec4 dye = texture2D(uDye, backPos) * uDecay;
// Inject audio-driven color
float mixFactor = 0.15;
dye.rgb += uAudioColor * mixFactor;
dye.rgb = clamp(dye.rgb, 0.0, 1.0);
gl_FragColor = dye;
}
`
// =========================================================================
// SHADER SOURCE: BLOOM DOWNSAMPLE
// =========================================================================
var BLOOM_DOWNSAMPLE_FS = `
precision highp float;
uniform sampler2D uSource;
uniform vec2 uInvSize;
varying vec2 vUv;
void main() {
vec2 uv0 = vUv - uInvSize * 0.5;
vec2 uv1 = vUv + uInvSize * 0.5;
vec4 s0 = texture2D(uSource, vec2(uv0.x, uv0.y));
vec4 s1 = texture2D(uSource, vec2(uv1.x, uv0.y));
vec4 s2 = texture2D(uSource, vec2(uv0.x, uv1.y));
vec4 s3 = texture2D(uSource, vec2(uv1.x, uv1.y));
gl_FragColor = (s0 + s1 + s2 + s3) * 0.25;
}
`
// =========================================================================
// SHADER SOURCE: BLOOM COMPOSITE
// =========================================================================
var BLOOM_COMPOSITE_FS = `
precision highp float;
uniform sampler2D uFullRes;
uniform sampler2D uBloomUp;
uniform float uBloomIntensity;
varying vec2 vUv;
void main() {
vec4 full = texture2D(uFullRes, vUv);
vec4 bloom = texture2D(uBloomUp, vUv);
gl_FragColor = full + bloom * uBloomIntensity;
gl_FragColor.rgb = clamp(gl_FragColor.rgb, 0.0, 1.0);
}
`
// =========================================================================
// SHADER SOURCE: COMPOSITE (final render to canvas)
// =========================================================================
var COMPOSITE_FS = `
precision highp float;
uniform sampler2D uDye;
uniform sampler2D uCurl;
uniform sampler2D uVelocity;
uniform bool uShowVelocity;
uniform bool uShowCurl;
varying vec2 vUv;
void main() {
if (uShowCurl) {
float c = texture2D(uCurl, vUv).x;
float col = abs(c) * 10.0;
gl_FragColor = vec4(vec3(col), 1.0);
} else if (uShowVelocity) {
vec2 vel = texture2D(uVelocity, vUv).xy;
float speed = length(vel) * 0.1;
gl_FragColor = vec4(vec3(speed), 1.0);
} else {
vec4 col = texture2D(uDye, vUv);
gl_FragColor = vec4(col.rgb, 1.0);
}
}
`
// =========================================================================
// FRAMEBUFFER OBJECT (FBO) MANAGEMENT
// =========================================================================
function createFBO(gl, width, height, format, type) {
var fbo = gl.createFramebuffer()
var texture = gl.createTexture()
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo)
gl.bindTexture(gl.TEXTURE_2D, texture)
gl.texImage2D(gl.TEXTURE_2D, 0, format, width, height, 0, format, type, null)
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)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
texture,
0
)
var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER)
if (status !== gl.FRAMEBUFFER_COMPLETE) {
console.warn('[StemEngine][Fluid] FBO incomplete:', status)
}
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
return { fbo: fbo, texture: texture, width: width, height: height }
}
function deleteFBO(gl, fbo) {
if (fbo) {
gl.deleteFramebuffer(fbo.fbo)
gl.deleteTexture(fbo.texture)
}
}
// =========================================================================
// FLUID SIMULATOR STATE
// =========================================================================
function pickCanvas() {
return document.querySelector('canvas') || null
}
function getCanvasSize(canvas) {
if (!canvas) return { width: 512, height: 512 }
var w = canvas.clientWidth || canvas.width || 512
var h = canvas.clientHeight || canvas.height || 512
return {
width: Math.max(1, w | 0),
height: Math.max(1, h | 0),
}
}
function FluidSimulator(gl, width, height, quality) {
this.canvas = pickCanvas()
this.gl =
gl ||
(this.canvas &&
(this.canvas.getContext('webgl2', { alpha: false, antialias: false }) ||
this.canvas.getContext('webgl', { alpha: false, antialias: false })))
if (!this.gl) {
window.__stemengineEmitError__('WEBGL_CONTEXT', 'Unable to acquire WebGL context for FluidSimulator')
throw new Error('[StemEngine][Fluid] Unable to acquire WebGL context')
}
var initialSize = getCanvasSize(this.canvas)
this.width = Math.max(1, Number(width) || initialSize.width)
this.height = Math.max(1, Number(height) || initialSize.height)
this.quality = String(quality || window.__STEMENGINE_MUON_QUALITY__ || 'balanced')
var config = QUALITY_CONFIG[this.quality] || QUALITY_CONFIG.balanced
this.pressureIterations = config.pressure_iterations
this.bloomEnabled = config.bloom
// Capabilities
this.capabilities = detectCapabilities(this.gl)
// FBO pairs (ping-pong)
this.velocity = null
this.velocityBuffer = null
this.dye = null
this.dyeBuffer = null
this.divergence = null
this.pressure = null
this.pressureBuffer = null
this.curl = null
// Bloom chain (if enabled)
this.bloom = null
// Shader programs
this.programs = {}
// State
this.paused = false
this.currentStyle = 'default'
this.rafId = null
this.running = false
// Fullscreen quad VAO
this.quadVBO = null
// Initialize
this.initializeQuad()
this.compileShaders()
this.allocateFBOs()
this.start()
}
FluidSimulator.prototype.setQuality = function (quality) {
var q = String(quality || 'balanced')
if (!QUALITY_CONFIG[q] || q === this.quality) return
this.quality = q
var config = QUALITY_CONFIG[this.quality]
this.pressureIterations = config.pressure_iterations
this.bloomEnabled = config.bloom
this.allocateFBOs()
}
FluidSimulator.prototype.setViewport = function (width, height) {
var w = Math.max(1, Number(width) || this.width)
var h = Math.max(1, Number(height) || this.height)
if (w === this.width && h === this.height) return
this.width = w
this.height = h
if (this.canvas) {
this.canvas.width = w
this.canvas.height = h
}
this.allocateFBOs()
}
FluidSimulator.prototype.start = function () {
if (this.running) return
this.running = true
this.tick()
}
FluidSimulator.prototype.stop = function () {
this.running = false
if (this.rafId != null) {
cancelAnimationFrame(this.rafId)
this.rafId = null
}
}
FluidSimulator.prototype.tick = function () {
if (!this.running) return
// Apply host-driven runtime state.
this.paused = Boolean(window.__STEMENGINE_MUON_PAUSED__)
this.setQuality(window.__STEMENGINE_MUON_QUALITY__)
var viewport = window.__STEMENGINE_MUON_VIEWPORT__
if (viewport && typeof viewport === 'object') {
this.setViewport(viewport.width, viewport.height)
}
var features = window.__STEMENGINE_MUON_AUDIO__ || {
bass: 0,
treble: 0,
rms: 0,
beat: false,
}
this.step(features)
var self = this
this.rafId = requestAnimationFrame(function () {
self.tick()
})
}
FluidSimulator.prototype.initializeQuad = function () {
var gl = this.gl
var positions = new Float32Array([-1, -1, 1, -1, 1, 1, -1, 1])
this.quadVBO = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, this.quadVBO)
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW)
}
FluidSimulator.prototype.compileShaders = function () {
var gl = this.gl
this.programs.splat = linkProgram(gl, QUAD_VS, SPLAT_FS)
this.programs.curl = linkProgram(gl, QUAD_VS, CURL_FS)
this.programs.divergence = linkProgram(gl, QUAD_VS, DIVERGENCE_FS)
this.programs.pressureJacobi = linkProgram(gl, QUAD_VS, PRESSURE_JACOBI_FS)
this.programs.gradientSubtract = linkProgram(gl, QUAD_VS, GRADIENT_SUBTRACT_FS)
this.programs.advectVelocity = linkProgram(gl, QUAD_VS, ADVECT_VELOCITY_FS)
this.programs.advectDye = linkProgram(gl, QUAD_VS, ADVECT_DYE_FS)
this.programs.bloomDownsample = linkProgram(gl, QUAD_VS, BLOOM_DOWNSAMPLE_FS)
this.programs.bloomComposite = linkProgram(gl, QUAD_VS, BLOOM_COMPOSITE_FS)
this.programs.composite = linkProgram(gl, QUAD_VS, COMPOSITE_FS)
console.log('[StemEngine][Fluid] Shaders compiled successfully')
}
FluidSimulator.prototype.renderFullscreenQuad = function (program, fbo) {
var gl = this.gl
if (!program) return
gl.useProgram(program)
gl.bindFramebuffer(gl.FRAMEBUFFER, fbo ? fbo.fbo : null)
if (fbo) {
gl.viewport(0, 0, fbo.width, fbo.height)
} else {
gl.viewport(0, 0, this.width, this.height)
}
gl.bindBuffer(gl.ARRAY_BUFFER, this.quadVBO)
var posLoc = gl.getAttribLocation(program, 'aPos')
if (posLoc >= 0) {
gl.enableVertexAttribArray(posLoc)
gl.vertexAttribPointer(posLoc, 2, gl.FLOAT, false, 0, 0)
}
gl.drawArrays(gl.TRIANGLE_FAN, 0, 4)
}
FluidSimulator.prototype.allocateFBOs = function () {
var gl = this.gl
var w = this.width
var h = this.height
var halfW = Math.max(1, Math.floor(w / 2))
var halfH = Math.max(1, Math.floor(h / 2))
// Free old FBOs
this.releaseFBOs()
// Velocity field (float texture, 2-component or RGBA)
this.velocity = createFBO(gl, w, h, TEXTURE_FORMAT, FLOAT_TEXTURE_TYPE)
this.velocityBuffer = createFBO(gl, w, h, TEXTURE_FORMAT, FLOAT_TEXTURE_TYPE)
// Dye field (color, usually RGBA)
this.dye = createFBO(gl, w, h, gl.RGBA, gl.UNSIGNED_BYTE)
this.dyeBuffer = createFBO(gl, w, h, gl.RGBA, gl.UNSIGNED_BYTE)
// Divergence (scalar or 1-component)
this.divergence = createFBO(gl, w, h, TEXTURE_FORMAT, FLOAT_TEXTURE_TYPE)
// Pressure field (scalar)
this.pressure = createFBO(gl, w, h, TEXTURE_FORMAT, FLOAT_TEXTURE_TYPE)
this.pressureBuffer = createFBO(gl, w, h, TEXTURE_FORMAT, FLOAT_TEXTURE_TYPE)
// Curl (vorticity)
this.curl = createFBO(gl, w, h, TEXTURE_FORMAT, FLOAT_TEXTURE_TYPE)
// Bloom chain (downsamples 1, 2, 4x)
if (this.bloomEnabled) {
this.bloom = {
down1: createFBO(gl, halfW, halfH, gl.RGBA, gl.UNSIGNED_BYTE),
down2: createFBO(gl, Math.max(1, Math.floor(halfW / 2)), Math.max(1, Math.floor(halfH / 2)), gl.RGBA, gl.UNSIGNED_BYTE),
up1: createFBO(gl, halfW, halfH, gl.RGBA, gl.UNSIGNED_BYTE),
}
}
console.log('[StemEngine][Fluid] FBOs allocated:', { w: w, h: h, quality: this.quality })
}
FluidSimulator.prototype.releaseFBOs = function () {
var gl = this.gl
deleteFBO(gl, this.velocity)
deleteFBO(gl, this.velocityBuffer)
deleteFBO(gl, this.dye)
deleteFBO(gl, this.dyeBuffer)
deleteFBO(gl, this.divergence)
deleteFBO(gl, this.pressure)
deleteFBO(gl, this.pressureBuffer)
deleteFBO(gl, this.curl)
if (this.bloom) {
deleteFBO(gl, this.bloom.down1)
deleteFBO(gl, this.bloom.down2)
deleteFBO(gl, this.bloom.up1)
}
}
// =========================================================================
// RENDER PASSES (IMPLEMENTATIONS)
// =========================================================================
/**
* Splat pass: inject audio-driven forces
*/
FluidSimulator.prototype.splat = function (x, y, force, color, radius) {
var gl = this.gl
var prog = this.programs.splat
if (!prog) return
gl.useProgram(prog)
gl.bindFramebuffer(gl.FRAMEBUFFER, this.velocityBuffer.fbo)
gl.viewport(0, 0, this.velocityBuffer.width, this.velocityBuffer.height)
// Bind input velocity
gl.uniform1i(gl.getUniformLocation(prog, 'uVelocity'), 0)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.velocity.texture)
// Set uniforms
gl.uniform2f(gl.getUniformLocation(prog, 'uSplatPos'), x, y)
gl.uniform1f(gl.getUniformLocation(prog, 'uSplatRadius'), radius || 15)
gl.uniform1f(gl.getUniformLocation(prog, 'uSplatForce'), force || 10)
gl.uniform1i(gl.getUniformLocation(prog, 'uSplatIsBeat'), force > 50 ? 1 : 0)
gl.uniform1f(gl.getUniformLocation(prog, 'uTurbulenceAmplitude'), color.g || 0)
this.renderFullscreenQuad(prog, this.velocityBuffer)
// Swap
var tmp = this.velocity
this.velocity = this.velocityBuffer
this.velocityBuffer = tmp
}
/**
* Curl pass: compute vorticity
*/
FluidSimulator.prototype.computeCurl = function () {
var gl = this.gl
var prog = this.programs.curl
if (!prog) return
gl.useProgram(prog)
gl.bindFramebuffer(gl.FRAMEBUFFER, this.curl.fbo)
gl.viewport(0, 0, this.curl.width, this.curl.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uVelocity'), 0)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.velocity.texture)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.velocity.width, 1.0 / this.velocity.height)
this.renderFullscreenQuad(prog, this.curl)
}
/**
* Divergence pass: compute incompressibility metric
*/
FluidSimulator.prototype.computeDivergence = function () {
var gl = this.gl
var prog = this.programs.divergence
if (!prog) return
gl.useProgram(prog)
gl.bindFramebuffer(gl.FRAMEBUFFER, this.divergence.fbo)
gl.viewport(0, 0, this.divergence.width, this.divergence.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uVelocity'), 0)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.velocity.texture)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.velocity.width, 1.0 / this.velocity.height)
this.renderFullscreenQuad(prog, this.divergence)
}
/**
* Pressure Jacobi iteration
*/
FluidSimulator.prototype.pressureJacobi = function () {
var gl = this.gl
var prog = this.programs.pressureJacobi
if (!prog) return
gl.useProgram(prog)
var alpha = -1.0
var rBeta = 0.25
for (var i = 0; i < this.pressureIterations; i++) {
gl.bindFramebuffer(gl.FRAMEBUFFER, this.pressureBuffer.fbo)
gl.viewport(0, 0, this.pressureBuffer.width, this.pressureBuffer.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uPressure'), 0)
gl.uniform1i(gl.getUniformLocation(prog, 'uDivergence'), 1)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.pressure.texture)
gl.activeTexture(gl.TEXTURE1)
gl.bindTexture(gl.TEXTURE_2D, this.divergence.texture)
gl.uniform1f(gl.getUniformLocation(prog, 'uAlpha'), alpha)
gl.uniform1f(gl.getUniformLocation(prog, 'uRBeta'), rBeta)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.pressure.width, 1.0 / this.pressure.height)
this.renderFullscreenQuad(prog, this.pressureBuffer)
// Swap
var tmp = this.pressure
this.pressure = this.pressureBuffer
this.pressureBuffer = tmp
}
}
/**
* Gradient subtract pass: Remove pressure from velocity
*/
FluidSimulator.prototype.gradientSubtract = function () {
var gl = this.gl
var prog = this.programs.gradientSubtract
if (!prog) return
gl.useProgram(prog)
gl.bindFramebuffer(gl.FRAMEBUFFER, this.velocityBuffer.fbo)
gl.viewport(0, 0, this.velocityBuffer.width, this.velocityBuffer.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uVelocity'), 0)
gl.uniform1i(gl.getUniformLocation(prog, 'uPressure'), 1)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.velocity.texture)
gl.activeTexture(gl.TEXTURE1)
gl.bindTexture(gl.TEXTURE_2D, this.pressure.texture)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.velocity.width, 1.0 / this.velocity.height)
gl.uniform1f(gl.getUniformLocation(prog, 'uGradientScale'), 1.0)
this.renderFullscreenQuad(prog, this.velocityBuffer)
// Swap
var tmp = this.velocity
this.velocity = this.velocityBuffer
this.velocityBuffer = tmp
}
/**
* Advection pass for velocity
*/
FluidSimulator.prototype.advectVelocity = function () {
var gl = this.gl
var prog = this.programs.advectVelocity
if (!prog) return
gl.useProgram(prog)
gl.bindFramebuffer(gl.FRAMEBUFFER, this.velocityBuffer.fbo)
gl.viewport(0, 0, this.velocityBuffer.width, this.velocityBuffer.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uVelocity'), 0)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.velocity.texture)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.velocity.width, 1.0 / this.velocity.height)
gl.uniform1f(gl.getUniformLocation(prog, 'uDt'), 0.01)
gl.uniform1f(gl.getUniformLocation(prog, 'uDecay'), 0.99)
this.renderFullscreenQuad(prog, this.velocityBuffer)
// Swap
var tmp = this.velocity
this.velocity = this.velocityBuffer
this.velocityBuffer = tmp
}
/**
* Advection pass for dye
*/
FluidSimulator.prototype.advectDye = function (audioFeatures) {
var gl = this.gl
var prog = this.programs.advectDye
if (!prog) return
var audio = audioFeatures || {}
var sub = getBand(audio, 'sub', audio.sub || 0)
var bass = getBand(audio, 'bass', audio.bass || 0)
var highMid = getBand(audio, 'highMid', audio.treble || 0)
var brilliance = getBand(audio, 'brilliance', audio.treble || 0)
var rms = clamp01(audio.rms || 0)
var audioColor = [
shaped(0.55 * bass + 0.45 * sub, 0.62),
shaped(rms, 0.75),
shaped(0.6 * highMid + 0.4 * brilliance, 0.65),
]
gl.useProgram(prog)
gl.bindFramebuffer(gl.FRAMEBUFFER, this.dyeBuffer.fbo)
gl.viewport(0, 0, this.dyeBuffer.width, this.dyeBuffer.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uDye'), 0)
gl.uniform1i(gl.getUniformLocation(prog, 'uVelocity'), 1)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.dye.texture)
gl.activeTexture(gl.TEXTURE1)
gl.bindTexture(gl.TEXTURE_2D, this.velocity.texture)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.dye.width, 1.0 / this.dye.height)
gl.uniform1f(gl.getUniformLocation(prog, 'uDt'), 0.01)
gl.uniform1f(gl.getUniformLocation(prog, 'uDecay'), 0.98)
gl.uniform3f(gl.getUniformLocation(prog, 'uAudioColor'), audioColor[0], audioColor[1], audioColor[2])
this.renderFullscreenQuad(prog, this.dyeBuffer)
// Swap
var tmp = this.dye
this.dye = this.dyeBuffer
this.dyeBuffer = tmp
}
/**
* Bloom downsample pass
*/
FluidSimulator.prototype.bloomDownsample = function () {
if (!this.bloomEnabled || !this.bloom) return
var gl = this.gl
var prog = this.programs.bloomDownsample
if (!prog) return
gl.useProgram(prog)
// Down to 1/2 resolution
gl.bindFramebuffer(gl.FRAMEBUFFER, this.bloom.down1.fbo)
gl.viewport(0, 0, this.bloom.down1.width, this.bloom.down1.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uSource'), 0)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.dye.texture)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.dye.width, 1.0 / this.dye.height)
this.renderFullscreenQuad(prog, this.bloom.down1)
// Down to 1/4 resolution
gl.bindFramebuffer(gl.FRAMEBUFFER, this.bloom.down2.fbo)
gl.viewport(0, 0, this.bloom.down2.width, this.bloom.down2.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uSource'), 0)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.bloom.down1.texture)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.bloom.down1.width, 1.0 / this.bloom.down1.height)
this.renderFullscreenQuad(prog, this.bloom.down2)
// Upsample back to 1/2 resolution
gl.bindFramebuffer(gl.FRAMEBUFFER, this.bloom.up1.fbo)
gl.viewport(0, 0, this.bloom.up1.width, this.bloom.up1.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uSource'), 0)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.bloom.down2.texture)
gl.uniform2f(gl.getUniformLocation(prog, 'uInvSize'), 1.0 / this.bloom.down2.width, 1.0 / this.bloom.down2.height)
this.renderFullscreenQuad(prog, this.bloom.up1)
}
/**
* Bloom upsample + composite pass
*/
FluidSimulator.prototype.bloomComposite = function (audioFeatures) {
if (!this.bloomEnabled || !this.bloom) return
var gl = this.gl
var prog = this.programs.bloomComposite
if (!prog) return
var audio = audioFeatures || {}
var rms = clamp01(audio.rms || 0)
var sparkle = getBand(audio, 'brilliance', audio.treble || 0)
var bloomIntensity = 0.45 + shaped(rms, 0.8) * 1.35 + shaped(sparkle, 0.65) * 0.45
gl.useProgram(prog)
gl.bindFramebuffer(gl.FRAMEBUFFER, this.dye.fbo)
gl.viewport(0, 0, this.dye.width, this.dye.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uFullRes'), 0)
gl.uniform1i(gl.getUniformLocation(prog, 'uBloomUp'), 1)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.dye.texture)
gl.activeTexture(gl.TEXTURE1)
gl.bindTexture(gl.TEXTURE_2D, this.bloom.up1.texture)
gl.uniform1f(gl.getUniformLocation(prog, 'uBloomIntensity'), bloomIntensity)
this.renderFullscreenQuad(prog, this.dye)
}
/**
* Final composite: render dye to screen
*/
FluidSimulator.prototype.composite = function () {
var gl = this.gl
var prog = this.programs.composite
if (!prog) return
gl.useProgram(prog)
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.viewport(0, 0, this.width, this.height)
gl.uniform1i(gl.getUniformLocation(prog, 'uDye'), 0)
gl.uniform1i(gl.getUniformLocation(prog, 'uCurl'), 1)
gl.uniform1i(gl.getUniformLocation(prog, 'uVelocity'), 2)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, this.dye.texture)
gl.activeTexture(gl.TEXTURE1)
gl.bindTexture(gl.TEXTURE_2D, this.curl.texture)
gl.activeTexture(gl.TEXTURE2)
gl.bindTexture(gl.TEXTURE_2D, this.velocity.texture)
gl.uniform1i(gl.getUniformLocation(prog, 'uShowVelocity'), 0)
gl.uniform1i(gl.getUniformLocation(prog, 'uShowCurl'), 0)
this.renderFullscreenQuad(prog, null)
}
// =========================================================================
// MAIN LOOP & DISPATCH
// =========================================================================
FluidSimulator.prototype.step = function (audioFeatures) {
if (this.paused || !this.velocity) return
var gl = this.gl
// Richer band-aware signal shaping for better dynamic range.
var bassBand = getBand(audioFeatures, 'bass', audioFeatures?.bass || 0)
var subBand = getBand(audioFeatures, 'sub', audioFeatures?.sub || 0)
var lowMidBand = getBand(audioFeatures, 'lowMid', audioFeatures?.bass || 0)
var highMidBand = getBand(audioFeatures, 'highMid', audioFeatures?.treble || 0)
var brillianceBand = getBand(audioFeatures, 'brilliance', audioFeatures?.treble || 0)
var bass = shaped(0.55 * bassBand + 0.45 * subBand, 0.62)
var beat = Boolean(audioFeatures?.beat)
var treble = shaped(0.45 * highMidBand + 0.55 * brillianceBand, 0.68)
var rms = shaped(clamp01(audioFeatures?.rms || 0), 0.78)
var beatPhase = clamp01(audioFeatures?.beatPhase || 0)
var drumInput = audioFeatures?.drumIntensity != null ? Number(audioFeatures.drumIntensity) : (0.7 * bassBand + 0.3 * lowMidBand)
var drumIntensity = shaped(clamp01(drumInput), 0.72)
try {
// --- PIPELINE ---
// 1. Splat pass (inject audio-driven forces)
var phaseBoost = 0.65 + 0.35 * Math.sin(beatPhase * Math.PI * 2)
var splatRadius = SPLAT_RADIUS_BASE + (drumIntensity * phaseBoost * (SPLAT_RADIUS_MAX - SPLAT_RADIUS_BASE))
var splatForce = drumIntensity * (40 + phaseBoost * 20)
this.splat(this.width / 2, this.height / 2, splatForce, { r: bass, g: treble, b: rms, a: 1 }, splatRadius)
if (beat) {
// Impulse on beat detection
var beatImpulse = 90 + drumIntensity * 30
this.splat(this.width / 2, this.height / 2, beatImpulse, { r: 1, g: 1, b: 1, a: 1 }, 30)
}
// 2. Vorticity confinement
this.computeCurl()
// 3. Pressure solve
this.computeDivergence()
this.pressureJacobi()
this.gradientSubtract()
// 4. Advection
this.advectVelocity()
this.advectDye(audioFeatures)
// 5. Bloom (optional)
if (this.bloomEnabled) {
this.bloomDownsample()
this.bloomComposite(audioFeatures)
}
// 6. Composite to screen
this.composite()
} catch (e) {
console.error('[StemEngine][Fluid] Error in step():', e)
window.__stemengineEmitError__('STEP_ERROR', String(e))
}
}
FluidSimulator.prototype.dispose = function () {
this.stop()
if (this.quadVBO) {
this.gl.deleteBuffer(this.quadVBO)
this.quadVBO = null
}
this.releaseFBOs()
for (var key in this.programs) {
if (this.programs[key]) {
this.gl.deleteProgram(this.programs[key])
}
}
}
// =========================================================================
// GLOBAL EXPORT & REGISTRATION
// =========================================================================
window.__StemEngineFluidRenderGraph__ = FluidSimulator
console.log('[StemEngine][Fluid] Render graph module loaded')
})()