-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcesium-quantized-mesh-terrain-format-logger.js
268 lines (215 loc) · 9.04 KB
/
cesium-quantized-mesh-terrain-format-logger.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
function addRow(table, name, value) {
var toCell = function(n) {
return "<td>" + n + "</td>";
};
var concat = function(a, b) {
return a + b;
};
var newRow = document.createElement('tr');
if (name instanceof Array) {
var cells1 = name.map(toCell).reduce(concat);
var cells2 = value.map(toCell).reduce(concat);
newRow.innerHTML = cells1 + cells2;
}
else {
newRow.innerHTML = "<td>" + name + "</td><td>" + value + "</td>";
}
document.getElementById(table).appendChild(newRow);
return newRow;
}
function addElement(parent, text) {
var newItem = document.createElement('li');
newItem.innerHTML = text;
document.getElementById(parent).appendChild(newItem);
return newItem;
}
function createTypedArrayFromArrayBuffer(numberOfVertices, sourceArray, byteOffset, length) {
if (numberOfVertices > 64 * 1024) {
return new Uint32Array(sourceArray, byteOffset, length);
}
return new Uint16Array(sourceArray, byteOffset, length);
}
//
// Decoding code copy-pasted from Cesium's release 1.10
//
// https://github.com/AnalyticalGraphicsInc/cesium/blob/1a6d98695be89279388220b7592d9326535305d9/Source/Core/CesiumTerrainProvider.js#L296
//
function loadQuantizedMeshTerrainData(buffer) {
var pos = 0;
var cartesian3Elements = 3;
var boundingSphereElements = cartesian3Elements + 1;
var cartesian3Length = Float64Array.BYTES_PER_ELEMENT * cartesian3Elements;
var boundingSphereLength = Float64Array.BYTES_PER_ELEMENT * boundingSphereElements;
var encodedVertexElements = 3;
var encodedVertexLength = Uint16Array.BYTES_PER_ELEMENT * encodedVertexElements;
var triangleElements = 3;
var bytesPerIndex = Uint16Array.BYTES_PER_ELEMENT;
var triangleLength = bytesPerIndex * triangleElements;
var view = new DataView(buffer);
addRow("header", "CenterX", view.getFloat64(pos, true));
addRow("header", "CenterY", view.getFloat64(pos + 8, true));
addRow("header", "CenterZ", view.getFloat64(pos + 16, true));
pos += cartesian3Length;
var minimumHeight = view.getFloat32(pos, true);
addRow("header", "MinimumHeight", minimumHeight);
pos += Float32Array.BYTES_PER_ELEMENT;
var maximumHeight = view.getFloat32(pos, true);
addRow("header", "MaximumHeight", maximumHeight);
pos += Float32Array.BYTES_PER_ELEMENT;
addRow("header", "BoundingSphereCenterX", view.getFloat64(pos, true));
addRow("header", "BoundingSphereCenterY", view.getFloat64(pos + 8, true));
addRow("header", "BoundingSphereCenterZ", view.getFloat64(pos + 16, true));
addRow("header", "BoundingSphereCenterRadius", view.getFloat64(pos + 24, true));
pos += boundingSphereLength;
addRow("header", "HorizonOcclusionPointX", view.getFloat64(pos, true));
addRow("header", "HorizonOcclusionPointY", view.getFloat64(pos + 8, true));
addRow("header", "HorizonOcclusionPointZ", view.getFloat64(pos + 16, true));
pos += cartesian3Length;
var vertexCount = view.getUint32(pos, true);
document.getElementById("verticesTitle").innerHTML = "Vertices: " + vertexCount;
pos += Uint32Array.BYTES_PER_ELEMENT;
var encodedVertexBuffer = new Uint16Array(buffer, pos, vertexCount * 3);
pos += vertexCount * encodedVertexLength;
if (vertexCount > 64 * 1024) {
// More than 64k vertices, so indices are 32-bit.
bytesPerIndex = Uint32Array.BYTES_PER_ELEMENT;
triangleLength = bytesPerIndex * triangleElements;
}
// Decode the vertex buffer.
var uBuffer = encodedVertexBuffer.subarray(0, vertexCount);
var vBuffer = encodedVertexBuffer.subarray(vertexCount, 2 * vertexCount);
var heightBuffer = encodedVertexBuffer.subarray(vertexCount * 2, 3 * vertexCount);
var i;
var u = 0;
var v = 0;
var height = 0;
function zigZagDecode(value) {
return (value >> 1) ^ (-(value & 1));
}
for (i = 0; i < vertexCount; ++i) {
u += zigZagDecode(uBuffer[i]);
v += zigZagDecode(vBuffer[i]);
height += zigZagDecode(heightBuffer[i]);
addRow("vertices", [uBuffer[i], vBuffer[i], heightBuffer[i]], [u, v, height]);
uBuffer[i] = u;
vBuffer[i] = v;
heightBuffer[i] = height;
}
// skip over any additional padding that was added for 2/4 byte alignment
if (pos % bytesPerIndex !== 0) {
pos += (bytesPerIndex - (pos % bytesPerIndex));
}
var triangleCount = view.getUint32(pos, true);
document.getElementById("trianglesTitle").innerHTML = "Triangles: " + triangleCount;
pos += Uint32Array.BYTES_PER_ELEMENT;
var indices = createTypedArrayFromArrayBuffer(vertexCount, buffer, pos, triangleCount * triangleElements);
pos += triangleCount * triangleLength;
// High water mark decoding based on decompressIndices_ in webgl-loader's loader.js.
// https://code.google.com/p/webgl-loader/source/browse/trunk/samples/loader.js?r=99#55
// Copyright 2012 Google Inc., Apache 2.0 license.
var highest = 0;
for (i = 0; i < indices.length; ++i) {
var code = indices[i];
indices[i] = highest - code;
if (code === 0) {
++highest;
}
}
for (i = 0; i < indices.length; ) {
addRow("triangles", [i/3], [indices[i], indices[i + 1], indices[i + 2]]);
i += 3;
}
var westVertexCount = view.getUint32(pos, true);
document.getElementById("westTitle").innerHTML = "West Vertices: " + westVertexCount;
pos += Uint32Array.BYTES_PER_ELEMENT;
var westIndices = createTypedArrayFromArrayBuffer(vertexCount, buffer, pos, westVertexCount);
pos += westVertexCount * bytesPerIndex;
for (i = 0; i < westVertexCount; ++i) {
addRow("west", i, westIndices[i]);
}
var southVertexCount = view.getUint32(pos, true);
document.getElementById("southTitle").innerHTML = "South Vertices: " + southVertexCount;
pos += Uint32Array.BYTES_PER_ELEMENT;
var southIndices = createTypedArrayFromArrayBuffer(vertexCount, buffer, pos, southVertexCount);
pos += southVertexCount * bytesPerIndex;
for (i = 0; i < southVertexCount; ++i) {
addRow("south", i, southIndices[i]);
}
var eastVertexCount = view.getUint32(pos, true);
document.getElementById("eastTitle").innerHTML = "East Vertices: " + eastVertexCount;
pos += Uint32Array.BYTES_PER_ELEMENT;
var eastIndices = createTypedArrayFromArrayBuffer(vertexCount, buffer, pos, eastVertexCount);
pos += eastVertexCount * bytesPerIndex;
for (i = 0; i < eastVertexCount; ++i) {
addRow("east", i, eastIndices[i]);
}
var northVertexCount = view.getUint32(pos, true);
document.getElementById("northTitle").innerHTML = "North Vertices: " + northVertexCount;
pos += Uint32Array.BYTES_PER_ELEMENT;
var northIndices = createTypedArrayFromArrayBuffer(vertexCount, buffer, pos, northVertexCount);
pos += northVertexCount * bytesPerIndex;
for (i = 0; i < northVertexCount; ++i) {
addRow("north", i, northIndices[i]);
}
var encodedNormalBuffer;
var waterMaskBuffer;
while (pos < view.byteLength) {
var extensionId = view.getUint8(pos, true);
pos += Uint8Array.BYTES_PER_ELEMENT;
var extensionLength = view.getUint32(pos, true);
pos += Uint32Array.BYTES_PER_ELEMENT;
if (extensionId === 1) {
encodedNormalBuffer = new Uint8Array(buffer, pos, vertexCount * 2);
for (i = 0; i < vertexCount; ) {
var decoded = [0,0,0];
var encodedX = encodedNormalBuffer[i];
var encodedY = encodedNormalBuffer[i+1];
addRow("normals", [encodedX, encodedY], octDecode(encodedX, encodedY));
i += 2;
}
} else if (extensionId === 2) {
waterMaskBuffer = new Uint8Array(buffer, pos, extensionLength);
}
pos += extensionLength;
}
}
// oct decoding
// =============
var octDecode = function(x, y) {
var fromSNorm = function(value) {
var clamp = function(value, min, max) {
return value < min ? min : (value > max ? max : value);
};
return clamp(value, 0.0, 255.0) / 255.0 * 2.0 - 1.0;
};
var signNotZero = function(value) {
return value < 0.0 ? -1.0 : 1.0;
};
var normalize = function(cartesian) {
var magnitude = function(cartesian) {
var magnitudeSquared = function(cartesian) {
return cartesian[0] * cartesian[0] + cartesian[1] * cartesian[1] + cartesian[2] * cartesian[2];
};
return Math.sqrt(magnitudeSquared(cartesian));
};
var mag = magnitude(cartesian);
cartesian[0] /= mag;
cartesian[1] /= mag;
cartesian[2] /= mag;
return cartesian;
};
if (x < 0 || x > 255 || y < 0 || y > 255) {
throw new DeveloperError('x and y must be a signed normalized integer between 0 and 255');
}
var result = [0,0,0]
// result holds x,y,z
result[0] = fromSNorm(x);
result[1] = fromSNorm(y);
result[2] = 1.0 - (Math.abs(result[0]) + Math.abs(result[1]));
if (result[2] < 0.0) {
var oldVX = result[0];
result[0] = (1.0 - Math.abs(result[1])) * signNotZero(oldVX);
result[1] = (1.0 - Math.abs(oldVX)) * signNotZero(result[1]);
}
return normalize(result);
};