From 9c86ecc338f07278a409f8adc4fb46e7964081a7 Mon Sep 17 00:00:00 2001 From: chenchun Date: Fri, 4 Sep 2026 13:23:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20Dark=20=E4=B8=BB=E9=A2=98=E7=8E=BB?= =?UTF-8?q?=E7=92=83=E6=A8=A1=E5=9E=8B=E5=A4=B1=E8=89=B2=20=E2=80=94=20uni?= =?UTF-8?q?forms=20=E7=A9=BF=E9=80=8F=E6=9D=90=E8=B4=A8=20ref=20+=20FBO=20?= =?UTF-8?q?HalfFloatType?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/canvas/GlassModels.tsx | 62 +++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/components/canvas/GlassModels.tsx b/components/canvas/GlassModels.tsx index ad4b066..bcc392e 100644 --- a/components/canvas/GlassModels.tsx +++ b/components/canvas/GlassModels.tsx @@ -452,11 +452,18 @@ function GlassModel({ // Scene-texture FBO: render layer 0 (background + stickers) at half res. // Same as the original GlassSystem: one shared pass, camera mask forced to // layer 0 so glass meshes (layer 10) are excluded from their own refraction. + // ⚠ type: HalfFloatType —— 原版 sb() 工厂创建的所有 RT 均为 HalfFloat + // (4d3f3b68 @200877: {minFilter,magFilter,type:HalfFloatType,...})。 + // UnsignedByte 的默认 RT 会让折射纹理的暗部量化/截断,Dark 主题下 + // Hard-Light 着色的底色因此偏灰(模型"发透明感"而非饱和蓝)。 const fbo = useMemo(() => { const dpr = gl.getPixelRatio(); const w = Math.max(2, Math.floor((size.width * dpr) / 2)); const h = Math.max(2, Math.floor((size.height * dpr) / 2)); const target = new THREE.WebGLRenderTarget(w, h, { + minFilter: THREE.LinearFilter, + magFilter: THREE.LinearFilter, + type: THREE.HalfFloatType, stencilBuffer: false, depthBuffer: true, samples: 0, @@ -523,44 +530,59 @@ function GlassModel({ // flips. Per-model tingColor overrides: m[0]/m[1] light A/B, // m[2]/m[3] dark A/B, missing entries fall back to the theme defaults. const isDark = useResolvedTheme() === "dark"; + // ⚠ R3F 对 ShaderMaterial 的 `uniforms` prop 做的是「按键浅合并」: + // material.uniforms 是独立容器,标量按值拷贝、Vector4/Texture 共享引用, + // 且仅在 prop 引用变化时重新合并。材质创建后再改 memo 对象,标量 + // (uDark、brightness、thickness alpha、loop…)永远到不了 GPU —— Dark + // 主题下 uDark 卡在 0、参数卡在 light 值,模型因此失色。原版所有 + // apply 都写 T.current.uniforms(材质 ref);这里同样穿透到材质, + // 材质未挂载时回落到 memo(创建时会被拷贝进去),并以 mergedGeometry + // 作为「材质已存在」信号重跑(原版靠 tintYRange Y 的变化触发同一效果)。 + const matUniforms = () => + (materialRef.current?.uniforms ?? uniforms) as typeof uniforms; // 原版移动端钳制 loop(apply effect: t=O?Math.min(B.loop,2):B.loop)—— // 折射采样少一轮,色散/亮度观感随设备宽度变化;桌面 3、移动 2。 const loop = isMobile ? Math.min(LIGHT_PARAMS.loop, 2) : LIGHT_PARAMS.loop; useEffect(() => { - uniforms.uLoop.value = loop; - uniforms.uRgbRefraction.value = +(loop <= 3); - }, [loop, uniforms]); + const u = matUniforms(); + u.uLoop.value = loop; + u.uRgbRefraction.value = +(loop <= 3); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [loop, uniforms, mergedGeometry]); useEffect(() => { + const u = matUniforms(); const P = isDark ? DARK_PARAMS : LIGHT_PARAMS; - uniforms.uShininess.value = P.shininess; - uniforms.uDiffuseness.value = P.diffuseness; - uniforms.uFresnelPower.value = P.fresnelPower; - uniforms.uFresnelStrength.value = P.fresnelStrength; - uniforms.uBrightness.value = P.brightness; - uniforms.uContrast.value = P.contrast; - uniforms.uGamma.value = P.gamma; - uniforms.uSaturation.value = P.saturation; - uniforms.uTintMix.value = P.tintMix; - uniforms.uTintThicknessMinAlpha.value = P.tintThicknessMinAlpha; - uniforms.uTintThicknessMaxAlpha.value = P.tintThicknessMaxAlpha; + u.uShininess.value = P.shininess; + u.uDiffuseness.value = P.diffuseness; + u.uFresnelPower.value = P.fresnelPower; + u.uFresnelStrength.value = P.fresnelStrength; + u.uBrightness.value = P.brightness; + u.uContrast.value = P.contrast; + u.uGamma.value = P.gamma; + u.uSaturation.value = P.saturation; + u.uTintMix.value = P.tintMix; + u.uTintThicknessMinAlpha.value = P.tintThicknessMinAlpha; + u.uTintThicknessMaxAlpha.value = P.tintThicknessMaxAlpha; const tintA = isDark ? (tintColors?.[2] ?? DARK_PARAMS.tintColorA) : (tintColors?.[0] ?? LIGHT_PARAMS.tintColorA); const tintB = isDark ? (tintColors?.[3] ?? DARK_PARAMS.tintColorB) : (tintColors?.[1] ?? LIGHT_PARAMS.tintColorB); - uniforms.uTintColorA.value.copy(hexToVec4(tintA)); - uniforms.uTintColorB.value.copy(hexToVec4(tintB)); - uniforms.uDark.value = isDark ? 1 : 0; - }, [isDark, tintColors, uniforms]); + u.uTintColorA.value.copy(hexToVec4(tintA)); + u.uTintColorB.value.copy(hexToVec4(tintB)); + u.uDark.value = isDark ? 1 : 0; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [isDark, tintColors, uniforms, mergedGeometry]); useEffect(() => { const drawingBufferSize = gl.getDrawingBufferSize(new THREE.Vector2()); - uniforms.uScreenResolutionPx.value.set( + matUniforms().uScreenResolutionPx.value.set( drawingBufferSize.x, drawingBufferSize.y ); - }, [gl, size, uniforms]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [gl, size, uniforms, mergedGeometry]); // Float bob: desktop only in the original (floatingMotion && !isMobileWidth) const floatEnabled = floating && !isMobile;