fix: 首载流体特效位置偏移 — dprRef 在 composer 挂载前卡在 1

- 症状:刷新后鼠标流体折射/拖尾不在指针位置出现,任意窗口 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×缓冲宽高
This commit is contained in:
2026-09-02 13:32:52 +08:00
parent 040dc24701
commit 88ccc79be1
+12 -2
View File
@@ -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;