基于Three.js實現(xiàn)酷炫3D地圖效果
實現(xiàn)效果

前言
本文主要說明使用threejs技巧,來定制適合項目需求的樣式,源碼將在本文最后附上gitee地址。
使用
1.修改整體的背景圖可以使用顏色或用貼圖改材質(zhì)


方法:
只需修改createChinaMap()方法中的color屬性即可,注意一共要修改4個color,其中有兩個是地圖邊界線的顏色。也可以使用貼圖,

2. 取消地圖上柱狀圖顯示
create鉤子函數(shù)里注釋掉// this.createBar()即可
3.更換地圖、更換省份、市

更換很簡單,就是如圖位置修改引入的地圖文件即可,但是修改之后需要注意的是,地圖中心點改變了,比如現(xiàn)在將地圖展示由金華市改為臺州市,那么還需要修改@/comfig文件下的配置,如下圖所示:

修改之后的效果如下:

4.修改相機的視角,頁面展示的遠近角度

5.修改地圖的顏色及貼圖

let city = new BaseMap(this, {
data: data,
// topFaceMaterial: material.getMaterial(),
topFaceMaterial: new THREE.MeshPhongMaterial({
color: "red", //想要的顏色
emissive: 0x072534,
transparent: true,
opacity: 1,
}),
sideMaterial: sideMaterial.getMaterial(),
renderOrder: 6,
depth: config.cityName ? 0.3 : 3,
})如果你想引入貼圖,這樣會更好看,可以使用以下方法:
// 在index.js中引入的給地圖做材質(zhì)estart
const texture = new THREE.TextureLoader()
const textureMap = texture.load(require('./data/map/gz-map.jpg'))
const texturefxMap = texture.load(require('./data/map/gz-map-fx.jpg'))
textureMap.wrapS = texturefxMap.wrapS = THREE.RepeatWrapping
textureMap.wrapT = texturefxMap.wrapT = THREE.RepeatWrapping
textureMap.flipY = texturefxMap.flipY = false
textureMap.rotation = texturefxMap.rotation = THREE.MathUtils.degToRad(45)
const scale = 0.1
textureMap.repeat.set(scale, scale)然后
let city = new BaseMap(this, {
data: data,
// topFaceMaterial: material.getMaterial(),
topFaceMaterial: new THREE.MeshPhongMaterial({
map: textureMap,//不要忘記這里使用貼圖
color: "red", //想要的顏色
emissive: 0x072534,
transparent: true,
opacity: 1,
}),
sideMaterial: sideMaterial.getMaterial(),
renderOrder: 6,
depth: config.cityName ? 0.3 : 3,
})6.關(guān)閉一些特效
create中是所有方法的開關(guān),在這里可以進行調(diào)試
create () {
// 添加霧
this.scene.fog = new THREE.Fog(0x191919, 30, 70)
this.getCenterPoint()
this.createPlane()
this.createChinaMap()
this.createProvinceMap()
this.createCityMap()
this.createGrid()
this.createLight()
this.createRotateBorder()
this.createLabel()
this.createWall()
// this.createBar()
this.createParticles()
}7.頁面適配和在vue2版本中使用
頁面適配建議給這個地圖使用絕對定位,樣式代碼可參考以下:
width: 1920px; height: 1080px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
在vue2中使用:
npm 下載這個插件:@vue/composition-api
然后main.js注冊下即可
到此這篇關(guān)于基于Three.js實現(xiàn)酷炫3D地圖效果的文章就介紹到這了,更多相關(guān)Three.js 3D地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iframe中子父類窗口調(diào)用JS的方法及注意事項
本文給大家介紹iframe中子父類窗口調(diào)用JS的方法及注意事項,介紹的超詳細,需要的朋友快來學(xué)習(xí)下2015-08-08
js利用FileReader實現(xiàn)圖片轉(zhuǎn)base64格式并上傳預(yù)覽頭像
本文主要介紹了js利用FileReader實現(xiàn)圖片轉(zhuǎn)base64格式并上傳預(yù)覽頭像,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

