xy-map 1.0.60 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/demo.html +1 -0
- package/{src/image/marker.png → img/marker.42b5782a.png} +0 -0
- package/package.json +11 -47
- package/xy-map.common.js +67654 -0
- package/xy-map.css +1 -0
- package/xy-map.umd.js +67673 -0
- package/xy-map.umd.min.js +20 -0
- package/.browserslistrc +0 -3
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -29
- package/README.md +0 -36
- package/babel.config.js +0 -3
- package/jsconfig.json +0 -12
- package/lint-staged.config.js +0 -3
- package/public/draco/README.md +0 -32
- package/public/draco/draco_decoder.js +0 -52
- package/public/draco/draco_decoder.wasm +0 -0
- package/public/draco/draco_encoder.js +0 -33
- package/public/draco/draco_wasm_wrapper.js +0 -104
- package/public/draco/gltf/draco_decoder.js +0 -48
- package/public/draco/gltf/draco_decoder.wasm +0 -0
- package/public/draco/gltf/draco_encoder.js +0 -33
- package/public/draco/gltf/draco_wasm_wrapper.js +0 -104
- package/public/favicon.ico +0 -0
- package/public/img/MM.png +0 -0
- package/public/img/MM_D.png +0 -0
- package/public/img/MM_W.png +0 -0
- package/public/img/loading.gif +0 -0
- package/public/img/waternormals.jpg +0 -0
- package/public/index.html +0 -29
- package/src/App.vue +0 -13
- package/src/image/arrow_white.png +0 -0
- package/src/image/map_end.png +0 -0
- package/src/image/map_start.png +0 -0
- package/src/index.js +0 -154
- package/src/layers/Line.js +0 -203
- package/src/layers/Point.js +0 -384
- package/src/layers/Polygon.js +0 -119
- package/src/layers/Text.js +0 -53
- package/src/layers/circle.js +0 -148
- package/src/layers/circleAnimate.js +0 -147
- package/src/layers/index.js +0 -128
- package/src/layers/model copy.js +0 -314
- package/src/layers/model.js +0 -341
- package/src/layers/scene.js +0 -314
- package/src/layers/water.js +0 -239
- package/src/map.js +0 -486
- package/src/package/draw/index.vue +0 -239
- package/src/package/draw/line.vue +0 -172
- package/src/package/draw/point.vue +0 -152
- package/src/package/draw/polygon.vue +0 -172
- package/src/package/draw/shpFile.vue +0 -49
- package/src/package/draw/styles.js +0 -162
- package/src/package/mapFullScreen.vue +0 -105
- package/src/package/mapLoad.vue +0 -120
- package/src/package/mapMarker.vue +0 -50
- package/src/package/mapModel.vue +0 -69
- package/src/package/mapRain.vue +0 -110
- package/src/style/hoverHtml.css +0 -29
- package/src/style/main.scss +0 -186
- package/src/style/map.css +0 -7
- package/src/util/gaodeApi.js +0 -67
- package/src/util/mapEvent.js +0 -111
- package/src/util/mapHoverHtml.js +0 -59
- package/src/util/mapTools.js +0 -169
- package/src/util/measure-area.js +0 -215
- package/src/util/measure-distance.js +0 -600
- package/vue.config.js +0 -32
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import mapSdk from '../map'
|
|
2
|
-
import * as turf from '@turf/turf'
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
hasSource,
|
|
6
|
-
setSource,
|
|
7
|
-
hasLayer,
|
|
8
|
-
} from './index'
|
|
9
|
-
import {
|
|
10
|
-
addLayerText
|
|
11
|
-
} from './Text'
|
|
12
|
-
import {
|
|
13
|
-
mapClick
|
|
14
|
-
} from '../util/mapEvent'
|
|
15
|
-
|
|
16
|
-
const defaultOptions = {
|
|
17
|
-
id: '',
|
|
18
|
-
data: [],
|
|
19
|
-
text: {
|
|
20
|
-
show: true,
|
|
21
|
-
layout: {},
|
|
22
|
-
paint: {}
|
|
23
|
-
},
|
|
24
|
-
style: {
|
|
25
|
-
layout: {},
|
|
26
|
-
paint: {},
|
|
27
|
-
},
|
|
28
|
-
hover: null,
|
|
29
|
-
click: null,
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 添加圆图层
|
|
34
|
-
*/
|
|
35
|
-
export const addCircleAnimate = (option, layerId) => {
|
|
36
|
-
let {
|
|
37
|
-
map
|
|
38
|
-
} = mapSdk
|
|
39
|
-
|
|
40
|
-
let opt = Object.assign({}, defaultOptions, option)
|
|
41
|
-
let id = opt.id
|
|
42
|
-
if (hasLayer(id)) {
|
|
43
|
-
setSource(id, opt.data)
|
|
44
|
-
return
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
let layout = opt.style ? opt.style.layout : {}
|
|
48
|
-
let layoutOpt = Object.assign({
|
|
49
|
-
'visibility': 'visible',
|
|
50
|
-
}, layout)
|
|
51
|
-
|
|
52
|
-
let paint = opt.style ? opt.style.paint : {}
|
|
53
|
-
let paintOpt = Object.assign({
|
|
54
|
-
'circle-opacity': 0.5, // 圆点的不透明度(可选,取值范围为 0 ~ 1,默认值为 1)
|
|
55
|
-
'circle-radius': 50, // 圆点的半径(可选,值 >= 0,默认值为 5,单位:像素)
|
|
56
|
-
'circle-color': '#000000', // 圆点的颜色(可选,默认值为 #000000)
|
|
57
|
-
'circle-blur': 0, // 圆点的虚化(可选,默认值为 0。当值为 1 时,表示把圆虚化到只有圆心是不透明的)
|
|
58
|
-
'circle-translate': [0, 0], // 圆点的平移(可选,通过平移 [x, y] 达到一定的偏移量。默认值为 [0, 0],单位:像素。)
|
|
59
|
-
'circle-translate-anchor': 'map', // 圆点的平移锚点,即相对的参考物(可选,可选值为 map、viewport,默认为 map)
|
|
60
|
-
'circle-pitch-scale': 'map', // 地图倾斜时圆点的缩放(可选,可选值为 map、viewport,默认为 map。值为 viewport 时,圆点不会缩放)
|
|
61
|
-
'circle-pitch-alignment': 'map', // 地图倾斜时圆点的对齐方式(可选,可选值为 map、viewport,默认为 map)
|
|
62
|
-
'circle-stroke-width': 0, // 圆点的描边宽度(可选,值 >= 0,默认值为 0,单位:像素)
|
|
63
|
-
'circle-stroke-color': '#000000', // 圆点的描边颜色(可选,默认值为 #000000)
|
|
64
|
-
'circle-stroke-opacity': 1 // 圆点的描边不透明度(可选,取值范围为 0 ~ 1,默认值为 1)
|
|
65
|
-
}, paint)
|
|
66
|
-
|
|
67
|
-
if (!hasSource(id)) {
|
|
68
|
-
map.addSource(opt.id, {
|
|
69
|
-
type: 'geojson',
|
|
70
|
-
data: opt.data,
|
|
71
|
-
cluster: false,
|
|
72
|
-
clusterMaxZoom: 16,
|
|
73
|
-
clusterRadius: 500,
|
|
74
|
-
})
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
map.addLayer({
|
|
78
|
-
id: id,
|
|
79
|
-
type: 'circle',
|
|
80
|
-
metadata: { // 元数据(可选,用于为 layer 附加任意的属性。为避免冲突,建议添加前缀,如 mapbox:)
|
|
81
|
-
'mapbox:name': 'test'
|
|
82
|
-
},
|
|
83
|
-
source: id,
|
|
84
|
-
layout: layoutOpt,
|
|
85
|
-
paint: paintOpt,
|
|
86
|
-
...opt.other
|
|
87
|
-
}, layerId)
|
|
88
|
-
|
|
89
|
-
// 高亮样式
|
|
90
|
-
if (opt.high) {
|
|
91
|
-
let highPaint = opt.high.paint ? opt.high.paint : {
|
|
92
|
-
'circle-color': '#ff0'
|
|
93
|
-
}
|
|
94
|
-
let highPaintOpt = Object.assign({
|
|
95
|
-
...paintOpt
|
|
96
|
-
}, highPaint)
|
|
97
|
-
|
|
98
|
-
map.addLayer({
|
|
99
|
-
id: id + '-high',
|
|
100
|
-
type: 'circle',
|
|
101
|
-
source: id,
|
|
102
|
-
layout: layoutOpt,
|
|
103
|
-
paint: highPaintOpt,
|
|
104
|
-
filter: ['==', ['boolean', ['get', 'high']], true]
|
|
105
|
-
}, layerId)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (opt.click) {
|
|
109
|
-
mapClick(opt.id, opt.click)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// 添加文本图层
|
|
113
|
-
if (opt.text && opt.text.show) {
|
|
114
|
-
addLayerText('point', opt)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// 添加扩散动画
|
|
118
|
-
let radius = 0
|
|
119
|
-
let framesPerSecond = 15
|
|
120
|
-
let time = opt.animate.time || 1000
|
|
121
|
-
let maxRadius = opt.animate.radius || 200
|
|
122
|
-
let opacity = 1
|
|
123
|
-
|
|
124
|
-
if (opt.animate) {
|
|
125
|
-
animateMarker()
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function animateMarker() {
|
|
129
|
-
setTimeout(() => {
|
|
130
|
-
requestAnimationFrame(animateMarker)
|
|
131
|
-
|
|
132
|
-
radius += (maxRadius - radius) / framesPerSecond
|
|
133
|
-
opacity -= (.9 / framesPerSecond)
|
|
134
|
-
|
|
135
|
-
map.setPaintProperty(id, 'circle-radius', radius)
|
|
136
|
-
map.setPaintProperty(id, 'circle-opacity', opacity < 0 ? 0 : opacity)
|
|
137
|
-
|
|
138
|
-
if (opacity <= 0) {
|
|
139
|
-
radius = 0
|
|
140
|
-
setTimeout(() => {
|
|
141
|
-
opacity = 1
|
|
142
|
-
}, 200)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
}, time / framesPerSecond)
|
|
146
|
-
}
|
|
147
|
-
}
|
package/src/layers/index.js
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
// import mapBoxGl from 'mapbox-gl'
|
|
2
|
-
import mapSdk from '../map'
|
|
3
|
-
|
|
4
|
-
// 更新数据
|
|
5
|
-
export const setSource = (id, data) => {
|
|
6
|
-
let {
|
|
7
|
-
map
|
|
8
|
-
} = mapSdk
|
|
9
|
-
let hasSource = map.getSource(id)
|
|
10
|
-
if (hasSource) {
|
|
11
|
-
map.getSource(id).setData(data)
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
// 判断图层是否存在
|
|
15
|
-
export const hasLayer = (id) => {
|
|
16
|
-
let {
|
|
17
|
-
map
|
|
18
|
-
} = mapSdk
|
|
19
|
-
|
|
20
|
-
return map.getLayer(id)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// 判断数据是否存在
|
|
24
|
-
export const hasSource = (id) => {
|
|
25
|
-
let {
|
|
26
|
-
map
|
|
27
|
-
} = mapSdk
|
|
28
|
-
|
|
29
|
-
return map.getSource(id)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// 删除图层
|
|
33
|
-
export const removeLayer = (id) => {
|
|
34
|
-
let {
|
|
35
|
-
map
|
|
36
|
-
} = mapSdk
|
|
37
|
-
map.removeLayer(id)
|
|
38
|
-
let arr = getLayerAll()
|
|
39
|
-
let layers = arr.filter(item => item.id.includes(id + '-'))
|
|
40
|
-
layers.forEach(item => {
|
|
41
|
-
if (hasLayer(item.id)) {
|
|
42
|
-
map.removeLayer(item.id)
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 获取所有图层
|
|
49
|
-
*/
|
|
50
|
-
export const getLayerAll = () => {
|
|
51
|
-
let {
|
|
52
|
-
map
|
|
53
|
-
} = mapSdk
|
|
54
|
-
|
|
55
|
-
let data = map.getStyle().layers
|
|
56
|
-
// console.log(data)
|
|
57
|
-
return data
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// 显示隐藏图层
|
|
61
|
-
export const showLayer = (id, show = false) => {
|
|
62
|
-
let {
|
|
63
|
-
map
|
|
64
|
-
} = mapSdk
|
|
65
|
-
show = show ? 'visible' : 'none'
|
|
66
|
-
map.setLayoutProperty(id, 'visibility', show)
|
|
67
|
-
let arr = getLayerAll()
|
|
68
|
-
let layers = arr.filter(item => item.id.includes(id + '-'))
|
|
69
|
-
layers.forEach(item => {
|
|
70
|
-
if (hasLayer(item.id)) {
|
|
71
|
-
map.setLayoutProperty(item.id, 'visibility', show)
|
|
72
|
-
}
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 显隐文字
|
|
77
|
-
export const showLayerText = (show, id = '') => {
|
|
78
|
-
let {
|
|
79
|
-
map
|
|
80
|
-
} = mapSdk
|
|
81
|
-
|
|
82
|
-
if (id) {
|
|
83
|
-
setPaint(id, 'text-opacity', show ? 1 : 0)
|
|
84
|
-
} else {
|
|
85
|
-
let textLayer = map.getStyle().layers
|
|
86
|
-
textLayer.forEach(layer => {
|
|
87
|
-
if (layer.type === 'symbol') {
|
|
88
|
-
setPaint(layer.id, 'text-opacity', show ? 1 : 0)
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 修改Layout
|
|
95
|
-
export const setLayout = (id, key, value) => {
|
|
96
|
-
let {
|
|
97
|
-
map
|
|
98
|
-
} = mapSdk
|
|
99
|
-
|
|
100
|
-
map.setLayoutProperty(id, key, value)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// 修改Paint
|
|
104
|
-
export const setPaint = (id, key, value) => {
|
|
105
|
-
let {
|
|
106
|
-
map
|
|
107
|
-
} = mapSdk
|
|
108
|
-
|
|
109
|
-
map.setPaintProperty(id, key, value)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// 修改Filter
|
|
113
|
-
export const setFilter = (key, filter) => {
|
|
114
|
-
let {
|
|
115
|
-
map
|
|
116
|
-
} = mapSdk
|
|
117
|
-
|
|
118
|
-
map.setFilter(key, filter)
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// 设置高亮
|
|
122
|
-
export const setHigh = (id, filter) => {
|
|
123
|
-
let {
|
|
124
|
-
map
|
|
125
|
-
} = mapSdk
|
|
126
|
-
|
|
127
|
-
map.setFilter(id + '-high', filter)
|
|
128
|
-
}
|
package/src/layers/model copy.js
DELETED
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
import mapSdk from '../map'
|
|
2
|
-
import * as THREE from 'three'
|
|
3
|
-
import * as dat from 'dat.gui'
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
EffectComposer
|
|
7
|
-
} from 'three/examples/jsm/postprocessing/EffectComposer'
|
|
8
|
-
import {
|
|
9
|
-
RenderPass
|
|
10
|
-
} from 'three/examples/jsm/postprocessing/RenderPass'
|
|
11
|
-
import {
|
|
12
|
-
Sky
|
|
13
|
-
} from 'three/examples/jsm/objects/Sky'
|
|
14
|
-
// import {
|
|
15
|
-
// RoomEnvironment
|
|
16
|
-
// } from 'three/examples/jsm/environments/RoomEnvironment'
|
|
17
|
-
// import { // 环境贴图
|
|
18
|
-
// RGBELoader
|
|
19
|
-
// } from 'three/examples/jsm/loaders/RGBELoader'
|
|
20
|
-
|
|
21
|
-
import mapBoxGl from 'mapbox-gl'
|
|
22
|
-
|
|
23
|
-
import {
|
|
24
|
-
hasLayer,
|
|
25
|
-
removeLayer
|
|
26
|
-
} from './index'
|
|
27
|
-
|
|
28
|
-
const defaultOptions = {
|
|
29
|
-
id: '3dModel',
|
|
30
|
-
path: '/img/30.gltf',
|
|
31
|
-
position: [103.353557, 23.362395], // 位置
|
|
32
|
-
directionalLight: [{
|
|
33
|
-
color: 0xfffcce,
|
|
34
|
-
intensity: 1,
|
|
35
|
-
position: [-4100, 3000, -1800],
|
|
36
|
-
shadow: {},
|
|
37
|
-
}],
|
|
38
|
-
ambientLight: {
|
|
39
|
-
color: 0xFFFFFF,
|
|
40
|
-
intensity: 0.2,
|
|
41
|
-
},
|
|
42
|
-
sky: {
|
|
43
|
-
size: 1000000,
|
|
44
|
-
box: [1, 1, 0]
|
|
45
|
-
},
|
|
46
|
-
shadow: false,
|
|
47
|
-
edit: false,
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 添加模型
|
|
52
|
-
*/
|
|
53
|
-
export const addLayerModel = (option, layerId = '') => {
|
|
54
|
-
return new Promise((resolve) => {
|
|
55
|
-
let {
|
|
56
|
-
map
|
|
57
|
-
} = mapSdk
|
|
58
|
-
|
|
59
|
-
let opt = Object.assign({}, defaultOptions, option)
|
|
60
|
-
let id = opt.id
|
|
61
|
-
|
|
62
|
-
let camera = ''
|
|
63
|
-
let scene = ''
|
|
64
|
-
let renderer = ''
|
|
65
|
-
let customLayer = ''
|
|
66
|
-
let sun = opt.sky && opt.sky.box ? new THREE.Vector3(opt.sky.box[0], opt.sky.box[1], opt.sky.box[2]) : new THREE.Vector3(1, 1, 0)
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (hasLayer(id)) {
|
|
70
|
-
removeLayer(id)
|
|
71
|
-
}
|
|
72
|
-
addModel(id)
|
|
73
|
-
map.addLayer(customLayer, layerId)
|
|
74
|
-
|
|
75
|
-
resolve({
|
|
76
|
-
scene,
|
|
77
|
-
sun
|
|
78
|
-
})
|
|
79
|
-
|
|
80
|
-
function addModel() {
|
|
81
|
-
const modelOrigin = opt.position // 中心点
|
|
82
|
-
const modelAltitude = 0 // 高度
|
|
83
|
-
const modelRotate = [Math.PI / 2, 0, 0]
|
|
84
|
-
|
|
85
|
-
const modelAsMercatorCoordinate = mapBoxGl.MercatorCoordinate.fromLngLat(
|
|
86
|
-
modelOrigin,
|
|
87
|
-
modelAltitude
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
const modelTransform = {
|
|
91
|
-
translateX: modelAsMercatorCoordinate.x,
|
|
92
|
-
translateY: modelAsMercatorCoordinate.y,
|
|
93
|
-
translateZ: modelAsMercatorCoordinate.z,
|
|
94
|
-
rotateX: modelRotate[0],
|
|
95
|
-
rotateY: modelRotate[1],
|
|
96
|
-
rotateZ: modelRotate[2],
|
|
97
|
-
scale: modelAsMercatorCoordinate.meterInMercatorCoordinateUnits()
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// 根据CustomLayerInterface为三维模型配置自定义层
|
|
101
|
-
customLayer = {
|
|
102
|
-
id: opt.id,
|
|
103
|
-
type: 'custom',
|
|
104
|
-
renderingMode: '3d',
|
|
105
|
-
onAdd: (map, gl) => {
|
|
106
|
-
const gui = opt.edit ? new dat.GUI() : null
|
|
107
|
-
const container = map.getCanvas()
|
|
108
|
-
|
|
109
|
-
// 摄像机
|
|
110
|
-
camera = new THREE.PerspectiveCamera(70, container.clientWidth / container.clientHeight, 1, 5000)
|
|
111
|
-
// camera = new THREE.Camera()
|
|
112
|
-
// 场景
|
|
113
|
-
scene = new THREE.Scene()
|
|
114
|
-
|
|
115
|
-
// 辅助坐标轴
|
|
116
|
-
// if (gui) {
|
|
117
|
-
// let AxesHelper = new THREE.AxesHelper(100)
|
|
118
|
-
// AxesHelper.position.y = opt.translate[1]
|
|
119
|
-
// scene.add(AxesHelper)
|
|
120
|
-
// }
|
|
121
|
-
|
|
122
|
-
// 创建灯光
|
|
123
|
-
if (opt.directionalLight && opt.directionalLight.length > 0) {
|
|
124
|
-
opt.directionalLight.forEach((item, index) => {
|
|
125
|
-
let position = {
|
|
126
|
-
x: item.position[0],
|
|
127
|
-
y: item.position[1],
|
|
128
|
-
z: item.position[2],
|
|
129
|
-
}
|
|
130
|
-
const directionalLight = new THREE.DirectionalLight(item.color, item.intensity)
|
|
131
|
-
directionalLight.position.set(position.x, position.y, position.z)
|
|
132
|
-
directionalLight.width = 1
|
|
133
|
-
directionalLight.height = 1
|
|
134
|
-
// directionalLight.distance = 0
|
|
135
|
-
// directionalLight.decay = 0 // 衰减强度
|
|
136
|
-
// directionalLight.exponent = 0 // 衰减速度
|
|
137
|
-
// directionalLight.angle = 0 // 光源角度
|
|
138
|
-
directionalLight.castShadow = true
|
|
139
|
-
|
|
140
|
-
// 开启编辑模式
|
|
141
|
-
if (gui) {
|
|
142
|
-
const lightGui = gui.addFolder('灯光' + (index + 1))
|
|
143
|
-
lightGui.addColor(item, 'color').name('颜色').onChange(val => directionalLight.color = new THREE.Color(val))
|
|
144
|
-
lightGui.add(item, 'intensity').min(0).step(0.1).name('强度').onChange(val => directionalLight.intensity = val)
|
|
145
|
-
lightGui.add(position, 'x').step(100).name('x').onChange(val => directionalLight.position.x = val)
|
|
146
|
-
lightGui.add(position, 'y').step(100).name('y').onChange(val => directionalLight.position.y = val)
|
|
147
|
-
lightGui.add(position, 'z').step(100).name('z').onChange(val => directionalLight.position.z = val)
|
|
148
|
-
lightGui.open()
|
|
149
|
-
|
|
150
|
-
let pointLightHelper = new THREE.DirectionalLightHelper(directionalLight, 1000)
|
|
151
|
-
scene.add(pointLightHelper)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// 开启阴影
|
|
155
|
-
let shadow = Object.assign({
|
|
156
|
-
size: 2000,
|
|
157
|
-
near: 1,
|
|
158
|
-
far: 8000,
|
|
159
|
-
clarity: 14, // 阴影清晰度 0-20
|
|
160
|
-
bias: -0.0005
|
|
161
|
-
}, item.shadow)
|
|
162
|
-
if (shadow) {
|
|
163
|
-
directionalLight.castShadow = true
|
|
164
|
-
const d = shadow.size || 1000 //阴影范围
|
|
165
|
-
directionalLight.shadow.camera.left = -d
|
|
166
|
-
directionalLight.shadow.camera.right = d
|
|
167
|
-
directionalLight.shadow.camera.top = d
|
|
168
|
-
directionalLight.shadow.camera.bottom = -d
|
|
169
|
-
directionalLight.shadow.camera.near = shadow.near || 1
|
|
170
|
-
directionalLight.shadow.camera.far = shadow.far || 8000
|
|
171
|
-
directionalLight.shadow.mapSize.width = 2 ** shadow.clarity // 定义阴影贴图的宽度和高度,必须为2的整数此幂
|
|
172
|
-
directionalLight.shadow.mapSize.height = 2 ** shadow.clarity // 较高的值会以计算时间为代价提供更好的阴影质量
|
|
173
|
-
directionalLight.shadow.bias = shadow.bias || -0.05 //解决条纹阴影的出现
|
|
174
|
-
}
|
|
175
|
-
scene.add(directionalLight)
|
|
176
|
-
})
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// 环境光
|
|
180
|
-
if (opt.ambientLight) {
|
|
181
|
-
let item = {
|
|
182
|
-
color: opt.ambientLight.color,
|
|
183
|
-
intensity: opt.ambientLight.intensity
|
|
184
|
-
}
|
|
185
|
-
let ambientLight = new THREE.AmbientLight(item.color, item.intensity)
|
|
186
|
-
if (gui) {
|
|
187
|
-
const lightGui = gui.addFolder('环境光')
|
|
188
|
-
lightGui.addColor(item, 'color').name('颜色').onChange(val => ambientLight.color = new THREE.Color(val))
|
|
189
|
-
lightGui.add(item, 'intensity').min(0).step(0.1).name('强度').onChange(val => ambientLight.intensity = val)
|
|
190
|
-
lightGui.open()
|
|
191
|
-
}
|
|
192
|
-
scene.add(ambientLight)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// 创建天空
|
|
196
|
-
if (opt.sky) {
|
|
197
|
-
let sky = new Sky()
|
|
198
|
-
sky.scale.setScalar(opt.sky.size || 1000000)
|
|
199
|
-
let uniforms = sky.material.uniforms
|
|
200
|
-
uniforms['sunPosition'].value.copy(sun)
|
|
201
|
-
scene.add(sky)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// 渲染器
|
|
205
|
-
renderer = new THREE.WebGLRenderer({
|
|
206
|
-
canvas: map.getCanvas(),
|
|
207
|
-
context: gl,
|
|
208
|
-
antialias: true,
|
|
209
|
-
// logarithmicDepthBuffer: true
|
|
210
|
-
})
|
|
211
|
-
|
|
212
|
-
// 创建一个EffectComposer(效果组合器)对象,然后在该对象上添加后期处理通道。
|
|
213
|
-
let composer = new EffectComposer(renderer)
|
|
214
|
-
composer.setSize(container.clientWidth, container.clientHeight)
|
|
215
|
-
|
|
216
|
-
let renderPass = new RenderPass(scene, camera)
|
|
217
|
-
composer.addPass(renderPass)
|
|
218
|
-
|
|
219
|
-
// 处理阴影
|
|
220
|
-
if (opt.shadow) {
|
|
221
|
-
renderer.shadowMap.enabled = true
|
|
222
|
-
renderer.shadowMap.type = THREE.PCFSoftShadowMap
|
|
223
|
-
}
|
|
224
|
-
renderer.autoClear = false
|
|
225
|
-
renderer.outputEncoding = THREE.sRGBEncoding
|
|
226
|
-
renderer.setSize(container.clientWidth, container.clientHeight)
|
|
227
|
-
|
|
228
|
-
// RoomEnvironment
|
|
229
|
-
// const pmremGenerator = new THREE.PMREMGenerator(renderer)
|
|
230
|
-
// scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.001).texture
|
|
231
|
-
|
|
232
|
-
// 缩放比例调整
|
|
233
|
-
// window.addEventListener('resize', () => {
|
|
234
|
-
// renderer.setSize(container.clientWidth, container.clientHeight)
|
|
235
|
-
// camera.aspect = container.clientWidth / container.clientHeight
|
|
236
|
-
// camera.updateProjectionMatrix()
|
|
237
|
-
// })
|
|
238
|
-
|
|
239
|
-
// getIntersects() // 点击事件
|
|
240
|
-
},
|
|
241
|
-
render: cameraUpdate
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return customLayer
|
|
245
|
-
|
|
246
|
-
// 渲染摄像机
|
|
247
|
-
function cameraUpdate(gl, matrix) {
|
|
248
|
-
const rotationX = new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(1, 0, 0), modelTransform.rotateX)
|
|
249
|
-
const rotationY = new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(0, 1, 0), modelTransform.rotateY)
|
|
250
|
-
const rotationZ = new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(0, 0, 1), modelTransform.rotateZ)
|
|
251
|
-
|
|
252
|
-
const m = new THREE.Matrix4().fromArray(matrix)
|
|
253
|
-
const l = new THREE.Matrix4().makeTranslation(
|
|
254
|
-
modelTransform.translateX,
|
|
255
|
-
modelTransform.translateY,
|
|
256
|
-
modelTransform.translateZ
|
|
257
|
-
).scale(new THREE.Vector3(
|
|
258
|
-
modelTransform.scale,
|
|
259
|
-
-modelTransform.scale,
|
|
260
|
-
modelTransform.scale
|
|
261
|
-
)).multiply(rotationX)
|
|
262
|
-
.multiply(rotationY)
|
|
263
|
-
.multiply(rotationZ)
|
|
264
|
-
|
|
265
|
-
camera.projectionMatrix.elements = matrix
|
|
266
|
-
camera.projectionMatrix = m.multiply(l)
|
|
267
|
-
|
|
268
|
-
renderer.toneMapping = THREE.ACESFilmicToneMapping
|
|
269
|
-
|
|
270
|
-
renderer.resetState()
|
|
271
|
-
renderer.render(scene, camera)
|
|
272
|
-
map.triggerRepaint()
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// 点击事件
|
|
276
|
-
function getIntersects() {
|
|
277
|
-
const container = map.getCanvas()
|
|
278
|
-
|
|
279
|
-
container.addEventListener('click', (event) => {
|
|
280
|
-
event.preventDefault()
|
|
281
|
-
|
|
282
|
-
// 声明 raycaster 和 mouse 变量
|
|
283
|
-
let raycaster = new THREE.Raycaster()
|
|
284
|
-
let mouse = new THREE.Vector2()
|
|
285
|
-
|
|
286
|
-
// 通过鼠标点击位置,计算出 raycaster 所需点的位置,以屏幕为中心点,范围 -1 到 1
|
|
287
|
-
mouse.x = (event.layerX / container.clientWidth) * 2 - 1
|
|
288
|
-
mouse.y = -(event.layerY / container.clientHeight) * 2 + 1
|
|
289
|
-
|
|
290
|
-
//通过鼠标点击的位置(二维坐标)和当前相机的矩阵计算出射线位置
|
|
291
|
-
raycaster.setFromCamera(mouse, camera)
|
|
292
|
-
|
|
293
|
-
// 获取与射线相交的对象数组,其中的元素按照距离排序,越近的越靠前
|
|
294
|
-
let meshArr = []
|
|
295
|
-
findMesh(scene.children)
|
|
296
|
-
let intersects = raycaster.intersectObjects(meshArr, true)
|
|
297
|
-
|
|
298
|
-
// 获取所有mesh
|
|
299
|
-
function findMesh(list) {
|
|
300
|
-
list.forEach(item => {
|
|
301
|
-
if (item.children.length > 0) {
|
|
302
|
-
findMesh(item.children)
|
|
303
|
-
} else {
|
|
304
|
-
meshArr.push(item)
|
|
305
|
-
}
|
|
306
|
-
})
|
|
307
|
-
}
|
|
308
|
-
console.log(intersects)
|
|
309
|
-
return intersects
|
|
310
|
-
})
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
})
|
|
314
|
-
}
|