3D热力图 ·Heatmap 3D· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么ShaderMaterial 自定义着色器实现核心视觉效果OrbitControls 相机轨道交互Canvas 动态纹理贴图requestAnimationFrame渲染循环与resize自适应效果说明本案例演示3D热力图效果用 Canvas 2D 绘制内容并实时映射为 Three.js 纹理核心用到 ShaderMaterial、OrbitControls、Canvas。建议先打开文首在线案例查看动态画面再对照下方源码逐步理解。核心概念Scene / Camera / WebGLRenderer构成最小渲染闭环大场景可开logarithmicDepthBuffer缓解 Z-fighting。ShaderMaterial通过uniforms 自定义 GLSL 控制逐像素/逐点效果透明粒子常配合depthTest: false。OrbitControls提供轨道旋转/缩放开启enableDamping后需在 animate 中controls.update()。CanvasTexture每帧或按需把 2D Canvas 内容上传 GPU适合动态文字、图表、视频帧贴图。实现步骤搭建 Scene、PerspectiveCamera、WebGLRenderer挂载 canvas 并处理resize定义 uniforms / onBeforeCompile 或 ShaderMaterial编写 GLSL 与材质参数创建 OrbitControls及 Raycaster 等交互控件若源码包含在requestAnimationFrame循环中更新状态并 renderCesium 为viewer.render或自动渲染代码要点import * as THREE from three;import { OrbitControls } from three/examples/jsm/controls/OrbitControls.js import * as dat from dat.gui /heatmap.js 自行安装module 方式引入 此处我为src 方式引入/const DOM document.getElementById(box)const scene new THREE.Scene()const camera new THREE.PerspectiveCamera(75, DOM.clientWidth / DOM.clientHeight, 0.1, 1000)camera.position.set(0, 40, 40)const renderer new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })renderer.setSize(DOM.clientWidth, DOM.clientHeight)DOM.appendChild(renderer.domElement)new OrbitControls(camera, renderer.domElement)scene.add(new THREE.AxesHelper(500), new THREE.AmbientLight(0xffffff, 3))window.onresize () {renderer.setSize(DOM.clientWidth, DOM.clientHeight)camera.aspect DOM.clientWidth / DOM.clientHeightcamera.updateProjectionMatrix()}animate()// 渲染 function animate() {renderer.render(scene, camera)requestAnimationFrame(animate)}const getRandom (max, min) Math.round((Math.random()(max - min 1) min)10) / 10var heatmap h337.create({ container: document.createElement(div), width: 256, height: 256, blur: 0.8, radius: 10 });var i 0, max 10, data []; while (i 2000) { data.push({ x: getRandom(1, 256), y: getRandom(1, 256), value: getRandom(1, 6) }); i; }heatmap.setData({ max: max, data: data });const texture new THREE.CanvasTexture(heatmap._renderer.canvas); const geometry new THREE.PlaneGeometry(50, 50, 1000, 1000); geometry.rotateX(-Math.PI * 0.5);const material new THREE.ShaderMaterial({ uniforms: { heightMap: { value: texture }, heightRatio: { value: 5 } }, vertexShader:uniform sampler2D heightMap; uniform float heightRatio; varying vec2 vUv; varying float hValue; varying vec3 cl; void main() { vUv uv; vec3 pos position; cl texture2D(heightMap, vUv).rgb; hValue texture2D(heightMap, vUv).r; pos.y hValue * heightRatio; gl_Position projectionMatrixmodelViewMatrixvec4(pos,1.0); }, fragmentShader:varying float hValue; varying vec3 cl; void main() { float v abs(hValue - 1.); gl_FragColor vec4(cl, .8 - v * v) ; }, transparent: true, })const mesh new THREE.Mesh(geometry, material); scene.add(mesh);new dat.GUI().add(mesh.material.uniforms.heightRatio, value, 1, 15).name(heightRatio)完整源码GitHub小结本文提供3D热力图完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库