尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

Three.js 场景雾化教程

Three.js 场景雾化教程
📅 发布时间:2026/6/30 21:05:21

场景雾化 ·Scene Fog· ▶ 在线运行案例

  • 案例合集:三维可视化功能案例(threehub.cn)
  • 开源仓库github地址:https://github.com/z2586300277/three-cesium-examples
  • 400个案例代码:网盘链接

你将学到什么

  • Fog(线性雾)与FogExp2(指数雾)的区别
  • scene.fog与renderer.setClearColor颜色一致的重要性
  • 远距离物体如何被雾自然隐没

效果说明

场景中有一只Fox 模型(z = -500,很远)和一块放大的地面(较近)。开启雾后,远处狐狸逐渐融入雾色看不见,近处地面清晰。GUI 可切换:

  • 雾类型:linear / exp2
  • 启用/关闭雾
  • 雾颜色、near/far(线性)或 density(指数)

核心概念

雾如何工作?

雾在片元着色阶段根据像素到相机的距离,将物体颜色向雾色混合:

最终颜色 = mix(物体颜色, 雾色, fogFactor)

| 类型 | 构造函数 | fogFactor 依据 | |------|---------|----------------| |Fog线性雾 |new THREE.Fog(color, near, far)| near 内无雾,far 外全雾,之间线性 | |FogExp2指数雾 |new THREE.FogExp2(color, density)| 距离越远指数增长,更自然 |

// 线性雾:10~800 单位之间渐变

scene.fog = new THREE.Fog(0xffffff, 10, 800);

// 指数雾:density 越大雾越浓 scene.fog = new THREE.FogExp2(0xffffff, 0.005);

背景色必须匹配

renderer.setClearColor(fogColor);

scene.fog = new THREE.Fog(fogColor, near, far);

若setClearColor与fog.color不一致,地平线会出现明显色差接缝。

本案例场景布局

// 狐狸在很远的 -Z

gltf.scene.position.set(0, 0, -500);

// 地面放大 10 倍,偏移到相机前方 model.scale.set(10, 10, 10); model.position.z += 60; model.position.x -= 200;

far = 800时,狐狸距相机约 500+,已在雾区边缘,适合演示「远物消失」。

logarithmicDepthBuffer

本案例 Renderer 开了logarithmicDepthBuffer: true,大near/far 跨度(0.1 ~ 100000)时减轻 Z-fighting,与雾效配合展示大场景。

