Threejs Materials Lighting

作者 gamedev-skillsd4b0e35550c5无许可证1.3K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库11天前更新

Light and shade a three.js scene: choose materials (MeshStandardMaterial PBR vs unlit MeshBasicMaterial), add ambient/hemisphere/directional/point/spot lights, turn on shadow maps, and use an environment map (IBL) for realistic reflections. Use when a three.js model looks black, flat, or wrong — when the user mentions three.js materials, MeshStandardMaterial, lights, shadows, envMap, or PBR. For renderer/loop setup use threejs-scene-setup; for loading models use threejs-gltf-loading.

AI 生成的概览

指导 three.js 的材质选择、灯光布置、阴影设置和环境贴图反射,解决模型发黑或发平的问题。

功能
提供选择 three.js 材质的说明与代码示例,例如 MeshStandardMaterial、MeshPhysicalMaterial 以及不受光照影响的 MeshBasicMaterial,并介绍如何添加环境光、半球光、平行光、点光和聚光灯。内容涵盖在渲染器、灯光和网格三处开启阴影贴图、调整阴影相机、使用正确色彩空间应用 PBR 贴图,以及通过 HDR 环境贴图实现基于图像的照明。还列出常见问题,如网格全黑、场景过暗、阴影缺失和颜色发白。
适用场景
当 three.js 网格显示为黑色、平淡或光照不正确时使用,也适用于用户提到 three.js 材质、MeshStandardMaterial、灯光、阴影、envMap 或 PBR 的场景。不适用于渲染器、相机或动画循环的搭建、模型加载,以及自定义 GLSL 着色器。
运行要求
不含脚本,仅为说明文档。需要 three.js 项目(示例针对 r186);基于图像的照明示例还需要通过 HDRLoader 加载的 HDR 环境文件。随附参考文件 references/materials-lights-table.md,用于查阅材质速查表和灯光参数。

three.js Materials & Lighting

Make three.js surfaces look right: pick the correct material, light the scene, enable shadows, and add image-based lighting. Patterns target r186, verified against r186 (lighting is physically based by default since r155).

When to use

  • Use when a mesh renders black or flat, when choosing a material, adding lights, enabling shadows, or setting up environment-map reflections (IBL).
  • Use when code constructs MeshStandardMaterial, DirectionalLight, etc., or sets renderer.shadowMap.enabled or scene.environment.

When not to use: the renderer/camera/loop → threejs-scene-setup. Loading models (whose PBR materials this complements) → threejs-gltf-loading. Custom GLSL/ShaderMaterial is its own topic; for the portable concept see shader-programming.

Core workflow

  1. Pick a material by need. MeshStandardMaterial (PBR: roughness, metalness, reacts to lights/IBL) for realism; MeshPhysicalMaterial for clearcoat/transmission; MeshBasicMaterial (unlit, ignores lights) for UI/flat; MeshNormalMaterial/MeshDepthMaterial for debugging.
  2. Add light, or nothing shows. Lit materials need a light source and/or scene.environment. Combine a soft fill (AmbientLight/HemisphereLight) with a key DirectionalLight.
  3. Mind light intensity. Since r155, lighting is physically based; modern intensities are higher than old tutorials (a key DirectionalLight ≈ 1–3).
  4. Enable shadows in three places. renderer.shadowMap.enabled = true, the light's castShadow = true, and each mesh's castShadow/receiveShadow. Then fit the light's shadow camera to the scene.
  5. Use an environment map for grounded reflections. Assign an equirectangular or PMREM-processed texture to scene.environment; PBR materials pick it up automatically.
  6. Verify under real lighting — confirm the surface responds to the key light (highlights move), shadows land where expected, and reflections look plausible.

Patterns

1. PBR material under a 3-light rig

