15.Three.js PointLight 知识详解 + Vue3 实战案例

🎯 本文将带你全方位了解 Three.js 中的 PointLight 点光源,包括它的属性、使用方式、配套工具以及如何在 Vue 3 中使用 Composition API 构建一个完整可视化场景 ✨!


📖 一、什么是 PointLight?

PointLight 是 Three.js 中一种 点光源,从一个点向所有方向发射光线,类似生活中的 灯泡💡

const light = new THREE.PointLight(color, intensity, distance, decay);


🔍 二、构造函数参数详解

参数名类型默认值含义 📝
colorColor0xffffff光的颜色 🎨
intensityNumber1光照强度(越大越亮)🌞
distanceNumber0光线照射距离,0表示无限远 🚫📏
decayNumber1衰减系数,代表光强随距离减弱程度(需开启 physicallyCorrectLights)

👉 注意distancedecay 共同控制光线的衰减效果,模拟真实场景下的照明逻辑。


🎛️ 三、常用属性

属性名类型含义
.powerNumber光源功率(单位流明),power = intensity × 4π 💥
.decayNumber衰减系数(见上)
.distanceNumber最大照射距离
.castShadowBoolean是否投射阴影(默认为 false)🌑
.shadowLightShadow阴影属性配置对象


🧪 四、辅助工具:PointLightHelper

const helper = new THREE.PointLightHelper(pointLight, sphereSize, color); scene.add(helper);

✅ 可视化点光源位置和范围,调试好帮手!


🧱 五、完整基础示例

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, w / h, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();

const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);

const pointLight = new THREE.PointLight(0xffffff, 1, 100);
pointLight.position.set(10, 10, 10);
pointLight.castShadow = true;
scene.add(pointLight);

const helper = new THREE.PointLightHelper(pointLight);
scene.add(helper);

💻 六、Vue 3 + Three.js Composition API 实战案例

👇 创建一个 Vue 3 组件,渲染立方体并加上点光源 🌈

<script setup lang="ts">
import { onMounted } from 'vue';
import * as THREE from 'three';

onMounted(() => {
  const scene = new THREE.Scene();
  const camera = new THREE.PerspectiveCamera(75, 800 / 600, 0.1, 1000);
  const renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.setSize(800, 600);
  document.getElementById('three-container')?.appendChild(renderer.domElement);

  const geometry = new THREE.BoxGeometry();
  const material = new THREE.MeshStandardMaterial({ color: 0x44aa88 });
  const cube = new THREE.Mesh(geometry, material);
  scene.add(cube);

  const pointLight = new THREE.PointLight(0xffffff, 1, 50, 2);
  pointLight.position.set(10, 10, 10);
  pointLight.castShadow = true;
  scene.add(pointLight);

  const helper = new THREE.PointLightHelper(pointLight);
  scene.add(helper);

  camera.position.z = 5;

  const animate = () => {
    requestAnimationFrame(animate);
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;
    renderer.render(scene, camera);
  };
  animate();
});
</script>

<template>
  <div id="three-container" class="w-[800px] h-[600px]"></div>
</template>

<style scoped>
#three-container {
  border: 1px solid #ccc;
}
</style>

🌈 七、PointLight 常见使用场景

场景说明
局部照明如桌面台灯、手电筒效果等
多光源构建氛围灯光和 AmbientLight/SpotLight 搭配使用
与材质联动MeshStandardMaterial/MeshPhysicalMaterial 支持真实光照模型 🧪

📌 八、性能优化建议

  • ✅ 尽量控制光源数量(尤其是投影)

  • ✅ 减少阴影贴图大小或频率

  • ✅ 使用 light.visible = false 动态关闭不必要光源

  • ✅ 结合 LightHelper 可视化调试区域,精准控制距离和衰减范围


✅ 总结一下!

🎓 通过本文你学到了:

  • PointLight 的构造参数与作用 🏗️

  • 光源属性与阴影配置 ☀️

  • Vue 3 Composition API + Three.js 实战 💻

  • 优化建议与常见问题 ⚙️


📢 下一篇预告:RectAreaLight 高级光源实战 + 动态移动效果实现 🚀

如果你觉得这篇文章对你有帮助,记得 👉 点赞 + 收藏 + 评论 支持我写下去 🙌!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吉檀迦俐

你的鼓励奖是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值