// CRT-style chromatic glitch — ported from the `glitch` shader preset. // Zero-runtime-dep effect — imports ONLY types from @vfx-js/core. import type { Effect, EffectContext } from "@vfx-js/core"; const FRAG_GLITCH = `#version 300 es precision highp float; in vec2 uvContent; out vec4 outColor; uniform sampler2D src; uniform vec4 srcRectUv; uniform float time; uniform float intensity; // Sample src at a content-space UV (0..1 over the element), remapped // into the padded src buffer via srcRectUv. vec4 sampleSrc(vec2 c) { return texture(src, srcRectUv.xy + c * srcRectUv.zw); } // Like sampleSrc but transparent outside the content rect (preset autoCrop). vec4 readTex(vec2 c) { if (c.x < 0.0 || c.x > 1.0 || c.y < 0.0 || c.y > 1.0) return vec4(0.0); return sampleSrc(c); } float nn(float y, float t) { float n = ( sin(y * .07 + t * 8. + sin(y * .5 + t * 10.)) + sin(y * .7 + t * 2. + sin(y * .3 + t * 8.)) * .7 + sin(y * 1.1 + t * 2.8) * .4 ); n += sin(y * 124. + t * 100.7) * sin(y * 877. - t * 38.8) * .3; return n; } void main (void) { vec2 uv = uvContent; vec4 color = readTex(uv); float t = mod(time, 3.14 * 10.); // Seed value float v = fract(sin(t * 2.) * 700.); if (abs(nn(uv.y, t)) < 1.2) { v *= 0.01; } // Prepare for chromatic aberration vec2 focus = vec2(0.5); float d = v * 0.6 * intensity; vec2 ruv = focus + (uv - focus) * (1. - d); vec2 guv = focus + (uv - focus) * (1. - 2. * d); vec2 buv = focus + (uv - focus) * (1. - 3. * d); // Random Glitch if (v > 0.1) { // Randomize y float y = floor(uv.y * 13. * sin(35. * t)) + 1.; if (sin(36. * y * v) > 0.9) { ruv.x = uv.x + sin(76. * y) * 0.1 * intensity; guv.x = uv.x + sin(34. * y) * 0.1 * intensity; buv.x = uv.x + sin(59. * y) * 0.1 * intensity; } // RGB Shift v = pow(v * 1.5, 2.) * 0.15 * intensity; color.rgb *= 0.3; color.r += readTex(vec2(uv.x + sin(t * 123.45) * v, uv.y)).r; color.g += readTex(vec2(uv.x + sin(t * 157.67) * v, uv.y)).g; color.b += readTex(vec2(uv.x + sin(t * 143.67) * v, uv.y)).b; } // Compose chromatic aberration if (abs(nn(uv.y, t)) > 1.1) { color.r = color.r * 0.5 + color.r * sampleSrc(ruv).r; color.g = color.g * 0.5 + color.g * sampleSrc(guv).g; color.b = color.b * 0.5 + color.b * sampleSrc(buv).b; color *= 2.; } outColor = color; outColor.a = smoothstep(0.0, 0.8, max(color.r, max(color.g, color.b))); } `; export type GlitchParams = { /** Time scale applied to the animation. */ speed: number; /** Overall glitch strength — scales shift and aberration amounts. */ intensity: number; }; const DEFAULT_PARAMS: GlitchParams = { speed: 1, intensity: 1 }; export class GlitchEffect implements Effect { params: GlitchParams; constructor(initial: Partial = {}) { this.params = { ...DEFAULT_PARAMS, ...initial }; } setParams(updates: Partial): void { Object.assign(this.params, updates); } render(ctx: EffectContext): void { ctx.draw({ frag: FRAG_GLITCH, uniforms: { src: ctx.src, time: ctx.time * this.params.speed, intensity: this.params.intensity, }, target: ctx.target, }); } }