实现步骤

  • Scene + 相机远裁剪far: 100000+ 灯光
  • 加载远距 Fox + 近距地面 glb
  • getFog(type, color)工厂函数创建两种雾
  • GUI 切换 type / enable,动态setFogFolder重建参数面板
  • 改雾色时同步renderer.setClearColor
  • 代码要点

    import * as THREE from 'three'

    import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js' import { GUI } from 'three/addons/libs/lil-gui.module.min.js'

    const box = document.getElementById('box')

    const scene = new THREE.Scene()

    const camera = new THREE.PerspectiveCamera(50, box.clientWidth / box.clientHeight, 0.1, 100000)

    camera.position.set(0, 20, 60)

    const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })

    renderer.setSize(box.clientWidth, box.clientHeight)

    box.appendChild(renderer.domElement)

    const controls = new OrbitControls(camera, renderer.domElement)

    controls.enableDamping = true

    animate()

    function animate() {

    requestAnimationFrame(animate)

    controls.update()

    renderer.render(scene, camera)

    }

    scene.add(new THREE.AmbientLight(0xffffff, 1))

    const directionalLight = new THREE.DirectionalLight(0xffffff, 1)

    directionalLight.position.set(0, 100, 0)

    scene.add(directionalLight)

    window.onresize = () => {

    renderer.setSize(box.clientWidth, box.clientHeight)

    camera.aspect = box.clientWidth / box.clientHeight

    camera.updateProjectionMatrix()

    }

    new GLTFLoader().load(GLOBAL_CONFIG.getFileUrl('files/model/Fox.glb'), (gltf) => {

    gltf.scene.position.set(0, 0, -500)

    scene.add(gltf.scene)

    })

    new GLTFLoader().load(GLOBAL_CONFIG.getFileUrl('models/glb/foorGround.glb'), (gltf) => {

    const model = gltf.scene

    model.position.z += 60

    model.position.x -= 200

    model.scale.set(10, 10, 10)

    scene.add(model)

    })

    function getFog(type, color) {

    renderer.setClearColor(color || 0xffffff)

    if (type === 'linear') return new THREE.Fog(color || 0xffffff, 10, 800)

    else return new THREE.FogExp2(color || 0xffffff, 0.005)

    }

    const folder = new GUI()

    let fogFolder = null

    const fogOption = { type: scene.fog instanceof THREE.FogExp2 ? 'exp2' : 'linear', enable: !!scene.fog }

    folder.add(fogOption, 'type', ['linear', 'exp2']).name('雾类型').onChange((v) => {

    scene.fog = getFog(v, scene.fog?.color)

    setFogFolder(v)

    })

    folder.add(fogOption, 'enable').name('启用雾').onChange((v) => {

    if (v) scene.fog = getFog(fogOption.type)

    else scene.fog = null

    setFogFolder(fogOption.type)

    })

    fogOption.enable && setFogFolder(fogOption.type)

    function setFogFolder(type) {

    if (fogFolder) {

    fogFolder.destroy?.()

    fogFolder = null

    }

    if (!scene.fog) return

    fogFolder = folder.addFolder(type + '雾')

    fogFolder.addColor(scene.fog, 'color').name('颜色').onChange((v) => renderer.setClearColor(v))

    if (type === 'linear') {

    fogFolder.add(scene.fog, 'near').name('近点')

    fogFolder.add(scene.fog, 'far').name('远点')

    }

    else fogFolder.add(scene.fog, 'density').name('密度').min(0).max(0.1).step(0.00001)

    }

    完整源码:GitHub

    小结

    • 本文提供场景雾化完整 Three.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
    • 更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库

相关新闻

  • Java SpringBoot+Vue3+MyBatis 影城会员管理系统系统源码|前后端分离+MySQL数据库
  • 5步实现高效矿石定位:Advanced XRay模组深度解析与实战指南
  • 终极窗口置顶神器:3分钟告别多窗口遮挡烦恼

最新新闻

  • 如何免费解锁加密音乐文件:Unlock-Music完整指南
  • NestJS静态资源访问避坑指南:如何正确配置useStaticAssets让你的上传图片能被前端访问到
  • 账号别只看粉丝
  • 从 Hugging Face 到生产集群:开源模型部署的全链路实战
  • 何为实战派AI落地培训?任务驱动式AI特训营完整体系拆解
  • 2026年国内口碑好的电力测功机销售厂家,究竟有哪些值得关注?

日新闻

  • 2026年6月公司网站搭建最新热门渠道测评:四大低成本/零代码平台对比+避坑
  • 【Linux】Linux arm 编译QT程序,出现expected “}“报错
  • 【MATLAB例程】四基站二维AOA定位与距离辅助增强对比仿真。基于角度观测和测距修正的固定目标平面定位精度分析

周新闻

  • Windows字体自定义终极方案:No!! MeiryoUI完全指南
  • Deepin Boot Maker:告别命令行,3分钟制作Linux启动盘的智能解决方案
  • Plain Craft Launcher 2:重新定义你的Minecraft游戏体验

月新闻

  • 2026年6月公司网站搭建最新热门渠道测评:四大低成本/零代码平台对比+避坑
  • 【Linux】Linux arm 编译QT程序,出现expected “}“报错
  • 【MATLAB例程】四基站二维AOA定位与距离辅助增强对比仿真。基于角度观测和测距修正的固定目标平面定位精度分析

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号