mirror of
https://github.com/DavidHDev/vue-bits.git
synced 2026-03-07 06:29:30 -07:00
1 line
40 KiB
JSON
1 line
40 KiB
JSON
{"name":"SplashCursor","title":"SplashCursor","description":"Liquid splash burst at cursor with curling ripples and waves.","type":"registry:component","add":"when-added","files":[{"type":"registry:component","role":"file","content":"<template>\n <div class=\"fixed top-0 left-0 z-50 pointer-events-none w-full h-full\">\n <canvas ref=\"canvasRef\" id=\"fluid\" class=\"w-screen h-screen block\"></canvas>\n </div>\n</template>\n\n<script setup lang=\"ts\">\nimport { onMounted, withDefaults, useTemplateRef } from 'vue';\n\n/* ---------- types ---------- */\ninterface ColorRGB {\n r: number;\n g: number;\n b: number;\n}\n\ninterface SplashCursorProps {\n SIM_RESOLUTION?: number;\n DYE_RESOLUTION?: number;\n CAPTURE_RESOLUTION?: number;\n DENSITY_DISSIPATION?: number;\n VELOCITY_DISSIPATION?: number;\n PRESSURE?: number;\n PRESSURE_ITERATIONS?: number;\n CURL?: number;\n SPLAT_RADIUS?: number;\n SPLAT_FORCE?: number;\n SHADING?: boolean;\n COLOR_UPDATE_SPEED?: number;\n BACK_COLOR?: ColorRGB;\n TRANSPARENT?: boolean;\n}\n\n/* ---------- props & defaults ---------- */\nconst props = withDefaults(defineProps<SplashCursorProps>(), {\n SIM_RESOLUTION: 128,\n DYE_RESOLUTION: 1440,\n CAPTURE_RESOLUTION: 512,\n DENSITY_DISSIPATION: 3.5,\n VELOCITY_DISSIPATION: 2,\n PRESSURE: 0.1,\n PRESSURE_ITERATIONS: 20,\n CURL: 3,\n SPLAT_RADIUS: 0.2,\n SPLAT_FORCE: 6000,\n SHADING: true,\n COLOR_UPDATE_SPEED: 10,\n BACK_COLOR: () => ({ r: 0.5, g: 0, b: 0 }),\n TRANSPARENT: true\n});\n\n/* ---------- refs ---------- */\nconst canvasRef = useTemplateRef<HTMLCanvasElement>('canvasRef');\n\n/* ---------- helper types ---------- */\ninterface Pointer {\n id: number;\n texcoordX: number;\n texcoordY: number;\n prevTexcoordX: number;\n prevTexcoordY: number;\n deltaX: number;\n deltaY: number;\n down: boolean;\n moved: boolean;\n color: ColorRGB;\n}\n\nfunction pointerPrototype(): Pointer {\n return {\n id: -1,\n texcoordX: 0,\n texcoordY: 0,\n prevTexcoordX: 0,\n prevTexcoordY: 0,\n deltaX: 0,\n deltaY: 0,\n down: false,\n moved: false,\n color: { r: 0, g: 0, b: 0 }\n };\n}\n\n/* ---------- main logic ---------- */\nonMounted(() => {\n const canvas = canvasRef.value;\n if (!canvas) return;\n\n const pointers: Pointer[] = [pointerPrototype()];\n\n const config = {\n SIM_RESOLUTION: props.SIM_RESOLUTION!,\n DYE_RESOLUTION: props.DYE_RESOLUTION!,\n CAPTURE_RESOLUTION: props.CAPTURE_RESOLUTION!,\n DENSITY_DISSIPATION: props.DENSITY_DISSIPATION!,\n VELOCITY_DISSIPATION: props.VELOCITY_DISSIPATION!,\n PRESSURE: props.PRESSURE!,\n PRESSURE_ITERATIONS: props.PRESSURE_ITERATIONS!,\n CURL: props.CURL!,\n SPLAT_RADIUS: props.SPLAT_RADIUS!,\n SPLAT_FORCE: props.SPLAT_FORCE!,\n SHADING: props.SHADING,\n COLOR_UPDATE_SPEED: props.COLOR_UPDATE_SPEED!,\n PAUSED: false,\n BACK_COLOR: props.BACK_COLOR,\n TRANSPARENT: props.TRANSPARENT\n };\n\n /* ---------- WebGL context helpers ---------- */\n const { gl, ext } = getWebGLContext(canvas);\n if (!gl || !ext) return;\n\n if (!ext.supportLinearFiltering) {\n config.DYE_RESOLUTION = 256;\n config.SHADING = false;\n }\n\n function getWebGLContext(canvasEl: HTMLCanvasElement) {\n const params = {\n alpha: true,\n depth: false,\n stencil: false,\n antialias: false,\n preserveDrawingBuffer: false\n };\n\n let gl = canvasEl.getContext('webgl2', params) as WebGL2RenderingContext | null;\n\n if (!gl) {\n gl = (canvasEl.getContext('webgl', params) ||\n canvasEl.getContext('experimental-webgl', params)) as WebGL2RenderingContext | null;\n }\n\n if (!gl) {\n throw new Error('Unable to initialize WebGL.');\n }\n\n const isWebGL2 = 'drawBuffers' in gl;\n\n let supportLinearFiltering = false;\n let halfFloat: OES_texture_half_float | null = null;\n\n if (isWebGL2) {\n (gl as WebGL2RenderingContext).getExtension('EXT_color_buffer_float');\n supportLinearFiltering = !!(gl as WebGL2RenderingContext).getExtension('OES_texture_float_linear');\n } else {\n halfFloat = gl.getExtension('OES_texture_half_float');\n supportLinearFiltering = !!gl.getExtension('OES_texture_half_float_linear');\n }\n\n gl.clearColor(0, 0, 0, 1);\n\n const halfFloatTexType = isWebGL2\n ? (gl as WebGL2RenderingContext).HALF_FLOAT\n : (halfFloat && (halfFloat as OES_texture_half_float).HALF_FLOAT_OES) || 0;\n\n let formatRGBA: { internalFormat: number; format: number } | null;\n let formatRG: { internalFormat: number; format: number } | null;\n let formatR: { internalFormat: number; format: number } | null;\n\n if (isWebGL2) {\n formatRGBA = getSupportedFormat(gl, (gl as WebGL2RenderingContext).RGBA16F, gl.RGBA, halfFloatTexType);\n formatRG = getSupportedFormat(\n gl,\n (gl as WebGL2RenderingContext).RG16F,\n (gl as WebGL2RenderingContext).RG,\n halfFloatTexType\n );\n formatR = getSupportedFormat(\n gl,\n (gl as WebGL2RenderingContext).R16F,\n (gl as WebGL2RenderingContext).RED,\n halfFloatTexType\n );\n } else {\n formatRGBA = getSupportedFormat(gl, gl.RGBA, gl.RGBA, halfFloatTexType);\n formatRG = getSupportedFormat(gl, gl.RGBA, gl.RGBA, halfFloatTexType);\n formatR = getSupportedFormat(gl, gl.RGBA, gl.RGBA, halfFloatTexType);\n }\n\n return {\n gl,\n ext: {\n formatRGBA,\n formatRG,\n formatR,\n halfFloatTexType,\n supportLinearFiltering\n }\n };\n }\n\n function getSupportedFormat(\n gl: WebGLRenderingContext | WebGL2RenderingContext,\n internalFormat: number,\n format: number,\n type: number\n ): { internalFormat: number; format: number } | null {\n if (!supportRenderTextureFormat(gl, internalFormat, format, type)) {\n if ('drawBuffers' in gl) {\n const gl2 = gl as WebGL2RenderingContext;\n switch (internalFormat) {\n case gl2.R16F:\n return getSupportedFormat(gl2, gl2.RG16F, gl2.RG, type);\n case gl2.RG16F:\n return getSupportedFormat(gl2, gl2.RGBA16F, gl2.RGBA, type);\n default:\n return null;\n }\n }\n return null;\n }\n return { internalFormat, format };\n }\n\n function supportRenderTextureFormat(\n gl: WebGLRenderingContext | WebGL2RenderingContext,\n internalFormat: number,\n format: number,\n type: number\n ) {\n const texture = gl.createTexture();\n if (!texture) return false;\n\n gl.bindTexture(gl.TEXTURE_2D, texture);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, 4, 4, 0, format, type, null);\n\n const fbo = gl.createFramebuffer();\n if (!fbo) return false;\n\n gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);\n const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);\n return status === gl.FRAMEBUFFER_COMPLETE;\n }\n\n function hashCode(s: string) {\n if (!s.length) return 0;\n let hash = 0;\n for (let i = 0; i < s.length; i++) {\n hash = (hash << 5) - hash + s.charCodeAt(i);\n hash |= 0;\n }\n return hash;\n }\n\n function addKeywords(source: string, keywords: string[] | null) {\n if (!keywords) return source;\n let keywordsString = '';\n for (const keyword of keywords) {\n keywordsString += `#define ${keyword}\\n`;\n }\n return keywordsString + source;\n }\n\n function compileShader(type: number, source: string, keywords: string[] | null = null): WebGLShader | null {\n const shaderSource = addKeywords(source, keywords);\n const shader = gl.createShader(type);\n if (!shader) return null;\n gl.shaderSource(shader, shaderSource);\n gl.compileShader(shader);\n if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {\n console.trace(gl.getShaderInfoLog(shader));\n }\n return shader;\n }\n\n function createProgram(vertexShader: WebGLShader | null, fragmentShader: WebGLShader | null): WebGLProgram | null {\n if (!vertexShader || !fragmentShader) return null;\n const program = gl.createProgram();\n if (!program) return null;\n gl.attachShader(program, vertexShader);\n gl.attachShader(program, fragmentShader);\n gl.linkProgram(program);\n if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {\n console.trace(gl.getProgramInfoLog(program));\n }\n return program;\n }\n\n function getUniforms(program: WebGLProgram) {\n const uniforms: Record<string, WebGLUniformLocation | null> = {};\n const uniformCount = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);\n for (let i = 0; i < uniformCount; i++) {\n const uniformInfo = gl.getActiveUniform(program, i);\n if (uniformInfo) {\n uniforms[uniformInfo.name] = gl.getUniformLocation(program, uniformInfo.name);\n }\n }\n return uniforms;\n }\n\n class Program {\n program: WebGLProgram | null;\n uniforms: Record<string, WebGLUniformLocation | null>;\n\n constructor(vertexShader: WebGLShader | null, fragmentShader: WebGLShader | null) {\n this.program = createProgram(vertexShader, fragmentShader);\n this.uniforms = this.program ? getUniforms(this.program) : {};\n }\n\n bind() {\n if (this.program) gl.useProgram(this.program);\n }\n }\n\n class Material {\n vertexShader: WebGLShader | null;\n fragmentShaderSource: string;\n programs: Record<number, WebGLProgram | null>;\n activeProgram: WebGLProgram | null;\n uniforms: Record<string, WebGLUniformLocation | null>;\n\n constructor(vertexShader: WebGLShader | null, fragmentShaderSource: string) {\n this.vertexShader = vertexShader;\n this.fragmentShaderSource = fragmentShaderSource;\n this.programs = {};\n this.activeProgram = null;\n this.uniforms = {};\n }\n\n setKeywords(keywords: string[]) {\n let hash = 0;\n for (const kw of keywords) {\n hash += hashCode(kw);\n }\n let program = this.programs[hash];\n if (program == null) {\n const fragmentShader = compileShader(gl.FRAGMENT_SHADER, this.fragmentShaderSource, keywords);\n program = createProgram(this.vertexShader, fragmentShader);\n this.programs[hash] = program;\n }\n if (program === this.activeProgram) return;\n if (program) {\n this.uniforms = getUniforms(program);\n }\n this.activeProgram = program;\n }\n\n bind() {\n if (this.activeProgram) {\n gl.useProgram(this.activeProgram);\n }\n }\n }\n\n /* ---------- shaders ---------- */\n const baseVertexShader = compileShader(\n gl.VERTEX_SHADER,\n `\n precision highp float;\n attribute vec2 aPosition;\n varying vec2 vUv;\n varying vec2 vL;\n varying vec2 vR;\n varying vec2 vT;\n varying vec2 vB;\n uniform vec2 texelSize;\n void main () {\n vUv = aPosition * 0.5 + 0.5;\n vL = vUv - vec2(texelSize.x, 0.0);\n vR = vUv + vec2(texelSize.x, 0.0);\n vT = vUv + vec2(0.0, texelSize.y);\n vB = vUv - vec2(0.0, texelSize.y);\n gl_Position = vec4(aPosition, 0.0, 1.0);\n }\n `\n );\n\n const copyShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision mediump float;\n precision mediump sampler2D;\n varying highp vec2 vUv;\n uniform sampler2D uTexture;\n void main () {\n gl_FragColor = texture2D(uTexture, vUv);\n }\n `\n );\n\n const clearShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision mediump float;\n precision mediump sampler2D;\n varying highp vec2 vUv;\n uniform sampler2D uTexture;\n uniform float value;\n void main () {\n gl_FragColor = value * texture2D(uTexture, vUv);\n }\n `\n );\n\n const displayShaderSource = `\n precision highp float;\n precision highp sampler2D;\n varying vec2 vUv;\n varying vec2 vL;\n varying vec2 vR;\n varying vec2 vT;\n varying vec2 vB;\n uniform sampler2D uTexture;\n uniform sampler2D uDithering;\n uniform vec2 ditherScale;\n uniform vec2 texelSize;\n vec3 linearToGamma (vec3 color) {\n color = max(color, vec3(0));\n return max(1.055 * pow(color, vec3(0.416666667)) - 0.055, vec3(0));\n }\n void main () {\n vec3 c = texture2D(uTexture, vUv).rgb;\n #ifdef SHADING\n vec3 lc = texture2D(uTexture, vL).rgb;\n vec3 rc = texture2D(uTexture, vR).rgb;\n vec3 tc = texture2D(uTexture, vT).rgb;\n vec3 bc = texture2D(uTexture, vB).rgb;\n float dx = length(rc) - length(lc);\n float dy = length(tc) - length(bc);\n vec3 n = normalize(vec3(dx, dy, length(texelSize)));\n vec3 l = vec3(0.0, 0.0, 1.0);\n float diffuse = clamp(dot(n, l) + 0.7, 0.7, 1.0);\n c *= diffuse;\n #endif\n float a = max(c.r, max(c.g, c.b));\n gl_FragColor = vec4(c, a);\n }\n `;\n\n const splatShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision highp float;\n precision highp sampler2D;\n varying vec2 vUv;\n uniform sampler2D uTarget;\n uniform float aspectRatio;\n uniform vec3 color;\n uniform vec2 point;\n uniform float radius;\n void main () {\n vec2 p = vUv - point.xy;\n p.x *= aspectRatio;\n vec3 splat = exp(-dot(p, p) / radius) * color;\n vec3 base = texture2D(uTarget, vUv).xyz;\n gl_FragColor = vec4(base + splat, 1.0);\n }\n `\n );\n\n const advectionShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision highp float;\n precision highp sampler2D;\n varying vec2 vUv;\n uniform sampler2D uVelocity;\n uniform sampler2D uSource;\n uniform vec2 texelSize;\n uniform vec2 dyeTexelSize;\n uniform float dt;\n uniform float dissipation;\n vec4 bilerp (sampler2D sam, vec2 uv, vec2 tsize) {\n vec2 st = uv / tsize - 0.5;\n vec2 iuv = floor(st);\n vec2 fuv = fract(st);\n vec4 a = texture2D(sam, (iuv + vec2(0.5, 0.5)) * tsize);\n vec4 b = texture2D(sam, (iuv + vec2(1.5, 0.5)) * tsize);\n vec4 c = texture2D(sam, (iuv + vec2(0.5, 1.5)) * tsize);\n vec4 d = texture2D(sam, (iuv + vec2(1.5, 1.5)) * tsize);\n return mix(mix(a, b, fuv.x), mix(c, d, fuv.x), fuv.y);\n }\n void main () {\n #ifdef MANUAL_FILTERING\n vec2 coord = vUv - dt * bilerp(uVelocity, vUv, texelSize).xy * texelSize;\n vec4 result = bilerp(uSource, coord, dyeTexelSize);\n #else\n vec2 coord = vUv - dt * texture2D(uVelocity, vUv).xy * texelSize;\n vec4 result = texture2D(uSource, coord);\n #endif\n float decay = 1.0 + dissipation * dt;\n gl_FragColor = result / decay;\n }\n `,\n ext.supportLinearFiltering ? null : ['MANUAL_FILTERING']\n );\n\n const divergenceShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision mediump float;\n precision mediump sampler2D;\n varying highp vec2 vUv;\n varying highp vec2 vL;\n varying highp vec2 vR;\n varying highp vec2 vT;\n varying highp vec2 vB;\n uniform sampler2D uVelocity;\n void main () {\n float L = texture2D(uVelocity, vL).x;\n float R = texture2D(uVelocity, vR).x;\n float T = texture2D(uVelocity, vT).y;\n float B = texture2D(uVelocity, vB).y;\n vec2 C = texture2D(uVelocity, vUv).xy;\n if (vL.x < 0.0) { L = -C.x; }\n if (vR.x > 1.0) { R = -C.x; }\n if (vT.y > 1.0) { T = -C.y; }\n if (vB.y < 0.0) { B = -C.y; }\n float div = 0.5 * (R - L + T - B);\n gl_FragColor = vec4(div, 0.0, 0.0, 1.0);\n }\n `\n );\n\n const curlShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision mediump float;\n precision mediump sampler2D;\n varying highp vec2 vUv;\n varying highp vec2 vL;\n varying highp vec2 vR;\n varying highp vec2 vT;\n varying highp vec2 vB;\n uniform sampler2D uVelocity;\n void main () {\n float L = texture2D(uVelocity, vL).y;\n float R = texture2D(uVelocity, vR).y;\n float T = texture2D(uVelocity, vT).x;\n float B = texture2D(uVelocity, vB).x;\n float vorticity = R - L - T + B;\n gl_FragColor = vec4(0.5 * vorticity, 0.0, 0.0, 1.0);\n }\n `\n );\n\n const vorticityShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision highp float;\n precision highp sampler2D;\n varying vec2 vUv;\n varying vec2 vL;\n varying vec2 vR;\n varying vec2 vT;\n varying vec2 vB;\n uniform sampler2D uVelocity;\n uniform sampler2D uCurl;\n uniform float curl;\n uniform float dt;\n void main () {\n float L = texture2D(uCurl, vL).x;\n float R = texture2D(uCurl, vR).x;\n float T = texture2D(uCurl, vT).x;\n float B = texture2D(uCurl, vB).x;\n float C = texture2D(uCurl, vUv).x;\n vec2 force = 0.5 * vec2(abs(T) - abs(B), abs(R) - abs(L));\n force /= length(force) + 0.0001;\n force *= curl * C;\n force.y *= -1.0;\n vec2 velocity = texture2D(uVelocity, vUv).xy;\n velocity += force * dt;\n velocity = min(max(velocity, -1000.0), 1000.0);\n gl_FragColor = vec4(velocity, 0.0, 1.0);\n }\n `\n );\n\n const pressureShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision mediump float;\n precision mediump sampler2D;\n varying highp vec2 vUv;\n varying highp vec2 vL;\n varying highp vec2 vR;\n varying highp vec2 vT;\n varying highp vec2 vB;\n uniform sampler2D uPressure;\n uniform sampler2D uDivergence;\n void main () {\n float L = texture2D(uPressure, vL).x;\n float R = texture2D(uPressure, vR).x;\n float T = texture2D(uPressure, vT).x;\n float B = texture2D(uPressure, vB).x;\n float C = texture2D(uPressure, vUv).x;\n float divergence = texture2D(uDivergence, vUv).x;\n float pressure = (L + R + B + T - divergence) * 0.25;\n gl_FragColor = vec4(pressure, 0.0, 0.0, 1.0);\n }\n `\n );\n\n const gradientSubtractShader = compileShader(\n gl.FRAGMENT_SHADER,\n `\n precision mediump float;\n precision mediump sampler2D;\n varying highp vec2 vUv;\n varying highp vec2 vL;\n varying highp vec2 vR;\n varying highp vec2 vT;\n varying highp vec2 vB;\n uniform sampler2D uPressure;\n uniform sampler2D uVelocity;\n void main () {\n float L = texture2D(uPressure, vL).x;\n float R = texture2D(uPressure, vR).x;\n float T = texture2D(uPressure, vT).x;\n float B = texture2D(uPressure, vB).x;\n vec2 velocity = texture2D(uVelocity, vUv).xy;\n velocity.xy -= vec2(R - L, T - B);\n gl_FragColor = vec4(velocity, 0.0, 1.0);\n }\n `\n );\n\n /* ---------- geometry helper ---------- */\n const blit = (() => {\n const buffer = gl.createBuffer()!;\n gl.bindBuffer(gl.ARRAY_BUFFER, buffer);\n gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, -1, 1, 1, 1, 1, -1]), gl.STATIC_DRAW);\n const elemBuffer = gl.createBuffer()!;\n gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elemBuffer);\n gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0, 1, 2, 0, 2, 3]), gl.STATIC_DRAW);\n gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n gl.enableVertexAttribArray(0);\n return (target: FBO | null, doClear = false) => {\n if (!gl) return;\n if (!target) {\n gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);\n gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n } else {\n gl.viewport(0, 0, target.width, target.height);\n gl.bindFramebuffer(gl.FRAMEBUFFER, target.fbo);\n }\n if (doClear) {\n gl.clearColor(0, 0, 0, 1);\n gl.clear(gl.COLOR_BUFFER_BIT);\n }\n gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);\n };\n })();\n\n /* ---------- frame-buffer object helpers ---------- */\n interface FBO {\n texture: WebGLTexture;\n fbo: WebGLFramebuffer;\n width: number;\n height: number;\n texelSizeX: number;\n texelSizeY: number;\n attach: (id: number) => number;\n }\n\n interface DoubleFBO {\n width: number;\n height: number;\n texelSizeX: number;\n texelSizeY: number;\n read: FBO;\n write: FBO;\n swap: () => void;\n }\n\n let dye: DoubleFBO;\n let velocity: DoubleFBO;\n let divergence: FBO;\n let curl: FBO;\n let pressure: DoubleFBO;\n\n const copyProgram = new Program(baseVertexShader, copyShader);\n const clearProgram = new Program(baseVertexShader, clearShader);\n const splatProgram = new Program(baseVertexShader, splatShader);\n const advectionProgram = new Program(baseVertexShader, advectionShader);\n const divergenceProgram = new Program(baseVertexShader, divergenceShader);\n const curlProgram = new Program(baseVertexShader, curlShader);\n const vorticityProgram = new Program(baseVertexShader, vorticityShader);\n const pressureProgram = new Program(baseVertexShader, pressureShader);\n const gradienSubtractProgram = new Program(baseVertexShader, gradientSubtractShader);\n const displayMaterial = new Material(baseVertexShader, displayShaderSource);\n\n function createFBO(w: number, h: number, internalFormat: number, format: number, type: number, param: number): FBO {\n gl.activeTexture(gl.TEXTURE0);\n const texture = gl.createTexture()!;\n gl.bindTexture(gl.TEXTURE_2D, texture);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, param);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, param);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, w, h, 0, format, type, null);\n const fbo = gl.createFramebuffer()!;\n gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);\n gl.viewport(0, 0, w, h);\n gl.clear(gl.COLOR_BUFFER_BIT);\n const texelSizeX = 1 / w;\n const texelSizeY = 1 / h;\n return {\n texture,\n fbo,\n width: w,\n height: h,\n texelSizeX,\n texelSizeY,\n attach(id: number) {\n gl.activeTexture(gl.TEXTURE0 + id);\n gl.bindTexture(gl.TEXTURE_2D, texture);\n return id;\n }\n };\n }\n\n function createDoubleFBO(\n w: number,\n h: number,\n internalFormat: number,\n format: number,\n type: number,\n param: number\n ): DoubleFBO {\n const fbo1 = createFBO(w, h, internalFormat, format, type, param);\n const fbo2 = createFBO(w, h, internalFormat, format, type, param);\n return {\n width: w,\n height: h,\n texelSizeX: fbo1.texelSizeX,\n texelSizeY: fbo1.texelSizeY,\n read: fbo1,\n write: fbo2,\n swap() {\n const tmp = this.read;\n this.read = this.write;\n this.write = tmp;\n }\n };\n }\n\n function resizeFBO(\n target: FBO,\n w: number,\n h: number,\n internalFormat: number,\n format: number,\n type: number,\n param: number\n ) {\n const newFBO = createFBO(w, h, internalFormat, format, type, param);\n copyProgram.bind();\n if (copyProgram.uniforms.uTexture) gl.uniform1i(copyProgram.uniforms.uTexture, target.attach(0));\n blit(newFBO, false);\n return newFBO;\n }\n\n function resizeDoubleFBO(\n target: DoubleFBO,\n w: number,\n h: number,\n internalFormat: number,\n format: number,\n type: number,\n param: number\n ) {\n if (target.width === w && target.height === h) return target;\n target.read = resizeFBO(target.read, w, h, internalFormat, format, type, param);\n target.write = createFBO(w, h, internalFormat, format, type, param);\n target.width = w;\n target.height = h;\n target.texelSizeX = 1 / w;\n target.texelSizeY = 1 / h;\n return target;\n }\n\n function initFramebuffers() {\n const simRes = getResolution(config.SIM_RESOLUTION!);\n const dyeRes = getResolution(config.DYE_RESOLUTION!);\n const texType = ext.halfFloatTexType;\n const rgba = ext.formatRGBA;\n const rg = ext.formatRG;\n const r = ext.formatR;\n const filtering = ext.supportLinearFiltering ? gl.LINEAR : gl.NEAREST;\n gl.disable(gl.BLEND);\n\n if (!rgba) return;\n\n if (!dye) {\n dye = createDoubleFBO(dyeRes.width, dyeRes.height, rgba.internalFormat, rgba.format, texType, filtering);\n } else {\n dye = resizeDoubleFBO(dye, dyeRes.width, dyeRes.height, rgba.internalFormat, rgba.format, texType, filtering);\n }\n\n if (!rg) return;\n\n if (!velocity) {\n velocity = createDoubleFBO(simRes.width, simRes.height, rg.internalFormat, rg.format, texType, filtering);\n } else {\n velocity = resizeDoubleFBO(\n velocity,\n simRes.width,\n simRes.height,\n rg.internalFormat,\n rg.format,\n texType,\n filtering\n );\n }\n\n if (!r) return;\n\n divergence = createFBO(simRes.width, simRes.height, r.internalFormat, r.format, texType, gl.NEAREST);\n curl = createFBO(simRes.width, simRes.height, r.internalFormat, r.format, texType, gl.NEAREST);\n pressure = createDoubleFBO(simRes.width, simRes.height, r.internalFormat, r.format, texType, gl.NEAREST);\n }\n\n function updateKeywords() {\n const displayKeywords: string[] = [];\n if (config.SHADING) displayKeywords.push('SHADING');\n displayMaterial.setKeywords(displayKeywords);\n }\n\n function getResolution(resolution: number) {\n const w = gl.drawingBufferWidth;\n const h = gl.drawingBufferHeight;\n const aspectRatio = w / h;\n const aspect = aspectRatio < 1 ? 1 / aspectRatio : aspectRatio;\n const min = Math.round(resolution);\n const max = Math.round(resolution * aspect);\n if (w > h) {\n return { width: max, height: min };\n }\n return { width: min, height: max };\n }\n\n function scaleByPixelRatio(input: number) {\n const pixelRatio = window.devicePixelRatio || 1;\n return Math.floor(input * pixelRatio);\n }\n\n updateKeywords();\n initFramebuffers();\n\n let lastUpdateTime = Date.now();\n let colorUpdateTimer = 0.0;\n\n function updateFrame() {\n const dt = calcDeltaTime();\n if (resizeCanvas()) initFramebuffers();\n updateColors(dt);\n applyInputs();\n step(dt);\n render(null);\n requestAnimationFrame(updateFrame);\n }\n\n function calcDeltaTime() {\n const now = Date.now();\n let dt = (now - lastUpdateTime) / 1000;\n dt = Math.min(dt, 0.016666);\n lastUpdateTime = now;\n return dt;\n }\n\n function resizeCanvas() {\n const width = scaleByPixelRatio(canvas!.clientWidth);\n const height = scaleByPixelRatio(canvas!.clientHeight);\n if (canvas!.width !== width || canvas!.height !== height) {\n canvas!.width = width;\n canvas!.height = height;\n return true;\n }\n return false;\n }\n\n function updateColors(dt: number) {\n colorUpdateTimer += dt * config.COLOR_UPDATE_SPEED;\n if (colorUpdateTimer >= 1) {\n colorUpdateTimer = wrap(colorUpdateTimer, 0, 1);\n pointers.forEach(p => {\n p.color = generateColor();\n });\n }\n }\n\n function applyInputs() {\n for (const p of pointers) {\n if (p.moved) {\n p.moved = false;\n splatPointer(p);\n }\n }\n }\n\n /* ---------- simulation step ---------- */\n function step(dt: number) {\n gl.disable(gl.BLEND);\n\n curlProgram.bind();\n if (curlProgram.uniforms.texelSize) {\n gl.uniform2f(curlProgram.uniforms.texelSize, velocity.texelSizeX, velocity.texelSizeY);\n }\n if (curlProgram.uniforms.uVelocity) {\n gl.uniform1i(curlProgram.uniforms.uVelocity, velocity.read.attach(0));\n }\n blit(curl);\n\n vorticityProgram.bind();\n if (vorticityProgram.uniforms.texelSize) {\n gl.uniform2f(vorticityProgram.uniforms.texelSize, velocity.texelSizeX, velocity.texelSizeY);\n }\n if (vorticityProgram.uniforms.uVelocity) {\n gl.uniform1i(vorticityProgram.uniforms.uVelocity, velocity.read.attach(0));\n }\n if (vorticityProgram.uniforms.uCurl) {\n gl.uniform1i(vorticityProgram.uniforms.uCurl, curl.attach(1));\n }\n if (vorticityProgram.uniforms.curl) {\n gl.uniform1f(vorticityProgram.uniforms.curl, config.CURL);\n }\n if (vorticityProgram.uniforms.dt) {\n gl.uniform1f(vorticityProgram.uniforms.dt, dt);\n }\n blit(velocity.write);\n velocity.swap();\n\n divergenceProgram.bind();\n if (divergenceProgram.uniforms.texelSize) {\n gl.uniform2f(divergenceProgram.uniforms.texelSize, velocity.texelSizeX, velocity.texelSizeY);\n }\n if (divergenceProgram.uniforms.uVelocity) {\n gl.uniform1i(divergenceProgram.uniforms.uVelocity, velocity.read.attach(0));\n }\n blit(divergence);\n\n clearProgram.bind();\n if (clearProgram.uniforms.uTexture) {\n gl.uniform1i(clearProgram.uniforms.uTexture, pressure.read.attach(0));\n }\n if (clearProgram.uniforms.value) {\n gl.uniform1f(clearProgram.uniforms.value, config.PRESSURE);\n }\n blit(pressure.write);\n pressure.swap();\n\n pressureProgram.bind();\n if (pressureProgram.uniforms.texelSize) {\n gl.uniform2f(pressureProgram.uniforms.texelSize, velocity.texelSizeX, velocity.texelSizeY);\n }\n if (pressureProgram.uniforms.uDivergence) {\n gl.uniform1i(pressureProgram.uniforms.uDivergence, divergence.attach(0));\n }\n for (let i = 0; i < config.PRESSURE_ITERATIONS; i++) {\n if (pressureProgram.uniforms.uPressure) {\n gl.uniform1i(pressureProgram.uniforms.uPressure, pressure.read.attach(1));\n }\n blit(pressure.write);\n pressure.swap();\n }\n\n gradienSubtractProgram.bind();\n if (gradienSubtractProgram.uniforms.texelSize) {\n gl.uniform2f(gradienSubtractProgram.uniforms.texelSize, velocity.texelSizeX, velocity.texelSizeY);\n }\n if (gradienSubtractProgram.uniforms.uPressure) {\n gl.uniform1i(gradienSubtractProgram.uniforms.uPressure, pressure.read.attach(0));\n }\n if (gradienSubtractProgram.uniforms.uVelocity) {\n gl.uniform1i(gradienSubtractProgram.uniforms.uVelocity, velocity.read.attach(1));\n }\n blit(velocity.write);\n velocity.swap();\n\n advectionProgram.bind();\n if (advectionProgram.uniforms.texelSize) {\n gl.uniform2f(advectionProgram.uniforms.texelSize, velocity.texelSizeX, velocity.texelSizeY);\n }\n if (!ext.supportLinearFiltering && advectionProgram.uniforms.dyeTexelSize) {\n gl.uniform2f(advectionProgram.uniforms.dyeTexelSize, velocity.texelSizeX, velocity.texelSizeY);\n }\n const velocityId = velocity.read.attach(0);\n if (advectionProgram.uniforms.uVelocity) {\n gl.uniform1i(advectionProgram.uniforms.uVelocity, velocityId);\n }\n if (advectionProgram.uniforms.uSource) {\n gl.uniform1i(advectionProgram.uniforms.uSource, velocityId);\n }\n if (advectionProgram.uniforms.dt) {\n gl.uniform1f(advectionProgram.uniforms.dt, dt);\n }\n if (advectionProgram.uniforms.dissipation) {\n gl.uniform1f(advectionProgram.uniforms.dissipation, config.VELOCITY_DISSIPATION);\n }\n blit(velocity.write);\n velocity.swap();\n\n if (!ext.supportLinearFiltering && advectionProgram.uniforms.dyeTexelSize) {\n gl.uniform2f(advectionProgram.uniforms.dyeTexelSize, dye.texelSizeX, dye.texelSizeY);\n }\n if (advectionProgram.uniforms.uVelocity) {\n gl.uniform1i(advectionProgram.uniforms.uVelocity, velocity.read.attach(0));\n }\n if (advectionProgram.uniforms.uSource) {\n gl.uniform1i(advectionProgram.uniforms.uSource, dye.read.attach(1));\n }\n if (advectionProgram.uniforms.dissipation) {\n gl.uniform1f(advectionProgram.uniforms.dissipation, config.DENSITY_DISSIPATION);\n }\n blit(dye.write);\n dye.swap();\n }\n\n /* ---------- render ---------- */\n function render(target: FBO | null) {\n gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);\n gl.enable(gl.BLEND);\n drawDisplay(target);\n }\n\n function drawDisplay(target: FBO | null) {\n const width = target ? target.width : gl.drawingBufferWidth;\n const height = target ? target.height : gl.drawingBufferHeight;\n displayMaterial.bind();\n if (config.SHADING && displayMaterial.uniforms.texelSize) {\n gl.uniform2f(displayMaterial.uniforms.texelSize, 1 / width, 1 / height);\n }\n if (displayMaterial.uniforms.uTexture) {\n gl.uniform1i(displayMaterial.uniforms.uTexture, dye.read.attach(0));\n }\n blit(target, false);\n }\n\n /* ---------- splats ---------- */\n function splatPointer(pointer: Pointer) {\n const dx = pointer.deltaX * config.SPLAT_FORCE;\n const dy = pointer.deltaY * config.SPLAT_FORCE;\n splat(pointer.texcoordX, pointer.texcoordY, dx, dy, pointer.color);\n }\n\n function clickSplat(pointer: Pointer) {\n const color = generateColor();\n color.r *= 10;\n color.g *= 10;\n color.b *= 10;\n const dx = 10 * (Math.random() - 0.5);\n const dy = 30 * (Math.random() - 0.5);\n splat(pointer.texcoordX, pointer.texcoordY, dx, dy, color);\n }\n\n function splat(x: number, y: number, dx: number, dy: number, color: ColorRGB) {\n splatProgram.bind();\n if (splatProgram.uniforms.uTarget) {\n gl.uniform1i(splatProgram.uniforms.uTarget, velocity.read.attach(0));\n }\n if (splatProgram.uniforms.aspectRatio) {\n gl.uniform1f(splatProgram.uniforms.aspectRatio, canvas!.width / canvas!.height);\n }\n if (splatProgram.uniforms.point) {\n gl.uniform2f(splatProgram.uniforms.point, x, y);\n }\n if (splatProgram.uniforms.color) {\n gl.uniform3f(splatProgram.uniforms.color, dx, dy, 0);\n }\n if (splatProgram.uniforms.radius) {\n gl.uniform1f(splatProgram.uniforms.radius, correctRadius(config.SPLAT_RADIUS / 100)!);\n }\n blit(velocity.write);\n velocity.swap();\n\n if (splatProgram.uniforms.uTarget) {\n gl.uniform1i(splatProgram.uniforms.uTarget, dye.read.attach(0));\n }\n if (splatProgram.uniforms.color) {\n gl.uniform3f(splatProgram.uniforms.color, color.r, color.g, color.b);\n }\n blit(dye.write);\n dye.swap();\n }\n\n function correctRadius(radius: number) {\n const aspectRatio = canvas!.width / canvas!.height;\n if (aspectRatio > 1) radius *= aspectRatio;\n return radius;\n }\n\n /* ---------- pointer helpers ---------- */\n function updatePointerDownData(pointer: Pointer, id: number, posX: number, posY: number) {\n pointer.id = id;\n pointer.down = true;\n pointer.moved = false;\n pointer.texcoordX = posX / canvas!.width;\n pointer.texcoordY = 1 - posY / canvas!.height;\n pointer.prevTexcoordX = pointer.texcoordX;\n pointer.prevTexcoordY = pointer.texcoordY;\n pointer.deltaX = 0;\n pointer.deltaY = 0;\n pointer.color = generateColor();\n }\n\n function updatePointerMoveData(pointer: Pointer, posX: number, posY: number, color: ColorRGB) {\n pointer.prevTexcoordX = pointer.texcoordX;\n pointer.prevTexcoordY = pointer.texcoordY;\n pointer.texcoordX = posX / canvas!.width;\n pointer.texcoordY = 1 - posY / canvas!.height;\n pointer.deltaX = correctDeltaX(pointer.texcoordX - pointer.prevTexcoordX)!;\n pointer.deltaY = correctDeltaY(pointer.texcoordY - pointer.prevTexcoordY)!;\n pointer.moved = Math.abs(pointer.deltaX) > 0 || Math.abs(pointer.deltaY) > 0;\n pointer.color = color;\n }\n\n function updatePointerUpData(pointer: Pointer) {\n pointer.down = false;\n }\n\n function correctDeltaX(delta: number) {\n const aspectRatio = canvas!.width / canvas!.height;\n if (aspectRatio < 1) delta *= aspectRatio;\n return delta;\n }\n\n function correctDeltaY(delta: number) {\n const aspectRatio = canvas!.width / canvas!.height;\n if (aspectRatio > 1) delta /= aspectRatio;\n return delta;\n }\n\n /* ---------- color helpers ---------- */\n function generateColor(): ColorRGB {\n const c = HSVtoRGB(Math.random(), 1.0, 1.0);\n c.r *= 0.15;\n c.g *= 0.15;\n c.b *= 0.15;\n return c;\n }\n\n function HSVtoRGB(h: number, s: number, v: number): ColorRGB {\n let r = 0,\n g = 0,\n b = 0;\n const i = Math.floor(h * 6);\n const f = h * 6 - i;\n const p = v * (1 - s);\n const q = v * (1 - f * s);\n const t = v * (1 - (1 - f) * s);\n switch (i % 6) {\n case 0:\n r = v;\n g = t;\n b = p;\n break;\n case 1:\n r = q;\n g = v;\n b = p;\n break;\n case 2:\n r = p;\n g = v;\n b = t;\n break;\n case 3:\n r = p;\n g = q;\n b = v;\n break;\n case 4:\n r = t;\n g = p;\n b = v;\n break;\n case 5:\n r = v;\n g = p;\n b = q;\n break;\n }\n return { r, g, b };\n }\n\n function wrap(value: number, min: number, max: number) {\n const range = max - min;\n if (range === 0) return min;\n return ((value - min) % range) + min;\n }\n\n /* ---------- input events ---------- */\n window.addEventListener('mousedown', e => {\n const pointer = pointers[0];\n const posX = scaleByPixelRatio(e.clientX);\n const posY = scaleByPixelRatio(e.clientY);\n updatePointerDownData(pointer, -1, posX, posY);\n clickSplat(pointer);\n });\n\n function handleFirstMouseMove(e: MouseEvent) {\n const pointer = pointers[0];\n const posX = scaleByPixelRatio(e.clientX);\n const posY = scaleByPixelRatio(e.clientY);\n const color = generateColor();\n updateFrame();\n updatePointerMoveData(pointer, posX, posY, color);\n document.body.removeEventListener('mousemove', handleFirstMouseMove);\n }\n document.body.addEventListener('mousemove', handleFirstMouseMove);\n\n window.addEventListener('mousemove', e => {\n const pointer = pointers[0];\n const posX = scaleByPixelRatio(e.clientX);\n const posY = scaleByPixelRatio(e.clientY);\n const color = pointer.color;\n updatePointerMoveData(pointer, posX, posY, color);\n });\n\n function handleFirstTouchStart(e: TouchEvent) {\n const touches = e.targetTouches;\n const pointer = pointers[0];\n for (let i = 0; i < touches.length; i++) {\n const posX = scaleByPixelRatio(touches[i].clientX);\n const posY = scaleByPixelRatio(touches[i].clientY);\n updateFrame();\n updatePointerDownData(pointer, touches[i].identifier, posX, posY);\n }\n document.body.removeEventListener('touchstart', handleFirstTouchStart);\n }\n document.body.addEventListener('touchstart', handleFirstTouchStart);\n\n window.addEventListener(\n 'touchstart',\n e => {\n const touches = e.targetTouches;\n const pointer = pointers[0];\n for (let i = 0; i < touches.length; i++) {\n const posX = scaleByPixelRatio(touches[i].clientX);\n const posY = scaleByPixelRatio(touches[i].clientY);\n updatePointerDownData(pointer, touches[i].identifier, posX, posY);\n }\n },\n false\n );\n\n window.addEventListener(\n 'touchmove',\n e => {\n const touches = e.targetTouches;\n const pointer = pointers[0];\n for (let i = 0; i < touches.length; i++) {\n const posX = scaleByPixelRatio(touches[i].clientX);\n const posY = scaleByPixelRatio(touches[i].clientY);\n updatePointerMoveData(pointer, posX, posY, pointer.color);\n }\n },\n false\n );\n\n window.addEventListener('touchend', e => {\n const touches = e.changedTouches;\n const pointer = pointers[0];\n for (let i = 0; i < touches.length; i++) {\n updatePointerUpData(pointer);\n }\n });\n});\n</script>\n","path":"SplashCursor/SplashCursor.vue","_imports_":[],"registryDependencies":[],"dependencies":[],"devDependencies":[]}],"registryDependencies":[],"dependencies":[],"devDependencies":[],"categories":["Animations"]} |