
关于
Three.js 材质 - PBR、基础、Phong、着色器材质及材质属性。适用于网格样式设置、纹理处理、自定义着色器创建或材质性能优化。
name: threejs-materials description: Three.js 材质 - PBR、基础、Phong、着色器材质、材质属性。在为网格设置样式、处理纹理、创建自定义着色器或优化材质性能时使用。 risk: unknown source: community
Three.js 材质
快速开始
import * as THREE from "three";
// PBR material (recommended for realistic rendering)
const material = new THREE.MeshStandardMaterial({
color: 0x00ff00,
roughness: 0.5,
metalness: 0.5,
});
const mesh = new THREE.Mesh(geometry, material);
材质类型概览
| 材质 | 用途 | 光照 | |------|------|------| | MeshBasicMaterial | 无光照、纯色、线框 | 否 | | MeshLambertMaterial | 哑光表面、高性能 | 是(仅漫反射) | | MeshPhongMaterial | 光亮表面、镜面高光 | 是 | | MeshStandardMaterial | PBR、真实感材质 | 是(PBR) | | MeshPhysicalMaterial | 高级PBR、清漆、透射 | 是(PBR+) | | MeshToonMaterial | 卡通着色、动画风格 | 是(卡通) | | MeshNormalMaterial | 调试法线 | 否 | | MeshDepthMaterial | 深度可视化 | 否 | | ShaderMaterial | 自定义GLSL着色器 | 自定义 | | RawShaderMaterial | 完全着色器控制 | 自定义 |
MeshBasicMaterial
无光照计算。快速,始终可见。
const material = new THREE.MeshBasicMaterial({
color: 0xff0000,
transparent: true,
opacity: 0.5,
side: THREE.DoubleSide, // FrontSide, BackSide, DoubleSide
wireframe: false,
map: texture, // Color/diffuse texture
alphaMap: alphaTexture, // Transparency texture
envMap: envTexture, // Reflection texture
reflectivity: 1, // Env map intensity
fog: true, // Affected by scene fog
});
MeshLambertMaterial
仅漫反射光照。快速,无镜面高光。
const material = new THREE.MeshLambertMaterial({
color: 0x00ff00,
emissive: 0x111111, // Self-illumination color
emissiveIntensity: 1,
map: texture,
emissiveMap: emissiveTexture,
envMap: envTexture,
reflectivity: 0.5,
});
MeshPhongMaterial
镜面高光。适合光亮的塑料质感表面。
const material = new THREE.MeshPhongMaterial({
color: 0x0000ff,
specular: 0xffffff, // Highlight color
shininess: 100, // Highlight sharpness (0-1000)
emissive: 0x000000,
flatShading: false, // Flat vs smooth shading
map: texture,
specularMap: specTexture, // Per-pixel shininess
normalMap: normalTexture,
normalScale: new THREE.Vector2(1, 1),
bumpMap: bumpTexture,
bumpScale: 1,
displacementMap: dispTexture,
displacementScale: 1,
});
MeshStandardMaterial(PBR)
基于物理的渲染。推荐用于真实感效果。
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.5, // 0 = mirror, 1 = diffuse
metalness: 0.0, // 0 = dielectric, 1 = metal
// Textures
map: colorTexture, // Albedo/base color
roughnessMap: roughTexture, // Per-pixel roughness
metalnessMap: metalTexture, // Per-pixel metalness
normalMap: normalTexture, // Surface detail
normalScale: new THREE.Vector2(1, 1),
aoMap: aoTexture, // Ambient occlusion (uses uv2!)
aoMapIntensity: 1,
displacementMap: dispTexture, // Vertex displacement
displacementScale: 0.1,
displacementBias: 0,
// Emissive
emissive: 0x000000,
emissiveIntensity: 1,
emissiveMap: emissiveTexture,
// Environment
envMap: envTexture,
envMapIntensity: 1,
// Other
flatShading: false,
wireframe: false,
fog: true,
});
// Note: aoMap requires second UV channel
geometry.setAttribute("uv2", geometry.attributes.uv);
MeshPhysicalMaterial(高级PBR)
扩展MeshStandardMaterial,提供高级特性。
const material = new THREE.MeshPhysicalMaterial({
// All MeshStandardMaterial properties plus:
// Clearcoat (car paint, lacquer)
clearcoat: 1.0, // 0-1 clearcoat layer strength
clearcoatRoughness: 0.1,
clearcoatMap: ccTexture,
clearcoatRoughnessMap: ccrTexture,
clearcoatNormalMap: ccnTexture,
clearcoatNormalScale: new THREE.Vector2(1, 1),
// Transmission (glass, water)
transmission: 1.0, // 0 = opaque, 1 = fully transparent
transmissionMap: transTexture,
thickness: 0.5, // Volume thickness for refraction
thicknessMap: thickTexture,
attenuationDistance: 1, // Absorption distance
attenuationColor: new THREE.Color(0xffffff),
// Refraction
ior: 1.5, // Index of refraction (1-2.333)
// Sheen (fabric, velvet)
sheen: 1.0,
sheenRoughness: 0.5,
sheenColor: new THREE.Color(0xffffff),
sheenColorMap: sheenTexture,
});
兼容工具
Claude CodeCursor
标签
前端开发