import { Effect, BlendFunction } from 'postprocessing'; import * as THREE from 'three'; import { wrapEffect } from '@react-three/postprocessing'; const fragmentShader = ` uniform float pixelSize; uniform float uFeather; uniform float uAspect; uniform float uHoleRadius; uniform float uProgress; uniform vec3 uOverlayColor; void dotsMainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) { vec2 normalizedPixelSize = vec2(pixelSize) / resolution; float a = clamp(inputColor.a, 0.0, 1.0); vec2 cellUV = fract(uv / normalizedPixelSize); float radius = 0.8 * a; vec2 circleCenter = vec2(0.5, 0.5); float distanceFromCenter = distance(cellUV, circleCenter); float aa = fwidth(distanceFromCenter) * 1.5; // Safe smoothstep: edge0 must be < edge1 in GLSL float circleMask = 1.0 - smoothstep(radius - aa, radius, distanceFromCenter); outputColor = vec4(vec3(circleMask), circleMask); } float radialMaskAlpha(vec2 uv) { vec2 p = uv * 2.0 - 1.0; if (uAspect > 1.0) { p.x *= uAspect; } else { p.y /= max(uAspect, 0.0001); } float d = length(p); float edge = max(uFeather, uHoleRadius * 0.12); float alphaHole = smoothstep(uHoleRadius, uHoleRadius + edge, d); float fillMix = smoothstep(0.92, 1.0, uProgress); return mix(alphaHole, 1.0, fillMix); } void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) { vec2 normalizedPixelSize = vec2(pixelSize) / resolution; vec2 cellId = floor(uv / normalizedPixelSize); vec2 uvCellCenter = (cellId + vec2(0.5)) * normalizedPixelSize; float cellAlpha = clamp(radialMaskAlpha(uvCellCenter), 0.0, 1.0); vec4 dotOut; dotsMainImage(vec4(0.0, 0.0, 0.0, cellAlpha), uv, dotOut); float dotMask = clamp(dotOut.a, 0.0, 1.0); float finalAlpha = dotMask; vec3 outRgb = uOverlayColor * finalAlpha; // Blend with inputColor to only apply mask where finalAlpha > 0 // Since we use BlendFunction.NORMAL, we just return the color and alpha outputColor = vec4(outRgb, finalAlpha); } `; export class MaskedDotsEffectImpl extends Effect { constructor({ pixelSize = 32, feather = 0.5, aspect = 1, holeRadius = 2, progress = 0, overlayColor = new THREE.Color('#fbfaf4') } = {}) { super('MaskedDotsEffect', fragmentShader, { blendFunction: BlendFunction.NORMAL, uniforms: new Map | THREE.Uniform>([ ['pixelSize', new THREE.Uniform(pixelSize)], ['uFeather', new THREE.Uniform(feather)], ['uAspect', new THREE.Uniform(aspect)], ['uHoleRadius', new THREE.Uniform(holeRadius)], ['uProgress', new THREE.Uniform(progress)], ['uOverlayColor', new THREE.Uniform(overlayColor)] ]) }); } update( _renderer: THREE.WebGLRenderer, _inputBuffer: THREE.WebGLRenderTarget, _deltaTime?: number ) { // Uniforms are driven externally via ref.current.uniforms.get(...) } } export const MaskedDots = wrapEffect(MaskedDotsEffectImpl);