DirectionalLight:平行光(太阳光)
DirectionalLight 模拟太阳光:光线方向一致、不会随距离衰减。它很适合作为室外主光源,也是最常用的“有阴影光”之一。
基本用法
ts
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 10, 5);
scene.add(light);阴影(关键)
要看到阴影,至少要满足三件事:
renderer.shadowMap.enabled = true- 光源
light.castShadow = true - 物体
castShadow/receiveShadow设对
ts
renderer.shadowMap.enabled = true;
light.castShadow = true;
mesh.castShadow = true;
ground.receiveShadow = true;辅助工具
ts
const helper = new THREE.DirectionalLightHelper(light, 1);
scene.add(helper);常见坑
- 阴影“锯齿/抖动”:提高
shadow.mapSize、调整相机裁剪面与shadow.camera范围 - 阴影“看不到”:常见是物体/地面没有开启
castShadow/receiveShadow
