feat: 移植原版背景/点阵遮罩/入场幕布,修复贴纸淡出与 cursor 颜色
- BackgroundFX: 1:1 移植原版 sz 多通道程序化背景(vignette→swirl→sine→shatter→bokeh→output,30% 分辨率 ping-pong,指针跟随、进度门控跳帧) - DotOverlay: 移植原版 s1 滚动点阵遮罩(renderOrder 10,uOpacity=sT(sP))——贴纸随滚动溶入页面底色的原版机制 - LoadingCurtain+LoadingOverlay: 移植原版入场流程(径向圆洞幕布 + MaskedDots 点阵化后处理 + 居中进度条,字体/重资产 5:5 计时轴) - sP 修正: f2 无 footer 时取 1(原版语义),相机 lerp(24,32,sP) 拉远恢复;视差 y 系数修正为 0.6 - 相机: ready 门控 intro dolly(lerp(A+8,A,customCubic),1.2s)+ 鼠标视差(lag .18/leave .05) - GlassModels: cursor 贴色修正为亮色主题 #009dff/#009dff;新增指针跟随高光控制器 ca() - 刷新回顶: scrollRestoration=manual + pageshow(bfcache) 双 rAF 重置 + lenis immediate - 新增 lib/pointer.ts、lib/loadingBus.ts、lib/customCubic.ts;loadingBus 打点: 模型/贴纸/about 图
This commit is contained in:
@@ -0,0 +1,644 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useRef } from "react";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import {
|
||||
Color,
|
||||
DataTexture,
|
||||
Mesh,
|
||||
NoBlending,
|
||||
OrthographicCamera,
|
||||
PlaneGeometry,
|
||||
RGBAFormat,
|
||||
RepeatWrapping,
|
||||
Scene,
|
||||
ShaderMaterial,
|
||||
Vector2,
|
||||
Vector3,
|
||||
WebGLRenderTarget,
|
||||
} from "three";
|
||||
import { usePointer } from "@/lib/pointer";
|
||||
import { scrollBus } from "@/lib/scrollBus";
|
||||
|
||||
/**
|
||||
* Animated background — 1:1 port of the original `sz` + `sV` + `s$`
|
||||
* (chunk 4d3f3b68dbbde33a.js): a chain of fullscreen passes over two
|
||||
* ping-pong RTs at 30% resolution, composited by an output pass:
|
||||
*
|
||||
* vignette (radial color falloff, mouse-centered) →
|
||||
* swirl (radius .25) → sine (wave displacement) →
|
||||
* shatter (Voronoi cell offsets) → bokeh (32-tap golden-angle blur) →
|
||||
* output (base = bg, blend = inTex + output*0.35, mix .65) → screen
|
||||
*
|
||||
* Light-theme colors (sk/sO/sG): bg #ffead6, vignette #6196ff, output
|
||||
* #acffb9; mix {outputMix:.65, edgeIntensity:-.16}. Mouse smoothing .1 /
|
||||
* leave .05; mobile pins uPos/uMousePos to (.5,-.1). Progress-gated:
|
||||
* freezes when the page is fully scrolled (sP >= .98), frame-skip 1/2/4.
|
||||
*/
|
||||
|
||||
const BG_COLOR = "#ffead6";
|
||||
const VIGNETTE_COLOR = "#6196ff";
|
||||
const OUTPUT_COLOR = "#acffb9";
|
||||
const OUTPUT_MIX = 0.65;
|
||||
const MOUSE_POS = new Vector2(0.5, -0.1);
|
||||
const CENTER = new Vector2(0.5, 0.5);
|
||||
const clamp01 = (v: number) => Math.min(1, Math.max(0, v));
|
||||
|
||||
const COMMON_VERT = `precision mediump float;
|
||||
precision mediump int;
|
||||
|
||||
varying vec2 vUv;
|
||||
|
||||
void main() {
|
||||
vUv = uv;
|
||||
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
||||
}`;
|
||||
|
||||
const CLIP_VERT = `precision mediump float;
|
||||
precision mediump int;
|
||||
|
||||
varying vec2 vUv;
|
||||
|
||||
void main() {
|
||||
vUv = uv;
|
||||
// Render in clip-space to fill the screen, ignoring camera transforms
|
||||
gl_Position = vec4(position, 1.0);
|
||||
}`;
|
||||
|
||||
const VIGNETTE_FRAG = `precision mediump float;
|
||||
precision mediump int;
|
||||
varying vec2 vUv;
|
||||
uniform float uRadius;
|
||||
uniform float uFalloff;
|
||||
uniform float uMix;
|
||||
uniform float uDisplace;
|
||||
uniform float uSkew;
|
||||
uniform float uAngle;
|
||||
uniform vec3 uVignetteColor;
|
||||
uniform vec2 uPos; // 动态中心(跟随指针,如原始实现)
|
||||
uniform vec2 uResolution;
|
||||
uniform vec3 uClearColor;
|
||||
// 边缘明暗强度:[-1,1],负值加深暗角,正值提亮边缘
|
||||
uniform float uEdgeIntensity;
|
||||
|
||||
mat2 rot(float a) {
|
||||
return mat2(cos(a),-sin(a),sin(a),cos(a));
|
||||
}
|
||||
void main() {
|
||||
vec2 uv = vUv;
|
||||
vec4 color = vec4(vec3(1.), 0.);
|
||||
float luma = dot(color.rgb, vec3(0.299, 0.587, 0.114));
|
||||
float displacement = (luma - 0.5) * uDisplace * 0.5;
|
||||
vec2 aspectRatio = vec2(uResolution.x/uResolution.y, 1.0);
|
||||
vec2 skew = vec2(uSkew, 1.0 - uSkew);
|
||||
float halfRadius = uRadius * 0.5;
|
||||
float innerEdge = halfRadius - uFalloff * halfRadius * 0.5;
|
||||
float outerEdge = halfRadius + uFalloff * halfRadius * 0.5;
|
||||
// 使用动态指针位置作为暗角中心(原始方案)
|
||||
vec2 pos = uPos;
|
||||
vec2 scaledUV = uv * aspectRatio * rot(uAngle * 6.28318530718) * skew;
|
||||
vec2 scaledPos = pos * aspectRatio * rot(uAngle * 6.28318530718) * skew;
|
||||
float radius = distance(scaledUV, scaledPos);
|
||||
float falloff = smoothstep(innerEdge + displacement, outerEdge + displacement, radius);
|
||||
// 原始实现不额外乘 uMix(保留 uniform 以兼容但不使用)
|
||||
|
||||
// 根据 uEdgeIntensity 调整边缘亮暗:
|
||||
// uEdgeIntensity > 0 推向 0(提亮边缘),< 0 推向 1(加深暗角)
|
||||
float brighten = max(uEdgeIntensity, 0.0);
|
||||
float darken = max(-uEdgeIntensity, 0.0);
|
||||
falloff = mix(falloff, 0.0, brighten);
|
||||
falloff = mix(falloff, 1.0, darken);
|
||||
|
||||
vec3 mixed = mix(uClearColor, uVignetteColor, falloff);
|
||||
gl_FragColor = vec4(mixed, falloff);
|
||||
}`;
|
||||
|
||||
const SWIRL_FRAG = `precision mediump float;
|
||||
precision mediump int;
|
||||
varying vec2 vUv;
|
||||
uniform vec2 uResolution;
|
||||
uniform sampler2D tInput;
|
||||
uniform float uRadius;
|
||||
uniform float uAngle;
|
||||
uniform float uPhase;
|
||||
uniform float uTime;
|
||||
uniform float uMix;
|
||||
uniform vec2 uPos;
|
||||
|
||||
void main() {
|
||||
vec2 uv = vUv;
|
||||
float angle = uAngle * 10.;
|
||||
vec2 originalUV = uv;
|
||||
vec2 pos = uPos;
|
||||
uv -= pos;
|
||||
vec2 R = vec2(uv.x * uResolution.x / uResolution.y, uv.y);
|
||||
float distanceToCenter = length(R);
|
||||
if (distanceToCenter <= uRadius) {
|
||||
float rot = atan(R.y, R.x) + angle * smoothstep(uRadius, 0., distanceToCenter);
|
||||
uv = vec2(cos(rot + uTime / 20. + uPhase * 6.28318530718), sin(rot + uTime / 20. + uPhase * 6.28318530718));
|
||||
uv = distanceToCenter * uv + pos;
|
||||
}
|
||||
float t = smoothstep(0., uRadius, distanceToCenter);
|
||||
vec2 mixedUV = mix(uv, originalUV, t);
|
||||
gl_FragColor = texture2D(tInput, mix(vUv, mixedUV, uMix));
|
||||
}`;
|
||||
|
||||
const SINE_FRAG = `precision mediump float;
|
||||
precision mediump int;
|
||||
varying vec2 vUv;
|
||||
uniform sampler2D tInput;
|
||||
uniform float uMixRadius;
|
||||
uniform vec2 uPos;
|
||||
uniform float uFrequency;
|
||||
uniform float uAmplitude;
|
||||
uniform float uRotation;
|
||||
uniform float uTime;
|
||||
uniform vec2 uResolution;
|
||||
uniform vec2 uMousePos;
|
||||
uniform float uTrackMouse;
|
||||
|
||||
void main() {
|
||||
vec2 uv = vUv;
|
||||
vec2 waveCoord = vUv.xy * 2.0 - 1.0;
|
||||
float time = uTime * 0.25;
|
||||
float frequency = 20.0 * uFrequency;
|
||||
float amp = uAmplitude * 0.2;
|
||||
float waveX = sin((waveCoord.y + uPos.y) * frequency + (time)) * amp;
|
||||
float waveY = sin((waveCoord.x - uPos.x) * frequency + (time)) * amp;
|
||||
waveCoord.xy += vec2(mix(waveX, 0., uRotation), mix(0., waveY, uRotation));
|
||||
vec2 finalUV = waveCoord * 0.5 + 0.5;
|
||||
float aspectRatio = uResolution.x/uResolution.y;
|
||||
vec2 mPos = uPos + mix(vec2(0.), (uMousePos-0.5), uTrackMouse);
|
||||
float dist = (max(0.,1.-distance(uv * vec2(aspectRatio, 1.), mPos * vec2(aspectRatio, 1.)) * 4. * (1. - uMixRadius)));
|
||||
uv = mix(uv, finalUV, dist);
|
||||
gl_FragColor = texture2D(tInput, uv);
|
||||
}`;
|
||||
|
||||
const SHATTER_FRAG = `precision mediump float;
|
||||
precision mediump int;
|
||||
varying vec2 vUv;
|
||||
uniform sampler2D tInput;
|
||||
uniform float uAmount;
|
||||
uniform float uSpread;
|
||||
uniform float uAngle;
|
||||
uniform float uTime;
|
||||
uniform float uSkew;
|
||||
uniform float uCellScale;
|
||||
uniform vec2 uPos;
|
||||
uniform vec2 uResolution;
|
||||
uniform float uMixRadius;
|
||||
uniform int uMixRadiusInvert;
|
||||
uniform int uEasing;
|
||||
uniform vec2 uMousePos;
|
||||
uniform float uTrackMouse;
|
||||
uniform float uRoundness;
|
||||
|
||||
vec2 random2( vec2 p ) {
|
||||
return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
|
||||
}
|
||||
mat2 rot(float a) { return mat2(cos(a),-sin(a),sin(a),cos(a)); }
|
||||
|
||||
float ease(int mode, float t){
|
||||
if(mode==1){ return 1.0 - (1.0 - t)*(1.0 - t); }
|
||||
if(mode==2){ return t < 0.5 ? 4.0*t*t*t : 1.0 - pow(-2.0*t + 2.0, 3.0)/2.0; }
|
||||
return t;
|
||||
}
|
||||
|
||||
void main(){
|
||||
vec2 uv = vUv;
|
||||
float aspectRatio = uResolution.x / uResolution.y;
|
||||
vec2 skew = mix(vec2(1.0), vec2(1.0, 0.0), uSkew);
|
||||
vec2 st = (uv - uPos) * vec2(aspectRatio, 1.0) * uCellScale * uAmount;
|
||||
st = st * rot(uAngle * 2.0 * 3.14159265359) * skew;
|
||||
vec2 i_st = floor(st);
|
||||
vec2 f_st = fract(st);
|
||||
|
||||
float m_dist = 15.0;
|
||||
float m_dist2 = 15.0;
|
||||
vec2 m_point = vec2(0.0);
|
||||
vec2 diffBest = vec2(0.0);
|
||||
for(int j=-1;j<=1;j++){
|
||||
for(int i=-1;i<=1;i++){
|
||||
vec2 neighbor = vec2(float(i), float(j));
|
||||
vec2 point = random2(i_st + neighbor);
|
||||
point = 0.5 + 0.5 * sin(5.0 + uTime * 0.2 + 6.2831 * point);
|
||||
vec2 diff = neighbor + point - f_st;
|
||||
float dist = length(diff);
|
||||
if(dist < m_dist){
|
||||
m_dist2 = m_dist;
|
||||
m_dist = dist;
|
||||
m_point = point;
|
||||
diffBest = diff;
|
||||
} else if (dist < m_dist2) {
|
||||
m_dist2 = dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vec2 offset = (m_point * 0.2 * uSpread * 2.0) - (uSpread * 0.2);
|
||||
// soften offsets near cell edges to get rounder pieces
|
||||
// Use F2-F1 (second nearest minus nearest) to detect corners and soften further
|
||||
float cornerSoft = smoothstep(0.0, max(0.0001, uRoundness) * 2.0, m_dist2 - m_dist);
|
||||
float edgeSoft = smoothstep(0.0, max(0.0001, uRoundness), m_dist) * cornerSoft;
|
||||
offset *= edgeSoft;
|
||||
|
||||
vec2 mPos = uPos + mix(vec2(0.0), (uMousePos - 0.5), uTrackMouse);
|
||||
vec2 pos = mix(uPos, mPos, floor(uMixRadius));
|
||||
|
||||
float rawDist = max(0.0, 1.0 - distance(uv * vec2(aspectRatio,1.0), mPos * vec2(aspectRatio,1.0)) * 4.0 * (1.0 - uMixRadius));
|
||||
if(uMixRadiusInvert == 1){ rawDist = 1.0 - rawDist; }
|
||||
float dist = ease(uEasing, rawDist);
|
||||
|
||||
vec4 color = texture2D(tInput, uv + offset * dist);
|
||||
gl_FragColor = color;
|
||||
}`;
|
||||
|
||||
const BOKEH_FRAG = `precision mediump float;
|
||||
precision mediump int;
|
||||
varying vec2 vUv;
|
||||
uniform sampler2D tInput;
|
||||
uniform sampler2D tBlueNoise;
|
||||
uniform float uAmount;
|
||||
uniform float uTilt;
|
||||
uniform float uTime;
|
||||
uniform vec2 uPos;
|
||||
uniform vec2 uResolution;
|
||||
uniform vec2 uBlueNoiseResolution;
|
||||
uniform vec2 uMousePos;
|
||||
uniform float uTrackMouse;
|
||||
|
||||
#define PI 3.14159265
|
||||
#define PI2 6.28318530718
|
||||
// 优化:降低采样迭代次数 (原 50.0 -> 24.0) 以大幅提升性能
|
||||
#define ITERATIONS 32.0
|
||||
#define GOLDEN_ANGLE 2.39996323
|
||||
|
||||
vec2 Sample(in float theta, inout float r) {
|
||||
r += 1.0 / r;
|
||||
return (r - 1.0) * vec2(cos(theta), sin(theta));
|
||||
}
|
||||
|
||||
float getBlueNoiseOffset(vec2 st) {
|
||||
vec2 texSize = uBlueNoiseResolution;
|
||||
vec2 uv = fract(st * (uResolution/texSize) * vec2(texSize.x/texSize.y, 1.0));
|
||||
vec4 blueNoise = texture2D(tBlueNoise, uv);
|
||||
return mod((blueNoise.r - 0.5) * PI2, PI2);
|
||||
}
|
||||
|
||||
vec4 Bokeh(sampler2D tex, vec2 uv, float blurRadius) {
|
||||
vec3 accumulatedColor = vec3(0.0);
|
||||
vec3 accumulatedWeights = vec3(0.0);
|
||||
float accumulatedAlpha = 0.0;
|
||||
float aspectRatio = uResolution.x / uResolution.y;
|
||||
vec2 basePixelSize = vec2(1.0 / aspectRatio, 1.0) * 0.04 * 0.075;
|
||||
float r = 1.0;
|
||||
float noiseOffset = (getBlueNoiseOffset(uv) - 0.5) * 0.01;
|
||||
float noiseAngle = noiseOffset * PI2;
|
||||
mat2 rotationMatrix = mat2(
|
||||
cos(noiseAngle), -sin(noiseAngle),
|
||||
sin(noiseAngle), cos(noiseAngle)
|
||||
);
|
||||
for (float j = 0.0; j < GOLDEN_ANGLE * ITERATIONS; j += GOLDEN_ANGLE) {
|
||||
vec2 offset = Sample(j, r) * basePixelSize * blurRadius;
|
||||
float jitterAmount = 0.05 * (sin(j * 0.1) * 0.5 + 0.5);
|
||||
offset *= 1.0 + jitterAmount * sin(j * 0.7 + noiseOffset);
|
||||
vec2 sampleOffset = rotationMatrix * offset;
|
||||
vec4 colorSample = texture2D(tex, uv + sampleOffset);
|
||||
// Render targets are in Three.js working space (linear) by default.
|
||||
vec3 linearSample = colorSample.rgb;
|
||||
vec3 bokehWeight = vec3(5.0) + pow(linearSample, vec3(9.0)) * 150.0;
|
||||
accumulatedAlpha += colorSample.a;
|
||||
accumulatedColor += linearSample * bokehWeight;
|
||||
accumulatedWeights += bokehWeight;
|
||||
}
|
||||
vec3 linearOut = accumulatedColor / accumulatedWeights;
|
||||
return vec4(linearOut, accumulatedAlpha / ITERATIONS);
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec2 uv = vUv;
|
||||
if(uAmount == 0.0) { gl_FragColor = vec4(0.0); return; }
|
||||
vec2 pos = uPos + mix(vec2(0.0), (uMousePos - 0.5), uTrackMouse);
|
||||
float dis = distance(uv, pos) * 1000.0;
|
||||
float tilt = mix(1.0 - dis * 0.001, dis * 0.001, uTilt);
|
||||
float blurRadius = uAmount * tilt;
|
||||
gl_FragColor = Bokeh(tInput, uv, blurRadius);
|
||||
}`;
|
||||
|
||||
const OUTPUT_FRAG = `precision mediump float;
|
||||
precision mediump int;
|
||||
varying vec2 vUv;
|
||||
uniform sampler2D tInput;
|
||||
uniform vec3 uBgColor;
|
||||
uniform vec3 uOutputColor;
|
||||
uniform int uLoaded;
|
||||
// 可调输出混合权重(0.0~1.0),用于替代固定 0.6
|
||||
uniform float uOutputMix;
|
||||
// 方案A:更接近 before.js 的合成逻辑 (base * mix(1, blend, 0.26))
|
||||
|
||||
vec3 overlay(vec3 base, vec3 blend){
|
||||
return mix(2.0 * base * blend, 1.0 - 2.0 * (1.0 - base) * (1.0 - blend), step(0.5, base));
|
||||
}
|
||||
|
||||
void main(){
|
||||
if(uLoaded!=1){
|
||||
gl_FragColor = vec4(197./255.,136./255.,122./255.,1.);
|
||||
return;
|
||||
}
|
||||
|
||||
// uBgColor/uOutputColor are provided in Three.js working space (linear).
|
||||
vec3 bgTex = vec3(1.0); // 无背景贴图时近似常量
|
||||
vec3 base = mix(uBgColor, overlay(uBgColor, bgTex), 0.61);
|
||||
|
||||
vec4 inTex = texture2D(tInput, vUv);
|
||||
// 作为 tint 加色,不依赖 alpha,保证 OUTPUT_COLOR 可见
|
||||
vec3 tint = uOutputColor * 0.35;
|
||||
vec3 blend = clamp(inTex.rgb + tint, 0.0, 1.0);
|
||||
vec3 finalColor = base * mix(vec3(1.0), blend, clamp(uOutputMix, 0.0, 1.0));
|
||||
|
||||
gl_FragColor = vec4(finalColor, 1.0);
|
||||
|
||||
#include <colorspace_fragment>
|
||||
}`;
|
||||
|
||||
function makeNoiseTexture(size = 128): { texture: DataTexture; resolution: Vector2 } {
|
||||
const data = new Uint8ClampedArray(size * size * 4);
|
||||
for (let i = 0; i < data.length; i += 4) {
|
||||
const v = Math.floor(255 * Math.random());
|
||||
data[i] = v;
|
||||
data[i + 1] = v;
|
||||
data[i + 2] = v;
|
||||
data[i + 3] = 255;
|
||||
}
|
||||
const texture = new DataTexture(data, size, size, RGBAFormat);
|
||||
texture.needsUpdate = true;
|
||||
texture.wrapS = RepeatWrapping;
|
||||
texture.wrapT = RepeatWrapping;
|
||||
return { texture, resolution: new Vector2(size, size) };
|
||||
}
|
||||
|
||||
const toVec3 = (hex: string) => {
|
||||
const c = new Color(hex);
|
||||
return new Vector3(c.r, c.g, c.b);
|
||||
};
|
||||
|
||||
/** Frame skip by scroll progress (original sF): 1 / 2 / 4. */
|
||||
function frameSkip(progress: number): number {
|
||||
const p = clamp01(progress > 1 ? progress / 100 : progress);
|
||||
return p > 0.75 ? 4 : p > 0.5 ? 2 : 1;
|
||||
}
|
||||
|
||||
export function BackgroundFX() {
|
||||
const gl = useThree((s) => s.gl);
|
||||
const size = useThree((s) => s.size);
|
||||
const { uv: pointerUv, insideRef } = usePointer();
|
||||
const isMobile = size.width < 1024;
|
||||
|
||||
const resolutionScale = 0.3;
|
||||
const smoothing = 0.1;
|
||||
const leaveSmoothing = 0.05;
|
||||
|
||||
const noise = useMemo(() => makeNoiseTexture(128), []);
|
||||
useEffect(() => () => noise.texture.dispose(), [noise]);
|
||||
|
||||
// Shared uniform objects across all passes (original o / l / a vectors).
|
||||
const shared = useMemo(
|
||||
() => ({
|
||||
resolution: new Vector2(1, 1),
|
||||
time: { value: 0 },
|
||||
pos: new Vector2(0.5, 0.5),
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
||||
const materials = useMemo(() => {
|
||||
const mk = (frag: string, extra: Record<string, { value: unknown }> = {}) =>
|
||||
new ShaderMaterial({
|
||||
vertexShader: COMMON_VERT,
|
||||
fragmentShader: frag,
|
||||
uniforms: {
|
||||
tInput: { value: null },
|
||||
uResolution: { value: shared.resolution },
|
||||
uTime: shared.time,
|
||||
uPos: { value: shared.pos },
|
||||
uMousePos: { value: new Vector2(0.5, 0.5) },
|
||||
uTrackMouse: { value: 1 },
|
||||
...extra,
|
||||
},
|
||||
transparent: false,
|
||||
blending: NoBlending,
|
||||
depthTest: false,
|
||||
depthWrite: false,
|
||||
toneMapped: false,
|
||||
});
|
||||
|
||||
const vignette = mk(VIGNETTE_FRAG, {
|
||||
uRadius: { value: 0.354 },
|
||||
uFalloff: { value: 1 },
|
||||
uMix: { value: 1 },
|
||||
uDisplace: { value: 0 },
|
||||
uSkew: { value: 0.54 },
|
||||
uAngle: { value: 0 },
|
||||
uEdgeIntensity: { value: -0.16 },
|
||||
uVignetteColor: { value: toVec3(VIGNETTE_COLOR) },
|
||||
uColorAlpha: { value: 1 },
|
||||
uClearColor: { value: toVec3(BG_COLOR) },
|
||||
});
|
||||
const swirl = mk(SWIRL_FRAG, {
|
||||
uRadius: { value: 0.25 },
|
||||
uAngle: { value: 0.1 },
|
||||
uPhase: { value: 0 },
|
||||
uMix: { value: 0.5 },
|
||||
uPinch: { value: 0 },
|
||||
});
|
||||
const sine = mk(SINE_FRAG, {
|
||||
uMixRadius: { value: 1 },
|
||||
uFrequency: { value: 0.35 },
|
||||
uAmplitude: { value: 1.18 },
|
||||
uRotation: { value: 0 },
|
||||
});
|
||||
const shatter = mk(SHATTER_FRAG, {
|
||||
uAmount: { value: 1 },
|
||||
uSpread: { value: 0.9 },
|
||||
uAngle: { value: -45 / 360 },
|
||||
uSkew: { value: 0.9 },
|
||||
uCellScale: { value: 16 },
|
||||
uMixRadius: { value: 1 },
|
||||
uMixRadiusInvert: { value: 0 },
|
||||
uEasing: { value: 1 },
|
||||
uTrackMouse: { value: 0 },
|
||||
uPos: { value: new Vector2(0.5, 0.5) },
|
||||
uRoundness: { value: 0.02 },
|
||||
});
|
||||
const bokeh = mk(BOKEH_FRAG, {
|
||||
tBlueNoise: { value: noise.texture },
|
||||
uBlueNoiseResolution: { value: noise.resolution.clone() },
|
||||
uAmount: { value: 3.125 * 0.754 },
|
||||
uTilt: { value: 0.5 },
|
||||
uPos: { value: new Vector2(0.5, 0.5) },
|
||||
uTrackMouse: { value: 0 },
|
||||
});
|
||||
const output = mk(OUTPUT_FRAG, {
|
||||
uBgColor: { value: toVec3(BG_COLOR) },
|
||||
uOutputColor: { value: toVec3(OUTPUT_COLOR) },
|
||||
uLoaded: { value: 1 },
|
||||
uOutputMix: { value: OUTPUT_MIX },
|
||||
});
|
||||
output.vertexShader = CLIP_VERT;
|
||||
output.depthTest = false;
|
||||
output.depthWrite = false;
|
||||
output.toneMapped = false;
|
||||
output.transparent = false;
|
||||
output.blending = NoBlending;
|
||||
|
||||
const prePasses = [
|
||||
{ name: "vignette", material: vignette },
|
||||
{ name: "swirl", material: swirl },
|
||||
{ name: "sine", material: sine },
|
||||
{ name: "shatter", material: shatter },
|
||||
{ name: "bokeh", material: bokeh },
|
||||
];
|
||||
return {
|
||||
prePasses,
|
||||
outputMaterial: output,
|
||||
vignetteMaterial: vignette,
|
||||
shatterMaterial: shatter,
|
||||
mouseConsumers: prePasses.filter((p) => p.name !== "shatter"),
|
||||
};
|
||||
}, [noise, shared]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
materials.prePasses.forEach((p) => p.material.dispose());
|
||||
materials.outputMaterial.dispose();
|
||||
},
|
||||
[materials]
|
||||
);
|
||||
|
||||
// Ping-pong RTs at 30% resolution + clear color (original resize effect;
|
||||
// the original does not restore the renderer clear color afterwards).
|
||||
const rtsRef = useRef({
|
||||
read: new WebGLRenderTarget(1, 1, { depthBuffer: false }),
|
||||
write: new WebGLRenderTarget(1, 1, { depthBuffer: false }),
|
||||
});
|
||||
const frameRef = useRef(0);
|
||||
useEffect(() => {
|
||||
const w = Math.max(1, Math.floor(size.width * resolutionScale));
|
||||
const h = Math.max(1, Math.floor(size.height * resolutionScale));
|
||||
shared.resolution.set(w, h);
|
||||
rtsRef.current.read.dispose();
|
||||
rtsRef.current.write.dispose();
|
||||
rtsRef.current = {
|
||||
read: new WebGLRenderTarget(w, h, { depthBuffer: false }),
|
||||
write: new WebGLRenderTarget(w, h, { depthBuffer: false }),
|
||||
};
|
||||
gl.setClearColor(new Color(BG_COLOR), 1);
|
||||
gl.setRenderTarget(rtsRef.current.read);
|
||||
gl.clear();
|
||||
gl.setRenderTarget(null);
|
||||
frameRef.current = 0;
|
||||
materials.outputMaterial.uniforms.tInput.value = rtsRef.current.read.texture;
|
||||
}, [gl, size.width, size.height, materials, shared]);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
rtsRef.current.read.dispose();
|
||||
rtsRef.current.write.dispose();
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
const bgColor = useMemo(() => toVec3(BG_COLOR), []);
|
||||
const vignetteColor = useMemo(() => toVec3(VIGNETTE_COLOR), []);
|
||||
const outputColor = useMemo(() => toVec3(OUTPUT_COLOR), []);
|
||||
|
||||
// Internal ortho scene for pre-passes (original v/A + first-pass mesh).
|
||||
const preScene = useMemo(() => new Scene(), []);
|
||||
const preCamera = useMemo(() => new OrthographicCamera(-1, 1, 1, -1, 0, 1), []);
|
||||
const preMeshRef = useRef<Mesh | null>(null);
|
||||
useEffect(() => {
|
||||
const geo = new PlaneGeometry(2, 2);
|
||||
const mesh = new Mesh(geo, materials.prePasses[0]?.material);
|
||||
preScene.add(mesh);
|
||||
preMeshRef.current = mesh;
|
||||
return () => {
|
||||
preScene.remove(mesh);
|
||||
geo.dispose();
|
||||
};
|
||||
}, [materials, preScene]);
|
||||
|
||||
// uTrackMouse = +!isMobile for every pre-pass (original effect).
|
||||
useEffect(() => {
|
||||
const flag = isMobile ? 0 : 1;
|
||||
for (const p of materials.prePasses) {
|
||||
const tm = p.material.uniforms.uTrackMouse;
|
||||
if (tm) tm.value = flag;
|
||||
}
|
||||
}, [isMobile, materials]);
|
||||
|
||||
// Mouse tracking + per-frame chain (original sz useFrame).
|
||||
const smoothed = useRef(new Vector2(0.5, 0.5));
|
||||
const target = useRef(new Vector2(0.5, 0.5));
|
||||
|
||||
useFrame((state) => {
|
||||
const rts = rtsRef.current;
|
||||
const mesh = preMeshRef.current;
|
||||
if (!rts || !mesh) return;
|
||||
|
||||
shared.time.value = state.clock.getElapsedTime();
|
||||
|
||||
if (isMobile) {
|
||||
// Pin to (.5, -.1) like the original mobile branch.
|
||||
shared.pos.copy(MOUSE_POS);
|
||||
for (const p of materials.mouseConsumers) {
|
||||
const mm = p.material.uniforms.uMousePos;
|
||||
if (mm) (mm.value as Vector2).copy(MOUSE_POS);
|
||||
}
|
||||
(materials.shatterMaterial.uniforms.uPos.value as Vector2).copy(MOUSE_POS);
|
||||
} else {
|
||||
if (insideRef.current) target.current.set(clamp01(pointerUv.x), clamp01(pointerUv.y));
|
||||
else target.current.copy(CENTER);
|
||||
smoothed.current.lerp(target.current, insideRef.current ? smoothing : leaveSmoothing);
|
||||
shared.pos.copy(smoothed.current);
|
||||
for (const p of materials.mouseConsumers) {
|
||||
const mm = p.material.uniforms.uMousePos;
|
||||
if (mm) (mm.value as Vector2).copy(smoothed.current);
|
||||
}
|
||||
(materials.shatterMaterial.uniforms.uPos.value as Vector2).copy(CENTER);
|
||||
}
|
||||
|
||||
// Theme color smoothing (light-only: instant convergence to constants).
|
||||
materials.vignetteMaterial.uniforms.uVignetteColor.value.copy(vignetteColor);
|
||||
materials.vignetteMaterial.uniforms.uClearColor.value.copy(bgColor);
|
||||
materials.outputMaterial.uniforms.uBgColor.value.copy(bgColor);
|
||||
materials.outputMaterial.uniforms.uOutputColor.value.copy(outputColor);
|
||||
|
||||
const progress = clamp01(scrollBus.sP);
|
||||
if (progress >= 0.98) return;
|
||||
|
||||
if (frameRef.current++ % Math.max(frameSkip(progress), 2) === 0) {
|
||||
for (const pass of materials.prePasses) {
|
||||
const tInput = pass.material.uniforms.tInput;
|
||||
if (tInput) tInput.value = rts.read.texture;
|
||||
mesh.material = pass.material;
|
||||
gl.setRenderTarget(rts.write);
|
||||
gl.render(preScene, preCamera);
|
||||
gl.setRenderTarget(null);
|
||||
const t = rts.read;
|
||||
rts.read = rts.write;
|
||||
rts.write = t;
|
||||
}
|
||||
materials.outputMaterial.uniforms.tInput.value = rts.read.texture;
|
||||
}
|
||||
}, -2);
|
||||
|
||||
// Output fullscreen quad at renderOrder -10 (original s$).
|
||||
return (
|
||||
<mesh frustumCulled={false} renderOrder={-10}>
|
||||
<planeGeometry args={[2, 2]} />
|
||||
<primitive object={materials.outputMaterial} attach="material" />
|
||||
</mesh>
|
||||
);
|
||||
}
|
||||
|
||||
export default BackgroundFX;
|
||||
Reference in New Issue
Block a user