js
import * as THREE from 'three';
const material = new THREE.MeshStandardMaterial({  color: 0xcc4444,  roughness: 0.5,     // 0 = mirror, 1 = fully matte  metalness: 0.0,     // 0 = dielectric (plastic/wood), 1 = metal});const mesh = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 16), material);scene.add(mesh);
// Soft sky/ground fill + a directional key light.scene.add(new THREE.HemisphereLight(0xbbddff, 0x443322, 1.0)); // sky, ground, intensityconst key = new THREE.DirectionalLight(0xffffff, 2.5);key.position.set(5, 10, 7);scene.add(key);

2. Unlit material (no light needed)

js
// MeshBasicMaterial ignores lights — for flat color, UI, or sprites/labels.const flat = new THREE.MeshBasicMaterial({ color: 0x44aa88 });// A textured color map should be tagged sRGB so colors aren't washed out:const tex = new THREE.TextureLoader().load('assets/logo.png');tex.colorSpace = THREE.SRGBColorSpace;const logo = new THREE.MeshBasicMaterial({ map: tex, transparent: true });

3. Shadows (the three required switches + camera fit)

js
renderer.shadowMap.enabled = true;renderer.shadowMap.type = THREE.PCFShadowMap;         // soft by default since r182
const sun = new THREE.DirectionalLight(0xffffff, 3);sun.position.set(8, 12, 6);sun.castShadow = true;sun.shadow.mapSize.set(2048, 2048);                   // default 512; raise for crisp// DirectionalLight uses an OrthographicCamera — fit it tightly to the scene:const cam = sun.shadow.camera;cam.near = 1; cam.far = 40;cam.left = -15; cam.right = 15; cam.top = 15; cam.bottom = -15;scene.add(sun);
mesh.castShadow = true;ground.receiveShadow = true;                          // a plane to catch the shadow

4. PBR textures on a material

js
const loader = new THREE.TextureLoader();const colorMap = loader.load('assets/brick_color.jpg');colorMap.colorSpace = THREE.SRGBColorSpace;           // color maps are sRGBconst normalMap = loader.load('assets/brick_normal.jpg'); // data maps stay linearconst roughMap  = loader.load('assets/brick_rough.jpg');
const brick = new THREE.MeshStandardMaterial({  map: colorMap,  normalMap,  roughnessMap: roughMap,  metalness: 0,});

5. Image-based lighting from an HDR environment

js
import { HDRLoader } from 'three/addons/loaders/HDRLoader.js'; // RGBELoader before r180
new HDRLoader().load('assets/studio.hdr', (hdr) => {  hdr.mapping = THREE.EquirectangularReflectionMapping;  scene.environment = hdr;     // lights + reflects all PBR materials  scene.background = hdr;       // optional: show it as the backdrop});// Optional cinematic tone curve:renderer.toneMapping = THREE.ACESFilmicToneMapping;renderer.toneMappingExposure = 1.0;

Pitfalls

  • Mesh is pure black → a lit material with no light and no scene.environment. Add a light or an environment map; to confirm geometry, temporarily swap to MeshBasicMaterial/MeshNormalMaterial.
  • Scene too dark even with lights → old tutorial intensities. r155+ is physically based; raise intensities (key light ≈ 2–3) or add an environment map.
  • Shadows don't appear → you missed one of the three switches (renderer.shadowMap.enabled, light.castShadow, mesh castShadow/ receiveShadow).
  • Shadows are cut off or blocky → the DirectionalLight's orthographic shadow.camera frustum is too big/small or doesn't cover the scene; tighten left/right/top/bottom/near/far and raise shadow.mapSize. Visualise it with new THREE.CameraHelper(light.shadow.camera).
  • Shadow acne / peter-panning → adjust light.shadow.bias (small negative) and light.shadow.normalBias.
  • Colors look washed out / too bright → color (albedo) textures need texture.colorSpace = THREE.SRGBColorSpace; normal/roughness/metalness maps must stay linear (leave them as NoColorSpace).
  • PointLight shadows tank performance → a point light renders the scene 6 times (cube map). Prefer one shadow-casting DirectionalLight; use cheaper fakes elsewhere.

References

  • For the material cheat-sheet (which Mesh*Material for which look), light types and their parameters/units, transparency vs alphaTest ordering, and the PMREMGenerator/RoomEnvironment route to IBL without an HDR file, read references/materials-lights-table.md.

Related skills

  • threejs-scene-setup — renderer, camera, and loop (set shadowMap, tone mapping).
  • threejs-gltf-loading — models arrive with PBR materials this skill tunes.
  • shader-programming — custom shader effects (engine-agnostic concept).

来源与署名

来源:gamedev-skills/awesome-gamedev-agent-skills位于skills/web-engines/threejs-materials-lighting提交d4b0e35

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架