欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Three.js制作一個3D中國地圖

 更新時間:2022年01月28日 10:39:46   作者:Saitmob  
這篇文章主要為大家介紹一個用Three.js制作的3D中國地圖,文中的示例代碼講解詳細,對我們學習three.js有一定幫助,感興趣的可以跟隨小編一起試一試

不想看繁瑣步驟的,可以直接去github下載項目,如果可以順便來個star哈哈

本項目使用vue-cli創(chuàng)建,但不影響使用,主要繪制都已封裝成類

1.使用geoJson繪制3d地圖

1.1 創(chuàng)建場景相關

// 創(chuàng)建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;   

// 根據(jù)自己的需要調(diào)整顏色模式
// 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 根據(jù)json繪制地圖

利用THREE.Shape繪制地圖的平面邊數(shù)據(jù),再用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();
        // 每個的 坐標 數(shù)組
        const { coordinates } = elem.geometry;
        const color = COLOR_ARR[index % COLOR_ARR.length]
        // 循環(huán)坐標數(shù)組
        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
                ]);
                // 設置高度將省區(qū)分開來
                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.增加光照

我們把各種光都打上,環(huán)境光,半球光,點光,平行光。以平行光為例,增加投影,調(diào)整投影分辨率,避免投影出現(xiàn)馬賽克

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的渲染一般調(diào)用原生的requestAnimationFrame,主要做的事就是調(diào)用rendererrender方法,當然因為我們做了陰影模糊處理,所以還有別的需要做的:

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

6.動畫效果

地圖上如果有一些動畫效果,可以使用TWEEN.js,github地址,比如地圖標注的出現(xiàn)動畫:

最后再奉上項目地址

到此這篇關于基于Three.js制作一個3D中國地圖的文章就介紹到這了,更多相關Three.js 3D中國地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論