首页 > 其他分享 >threejs_动态heatmap渲染

threejs_动态heatmap渲染

时间:2023-04-21 21:14:15浏览次数:39  
标签:threejs 渲染 innerHeight value window heatmap radius innerWidth base

heatmap > heatmap2d.ts

import {
  Mesh,
  Texture,
  MeshBasicMaterial,
  PlaneGeometry,
  Box3,
  Vector3,
} from 'three';

import Base from '../Base';
import HeatMap, { DataPoint } from 'heatmap-ts';
import { log } from 'console';

export default class HeatMap2D {
  private base: Base;

  constructor() {
    const base = Base.getInstance();
    this.base = base;

    const oCanvas = document.createElement('canvas');
    const heatMap = new HeatMap({
      maxOpacity: 0.6,
      radius: 50,
      blur: 0.9,
      canvas: oCanvas,
      width: this.base.canvasWidth,
      height: this.base.normalHeight,
    });

    // const heatData = [
    //   {
    //     x: window.innerWidth * 0.1,
    //     y: window.innerHeight * 0.1,
    //     value: 80,
    //     radius: 100,
    //   },
    //   {
    //     x: window.innerWidth * 0.2,
    //     y: window.innerHeight * 0.2,
    //     value: 50,
    //     radius: 200,
    //   },
    //   {
    //     x: window.innerWidth * 0.3,
    //     y: window.innerHeight * 0.3,
    //     value: 30,
    //     radius: 50,
    //   },
    //   {
    //     x: window.innerWidth * 0.3,
    //     y: window.innerHeight * 0.4,
    //     value: 30,
    //     radius: 200,
    //   },

    //   {
    //     x: window.innerWidth * 0.5,
    //     y: window.innerHeight * 0.5,
    //     value: 70,
    //     radius: 200,
    //   },
    //   {
    //     x: window.innerWidth * 0.6,
    //     y: window.innerHeight * 0.6,
    //     value: 100,
    //     radius: 20,
    //   },
    //   {
    //     x: window.innerWidth * 0.7,
    //     y: window.innerHeight * 0.7,
    //     value: 80,
    //     radius: 20,
    //   },
    //   {
    //     x: window.innerWidth * 0.8,
    //     y: window.innerHeight * 0.8,
    //     value: 100,
    //     radius: 20,
    //   },
    //   {
    //     x: window.innerWidth * 0.9,
    //     y: window.innerHeight * 0.9,
    //     value: 100,
    //     radius: 20,
    //   },
    //   {
    //     x: window.innerWidth * 1.0,
    //     y: window.innerHeight * 1.0,
    //     value: 100,
    //     radius: 20,
    //   },
    // ];

    const heatData = [
      {
        x: window.innerWidth * 0.1,
        y: window.innerHeight * 0.1,
        value: 80,
        radius: 100,
      },
    ];

    heatMap.setData({
      max: 100,
      min: 1,
      data: heatData,
    });

    let heatMapGeo = new PlaneGeometry(
      this.base.canvasWidth,
      this.base.normalHeight
    );
    let heatMapTexture = new Texture(oCanvas);
    let heatMapMaterial = new MeshBasicMaterial({
      map: heatMapTexture,
      transparent: true,
    });

    heatMapMaterial.map!.needsUpdate = true;

    let heatMapPlane = new Mesh(heatMapGeo, heatMapMaterial);

    // 获取贴图的大小
    const box = new Box3().setFromObject(heatMapPlane);
    const size = box.getSize(new Vector3());

    const xScale = this.base.map.width / size.x;
    const yScale = this.base.map.height / size.y;

    heatMapPlane.scale.set(xScale, yScale, 1);

    heatMapPlane.position.set(
      this.base.map.offsetXY.x,
      this.base.map.offsetXY.y,
      1
    );

    this.base.scene.add(heatMapPlane);
    //
    const newHeatMapData: DataPoint[] = [];
    this.base.time.on('update', () => {
      newHeatMapData.push({
        x:
          this.base.canvasWidth *
          parseFloat((Math.random() * (0.7 - 0.3) + 0.3).toFixed(2)),
        y:
          this.base.normalHeight *
          parseFloat((Math.random() * (0.7 - 0.3) + 0.3).toFixed(2)),

        value: Math.floor(Math.random() * 21) + 10,
        radius: 100,
      });

      if (newHeatMapData.length > 100) {
        newHeatMapData.shift();
      }

      heatMap.setData({
        data: newHeatMapData,
      });

      heatMap.repaint();
      heatMapMaterial.map!.needsUpdate = true;
    });
  }
}

