-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
TerrainEncoding.js
395 lines (323 loc) · 13.5 KB
/
TerrainEncoding.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*global define*/
define([
'./AttributeCompression',
'./Cartesian2',
'./Cartesian3',
'./ComponentDatatype',
'./defaultValue',
'./defined',
'./Math',
'./Matrix3',
'./Matrix4',
'./TerrainQuantization'
], function(
AttributeCompression,
Cartesian2,
Cartesian3,
ComponentDatatype,
defaultValue,
defined,
CesiumMath,
Matrix3,
Matrix4,
TerrainQuantization) {
'use strict';
var cartesian3Scratch = new Cartesian3();
var cartesian3DimScratch = new Cartesian3();
var cartesian2Scratch = new Cartesian2();
var matrix4Scratch = new Matrix4();
var matrix4Scratch2 = new Matrix4();
var SHIFT_LEFT_12 = Math.pow(2.0, 12.0);
/**
* Data used to quantize and pack the terrain mesh. The position can be unpacked for picking and all attributes
* are unpacked in the vertex shader.
*
* @alias TerrainEncoding
* @constructor
*
* @param {AxisAlignedBoundingBox} axisAlignedBoundingBox The bounds of the tile in the east-north-up coordinates at the tiles center.
* @param {Number} minimumHeight The minimum height.
* @param {Number} maximumHeight The maximum height.
* @param {Matrix4} fromENU The east-north-up to fixed frame matrix at the center of the terrain mesh.
* @param {Boolean} hasVertexNormals If the mesh has vertex normals.
* @param {Boolean} [hasWebMercatorT=false] true if the terrain data includes a Web Mercator texture coordinate; otherwise, false.
*
* @private
*/
function TerrainEncoding(axisAlignedBoundingBox, minimumHeight, maximumHeight, fromENU, hasVertexNormals, hasWebMercatorT) {
var quantization;
var center;
var toENU;
var matrix;
if (defined(axisAlignedBoundingBox) && defined(minimumHeight) && defined(maximumHeight) && defined(fromENU)) {
var minimum = axisAlignedBoundingBox.minimum;
var maximum = axisAlignedBoundingBox.maximum;
var dimensions = Cartesian3.subtract(maximum, minimum, cartesian3DimScratch);
var hDim = maximumHeight - minimumHeight;
var maxDim = Math.max(Cartesian3.maximumComponent(dimensions), hDim);
if (maxDim < SHIFT_LEFT_12 - 1.0) {
quantization = TerrainQuantization.BITS12;
} else {
quantization = TerrainQuantization.NONE;
}
center = axisAlignedBoundingBox.center;
toENU = Matrix4.inverseTransformation(fromENU, new Matrix4());
var translation = Cartesian3.negate(minimum, cartesian3Scratch);
Matrix4.multiply(Matrix4.fromTranslation(translation, matrix4Scratch), toENU, toENU);
var scale = cartesian3Scratch;
scale.x = 1.0 / dimensions.x;
scale.y = 1.0 / dimensions.y;
scale.z = 1.0 / dimensions.z;
Matrix4.multiply(Matrix4.fromScale(scale, matrix4Scratch), toENU, toENU);
matrix = Matrix4.clone(fromENU);
Matrix4.setTranslation(matrix, Cartesian3.ZERO, matrix);
fromENU = Matrix4.clone(fromENU, new Matrix4());
var translationMatrix = Matrix4.fromTranslation(minimum, matrix4Scratch);
var scaleMatrix = Matrix4.fromScale(dimensions, matrix4Scratch2);
var st = Matrix4.multiply(translationMatrix, scaleMatrix,matrix4Scratch);
Matrix4.multiply(fromENU, st, fromENU);
Matrix4.multiply(matrix, st, matrix);
}
/**
* How the vertices of the mesh were compressed.
* @type {TerrainQuantization}
*/
this.quantization = quantization;
/**
* The minimum height of the tile including the skirts.
* @type {Number}
*/
this.minimumHeight = minimumHeight;
/**
* The maximum height of the tile.
* @type {Number}
*/
this.maximumHeight = maximumHeight;
/**
* The center of the tile.
* @type {Cartesian3}
*/
this.center = center;
/**
* A matrix that takes a vertex from the tile, transforms it to east-north-up at the center and scales
* it so each component is in the [0, 1] range.
* @type {Matrix4}
*/
this.toScaledENU = toENU;
/**
* A matrix that restores a vertex transformed with toScaledENU back to the earth fixed reference frame
* @type {Matrix4}
*/
this.fromScaledENU = fromENU;
/**
* The matrix used to decompress the terrain vertices in the shader for RTE rendering.
* @type {Matrix4}
*/
this.matrix = matrix;
/**
* The terrain mesh contains normals.
* @type {Boolean}
*/
this.hasVertexNormals = hasVertexNormals;
/**
* The terrain mesh contains a vertical texture coordinate following the Web Mercator projection.
* @type {Boolean}
*/
this.hasWebMercatorT = defaultValue(hasWebMercatorT, false);
}
TerrainEncoding.prototype.encode = function(vertexBuffer, bufferIndex, position, uv, height, normalToPack, webMercatorT) {
var u = uv.x;
var v = uv.y;
if (this.quantization === TerrainQuantization.BITS12) {
position = Matrix4.multiplyByPoint(this.toScaledENU, position, cartesian3Scratch);
position.x = CesiumMath.clamp(position.x, 0.0, 1.0);
position.y = CesiumMath.clamp(position.y, 0.0, 1.0);
position.z = CesiumMath.clamp(position.z, 0.0, 1.0);
var hDim = this.maximumHeight - this.minimumHeight;
var h = CesiumMath.clamp((height - this.minimumHeight) / hDim, 0.0, 1.0);
Cartesian2.fromElements(position.x, position.y, cartesian2Scratch);
var compressed0 = AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
Cartesian2.fromElements(position.z, h, cartesian2Scratch);
var compressed1 = AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
Cartesian2.fromElements(u, v, cartesian2Scratch);
var compressed2 = AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
vertexBuffer[bufferIndex++] = compressed0;
vertexBuffer[bufferIndex++] = compressed1;
vertexBuffer[bufferIndex++] = compressed2;
if (this.hasWebMercatorT) {
Cartesian2.fromElements(webMercatorT, 0.0, cartesian2Scratch);
var compressed3 = AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
vertexBuffer[bufferIndex++] = compressed3;
}
} else {
Cartesian3.subtract(position, this.center, cartesian3Scratch);
vertexBuffer[bufferIndex++] = cartesian3Scratch.x;
vertexBuffer[bufferIndex++] = cartesian3Scratch.y;
vertexBuffer[bufferIndex++] = cartesian3Scratch.z;
vertexBuffer[bufferIndex++] = height;
vertexBuffer[bufferIndex++] = u;
vertexBuffer[bufferIndex++] = v;
if (this.hasWebMercatorT) {
vertexBuffer[bufferIndex++] = webMercatorT;
}
}
if (this.hasVertexNormals) {
vertexBuffer[bufferIndex++] = AttributeCompression.octPackFloat(normalToPack);
}
return bufferIndex;
};
TerrainEncoding.prototype.decodePosition = function(buffer, index, result) {
if (!defined(result)) {
result = new Cartesian3();
}
index *= this.getStride();
if (this.quantization === TerrainQuantization.BITS12) {
var xy = AttributeCompression.decompressTextureCoordinates(buffer[index], cartesian2Scratch);
result.x = xy.x;
result.y = xy.y;
var zh = AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
result.z = zh.x;
return Matrix4.multiplyByPoint(this.fromScaledENU, result, result);
}
result.x = buffer[index];
result.y = buffer[index + 1];
result.z = buffer[index + 2];
return Cartesian3.add(result, this.center, result);
};
TerrainEncoding.prototype.decodeTextureCoordinates = function(buffer, index, result) {
if (!defined(result)) {
result = new Cartesian2();
}
index *= this.getStride();
if (this.quantization === TerrainQuantization.BITS12) {
return AttributeCompression.decompressTextureCoordinates(buffer[index + 2], result);
}
return Cartesian2.fromElements(buffer[index + 4], buffer[index + 5], result);
};
TerrainEncoding.prototype.decodeHeight = function(buffer, index) {
index *= this.getStride();
if (this.quantization === TerrainQuantization.BITS12) {
var zh = AttributeCompression.decompressTextureCoordinates(buffer[index + 1], cartesian2Scratch);
return zh.y * (this.maximumHeight - this.minimumHeight) + this.minimumHeight;
}
return buffer[index + 3];
};
TerrainEncoding.prototype.getOctEncodedNormal = function(buffer, index, result) {
var stride = this.getStride();
index = (index + 1) * stride - 1;
var temp = buffer[index] / 256.0;
var x = Math.floor(temp);
var y = (temp - x) * 256.0;
return Cartesian2.fromElements(x, y, result);
};
TerrainEncoding.prototype.getStride = function() {
var vertexStride;
switch (this.quantization) {
case TerrainQuantization.BITS12:
vertexStride = 3;
break;
default:
vertexStride = 6;
}
if (this.hasWebMercatorT) {
++vertexStride;
}
if (this.hasVertexNormals) {
++vertexStride;
}
return vertexStride;
};
var attributesNone = {
position3DAndHeight : 0,
textureCoordAndEncodedNormals : 1
};
var attributes = {
compressed0 : 0,
compressed1 : 1
};
TerrainEncoding.prototype.getAttributes = function(buffer) {
var datatype = ComponentDatatype.FLOAT;
var sizeInBytes = ComponentDatatype.getSizeInBytes(datatype);
var stride;
if (this.quantization === TerrainQuantization.NONE) {
var position3DAndHeightLength = 4;
var numTexCoordComponents = 2;
if (this.hasWebMercatorT) {
++numTexCoordComponents;
}
if (this.hasVertexNormals) {
++numTexCoordComponents;
}
stride = (position3DAndHeightLength + numTexCoordComponents) * sizeInBytes;
return [{
index : attributesNone.position3DAndHeight,
vertexBuffer : buffer,
componentDatatype : datatype,
componentsPerAttribute : position3DAndHeightLength,
offsetInBytes : 0,
strideInBytes : stride
}, {
index : attributesNone.textureCoordAndEncodedNormals,
vertexBuffer : buffer,
componentDatatype : datatype,
componentsPerAttribute : numTexCoordComponents,
offsetInBytes : position3DAndHeightLength * sizeInBytes,
strideInBytes : stride
}];
}
var numCompressed0 = 3;
var numCompressed1 = 0;
if (this.hasWebMercatorT || this.hasVertexNormals) {
++numCompressed0;
}
if (this.hasWebMercatorT && this.hasVertexNormals) {
++numCompressed1;
stride = (numCompressed0 + numCompressed1) * sizeInBytes;
return [{
index : attributes.compressed0,
vertexBuffer : buffer,
componentDatatype : datatype,
componentsPerAttribute : numCompressed0,
offsetInBytes : 0,
strideInBytes : stride
}, {
index : attributes.compressed1,
vertexBuffer : buffer,
componentDatatype : datatype,
componentsPerAttribute : numCompressed1,
offsetInBytes : numCompressed0 * sizeInBytes,
strideInBytes : stride
}];
} else {
return [{
index : attributes.compressed0,
vertexBuffer : buffer,
componentDatatype : datatype,
componentsPerAttribute : numCompressed0
}];
}
};
TerrainEncoding.prototype.getAttributeLocations = function() {
if (this.quantization === TerrainQuantization.NONE) {
return attributesNone;
} else {
return attributes;
}
};
TerrainEncoding.clone = function(encoding, result) {
if (!defined(result)) {
result = new TerrainEncoding();
}
result.quantization = encoding.quantization;
result.minimumHeight = encoding.minimumHeight;
result.maximumHeight = encoding.maximumHeight;
result.center = Cartesian3.clone(encoding.center);
result.toScaledENU = Matrix4.clone(encoding.toScaledENU);
result.fromScaledENU = Matrix4.clone(encoding.fromScaledENU);
result.matrix = Matrix4.clone(encoding.matrix);
result.hasVertexNormals = encoding.hasVertexNormals;
result.hasWebMercatorT = encoding.hasWebMercatorT;
return result;
};
return TerrainEncoding;
});