Three.js 模型热力图教程
模型热力图 ·Model Heatmap· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么ShaderMaterial 自定义着色器实现核心视觉效果OrbitControls 相机轨道交互glTF/Draco 模型加载与优化BufferGeometry 自定义顶点/索引数据requestAnimationFrame渲染循环与resize自适应效果说明本案例演示模型热力图效果基于 WebGL 实现「模型热力图」可视化效果附完整可运行源码核心用到 ShaderMaterial、OrbitControls、glTF/Draco。建议先打开文首在线案例查看动态画面再对照下方源码逐步理解。核心概念Scene / Camera / WebGLRenderer构成最小渲染闭环大场景可开logarithmicDepthBuffer缓解 Z-fighting。ShaderMaterial通过uniforms 自定义 GLSL 控制逐像素/逐点效果透明粒子常配合depthTest: false。OrbitControls提供轨道旋转/缩放开启enableDamping后需在 animate 中controls.update()。实现步骤搭建 Scene、PerspectiveCamera、WebGLRenderer挂载 canvas 并处理resize异步加载模型 / 3D Tiles / GeoJSON 等资源并加入 scene 或 entities定义 uniforms / onBeforeCompile 或 ShaderMaterial编写 GLSL 与材质参数创建 OrbitControls及 Raycaster 等交互控件若源码包含在requestAnimationFrame循环中更新状态并 renderCesium 为viewer.render或自动渲染代码要点import * as THREE from threeimport { OrbitControls } from three/examples/jsm/controls/OrbitControls.js import { GLTFLoader } from three/examples/jsm/loaders/GLTFLoader.js import { GUI } from three/examples/jsm/libs/lil-gui.module.min.jsconst box document.getElementById(box)const scene new THREE.Scene()const camera new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 1000)camera.position.set(5, 5, 5)const renderer new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })renderer.setSize(box.clientWidth, box.clientHeight)box.appendChild(renderer.domElement)new OrbitControls(camera, renderer.domElement)window.onresize () {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect box.clientWidth / box.clientHeightcamera.updateProjectionMatrix()}animate()function animate() {requestAnimationFrame(animate)renderer.render(scene, camera)}scene.add(new THREE.AmbientLight(0xffffff, 3))new GLTFLoader().load(https://z2586300277.github.io/three-editor/dist/files/resource/datacenter.glb,gltf {scene.add(gltf.scene)callModel(gltf.scene)})let model nullfunction callModel(e) { model e const box3 new THREE.Box3().setFromObject(model) const { min, max } box3 // 根据模型的包围盒 固定y 生成一个平面 const p1 new THREE.Vector3(min.x, 0, min.z) const p2 new THREE.Vector3(min.x, 0, max.z) const p3 new THREE.Vector3(max.x, 0, max.z) const p4 new THREE.Vector3(max.x, 0, min.z) const geometry new THREE.BufferGeometry() const vertices new Float32Array([ p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, p3.x, p3.y, p3.z, p4.x, p4.y, p4.z, ]) geometry.setAttribute(position, new THREE.BufferAttribute(vertices, 3)) geometry.setIndex([0, 1, 2, 0, 2, 3]) geometry.attributes.uv new THREE.Float32BufferAttribute([ 0, 0, 0, 1, 1, 1, 1, 0 ], 2) geometry.computeVertexNormals()let list []model.traverse(i { if (i.isMesh) { i.material.transparent true i.material.opacity 0.5 i.isMesh list.push(i.name) } })// list 随机获取 5 - 10 个名字形成新的数组 const randomNum Math.floor(Math.random() * (10 - 5 1)) 5 list list.sort(() Math.random() - 0.5).slice(0, randomNum)let w max.x - min.x let h max.z - min.z/热力图实现/ const arr list.map(i { const obj model.getObjectByName(i) const worldPosition new THREE.Vector3() obj.getWorldPosition(worldPosition) return [(worldPosition.x - min.x) / w, (worldPosition.z - min.z) / h, Math.random() * 10] }).flat() const uniforms1 { HEAT_MAX: { value: 10, type: number, unit: float }, PointRadius: { value: 0.2, type: number, unit: float }, intensity: { value: 3, type: number, unit: float }, PointsCount: { value: arr.length, type: number-array, unit: int }, c1: { value: new THREE.Color(green), type: color, unit: vec3 }, // 蓝色 c2: { value: new THREE.Color(red), type: color, unit: vec3 }, // 红色 uvY: { value: 1, type: number, unit: float }, uvX: { value: 1, type: number, unit: float }, opacity: { value: 0.6, type: number, unit: float }, // 稍微降低整体不透明度 edgeFalloff: { value: 2.0, type: number, unit: float } // 边缘衰减参数 }const gui new GUI() gui.add(uniforms1.HEAT_MAX, value, 0, 10).name(HEAT_MAX) gui.add(uniforms1.PointRadius, value, 0, 1).name(PointRadius) gui.add(uniforms1.intensity, value, 0, 10).name(intensity) gui.add(uniforms1.uvY, value, 0, 1).name(uvY) gui.add(uniforms1.uvX, value, 0, 1).name(uvX) gui.add(uniforms1.opacity, value, 0, 1).name(opacity) gui.add(uniforms1.edgeFalloff, value, 0.1, 5).name(边缘衰减) gui.addColor(uniforms1.c1, value).name(冷色) gui.addColor(uniforms1.c2, value).name(热色)const uniforms2 { Points: { value: arr, type: vec3-array, unit: vec3 } }const uniforms { ...uniforms1, ...uniforms2 } const vertexShader varying vec2 vUv; void main() { vUv uv; gl_Position projectionMatrixmodelViewMatrixvec4(position, 1.0); }const getFragmentShader () precision highp float;\n varying vec2 vUv; \n Object.keys(uniforms1).map(i uniform uniforms1[i].unit i ;) .join(\n)\nuniform vec3 Points[uniforms1.PointsCount.value ];vec3 gradient(float w, vec2 uv) { // 平滑过渡的热力图颜色 w pow(clamp(w, 0., 1.), 0.8); return mix(c1, c2, w); } void main() { vec2 uv vUv; uv.xy * vec2(uvX, uvY); float d 0.; // 计算热度值 for (int i 0; i PointsCount; i) { vec3 v Points[i]; float intensity v.z / HEAT_MAX; float dist length(uv - v.xy); float pd (1. - dist / PointRadius) * intensity; d pow(max(0., pd), 1.5); } // 计算边缘衰减因子 float edgeFactor 1.0; vec2 center vec2(0.5, 0.5); float distFromCenter length(uv - center); // 在UV坐标的边缘部分应用透明度衰减 float edgeStart 0.4; if (distFromCenter edgeStart) { edgeFactor 1.0 - pow((distFromCenter - edgeStart) / (0.5 - edgeStart), edgeFalloff); } // 确保边缘的透明度为0 edgeFactor clamp(edgeFactor, 0.0, 1.0); // 应用热力颜色和透明度 vec3 heatColor gradient(d, uv); float alpha min(opacityedgeFactor, d 0.05 ? 1.0 : d20.0); gl_FragColor vec4(heatColor * vec3(intensity,intensity,intensity), alpha); }const shaderMaterial new THREE.ShaderMaterial({ uniforms, vertexShader, fragmentShader: getFragmentShader(), side: THREE.DoubleSide, depthWrite: false, depthTest: false, transparent: true, blending: THREE.AdditiveBlending // 使用加法混合使热力图更具光晕效果 }) const mesh new THREE.Mesh(geometry, shaderMaterial) scene.add(mesh) }完整源码GitHub小结本文提供模型热力图完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库

相关新闻

DeepSeek    LeetCode 3430. 最多 K 个元素的子数组的最值之和 Java实现

DeepSeek LeetCode 3430. 最多 K 个元素的子数组的最值之和 Java实现

LeetCode 3430. 最多 K 个元素的子数组的最值之和题目分析给定一个整数数组 nums 和一个整数 k,需要找出所有长度不超过 k 的连续子数组中,最大值和最小值之和的总和。解题思路核心思想对于每个元素,计算它作为最大值和最小值时,对…

2026/6/29 8:48:16阅读更多 →
告别网盘限速:9大平台直链下载助手全方位指南

告别网盘限速:9大平台直链下载助手全方位指南

告别网盘限速:9大平台直链下载助手全方位指南 【免费下载链接】Online-disk-direct-link-download-assistant 一个基于 JavaScript 的网盘文件下载地址获取工具。基于【网盘直链下载助手】修改 ,支持 百度网盘 / 阿里云盘 / 中国移动云盘 / 天翼云盘 / 迅…

2026/6/29 8:48:16阅读更多 →
FakeLocation终极指南:三步掌握Android位置模拟的完整教程

FakeLocation终极指南:三步掌握Android位置模拟的完整教程

FakeLocation终极指南:三步掌握Android位置模拟的完整教程 【免费下载链接】FakeLocation Xposed module to mock locations per app. 项目地址: https://gitcode.com/gh_mirrors/fak/FakeLocation 你是否曾经想过在手机上轻松切换自己的地理位置&#xff1f…

2026/6/29 8:48:16阅读更多 →
STM32G4与DRV8353S的SPI通信实战:寄存器配置与电机驱动优化

STM32G4与DRV8353S的SPI通信实战:寄存器配置与电机驱动优化

1. DRV8353S电机驱动芯片深度解析 DRV8353S是德州仪器(TI)推出的一款高性能三相无刷直流电机门驱动器,专为工业级电机控制应用设计。我第一次接触这颗芯片是在开发一款无人机电调时,当时就被它高度集成的特性所吸引。相比传统方案需要多个分立元件搭建驱…

2026/6/29 10:03:50阅读更多 →
支付宝满减8元券,

支付宝满减8元券,

支付宝满减8元券,千问APP,发送“千问新用户专属876028”,就可以领取了,这个是官方口令,可以喝奶茶、喝星巴克、吃麦当劳,外卖至少睡呢省下8元

2026/6/29 10:03:50阅读更多 →
DDrawCompat:Windows 10/11上老游戏兼容性问题的终极解决方案

DDrawCompat:Windows 10/11上老游戏兼容性问题的终极解决方案

DDrawCompat:Windows 10/11上老游戏兼容性问题的终极解决方案 【免费下载链接】DDrawCompat DirectDraw and Direct3D 1-7 compatibility, performance and visual enhancements for Windows Vista, 7, 8, 10 and 11 项目地址: https://gitcode.com/gh_mirrors/dd…

2026/6/29 10:03:50阅读更多 →
5.8G无线技术进阶指南:从原理到PCBA方案实战

5.8G无线技术进阶指南:从原理到PCBA方案实战

1. 5.8G无线技术入门:为什么它正在取代2.4G? 第一次接触5.8G技术时,我和大多数工程师一样有个疑问:既然2.4G已经够用,为什么还要折腾更高频段?直到去年开发无线游戏耳机时,2.4G频段的干扰问题让…

2026/6/29 10:03:50阅读更多 →
大麦BP链接手动生成与实战应用指南

大麦BP链接手动生成与实战应用指南

1. 大麦BP链接是什么? 每次抢演唱会门票的时候,你是不是也遇到过页面卡死、按钮点不动的情况?这时候BP链接就能派上大用场了。简单来说,BP链接就是绕过常规购票流程,直接跳转到最终购买页面的"快捷通道"。 我…

2026/6/29 10:03:50阅读更多 →
机考环境不适应?3类典型崩溃场景,7天模拟训练方案全公开

机考环境不适应?3类典型崩溃场景,7天模拟训练方案全公开

更多请点击: https://kaifayun.com 第一章:机考环境不适应?3类典型崩溃场景,7天模拟训练方案全公开 面对陌生的考试系统界面、限时提交机制与实时判分反馈,大量考生在正式机考中遭遇突发性操作失能。我们通过分析近3年…

2026/6/29 9:53:25阅读更多 →
AI Coding 六个月真实ROI账本:产品经理的血泪教训,研发的冷静忠告

AI Coding 六个月真实ROI账本:产品经理的血泪教训,研发的冷静忠告

6个月前的2025年12月,Boris Cherny 公开宣布自己卸载了 IDE。一时间,Vibe Coding 成了全行业最热的话题。6个月后,当我们回过头来拉一份真实账本,发现事情远没有"一句话生成一个App"那么浪漫。本文从产品经理和研发两个…

2026/6/29 3:27:55阅读更多 →
审计来了,数据权限全开——审计走了,怎么确保权限全部关掉?

审计来了,数据权限全开——审计走了,怎么确保权限全部关掉?

引言:审计结束三个月了,审计员的权限还没关某城商行每年按照监管要求开展至少一次数据安全审计。审计期间,内审部门需要抽样检查各类业务数据——交易流水、客户信息、员工操作日志、权限配置记录。这些数据分布在不同系统中,审计…

2026/6/29 2:19:08阅读更多 →
如何在3秒内从普通图片生成专业级法线贴图:DeepBump的终极指南

如何在3秒内从普通图片生成专业级法线贴图:DeepBump的终极指南

如何在3秒内从普通图片生成专业级法线贴图:DeepBump的终极指南 【免费下载链接】DeepBump Normal & height maps generation from single pictures 项目地址: https://gitcode.com/gh_mirrors/de/DeepBump 还在为3D建模中的纹理制作而烦恼吗?…

2026/6/29 0:01:47阅读更多 →
OCAuxiliaryTools:终极OpenCore配置工具,让黑苹果安装从未如此简单!

OCAuxiliaryTools:终极OpenCore配置工具,让黑苹果安装从未如此简单!

OCAuxiliaryTools:终极OpenCore配置工具,让黑苹果安装从未如此简单! 【免费下载链接】OCAuxiliaryTools Cross-platform GUI management tools for OpenCore(OCAT) 项目地址: https://gitcode.com/gh_mirrors/oc/OCA…

2026/6/29 0:01:47阅读更多 →
终极Windows 11精简指南:使用tiny11builder快速创建纯净系统镜像

终极Windows 11精简指南:使用tiny11builder快速创建纯净系统镜像

终极Windows 11精简指南:使用tiny11builder快速创建纯净系统镜像 【免费下载链接】tiny11builder Scripts to build a trimmed-down Windows 11 image. 项目地址: https://gitcode.com/GitHub_Trending/ti/tiny11builder 你是否厌倦了Windows 11系统自带的20…

2026/6/29 0:01:47阅读更多 →