首页 > 其他分享 >记录--可视化大屏-用threejs撸一个3d中国地图

记录--可视化大屏-用threejs撸一个3d中国地图

时间:2022-12-20 17:44:05浏览次数:55  
标签:threejs const -- THREE mesh renderer 大屏 new true

这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

可视化大屏-用threejs撸一个3d中国地图

不想看繁琐步骤的,可以直接去github下载项目,如果可以顺便来个star哈哈

本项目使用vue-cli创建,但不影响使用,主要绘制都已封装成类

1、使用geoJson绘制3d地图

1.1 创建场景相关

// 创建webGL渲染器
this.renderer = new THREE.WebGLRenderer( { antialias: true,alpha: true} );
this.renderer.shadowMap.enabled = true; // 开启阴影
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
this.renderer.toneMapping = THREE.ACESFilmicToneMapping;
this.renderer.toneMappingExposure = 1.25;   

// 根据自己的需要调整颜色模式
// this.renderer.outputEncoding = THREE.sRGBEncoding;  

this.renderer.outputEncoding = THREE.sHSVEncoding;
this.renderer.setPixelRatio( window.devicePixelRatio );
// 清除背景色,透明背景
this.renderer.setClearColor(0xffffff, 0);
this.renderer.setSize(this.width, this.height);

// 场景
this.scene = new THREE.Scene();
this.scene.background = null
// 相机 透视相机
this.camera = new THREE.PerspectiveCamera(45, this.width / this.height, 0.1, 5000);
this.camera.position.set(0, -40, 70);
this.camera.lookAt(0, 0, 0);

1.2 根据json绘制地图

利用THREE.Shape绘制地图的平面边数据,再用THREE.ExtrudeGeometry将一个面拉高成3d模型,3d饼图同理也可以这么制作

let jsonData = require('./json/china.json')
this.initMap(jsonData);

// initMap 方法主要部分
initMap(chinaJson) {
    /* ...省略
        ...
    */
    chinaJson.features.forEach((elem, index) => {
        // 定一个省份3D对象
        const province = new THREE.Object3D();
        // 每个的 坐标 数组
        const { coordinates } = elem.geometry;
        const color = COLOR_ARR[index % COLOR_ARR.length]
        // 循环坐标数组
        coordinates.forEach(multiPolygon => {
            
            multiPolygon.forEach((polygon) => {
                const shape = new THREE.Shape();
                
                for (let i = 0; i < polygon.length; i++) {
                    let [x, y] = projection(polygon[i]);
                    
                    if (i === 0) {
                        shape.moveTo(x, -y);
                    }
                    shape.lineTo(x, -y);
                }
    
                const extrudeSettings = {
                    depth: 4,
                    bevelEnabled: true,
                    bevelSegments: 1,
                    bevelThickness: 0.2
                };
    
                const geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
               
                // 平面部分材质
                const material = new THREE.MeshStandardMaterial( {
                    metalness: 1,
                    color: color,
                    
                } );
                // 拉高部分材质
                const material1 = new THREE.MeshStandardMaterial( {
                    metalness: 1,
                    roughness: 1,
                    color: color,
                    
                } );

                const mesh = new THREE.Mesh(geometry, [
                    material,
                    material1
                ]);
                // 设置高度将省区分开来
                if (index % 2 === 0) {
                    mesh.scale.set(1, 1, 1.2);
                }
                // 给mesh开启阴影
                mesh.castShadow = true
                mesh.receiveShadow = true
                mesh._color = color
                province.add(mesh);

            })
    
        })
    
        _this.map.add(province);
        
    })
}

geoJson的坐标需要进行墨卡托投影转换才能转换成平面坐标,这里需要用到d3

// 墨卡托投影转换
const projection = d3.geoMercator().center([104.0, 37.5]).scale(80).translate([0, 0]);

2、增加光照

我们把各种光都打上,环境光,半球光,点光,平行光。以平行光为例,增加投影,调整投影分辨率,避免投影出现马赛克

const light = new THREE.DirectionalLight( 0xffffff, 0.5 ); 
light.position.set( 20, -50, 20 );

light.castShadow = true;
light.shadow.mapSize.width = 1024;
light.shadow.mapSize.height = 1024;

this.scene.add(light);

castShadow = true表示开启投影

3、增加阴影模糊

默认的阴影没有模糊效果,看起来像白炽灯照射的样子,没有柔和感。使用官方示例中的csm来增加阴影模糊

import { CSM } from 'three/examples/jsm/csm/CSM.js';