标签:threejs,渲染,innerHeight,value,window,heatmap,radius,innerWidth,base
From: https://www.cnblogs.com/zhuoss/p/17341799.html

相关文章

  • 重塑元宇宙体验!3DCAT元宇宙实时云渲染解决方案来了
    元宇宙作为人工智能、云计算和数字孪生等前沿技术的结合体,近年来越发受到各大企业重视。元宇宙的应用场景层出不穷,不仅包括营销推广场景,还有品牌活动和电商销售,能有效提升品宣和商业转化效果。元宇宙也具有极大的建设价值,从品牌文化展示到3D场景化联动再到互动社群运营,参与元宇......
  • Three.js教程:顶点位置数据解析渲染
    推荐:将NSDT场景编辑器加入你3D工具链其他工具系列:NSDT简石数字孪生顶点位置数据解析渲染如果你没有WebGL基础,可以先不用记忆每个的threejs具体内容,有一个大致印象即可,学习本节课的重点是建立顶点的概念。如果你建立了顶点的概念,那么对于你深入理解学习Three.js很有帮助。如果......
  • 3DCAT实时云渲染助力广府庙会元宇宙焕新亮相,开启线上奇趣之旅!
    超400万人次打卡,商圈营业额逾3.6亿元,2023年广府庙会于2023年2月11日圆满落幕。活动期间,佳境美如画,融合VR、AR、虚拟直播等技术的广府庙会元宇宙焕新亮相,群众只需点击一个简单的链接或扫码,即可在线漫游丰富的广府场景,并可体验元宇宙直播观看精彩庙会内容,足不出户实现身临其......
  • Django框架模版渲染与过滤器使用
    前端模版使用过滤器,如时间格式化等'''都是在前端页面中使用'''#default 如果一个变量是false或者为空,使用给定的默认值。否则,使用变量的值。{{value|default:"nothing"}}#给value这个值设置一个默认值,如果value没有传值或者值为空的话就显示nothing#length 返回值的......
  • 3D轻量化引擎推出新技术,模型渲染更逼真!
    HOOPSCommunicator在2021版本中,推出了基于PBR(PhysicallyBasedRendering)的渲染特性以提供更高质量的渲染技术。PBR将材料表示为一系列方程,这些方程对光如何从表面反射进行建模,再通过GPU上运行的着色器代码进行有效地实现。一、工程领域可视化问题停滞严重在过去的30年里,PC......
  • 让 Spartacus 服务器端渲染引入 long API 调用超时机制的两种配置方法
    两种方法,使用config.backend.timeout={浏览器:...,服务器:...},或者可以更具体地配置,即基于Request粒度,通过将HTTP_TIMEOUT_CONFIGHttpContextToken传递给AngularHttpClient的方法来针对每个具体请求进行配置。在SSR(Node.js)中,超时处理耗时过长的外部http调用是一项尤为......
  • JS的for循环动态渲染html只有最后一个元素生效?
    背景:我想在html3个ul中动态生成5个子元素li目标效果:废话少说,代码直接梭起来~1.先写html结构<sectionclass="list-box"><ulid="myList"></ul><ulid="myListOne"></ul><ulid="myListTwo"></ul>&......
  • heatmapts_simple-heatmap的使用
    simpleheat的使用<scriptsetuplang="ts">import{SimpleHeat}from"simpleheat-ts";import*asdatfrom"dat.gui";letframe:number|null=null;constoCanvas=document.createElement("canvas");oCanva......
  • OSG 使用整理(2):设置渲染状态
    二、场景管理2.1遍历场景图场景图遍历类型有以下几种:(1)    事件遍历:在遍历节点时,处理鼠标和键盘输入。(2)    更新遍历:用于修改场景图,添加节点,设置节点属性,执行回调。(3)    裁剪遍历:根据节点是否位于一个视口内来筛选节点,裁剪掉不可见和不可用的......
  • AntDesign中a-tab的forcerender属性强制DOM渲染
    <a-tabsv-model:activeKey="activeKey"@change="clickTag"><a-tab-panekey="1"tab="警情"v-if="tab01Visible":forceRender="true"><AssociatedElementsInformingDetail......