18.Three.js中AmbientLightProbe 环境光探针详解(含 Vue 3 实战示例)

在 Three.js 的光照系统中,AmbientLightProbe 是一个不太常见但非常有用的光源类型。相比于 AmbientLight 的简单均匀照明,AmbientLightProbe 更真实地模拟了来自各个方向的环境光照。

🧠 关键词THREE.AmbientLightProbe、环境光探针、立体光照、Vue3 Composition API、真实感照明


🌍 一、AmbientLight vs AmbientLightProbe

光照类型特性是否有方向常见用途
AmbientLight简单均匀照亮所有物体场景基础照明
AmbientLightProbe模拟各个方向来的环境光✅(但不可控制方向)与环境贴图结合,提高真实感

🔍 AmbientLightProbe 基于三阶球谐函数(SH)采样环境贴图(environment)中的光照信息来创建一个更加真实的环境光。


🔧 二、如何使用 AmbientLightProbe?

✅ 基本用法:

const lightProbe = new THREE.AmbientLightProbe(0xffffff, 1); scene.add(lightProbe);

但是,仅仅添加 AmbientLightProbe 并不会有特别的视觉变化❗️——它需要配合 WebGLRenderer.environment(环境贴图)或 PMREMGenerator

📦 搭配 PMREM 示例(最常见)

const pmremGenerator = new THREE.PMREMGenerator(renderer);
const envMap = pmremGenerator.fromScene(yourEnvironmentScene).texture;

scene.environment = envMap;

const lightProbe = new THREE.AmbientLightProbe();
scene.add(lightProbe);

🎓 三、源码深入理解 AmbientLightProbe

AmbientLightProbe 实现于 src/lights/AmbientLightProbe.js,是基于 LightProbe 的封装:

class AmbientLightProbe extends LightProbe {
  constructor(color = 0xffffff, intensity = 1) {
    super();
    this.sh.fromArray( [
      0.282095 * intensity * color.r,
      0.282095 * intensity * color.g,
      0.282095 * intensity * color.b,
      // 其余六项为0
    ] );
  }
}

🧠 知识点解释

  • this.shSphericalHarmonics3 的一个对象,用来存储球谐函数数据。

  • 只对第一个 SH 系数(代表均匀分布)赋值,其余方向光为 0。


🛠️ 四、何时使用 AmbientLightProbe?

🎯 推荐使用场景:

  • 使用了真实环境贴图(如 .hdr 文件)。

  • 想模拟高质量的环境光,不只是一团白光。

  • 需要支持 MeshStandardMaterialMeshPhysicalMaterial 的高级光照。


🧪 五、🌈 Vue 3 + Three.js Composition API 示例

以下是一个使用 Vue 3 Composition API 的示例,演示如何使用 AmbientLightProbe 🌟

📁 文件结构建议:

📦 src/
 ┣ 📂 components/
 ┃ ┗ 🧩 AmbientLightProbeScene.vue
 ┗ 📜 main.js

🔹 AmbientLightProbeScene.vue

<template>
  <div ref="container" class="w-full h-full"></div>
</template>

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

const container = ref<HTMLDivElement | null>(null)

onMounted(() => {
  const scene = new THREE.Scene()
  const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
  camera.position.z = 3

  const renderer = new THREE.WebGLRenderer({ antialias: true })
  renderer.setSize(window.innerWidth, window.innerHeight)
  container.value?.appendChild(renderer.domElement)

  // 📦 几何体 + 材质
  const geometry = new THREE.SphereGeometry(1, 64, 64)
  const material = new THREE.MeshStandardMaterial({ metalness: 0.6, roughness: 0.4 })
  const sphere = new THREE.Mesh(geometry, material)
  scene.add(sphere)

  // 🔆 环境贴图加载
  const pmremGenerator = new THREE.PMREMGenerator(renderer)
  pmremGenerator.compileEquirectangularShader()

  const rgbeLoader = new THREE.RGBELoader()
  rgbeLoader.load('/env.hdr', (texture) => {
    const envMap = pmremGenerator.fromEquirectangular(texture).texture
    scene.environment = envMap
    texture.dispose()

    // 🌟 添加环境光探针
    const probe = new THREE.AmbientLightProbe()
    scene.add(probe)

    render()
  })

  // ✨ 灯光(可选)
  const dirLight = new THREE.DirectionalLight(0xffffff, 0.5)
  dirLight.position.set(3, 3, 3)
  scene.add(dirLight)

  const render = () => {
    renderer.render(scene, camera)
  }

  const animate = () => {
    requestAnimationFrame(animate)
    sphere.rotation.y += 0.01
    render()
  }

  animate()
})
</script>

<style scoped>
div {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
</style>

🎉 效果说明:

  • 支持使用 HDR 环境贴图渲染真实金属光照。

  • 通过 AmbientLightProbe 让反射更真实 🎯

  • Vue 3 的 Composition API 结构清晰、易维护 😎


🧠 六、小贴士

AmbientLightProbe 通常不单独使用,需要配合 environmentMap 才能达到应有效果。

🚫 不支持 castShadow,因为它是无方向的环境光。

🌟 更适用于 PBR 材质系统(如 MeshStandardMaterial,不建议在 MeshBasicMaterial 上使用,因为看不到效果。


🧾 七、参考资料

  • 🔗 Three.js 官方文档

  • 🔗 Three.js 示例 - light probe

  • 📚 《Real-Time Rendering》书中关于球谐函数的章节


🎉 总结

THREE.AmbientLightProbe 是提升 Three.js 场景真实感的重要工具!尤其在使用 HDRI 环境贴图和 PBR 材质时,它可以带来质的飞跃。

✨ 如果你想打造一个专业级的 Web 3D 应用,别忘了试试这个“隐形高手”!


📌 觉得文章有帮助?点个赞👍 收藏📂 评论📝!一起交流 Three.js 的奇妙世界吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吉檀迦俐

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

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

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

打赏作者

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

抵扣说明:

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

余额充值