this.csm = new CSM( {
    maxFar: params.far,
    cascades: 4,
    mode: params.mode,
    parent: this.scene,
    shadowMapSize: 1024,
    lightDirection: new THREE.Vector3( params.lightX, params.lightY, params.lightZ ).normalize(),
    camera: this.camera
} );

4、增加鼠标事件

3d空间中,鼠标事件主要通过射线来获取鼠标所在位置,可以想象成鼠标放出一道射线,照射到的第一个物体就是鼠标所在位置。此时用的threejsRaycaster,通过Raycaster给对应的省份增加鼠标移入高亮效果和省份民悬浮展示效果

this.raycaster = new THREE.Raycaster();
// 传入需要检测的对象 group,group下的所有对象都会被检测到,如果被射线照到,则intersects有值,表示鼠标当前在这些物体上   
const intersects = this.raycaster.intersectObject( this.group, true );
// 代码太多就不贴了,见 GitHub源码

5、渲染

threejs的渲染一般调用原生的requestAnimationFrame,主要做的事就是调用rendererrender方法,当然因为我们做了阴影模糊处理,所以还有别的需要做的:

this.camera.updateMatrixWorld();
this.csm.update();
this.renderer.render(this.scene, this.camera);

6、动画效果

地图上如果有一些动画效果,可以使用TWEEN.jsgithub地址,比如地图标注的出现动画:

7jCH74.gif

最后再奉上项目地址

本文转载于:

https://juejin.cn/post/7057808453263163422

如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

 

标签:threejs,const,--,THREE,mesh,renderer,大屏,new,true
From: https://www.cnblogs.com/smileZAZ/p/16994776.html

相关文章

  • Qt控件样式QSS
    Qt控件样式QSS​​QSS样式如何使用​​​​打开Qt助手,在索引输入stylesheet​​​​Qt部分控件QSS样式​​QSS样式如何使用打开Qt助手,在索引输入stylesheetQt部分控......
  • VScode配置支持c++11和配置自动编译调试功能
    VScode配置支持c++11和配置自动编译调试功能​​1在工程目录下新建.vscode目录​​​​2在.vscode目录下创建c_cpp_properties.json文件内容如下​​​​2.1添加c_cpp_pr......
  • Qt下MQTT模块的导入(源码直接导入)适用Windows和Linux系统
    Qt下MQTT模块的导入(源码直接导入)适用Windows和Linux系统​​0.环境​​​​1.MQTT源码下载(也可以去官网下载)​​​​2.MQTT源码解压成功复制src/mqtt文件夹到工程中​......
  • Linux Qt下MQTT模块的导入(编译成库版本)几乎适用所有版本
    LinuxQt下MQTT模块的导入(编译成库版本)几乎适用所有版本​​前言​​​​MQTT源码下载​​​​一、采用windows共享文件编译出错(ln:无法创建符号链接lib*.so:不支持的......
  • QtMqtt 源码编译设置KeepAlive后ping包超时错误不返回问题修复(QMQTT::MqttNoPingResp
    调用setKeepAlive()设置心跳包后,心跳可以正常收发此时断开网络出现如下情况明显出现还未超时就重启了超时定时器,查看源码如下:voidQMQTT::ClientPrivate::onTimerPingReq......
  • 动态路由rip
    实验拓扑实验配置R1(config)#interfacef0/0R1(config-if)#ipadR1(config-if)#ipaddress172.168.1.1255.255.255.0R1(config-if)#noshuR1(config-if)#noshutdownR1(c......
  • Qt 信号与槽 传输自定义结构体跨线程访问程序异常退出问题
    Qt信号与槽传输自定义结构体跨线程访问程序异常退出问题在使用自定义结构体的时候发现在同一个线程里面的信号发送和槽函数访问使用是正常的当跨线程信号与槽连接访问自......
  • TCP、UDP 网络编程
    TCP、UDP网络编程实验目的1.使用TCP进行套接字通信2.使用UDP进行套接字通信实验原理1.TCP2.UDP3.用到的API(1)intsocket(intdomain,inttype,intprotocol);根......
  • 静态路由配置
    静态路由​​实验目的​​​​实验拓扑​​​​ip配置​​​​实验步骤​​​​配置路由器端口​​​​配置静态路由​​​​实验结果​​实验目的·了解静态路由·掌握IPv......
  • CmakeLists语法与实战
    CmakeLists语法与实战1、指定cmake的最小版本cmake_minimum_required(VERSION3.4.1)2、设置项目名称,它会引入两个变量​​MyDemo_BINARY_DIR​​​和​​MyDemo_SOURCE_......