当前位置: 首页 > article >正文

three.js 热力图

使用three.js 和 heatMap.js 实现在 三维场景中展示热力图的效果,以下代码复制粘贴即可在你的本机运行。

在线编辑运行预览可方位 https://threehub.cn/#/codeMirror?navigation=ThreeJS&classify=expand&id=heatmap3D
在 https://threehub.cn 中还有很多案例

在这里插入图片描述

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://z2586300277.github.io/three-cesium-examples/js/heatmap.js"></script>
<script type="importmap">
{
    "imports": {
        "three": "https://threejs.org/build/three.module.min.js",
        "three/addons/": "https://threejs.org/examples/jsm/",
        "three/examples/jsm/": "https://threejs.org/examples/jsm/",
        "gsap": "https://z2586300277.github.io/3d-file-server/js/gsap/index.js",
        "postprocessing": "https://z2586300277.github.io/three-cesium-examples/js/postprocessing.js",
        "dat.gui": "https://z2586300277.github.io/three-cesium-examples/js/dat.gui.module.js",
        "@tweenjs/tween.js": "https://z2586300277.github.io/three-cesium-examples/js/tween.esm.js"
    }
}
</script>
<style>
    body {
        margin: 0;
        padding: 1px;
        box-sizing: border-box;
        background-color: #1f1f1f;
        display: flex;
        flex-direction: column;
        width: 100vw;
        height: 100vh;
        overflow: hidden;
    }
    #box {
        width: 100%;
        height: 100%;
    }
</style>
</head>
<body>
<div id="box"></div>
<script type="module">
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.clientHeight

    camera.updateProjectionMatrix()

}

animate()

// 渲染 
function animate() {

    renderer.render(scene, camera)

    requestAnimationFrame(animate)

}

const getRandom = (max, min) => Math.round((Math.random() * (max - min + 1) + min) * 10) / 10

var 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 = projectionMatrix * modelViewMatrix * vec4(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")






</script>
</body>
</html>

http://www.kler.cn/news/312933.html

相关文章:

  • Android Perfetto 学习
  • 聚焦于 Web 性能指标 TTI
  • 2024新动态:低代码开发占领新常态市场
  • ImportError: DLL load failed while importing _ssl: 找不到指定的模块的解决方法
  • man 命令:查看命令的帮助信息
  • 【学习资料】袋中共36个球,红白黑格12个,问能一次抽到3个红4个白5个黑的概率是多少?
  • 封装一个录音声音振动效果的组件
  • foc原理odrive驱动板的使用,以及功能介绍
  • 基于TRIZ的救援机器人轻量化设计
  • 搜索引擎onesearch3实现解释和升级到Elasticsearch v8系列(一)-概述
  • 翻唱技巧:AU和Cubase翻唱录制对轨技巧
  • Linux 系统进程理解——标识符,状态
  • uniapp中使用echarts 完整步骤,包括报错以及解决方案
  • 实验一:Windows下的IIS服务器配置和管理
  • 50页PPT麦肯锡精益运营转型五步法
  • 考研报名确认上传身份证户口本学历证明照片如何压缩裁剪
  • 聊聊Thread Local Storage
  • 代码随想录训练营第34天| 62.不同路径 、63. 不同路径 II
  • 【STL】map和set相关知识详细梳理
  • 4、论文阅读:基于深度学习和成像模型的水下图像增强
  • 21、Tomato
  • weblogic CVE-2017-3506 靶场攻略
  • 【随手笔记】使用J-LINK读写芯片内存数据
  • C++——类和对象
  • 鸿蒙HarmonyOS之使用ArkTs语言获取应用版本等信息
  • AI健身体能测试之基于paddlehub实现引体向上计数个数统计
  • 享元模式详解:解锁高效资源管理的终极武器
  • 技术美术百人计划 | 《4.1 Bloom算法》笔记
  • 【图像检索】基于傅里叶描述子的形状特征图像检索,matlab实现
  • 新品亮相|美格智能SLM530/SLM530P智能模组,助力金融新零售智慧升级