Three.js - 雾(十四)

这篇博客介绍了如何在three.js中使用Fog和FogExp2创建雾效。Fog是线性雾,雾的密度随距离线性增大;FogExp2是指数雾,远处雾浓度增长更快。通过设置雾的颜色、距离和密度,可以模拟不同环境的雾气效果。同时,雾效仅对场景中物体生效,可通过调整材质的fog属性来控制是否受影响。
摘要由CSDN通过智能技术生成

  • 雾通常是基于离摄像机的距离褪色至某种特定颜色的方式。
  • three.js中有两种设置雾的对象:
  1. .Fog() 定义了线性雾。简单来说就是雾的密度是随着距离线性增大的。
  2. .FogExp2() 定义了指数雾。在相机附近提供清晰的视野,且距离相机越远,雾的浓度随着指数增长越快。

基础模版

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>学习</title>
  </head>
  <body>
    <canvas id="c2d" class="c2d" width="1000" height="500"></canvas>
    <script type="module">
      import * as THREE from 'https://threejs.org/build/three.module.js'
      import { OrbitControls } from 'https://threejsfundamentals.org/threejs/resources/threejs/r132/examples/jsm/controls/OrbitControls.js'

      const canvas = document.querySelector('#c2d')
      // 渲染器
      const renderer = new THREE.WebGLRenderer({ canvas })

      const fov = 75 // 视野范围
      const aspect = 2 // 相机默认值 画布的宽高比
      const near = 0.1 // 近平面
      const far = 10 // 远平面
      // 透视投影相机
      const camera = new THREE.PerspectiveCamera(fov, aspect, near, far)
      camera.position.set(0, 0, 10)
      camera.lookAt(0, 0, 0)
      // 控制相机
      const controls = new OrbitControls(camera, canvas)
      controls.update()

      // 场景
      const scene = new THREE.Scene()

      // 渲染
      function render(time) {
        time *= 0.001

        const rot = time
        cube.rotation.x = rot
        cube.rotation.y = rot

        renderer.render(scene, camera)
        requestAnimationFrame(render)
      }

      requestAnimationFrame(render)
    </script>
  </body>
</html>

Fog

属性

  1. .name 对象的名称。
  2. .color 雾的颜色。
  3. .near 应用雾的最小距离。任何物体比 near 近不会受到影响。
  4. .far 应用雾的最大距离。任何物体比 far 远则完全是雾的颜色。
  • 需要注意雾是作用在渲染的物体上的,想要实现效果就需要设定雾 场景的背景颜色为同一种颜色。
  • 雾设置的最小距离,最大距离都是以相机所在位置计算的。
      {
        const near = 1
        const far = 11
        const color = 'lightblue'
        scene.fog = new THREE.Fog(color, near, far)
        scene.background = new THREE.Color(color)
      }

      {
        const color = 0xffffff
        const intensity = 1
        const light = new THREE.DirectionalLight(color, intensity)
        light.position.set(-1, 2, 4)
        scene.add(light)
      }
      const box = 3
      const geometry = new THREE.BoxGeometry(box, box, box)
      const material = new THREE.MeshPhongMaterial({ color: 0x8844aa })
      const cube = new THREE.Mesh(geometry, material)
      scene.add(cube)

1.gif

  • fog在材质上有个布尔属性,用来设置材质是否会受到雾的影响。默认是true
material.fog = false

image.png

FogExp2

属性

  1. .name 对象的名称。
  2. .color 雾的颜色。
  3. .density 定义雾的密度将会增长多块。
  • 使用方式和Fog一样,它效果更接近真实环境。
      {
        const color = 'lightblue'
        const density = 0.1
        scene.fog = new THREE.FogExp2(color, density)
        scene.background = new THREE.Color(color)
      }

1.gif

总结

在开发中使用更多的是Fog,因为它可以设定影响区域,你可以设定一定距离内显示清晰的场景,过了这段距离开始产生雾的效果。还要灵活的使用材质上的fog属性,比如在房间里设置窗外是雾,不设置fog属性在房间内的物体也会出现雾的效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值