Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 | 1x 1x 1x 1x 1x 1x 1157x 1157x 38x 38x 38x 38x 38x 38x 11x 27x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1157x 1x 1897x 1897x 1897x 1897x 1897x 1897x 1897x 1897x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 1867x 1867x 1867x 1867x 1867x 1867x 1867x 1807x 1897x 174x 1897x 171x 171x 171x 1897x 1x 1x 1x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 2654x 18574x 18574x 18574x 2654x 2654x 2654x 2654x 2654x 2654x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 641x 4483x 4483x 4483x 1x 145458x 145458x 145458x 26x 145458x 145458x 11994x 11994x 11994x 11994x 11994x 11994x 133464x 133464x 133464x 133464x 1x 142000x 142000x 142000x 142000x 142000x 2206x 2206x 2206x 2206x 2206x 2206x 142000x 1x 919x 919x 919x 2x 919x 919x 2x 917x 1x 4148x 4148x 4148x 4148x 2x 2x 4146x 1x 1x 13x 13x 13x 13x 13x 13x 13x 1x 156x 156x 156x 156x 156x 1x 2207x 2207x 2207x 2207x 2207x 2207x 2207x 2207x 1x 2343x 2343x 40x 40x 2303x 2343x 1223x 2343x 99x 99x 2343x 218x 218x 2343x 1x 1x 1x 583x 583x 583x 583x 583x 583x 1145x 1145x 583x 559x 559x 559x 559x 559x 559x 3x 24x 24x 24x 24x 24x 583x 1x 111x 109x 2x 1x 1091x 1091x 1056x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x | import AttributeCompression from "./AttributeCompression.js";
import Cartesian2 from "./Cartesian2.js";
import Cartesian3 from "./Cartesian3.js";
import Check from "./Check.js";
import ComponentDatatype from "./ComponentDatatype.js";
import defined from "./defined.js";
import CesiumMath from "./Math.js";
import Matrix4 from "./Matrix4.js";
import VerticalExaggeration from "./VerticalExaggeration.js";
import TerrainQuantization from "./TerrainQuantization.js";
const cartesian3Scratch = new Cartesian3();
const cartesian3DimScratch = new Cartesian3();
const cartesian2Scratch = new Cartesian2();
const matrix4Scratch = new Matrix4();
const matrix4Scratch2 = new Matrix4();
const 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 {Cartesian3} center The center point of the vertices.
* @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=false] If the mesh has vertex normals.
* @param {boolean} [hasWebMercatorT=false] true if the terrain data includes a Web Mercator texture coordinate; otherwise, false.
* @param {boolean} [hasGeodeticSurfaceNormals=false] true if the terrain data includes geodetic surface normals; otherwise, false.
* @param {number} [exaggeration=1.0] A scalar used to exaggerate terrain.
* @param {number} [exaggerationRelativeHeight=0.0] The relative height from which terrain is exaggerated.
*
* @private
*/
function TerrainEncoding(
center,
axisAlignedBoundingBox,
minimumHeight,
maximumHeight,
fromENU,
hasVertexNormals,
hasWebMercatorT,
hasGeodeticSurfaceNormals,
exaggeration,
exaggerationRelativeHeight,
) {
let quantization = TerrainQuantization.NONE;
let toENU;
let matrix;
if (
defined(axisAlignedBoundingBox) &&
defined(minimumHeight) &&
defined(maximumHeight) &&
defined(fromENU)
) {
const minimum = axisAlignedBoundingBox.minimum;
const maximum = axisAlignedBoundingBox.maximum;
const dimensions = Cartesian3.subtract(
maximum,
minimum,
cartesian3DimScratch,
);
const hDim = maximumHeight - minimumHeight;
const maxDim = Math.max(Cartesian3.maximumComponent(dimensions), hDim);
if (maxDim < SHIFT_LEFT_12 - 1.0) {
quantization = TerrainQuantization.BITS12;
} else {
quantization = TerrainQuantization.NONE;
}
// Scale and bias from [0,1] to [ENU min, ENU max]
// Also compute the inverse of the scale and bias
let st = Matrix4.fromScale(dimensions, matrix4Scratch);
st = Matrix4.setTranslation(st, minimum, st);
let invSt = Matrix4.fromScale(
Cartesian3.fromElements(
1.0 / dimensions.x,
1.0 / dimensions.y,
1.0 / dimensions.z,
cartesian3Scratch,
),
matrix4Scratch2,
);
invSt = Matrix4.multiplyByTranslation(
invSt,
Cartesian3.negate(minimum, cartesian3Scratch),
invSt,
);
matrix = Matrix4.clone(fromENU, new Matrix4());
let rtcOffset = Matrix4.getTranslation(fromENU, cartesian3Scratch);
rtcOffset = Cartesian3.subtract(rtcOffset, center, cartesian3Scratch);
matrix = Matrix4.setTranslation(matrix, rtcOffset, matrix);
matrix = Matrix4.multiply(matrix, st, matrix);
toENU = Matrix4.inverseTransformation(fromENU, new Matrix4());
toENU = Matrix4.multiply(invSt, toENU, toENU);
fromENU = Matrix4.multiply(fromENU, st, new Matrix4());
}
/**
* How the vertices of the mesh were compressed.
* @type {TerrainQuantization}
*/
this.quantization = quantization;
/**
* The minimum height of the tile including the skirts.
* @type {number|undefined}
*/
this.minimumHeight = minimumHeight;
/**
* The maximum height of the tile.
* @type {number|undefined}
*/
this.maximumHeight = maximumHeight;
/**
* The center of the tile.
* @type {Cartesian3}
*/
this.center = Cartesian3.clone(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|undefined}
*/
this.toScaledENU = toENU;
/**
* A matrix that restores a vertex transformed with toScaledENU back to the earth fixed reference frame
* @type {Matrix4|undefined}
*/
this.fromScaledENU = fromENU;
/**
* The matrix used to decompress the terrain vertices in the shader for RTE rendering.
* @type {Matrix4|undefined}
*/
this.matrix = matrix;
/**
* The terrain mesh contains normals.
* @type {boolean}
*/
this.hasVertexNormals = hasVertexNormals ?? false;
/**
* The terrain mesh contains a vertical texture coordinate following the Web Mercator projection.
* @type {boolean}
*/
this.hasWebMercatorT = hasWebMercatorT ?? false;
/**
* The terrain mesh contains geodetic surface normals, used for terrain exaggeration.
* @type {boolean}
*/
this.hasGeodeticSurfaceNormals = hasGeodeticSurfaceNormals ?? false;
/**
* A scalar used to exaggerate terrain.
* @type {number}
*/
this.exaggeration = exaggeration ?? 1.0;
/**
* The relative height from which terrain is exaggerated.
*/
this.exaggerationRelativeHeight = exaggerationRelativeHeight ?? 0.0;
/**
* The number of components in each vertex. This value can differ with different quantizations.
* @type {number}
*/
this.stride = 0;
this._offsetGeodeticSurfaceNormal = 0;
this._offsetVertexNormal = 0;
// Calculate the stride and offsets declared above
this._calculateStrideAndOffsets();
}
/**
* Encode information about the terrain at a given position into the vertex buffer.
* Position, texture coordinates, height, and (optionally) normal, projection information,
* and geodetic surface normal are all packed into the same buffer.
*
* @param {Float32Array} vertexBuffer The buffer to write to.
* @param {number} bufferIndex The index into the buffer to start writing at.
* @param {Cartesian3} position The position of the vertex.
* @param {Cartesian2} uv The texture coordinates of the vertex.
* @param {number} height The height of the vertex.
* @param {Cartesian2} [normalToPack] The normal vector of the vertex.
* @param {number} [webMercatorT] The Web Mercator texture coordinate of the vertex.
* @param {Cartesian3} [geodeticSurfaceNormal] The geodetic surface normal of the vertex.
*/
TerrainEncoding.prototype.encode = function (
vertexBuffer,
bufferIndex,
position,
uv,
height,
normalToPack,
webMercatorT,
geodeticSurfaceNormal,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("vertexBuffer", vertexBuffer);
Check.typeOf.number("bufferIndex", bufferIndex);
Check.typeOf.object("position", position);
Check.typeOf.object("uv", uv);
Check.typeOf.number("height", height);
//>>includeEnd('debug');
const u = uv.x;
const 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);
const hDim = this.maximumHeight - this.minimumHeight;
const h = CesiumMath.clamp((height - this.minimumHeight) / hDim, 0.0, 1.0);
Cartesian2.fromElements(position.x, position.y, cartesian2Scratch);
const compressed0 =
AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
Cartesian2.fromElements(position.z, h, cartesian2Scratch);
const compressed1 =
AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
Cartesian2.fromElements(u, v, cartesian2Scratch);
const compressed2 =
AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
vertexBuffer[bufferIndex++] = compressed0;
vertexBuffer[bufferIndex++] = compressed1;
vertexBuffer[bufferIndex++] = compressed2;
Iif (this.hasWebMercatorT) {
Cartesian2.fromElements(webMercatorT, 0.0, cartesian2Scratch);
const compressed3 =
AttributeCompression.compressTextureCoordinates(cartesian2Scratch);
vertexBuffer[bufferIndex++] = compressed3;
}
} else {
vertexBuffer[bufferIndex++] = position.x - this.center.x;
vertexBuffer[bufferIndex++] = position.y - this.center.y;
vertexBuffer[bufferIndex++] = position.z - this.center.z;
vertexBuffer[bufferIndex++] = height;
vertexBuffer[bufferIndex++] = u;
vertexBuffer[bufferIndex++] = v;
if (this.hasWebMercatorT) {
vertexBuffer[bufferIndex++] = webMercatorT;
}
}
if (this.hasVertexNormals) {
vertexBuffer[bufferIndex++] =
AttributeCompression.octPackFloat(normalToPack);
}
if (this.hasGeodeticSurfaceNormals) {
vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.x;
vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.y;
vertexBuffer[bufferIndex++] = geodeticSurfaceNormal.z;
}
return bufferIndex;
};
const scratchPosition = new Cartesian3();
const scratchGeodeticSurfaceNormal = new Cartesian3();
/**
* Add geodetic surface normals to a terrain vertex buffer.
* The new buffer will be larger than the old buffer.
*
* @param {Float32Array} oldBuffer The buffer without geodetic surface normals.
* @param {Float32Array} newBuffer The buffer with geodetic surface normals.
* @param {Ellipsoid} ellipsoid The ellipsoid to use to compute the geodetic surface normals.
*/
TerrainEncoding.prototype.addGeodeticSurfaceNormals = function (
oldBuffer,
newBuffer,
ellipsoid,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("oldBuffer", oldBuffer);
Check.typeOf.object("newBuffer", newBuffer);
Check.typeOf.object("ellipsoid", ellipsoid);
//>>includeEnd('debug');
Iif (this.hasGeodeticSurfaceNormals) {
return;
}
const oldStride = this.stride;
const vertexCount = oldBuffer.length / oldStride;
this.hasGeodeticSurfaceNormals = true;
this._calculateStrideAndOffsets();
const newStride = this.stride;
for (let index = 0; index < vertexCount; index++) {
for (let offset = 0; offset < oldStride; offset++) {
const oldIndex = index * oldStride + offset;
const newIndex = index * newStride + offset;
newBuffer[newIndex] = oldBuffer[oldIndex];
}
const position = this.decodePosition(newBuffer, index, scratchPosition);
const geodeticSurfaceNormal = ellipsoid.geodeticSurfaceNormal(
position,
scratchGeodeticSurfaceNormal,
);
const bufferIndex = index * newStride + this._offsetGeodeticSurfaceNormal;
newBuffer[bufferIndex] = geodeticSurfaceNormal.x;
newBuffer[bufferIndex + 1] = geodeticSurfaceNormal.y;
newBuffer[bufferIndex + 2] = geodeticSurfaceNormal.z;
}
};
/**
* Remove geodetic surface normals from a terrain vertex buffer.
*
* @param {Float32Array} oldBuffer The buffer with geodetic surface normals.
* @param {Float32Array} newBuffer The buffer without geodetic surface normals.
*/
TerrainEncoding.prototype.removeGeodeticSurfaceNormals = function (
oldBuffer,
newBuffer,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("oldBuffer", oldBuffer);
Check.typeOf.object("newBuffer", newBuffer);
//>>includeEnd('debug');
Iif (!this.hasGeodeticSurfaceNormals) {
return;
}
const oldStride = this.stride;
const vertexCount = oldBuffer.length / oldStride;
this.hasGeodeticSurfaceNormals = false;
this._calculateStrideAndOffsets();
const newStride = this.stride;
for (let index = 0; index < vertexCount; index++) {
for (let offset = 0; offset < newStride; offset++) {
const oldIndex = index * oldStride + offset;
const newIndex = index * newStride + offset;
newBuffer[newIndex] = oldBuffer[oldIndex];
}
}
};
/**
* Decode a position from the vertex buffer.
*
* @param {Float32Array} buffer The buffer to decode from.
* @param {number} index The index of the vertex to decode.
* @param {Cartesian3} [result] The object to store the result in.
* @returns {Cartesian3} The decoded position.
*/
TerrainEncoding.prototype.decodePosition = function (buffer, index, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("buffer", buffer);
Check.typeOf.number("index", index);
//>>includeEnd('debug');
if (!defined(result)) {
result = new Cartesian3();
}
index *= this.stride;
if (this.quantization === TerrainQuantization.BITS12) {
const xy = AttributeCompression.decompressTextureCoordinates(
buffer[index],
cartesian2Scratch,
);
result.x = xy.x;
result.y = xy.y;
const 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);
};
/**
* Decode a position from the vertex buffer and apply vertical exaggeration.
*
* @param {Float32Array} buffer
* @param {number} index
* @param {Cartesian3} [result]
* @returns {Cartesian3} The exaggerated position.
*/
TerrainEncoding.prototype.getExaggeratedPosition = function (
buffer,
index,
result,
) {
result = this.decodePosition(buffer, index, result);
const exaggeration = this.exaggeration;
const exaggerationRelativeHeight = this.exaggerationRelativeHeight;
const hasExaggeration = exaggeration !== 1.0;
if (hasExaggeration && this.hasGeodeticSurfaceNormals) {
const geodeticSurfaceNormal = this.decodeGeodeticSurfaceNormal(
buffer,
index,
scratchGeodeticSurfaceNormal,
);
const rawHeight = this.decodeHeight(buffer, index);
const heightDifference =
VerticalExaggeration.getHeight(
rawHeight,
exaggeration,
exaggerationRelativeHeight,
) - rawHeight;
// some math is unrolled for better performance
result.x += geodeticSurfaceNormal.x * heightDifference;
result.y += geodeticSurfaceNormal.y * heightDifference;
result.z += geodeticSurfaceNormal.z * heightDifference;
}
return result;
};
/**
* Decode texture coordinates from the vertex buffer.
*
* @param {Float32Array} buffer The buffer to decode from.
* @param {number} index The index of the vertex to decode.
* @param {Cartesian2} [result] The object to store the result in.
* @returns {Cartesian2} The decoded texture coordinates.
*/
TerrainEncoding.prototype.decodeTextureCoordinates = function (
buffer,
index,
result,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("buffer", buffer);
Check.typeOf.number("index", index);
//>>includeEnd('debug');
if (!defined(result)) {
result = new Cartesian2();
}
index *= this.stride;
if (this.quantization === TerrainQuantization.BITS12) {
return AttributeCompression.decompressTextureCoordinates(
buffer[index + 2],
result,
);
}
return Cartesian2.fromElements(buffer[index + 4], buffer[index + 5], result);
};
/**
* Decode a height from the vertex buffer.
*
* @param {Float32Array} buffer The buffer to decode from.
* @param {number} index The index of the vertex to decode.
* @returns {number} The decoded height.
*/
TerrainEncoding.prototype.decodeHeight = function (buffer, index) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("buffer", buffer);
Check.typeOf.number("index", index);
//>>includeEnd('debug');
index *= this.stride;
if (this.quantization === TerrainQuantization.BITS12) {
const zh = AttributeCompression.decompressTextureCoordinates(
buffer[index + 1],
cartesian2Scratch,
);
return (
zh.y * (this.maximumHeight - this.minimumHeight) + this.minimumHeight
);
}
return buffer[index + 3];
};
/**
* Decode a web mercator T coordinate from the vertex buffer.
*
* @param {Float32Array} buffer The buffer to decode from.
* @param {number} index The index of the vertex to decode.
* @returns {number} The decoded web mercator T coordinate.
*/
TerrainEncoding.prototype.decodeWebMercatorT = function (buffer, index) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("buffer", buffer);
Check.typeOf.number("index", index);
//>>includeEnd('debug');
index *= this.stride;
if (this.quantization === TerrainQuantization.BITS12) {
return AttributeCompression.decompressTextureCoordinates(
buffer[index + 3],
cartesian2Scratch,
).x;
}
return buffer[index + 6];
};
/**
* Decode an oct-encoded normal from the vertex buffer.
*
* @param {Float32Array} buffer The buffer to decode from.
* @param {number} index The index of the vertex to decode.
* @param {Cartesian2} [result] The object to store the result in.
* @returns {Cartesian2} The decoded oct-encoded normal.
*/
TerrainEncoding.prototype.getOctEncodedNormal = function (
buffer,
index,
result,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("buffer", buffer);
Check.typeOf.number("index", index);
//>>includeEnd('debug');
index = index * this.stride + this._offsetVertexNormal;
const temp = buffer[index] / 256.0;
const x = Math.floor(temp);
const y = (temp - x) * 256.0;
return Cartesian2.fromElements(x, y, result);
};
/**
* @param {Float32Array} buffer
* @param {number} index
* @param {Cartesian3} result
* @returns {Cartesian3}
*/
TerrainEncoding.prototype.decodeNormal = function (buffer, index, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("buffer", buffer);
Check.typeOf.number("index", index);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
const bufferIndex = (index = index * this.stride + this._offsetVertexNormal);
return AttributeCompression.octDecodeFloat(buffer[bufferIndex], result);
};
/**
* Decode a geodetic surface normal from the vertex buffer.
*
* @param {Float32Array} buffer The buffer to decode from.
* @param {number} index The index of the vertex to decode.
* @param {Cartesian3} result The object to store the result in.
* @returns {Cartesian3} The decoded geodetic surface normal.
*/
TerrainEncoding.prototype.decodeGeodeticSurfaceNormal = function (
buffer,
index,
result,
) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("buffer", buffer);
Check.typeOf.number("index", index);
Check.typeOf.object("result", result);
//>>includeEnd('debug');
index = index * this.stride + this._offsetGeodeticSurfaceNormal;
result.x = buffer[index];
result.y = buffer[index + 1];
result.z = buffer[index + 2];
return result;
};
/**
* Calculate the stride and offsets for sampling the vertex buffer.
*/
TerrainEncoding.prototype._calculateStrideAndOffsets = function () {
let vertexStride = 0;
switch (this.quantization) {
case TerrainQuantization.BITS12:
vertexStride += 3;
break;
default:
vertexStride += 6;
}
if (this.hasWebMercatorT) {
vertexStride += 1;
}
if (this.hasVertexNormals) {
this._offsetVertexNormal = vertexStride;
vertexStride += 1;
}
if (this.hasGeodeticSurfaceNormals) {
this._offsetGeodeticSurfaceNormal = vertexStride;
vertexStride += 3;
}
this.stride = vertexStride;
};
const attributesIndicesNone = {
position3DAndHeight: 0,
textureCoordAndEncodedNormals: 1,
geodeticSurfaceNormal: 2,
};
const attributesIndicesBits12 = {
compressed0: 0,
compressed1: 1,
geodeticSurfaceNormal: 2,
};
/**
* Get descriptors of the attributes stored in the vertex buffer.
*
* @param {Float32Array} buffer The vertex buffer.
* @returns {object[]} The attributes.
*/
TerrainEncoding.prototype.getAttributes = function (buffer) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object("buffer", buffer);
//>>includeEnd('debug');
const datatype = ComponentDatatype.FLOAT;
const sizeInBytes = ComponentDatatype.getSizeInBytes(datatype);
const strideInBytes = this.stride * sizeInBytes;
let offsetInBytes = 0;
const attributes = [];
function addAttribute(index, componentsPerAttribute) {
attributes.push({
index: index,
vertexBuffer: buffer,
componentDatatype: datatype,
componentsPerAttribute: componentsPerAttribute,
offsetInBytes: offsetInBytes,
strideInBytes: strideInBytes,
});
offsetInBytes += componentsPerAttribute * sizeInBytes;
}
if (this.quantization === TerrainQuantization.NONE) {
addAttribute(attributesIndicesNone.position3DAndHeight, 4);
let componentsTexCoordAndNormals = 2;
componentsTexCoordAndNormals += this.hasWebMercatorT ? 1 : 0;
componentsTexCoordAndNormals += this.hasVertexNormals ? 1 : 0;
addAttribute(
attributesIndicesNone.textureCoordAndEncodedNormals,
componentsTexCoordAndNormals,
);
if (this.hasGeodeticSurfaceNormals) {
addAttribute(attributesIndicesNone.geodeticSurfaceNormal, 3);
}
} else {
// When there is no webMercatorT or vertex normals, the attribute only needs 3 components: x/y, z/h, u/v.
// WebMercatorT and vertex normals each take up one component, so if only one of them is present the first
// attribute gets a 4th component. If both are present, we need an additional attribute that has 1 component.
const usingAttribute0Component4 =
this.hasWebMercatorT || this.hasVertexNormals;
const usingAttribute1Component1 =
this.hasWebMercatorT && this.hasVertexNormals;
addAttribute(
attributesIndicesBits12.compressed0,
usingAttribute0Component4 ? 4 : 3,
);
Iif (usingAttribute1Component1) {
addAttribute(attributesIndicesBits12.compressed1, 1);
}
Iif (this.hasGeodeticSurfaceNormals) {
addAttribute(attributesIndicesBits12.geodeticSurfaceNormal, 3);
}
}
return attributes;
};
/**
* Get indices pointing to the attribute locations in the vertex buffer.
*
* @returns {object} The attribute indices.
*/
TerrainEncoding.prototype.getAttributeLocations = function () {
if (this.quantization === TerrainQuantization.NONE) {
return attributesIndicesNone;
}
return attributesIndicesBits12;
};
/**
* Clones a TerrainEncoding object.
*
* @param {TerrainEncoding} [encoding] The encoding to clone.
* @param {TerrainEncoding} [result] The object to store the cloned encoding.
* @returns {TerrainEncoding|undefined} The cloned encoding.
*/
TerrainEncoding.clone = function (encoding, result) {
Iif (!defined(encoding)) {
return undefined;
}
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;
result.hasGeodeticSurfaceNormals = encoding.hasGeodeticSurfaceNormals;
result.exaggeration = encoding.exaggeration;
result.exaggerationRelativeHeight = encoding.exaggerationRelativeHeight;
result._calculateStrideAndOffsets();
return result;
};
export default TerrainEncoding;
|