fix: Dark 主题玻璃模型失色 — uniforms 穿透材质 ref + FBO HalfFloatType

This commit is contained in:
2026-09-04 13:23:18 +08:00
parent a2936fb39c
commit 9c86ecc338
+42 -20
View File
@@ -452,11 +452,18 @@ function GlassModel({
// Scene-texture FBO: render layer 0 (background + stickers) at half res. // Scene-texture FBO: render layer 0 (background + stickers) at half res.
// Same as the original GlassSystem: one shared pass, camera mask forced to // 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. // 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 fbo = useMemo(() => {
const dpr = gl.getPixelRatio(); const dpr = gl.getPixelRatio();
const w = Math.max(2, Math.floor((size.width * dpr) / 2)); const w = Math.max(2, Math.floor((size.width * dpr) / 2));
const h = Math.max(2, Math.floor((size.height * dpr) / 2)); const h = Math.max(2, Math.floor((size.height * dpr) / 2));
const target = new THREE.WebGLRenderTarget(w, h, { const target = new THREE.WebGLRenderTarget(w, h, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
type: THREE.HalfFloatType,
stencilBuffer: false, stencilBuffer: false,
depthBuffer: true, depthBuffer: true,
samples: 0, samples: 0,
@@ -523,44 +530,59 @@ function GlassModel({
// flips. Per-model tingColor overrides: m[0]/m[1] light A/B, // 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. // m[2]/m[3] dark A/B, missing entries fall back to the theme defaults.
const isDark = useResolvedTheme() === "dark"; 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)—— // 原版移动端钳制 loop(apply effect: t=O?Math.min(B.loop,2):B.loop)——
// 折射采样少一轮,色散/亮度观感随设备宽度变化;桌面 3、移动 2。 // 折射采样少一轮,色散/亮度观感随设备宽度变化;桌面 3、移动 2。
const loop = isMobile ? Math.min(LIGHT_PARAMS.loop, 2) : LIGHT_PARAMS.loop; const loop = isMobile ? Math.min(LIGHT_PARAMS.loop, 2) : LIGHT_PARAMS.loop;
useEffect(() => { useEffect(() => {
uniforms.uLoop.value = loop; const u = matUniforms();
uniforms.uRgbRefraction.value = +(loop <= 3); u.uLoop.value = loop;
}, [loop, uniforms]); u.uRgbRefraction.value = +(loop <= 3);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loop, uniforms, mergedGeometry]);
useEffect(() => { useEffect(() => {
const u = matUniforms();
const P = isDark ? DARK_PARAMS : LIGHT_PARAMS; const P = isDark ? DARK_PARAMS : LIGHT_PARAMS;
uniforms.uShininess.value = P.shininess; u.uShininess.value = P.shininess;
uniforms.uDiffuseness.value = P.diffuseness; u.uDiffuseness.value = P.diffuseness;
uniforms.uFresnelPower.value = P.fresnelPower; u.uFresnelPower.value = P.fresnelPower;
uniforms.uFresnelStrength.value = P.fresnelStrength; u.uFresnelStrength.value = P.fresnelStrength;
uniforms.uBrightness.value = P.brightness; u.uBrightness.value = P.brightness;
uniforms.uContrast.value = P.contrast; u.uContrast.value = P.contrast;
uniforms.uGamma.value = P.gamma; u.uGamma.value = P.gamma;
uniforms.uSaturation.value = P.saturation; u.uSaturation.value = P.saturation;
uniforms.uTintMix.value = P.tintMix; u.uTintMix.value = P.tintMix;
uniforms.uTintThicknessMinAlpha.value = P.tintThicknessMinAlpha; u.uTintThicknessMinAlpha.value = P.tintThicknessMinAlpha;
uniforms.uTintThicknessMaxAlpha.value = P.tintThicknessMaxAlpha; u.uTintThicknessMaxAlpha.value = P.tintThicknessMaxAlpha;
const tintA = isDark const tintA = isDark
? (tintColors?.[2] ?? DARK_PARAMS.tintColorA) ? (tintColors?.[2] ?? DARK_PARAMS.tintColorA)
: (tintColors?.[0] ?? LIGHT_PARAMS.tintColorA); : (tintColors?.[0] ?? LIGHT_PARAMS.tintColorA);
const tintB = isDark const tintB = isDark
? (tintColors?.[3] ?? DARK_PARAMS.tintColorB) ? (tintColors?.[3] ?? DARK_PARAMS.tintColorB)
: (tintColors?.[1] ?? LIGHT_PARAMS.tintColorB); : (tintColors?.[1] ?? LIGHT_PARAMS.tintColorB);
uniforms.uTintColorA.value.copy(hexToVec4(tintA)); u.uTintColorA.value.copy(hexToVec4(tintA));
uniforms.uTintColorB.value.copy(hexToVec4(tintB)); u.uTintColorB.value.copy(hexToVec4(tintB));
uniforms.uDark.value = isDark ? 1 : 0; u.uDark.value = isDark ? 1 : 0;
}, [isDark, tintColors, uniforms]); // eslint-disable-next-line react-hooks/exhaustive-deps
}, [isDark, tintColors, uniforms, mergedGeometry]);
useEffect(() => { useEffect(() => {
const drawingBufferSize = gl.getDrawingBufferSize(new THREE.Vector2()); const drawingBufferSize = gl.getDrawingBufferSize(new THREE.Vector2());
uniforms.uScreenResolutionPx.value.set( matUniforms().uScreenResolutionPx.value.set(
drawingBufferSize.x, drawingBufferSize.x,
drawingBufferSize.y 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) // Float bob: desktop only in the original (floatingMotion && !isMobileWidth)
const floatEnabled = floating && !isMobile; const floatEnabled = floating && !isMobile;