如何在React Three Fiber中为GLB模型实现截图同款玻璃效果?
在React Three Fiber中实现GLB模型的磨砂玻璃效果
我正在尝试给React Three Fiber中的GLB模型添加和下图一致的磨砂玻璃效果,当前实现代码如下:
import React, { useEffect } from 'react'; import { useGLTF, OrbitControls } from '@react-three/drei'; import { Canvas } from '@react-three/fiber'; import * as THREE from 'three'; const ModelViewer = ({ modelPath }) => { const { scene } = useGLTF(modelPath); // Apply frosted-glass material useEffect(() => { scene.traverse((child) => { if (child.isMesh) { child.material = new THREE.MeshPhysicalMaterial({ color: new THREE.Color('#eeeeee'), transparent: true, opacity: 0.3, // Adjust for frost transmission: 0.9, // Allows light through roughness: 0.6, // For that diffused frosted look metalness: 0, thickness: 1, ior: 1.3, clearcoat: 0.1, reflectivity: 0, envMapIntensity: 0.1, // Almost no reflections }); child.material.depthWrite = false; // Helps with transparency blending } }); }, [scene]); return ( <Canvas camera={{ position: [0,350, 0], fov: 50 }} style={{ background: '#1a1a1a' }} > <ambientLight intensity={0.5} /> <directionalLight position={[5, 5, 5]} intensity={0.3} /> <directionalLight position={[-5, -5, -5]} intensity={0.2} /> <primitive object={scene} /> <OrbitControls target={[0, 0, 0]} /> </Canvas> ); }; export default ModelViewer;
目标效果参考:
优化后的实现方案
原代码的参数配置可以调整,更贴近真实磨砂玻璃的质感,以下是修改后的代码:
import React, { useEffect } from 'react'; import { useGLTF, OrbitControls } from '@react-three/drei'; import { Canvas } from '@react-three/fiber'; import * as THREE from 'three'; const ModelViewer = ({ modelPath }) => { const { scene } = useGLTF(modelPath); useEffect(() => { scene.traverse((child) => { if (child.isMesh) { child.material = new THREE.MeshPhysicalMaterial({ color: new THREE.Color('#f0f0f0'), // 更浅的底色,贴近磨砂玻璃的通透感 transmission: 0.95, // 高透射率保证光线穿透,替代半透明实现通透 roughness: 0.8, // 提升粗糙度强化磨砂模糊效果 metalness: 0, thickness: 10, // 增加厚度让磨砂效果更自然 ior: 1.5, // 符合真实玻璃的折射率 clearcoat: 0.3, // 轻微清漆层模拟玻璃表面的光滑反射 clearcoatRoughness: 0.1, envMapIntensity: 0.4, // 适度增加环境反射提升真实感 depthWrite: false, side: THREE.DoubleSide, // 确保模型内外都显示材质 }); // 关闭transparent,避免和transmission冲突导致渲染问题 child.material.transparent = false; } }); }, [scene]); return ( <Canvas camera={{ position: [0, 350, 0], fov: 50 }} style={{ background: '#1a1a1a' }} > <ambientLight intensity={0.6} /> <directionalLight position={[8, 8, 5]} intensity={0.5} castShadow /> <directionalLight position={[-6, -6, -3]} intensity={0.3} /> <primitive object={scene} /> <OrbitControls target={[0, 0, 0]} enableDamping /> </Canvas> ); }; export default ModelViewer;
参数调整说明
- transmission:设置为0.95,用透射实现玻璃的通透感,替代原代码的
opacity(磨砂玻璃的模糊来自光线散射而非半透明) - roughness:提升到0.8,强化磨砂表面的散射模糊效果
- thickness:增加到10,让材质厚度感更强,磨砂效果更自然
- ior:调整为1.5,匹配真实玻璃的折射率
- clearcoat:添加0.3的清漆层,模拟玻璃表面的轻微光滑反射,避免完全哑光
- side: THREE.DoubleSide:确保模型内外表面都正确渲染材质
- 灯光调整:增强主方向光强度并开启阴影,提升玻璃的立体感和真实度
内容的提问来源于stack exchange,提问作者Masum Billah
相关产品推荐
相关产品推荐

