Vue項目引入Threebox


Vue項目中可以有兩個方式引入threebox。

npm方式:

1.先安裝three和threebox-map

1 npm install three --save
2 npm install threebox-map --save

引入

1 import * as THREE from 'three'
2 import { Threebox } from 'threebox-map'

2.初始化threebox前,設置window.THREE = THREE

 1 let tb = null
 2     window.THREE = THREE
 3     map && map.addLayer({
 4       id: constConfig.layers.VEHICLE,
 5       type: 'custom',
 6       renderingMode: '3d',
 7       onAdd: function (map, mbxContext) {
 8         tb = new Threebox(
 9           map,
10           mbxContext,
11           { defaultLights: true }
12         )
13         that.threebox = tb
14         let options = {
15           obj: './static/models/Truck.obj',
16           mtl: './static/models/Truck.mtl',
17           scale: 0.1
18         }
19         tb.loadObj(options, function (model) {
20           let truck = model.setCoords(origin)
22           truck.scale.set(0.5, 0.5, 0.5)
23           tb.add(truck)
24           that.vehicle = truck
25         })
26       },
27       render: function (gl, matrix) {
28         tb.update()
29       }
30     })

 

本地文件引入:

 1.從github上克隆 https://github.com/shiyuan598/threebox.git,build后得到dist/threebox.js

或直接下載https://raw.githubusercontent.com/shiyuan598/threebox/master/dist/threebox.js

2.把threebox.js 放入項目的static/libs下

1 import '@static/libs/threebox.js'

這里構建的threebox已包含了THREE,不需要單獨引入了。

使用window.Threebox,可以重新賦值為Threebox,其他無區別

 1 let tb = null
 2     let Threebox = window.Threebox
 3     map && map.addLayer({
 4       id: constConfig.layers.VEHICLE,
 5       type: 'custom',
 6       renderingMode: '3d',
 7       onAdd: function (map, mbxContext) {
 8         tb = new Threebox(
 9           map,
10           mbxContext,
11           { defaultLights: true }
12         )
13         that.threebox = tb
14         let options = {
15           obj: './static/models/Truck.obj',
16           mtl: './static/models/Truck.mtl',
17           scale: 10
18         }
19         tb.loadObj(options, function (model) {
20           let truck = model.setCoords(origin)
22           truck.scale.set(0.5, 0.5, 0.5)
23           tb.add(truck)
24           that.vehicle = truck
25         })
26       },
27       render: function (gl, matrix) {
28         tb.update()
29       }
30     })

代碼中的map為mapboxgl。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM