From 88ccc79be11f1593f805f3c011c709fc6bb83818 Mon Sep 17 00:00:00 2001 From: chenchun Date: Wed, 2 Sep 2026 13:32:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=A6=96=E8=BD=BD=E6=B5=81=E4=BD=93?= =?UTF-8?q?=E7=89=B9=E6=95=88=E4=BD=8D=E7=BD=AE=E5=81=8F=E7=A7=BB=20?= =?UTF-8?q?=E2=80=94=20dprRef=20=E5=9C=A8=20composer=20=E6=8C=82=E8=BD=BD?= =?UTF-8?q?=E5=89=8D=E5=8D=A1=E5=9C=A8=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 症状:刷新后鼠标流体折射/拖尾不在指针位置出现,任意窗口 resize 后立即归位 - 根因:displayMetrics effect 以 [size.width,size.height] 为依赖且开头 if(!composer) return,首帧 composer 未挂载时 dprRef 永远停在 1(实际 2); pointerDevRef = uv·size·dprRef 算出的设备坐标只有真实一半,而 EffectComposer.setSize 传给 pass 的是设备像素(3320×1798), mouseUv = uPointer/uResolution 因此错位一半;只有 size 变化才会重跑该 effect - 修复:dprRef 在 composer 判空之前赋值 + useFrame 每帧同步 dprRef = min(gl.getPixelRatio(), 2),坐标链不再依赖 effect 调度 - 已用 CDP 数值验证:两处指针位置的 devX/devY 均精确等于 uv×缓冲宽高 --- components/canvas/PostFX.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/components/canvas/PostFX.tsx b/components/canvas/PostFX.tsx index 16fd796..46ff390 100644 --- a/components/canvas/PostFX.tsx +++ b/components/canvas/PostFX.tsx @@ -130,10 +130,16 @@ export function PostFX() { const dprRef = useRef(1); useEffect(() => { + const t = Math.min(gl.getPixelRatio(), 2); + // Assign dpr BEFORE the composer guard: this effect only re-runs on a + // size change, and if the composer wasn't mounted on the first run the + // dpr would stay 1 forever — pointerDevRef (= uv·size·dprRef) then + // lands at HALF the real position (buffer is size·2) until the user + // resizes the window. Symptom: fluid trail offset from the pointer + // right after load, snapping into place after any resize. + dprRef.current = t; const composer = composerRef.current; if (!composer) return; - const t = Math.min(gl.getPixelRatio(), 2); - dprRef.current = t; const r = Math.max(1, Math.floor(size.width * t)); const n = Math.max(1, Math.floor(size.height * t)); composer.inputBuffer.setSize(r, n); @@ -161,6 +167,10 @@ export function PostFX() { if (!composer) return; const dprNow = Math.min(gl.getPixelRatio(), 2); + // Keep dprRef live every frame (see the resize-effect comment): the + // pointer device coords = uv · size · dprRef must always match the real + // drawing-buffer ratio, independent of effect scheduling. + dprRef.current = dprNow; const metricsKey = `${size.width}x${size.height}x${dprNow}`; if (lastMetricsKeyRef.current !== metricsKey) { lastMetricsKeyRef.current = metricsKey;