-
Notifications
You must be signed in to change notification settings - Fork 46
/
surface-provider.js
153 lines (132 loc) · 4.48 KB
/
surface-provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import Cesium from 'cesium'
import decode from '@here/quantized-mesh-decoder'
import dummyTileBuffer from './dummy-tile'
export default class SurfaceProvider {
constructor (options = {}) {
this.ready = false
this.dummyTile = decode(dummyTileBuffer)
this.tilingScheme = options.tilingScheme || new Cesium.WebMercatorTilingScheme()
if (options.getUrl === undefined) {
throw new Error('getUrl option is missing')
}
if (options.credit !== undefined) {
this.credits = [new Cesium.Credit(options.credit)]
}
this.getUrl = options.getUrl
this.readyPromise = Promise.resolve(true)
this.ready = true
}
generateDummyTileHeader (x, y, level) {
const tileRect = this.tilingScheme.tileXYToRectangle(x, y, level)
const tileNativeRect = this.tilingScheme.tileXYToNativeRectangle(x, y, level)
const tileCenter = Cesium.Cartographic.toCartesian(
Cesium.Rectangle.center(tileRect)
)
const horizonOcclusionPoint = Cesium.Ellipsoid.WGS84.transformPositionToScaledSpace(
tileCenter
)
return {
centerX: tileCenter.x,
centerY: tileCenter.y,
centerZ: tileCenter.z,
minHeight: 0,
maxHeight: 0,
boundingSphereCenterX: tileCenter.x,
boundingSphereCenterY: tileCenter.y,
boundingSphereCenterZ: tileCenter.z,
boundingSphereRadius: tileNativeRect.height,
horizonOcclusionPointX: horizonOcclusionPoint.x,
horizonOcclusionPointY: horizonOcclusionPoint.y,
horizonOcclusionPointZ: horizonOcclusionPoint.z
}
}
createQuantizedMeshData (decodedTile, x, y, level) {
const tileRect = this.tilingScheme.tileXYToRectangle(x, y, level)
const boundingSphereCenter = new Cesium.Cartesian3(
decodedTile.header.boundingSphereCenterX,
decodedTile.header.boundingSphereCenterY,
decodedTile.header.boundingSphereCenterZ
)
const boundingSphere = new Cesium.BoundingSphere(
boundingSphereCenter,
decodedTile.header.boundingSphereRadius
)
const horizonOcclusionPoint = new Cesium.Cartesian3(
decodedTile.header.horizonOcclusionPointX,
decodedTile.header.horizonOcclusionPointY,
decodedTile.header.horizonOcclusionPointZ
)
let orientedBoundingBox
if (tileRect.width < Cesium.Math.PI_OVER_TWO + Cesium.Math.EPSILON5) {
orientedBoundingBox = Cesium.OrientedBoundingBox.fromRectangle(
tileRect,
decodedTile.header.minHeight,
decodedTile.header.maxHeight
)
}
return new Cesium.QuantizedMeshTerrainData({
minimumHeight: decodedTile.header.minHeight,
maximumHeight: decodedTile.header.maxHeight,
quantizedVertices: decodedTile.vertexData,
indices: decodedTile.triangleIndices,
boundingSphere: boundingSphere,
orientedBoundingBox: orientedBoundingBox,
horizonOcclusionPoint: horizonOcclusionPoint,
westIndices: decodedTile.westIndices,
southIndices: decodedTile.southIndices,
eastIndices: decodedTile.eastIndices,
northIndices: decodedTile.northIndices,
westSkirtHeight: 100,
southSkirtHeight: 100,
eastSkirtHeight: 100,
northSkirtHeight: 100,
childTileMask: 15,
credits: this.credits
})
}
generateDummyTile (x, y, level) {
return Object.assign(
{},
this.dummyTile,
this.generateDummyTileHeader(x, y, level)
)
}
decodeResponse (res, x, y, level) {
return res.arrayBuffer()
.then(buffer => {
return decode(buffer)
}).catch((err) => {
console.error(`Decoding failed on tile ${this.getUrl(x, y, level)}`)
console.error(err)
return this.generateDummyTile(x, y, level)
})
}
requestTileGeometry (x, y, level) {
const url = this.getUrl(x, y, level)
return window.fetch(url)
.then(res => {
if (res.status !== 200) {
return this.generateDummyTile(x, y, level)
}
return this.decodeResponse(res, x, y, level)
})
.then(decodedTile => {
return this.createQuantizedMeshData(decodedTile, x, y, level)
})
.catch(err => {
console.error(err)
})
}
getTileDataAvailable (x, y, level) {
return true
}
getLevelMaximumGeometricError (level) {
const levelZeroMaximumGeometricError = Cesium.TerrainProvider
.getEstimatedLevelZeroGeometricErrorForAHeightmap(
this.tilingScheme.ellipsoid,
65,
this.tilingScheme.getNumberOfXTilesAtLevel(0)
)
return levelZeroMaximumGeometricError / (1 << level)
}
}