1️⃣6️⃣three.js_光源

16、光源

  • 3D虚拟工厂在线体验

  • 在 Three.js 中,环境光(AmbientLight)、点光源(PointLight)、平行光(DirectionalLight)、 聚光灯(SpotLight)、半球光(HemisphereLight)是几种常用的光源类型。

  • blender 中的日光、点光、面光、聚光:
    请添加图片描述

  • three.js:环境光、点光源、平行光、聚光灯、半球光:
    请添加图片描述

开启地面接收物体阴影

 const tempScene = scene.children.find(t => t.name == 'Scene')
 const tempFactoryGround = tempScene.children.find(t => t.name == '厂区');
 tempFactoryGround.receiveShadow = true; // 地面接收阴影

renderer开启阴影映射

renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // 更柔和的阴影

16.1、环境光

//===== 1. 添加一个小物体(测试聚光灯照射) =====
const sphereGeometry = new THREE.SphereGeometry(1, 32, 32);
const sphereMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
sphere.position.set(200, 5, -20); // 放在聚光灯照射范围内
sphere.castShadow = true;
scene.add(sphere); 
// ===== 2. 环境光(AmbientLight) =====
 const ambientLight = new THREE.AmbientLight(0x666666, 5) // 柔和的白光
scene.add(ambientLight)

✅执行结果:
请添加图片描述

  • 环境光(AmbientLight)会均匀地照亮场景中的所有物体,没有方向性,也不会产生阴影。场景中的基础亮度,好比阴天的自然光,避免纯黑区域。

16.2、点光源

 // ===== 3. 点光源(PointLight) =====
const pointLight = new THREE.PointLight(0xff0000, 5); // 红色,强度5,范围
pointLight.decay = 0.2//设置光源距离衰减度
pointLight.position.set(190, 10, -20);
pointLight.castShadow = true;
pointLight.shadow.mapSize.width = 1024; // 提高阴影质量
pointLight.shadow.mapSize.height = 1024;
scene.add(pointLight); 
const pointLightHelper = new THREE.PointLightHelper(pointLight, 1, 0x00ff00) //光源辅助观察
scene.add(pointLightHelper);

✅执行结果:
请添加图片描述

  • 点光源(PointLight)从一个点向所有方向发射光线(类似灯泡),设置为红色,强度为5时,可以看到中间的圆球在地面上投射出阴影。要使阴影正常显示,需要满足以下条件:
    1. 物体必须开启阴影投射:sphere.castShadow = true;
    2. 地面必须开启阴影接收:tempFactoryGround.receiveShadow = true;
    3. 渲染器需启用阴影映射:renderer.shadowMap.enabled = true;
    4. 点光源(或其他光源)需启用阴影投射:pointLight.castShadow = true;

16.3、平行光

// ===== 4. 平行光(DirectionalLight) =====
const directionalLight = new THREE.DirectionalLight(0x0000ff, 1); // 蓝色,强度1
directionalLight.position.set(200, 10, -20);
directionalLight.target.position.set(200, 0, -20); // 看向正下方
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;

// 扩大阴影相机的视锥范围
directionalLight.shadow.camera.left = -50;  // 左边界
directionalLight.shadow.camera.right = 50;  // 右边界
directionalLight.shadow.camera.top = 50;    // 上边界
directionalLight.shadow.camera.bottom = -50; // 下边界
directionalLight.shadow.camera.near = 0.1;  // 近平面
directionalLight.shadow.camera.far = 100;   // 远平面
directionalLight.shadow.camera.updateProjectionMatrix();// 更新投影矩阵

scene.add(directionalLight);
scene.add(directionalLight.target); // 将 target 添加到场景 
const dirLightHelper = new THREE.DirectionalLightHelper(directionalLight, 2, 0xffff00) //辅助平行光
scene.add(dirLightHelper);

✅执行结果:
请添加图片描述

  • 平行光(DirectionalLight) 模拟无限远处的光源(如太阳光),所有光线平行照射。将其设置为蓝色、强度为1时,可以观察到中间的圆球在地面上投射出清晰的阴影。

16.4、聚光灯

// ===== 5. 创建聚光灯(SpotLight) =====
const spotLight = new THREE.SpotLight(
  0xffffff,     // 颜色
  30,            // 强度
  100,          // 照射距离(增大)
  Math.PI / 6,  // 角度(30°)
  0.5,          // 边缘柔化
  0.5           // 衰减
);
spotLight.position.set(210, 50, -20); // 提高光源高度,扩大阴影范围
spotLight.target.position.set(210, 0, -20); // 照射目标点
spotLight.castShadow = true;
spotLight.shadow.mapSize.width = 1024; // 提高阴影分辨率
spotLight.shadow.mapSize.height = 1024;
scene.add(spotLight);
scene.add(spotLight.target);  
const spotLightHelper = new THREE.SpotLightHelper(spotLight,0x00ff00);// 添加聚光灯 Helper(辅助观察器),设置为绿色
scene.add(spotLightHelper);

✅执行结果:
请添加图片描述

  • 聚光灯(SpotLight) 模拟手电筒或舞台灯光效果,光线呈锥形照射。此时可以观察到中间圆球在地面上投射出清晰的阴影。

16.5、半球光

// ===== 6. 创建半球光(HemisphereLight) =====
const hemisphereLight = new THREE.HemisphereLight(
  0x87CEEB, // 天空颜色(天蓝色)
  0x708090, // 地面颜色(灰蓝色)
  5       // 强度
);
hemisphereLight.position.set(220, 0, -20); // 设置半球光位置(虽然无实际作用,但 Helper 需要)
scene.add(hemisphereLight); 
// 添加半球光 Helper(辅助观察器)
const hemisphereLightHelper = new THREE.HemisphereLightHelper(hemisphereLight, 2,0x00ff00);
scene.add(hemisphereLightHelper);

✅执行结果:
请添加图片描述

  • 半球光(HemisphereLight) 模拟自然光照效果,通过分别设置天空(上方)和地面(下方)的不同颜色,形成柔和的渐变环境光。这种光源比普通的环境光(AmbientLight)更接近真实自然光的散射效果,适合营造户外场景的自然照明。
  • 点击【专栏目录】查看专栏其他内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

3D虚拟工厂

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值