AttributeCompression-3cfab808.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['exports', './Matrix2-69c32d33', './ComponentDatatype-b1ea011a', './RuntimeError-c581ca93', './defaultValue-94c3e563'], (function (exports, Matrix2, ComponentDatatype, RuntimeError, defaultValue) { 'use strict';
  3. /**
  4. * An enum describing the attribute type for glTF and 3D Tiles.
  5. *
  6. * @enum {String}
  7. *
  8. * @private
  9. */
  10. const AttributeType = {
  11. /**
  12. * The attribute is a single component.
  13. *
  14. * @type {String}
  15. * @constant
  16. */
  17. SCALAR: "SCALAR",
  18. /**
  19. * The attribute is a two-component vector.
  20. *
  21. * @type {String}
  22. * @constant
  23. */
  24. VEC2: "VEC2",
  25. /**
  26. * The attribute is a three-component vector.
  27. *
  28. * @type {String}
  29. * @constant
  30. */
  31. VEC3: "VEC3",
  32. /**
  33. * The attribute is a four-component vector.
  34. *
  35. * @type {String}
  36. * @constant
  37. */
  38. VEC4: "VEC4",
  39. /**
  40. * The attribute is a 2x2 matrix.
  41. *
  42. * @type {String}
  43. * @constant
  44. */
  45. MAT2: "MAT2",
  46. /**
  47. * The attribute is a 3x3 matrix.
  48. *
  49. * @type {String}
  50. * @constant
  51. */
  52. MAT3: "MAT3",
  53. /**
  54. * The attribute is a 4x4 matrix.
  55. *
  56. * @type {String}
  57. * @constant
  58. */
  59. MAT4: "MAT4",
  60. };
  61. /**
  62. * Gets the scalar, vector, or matrix type for the attribute type.
  63. *
  64. * @param {AttributeType} attributeType The attribute type.
  65. * @returns {*} The math type.
  66. *
  67. * @private
  68. */
  69. AttributeType.getMathType = function (attributeType) {
  70. switch (attributeType) {
  71. case AttributeType.SCALAR:
  72. return Number;
  73. case AttributeType.VEC2:
  74. return Matrix2.Cartesian2;
  75. case AttributeType.VEC3:
  76. return Matrix2.Cartesian3;
  77. case AttributeType.VEC4:
  78. return Matrix2.Cartesian4;
  79. case AttributeType.MAT2:
  80. return Matrix2.Matrix2;
  81. case AttributeType.MAT3:
  82. return Matrix2.Matrix3;
  83. case AttributeType.MAT4:
  84. return Matrix2.Matrix4;
  85. //>>includeStart('debug', pragmas.debug);
  86. default:
  87. throw new RuntimeError.DeveloperError("attributeType is not a valid value.");
  88. //>>includeEnd('debug');
  89. }
  90. };
  91. /**
  92. * Gets the number of components per attribute.
  93. *
  94. * @param {AttributeType} attributeType The attribute type.
  95. * @returns {Number} The number of components.
  96. *
  97. * @private
  98. */
  99. AttributeType.getNumberOfComponents = function (attributeType) {
  100. switch (attributeType) {
  101. case AttributeType.SCALAR:
  102. return 1;
  103. case AttributeType.VEC2:
  104. return 2;
  105. case AttributeType.VEC3:
  106. return 3;
  107. case AttributeType.VEC4:
  108. case AttributeType.MAT2:
  109. return 4;
  110. case AttributeType.MAT3:
  111. return 9;
  112. case AttributeType.MAT4:
  113. return 16;
  114. //>>includeStart('debug', pragmas.debug);
  115. default:
  116. throw new RuntimeError.DeveloperError("attributeType is not a valid value.");
  117. //>>includeEnd('debug');
  118. }
  119. };
  120. /**
  121. * Get the number of attribute locations needed to fit this attribute. Most
  122. * types require one, but matrices require multiple attribute locations.
  123. *
  124. * @param {AttributeType} attributeType The attribute type.
  125. * @returns {Number} The number of attribute locations needed in the shader
  126. *
  127. * @private
  128. */
  129. AttributeType.getAttributeLocationCount = function (attributeType) {
  130. switch (attributeType) {
  131. case AttributeType.SCALAR:
  132. case AttributeType.VEC2:
  133. case AttributeType.VEC3:
  134. case AttributeType.VEC4:
  135. return 1;
  136. case AttributeType.MAT2:
  137. return 2;
  138. case AttributeType.MAT3:
  139. return 3;
  140. case AttributeType.MAT4:
  141. return 4;
  142. //>>includeStart('debug', pragmas.debug);
  143. default:
  144. throw new RuntimeError.DeveloperError("attributeType is not a valid value.");
  145. //>>includeEnd('debug');
  146. }
  147. };
  148. /**
  149. * Gets the GLSL type for the attribute type.
  150. *
  151. * @param {AttributeType} attributeType The attribute type.
  152. * @returns {String} The GLSL type for the attribute type.
  153. *
  154. * @private
  155. */
  156. AttributeType.getGlslType = function (attributeType) {
  157. //>>includeStart('debug', pragmas.debug);
  158. RuntimeError.Check.typeOf.string("attributeType", attributeType);
  159. //>>includeEnd('debug');
  160. switch (attributeType) {
  161. case AttributeType.SCALAR:
  162. return "float";
  163. case AttributeType.VEC2:
  164. return "vec2";
  165. case AttributeType.VEC3:
  166. return "vec3";
  167. case AttributeType.VEC4:
  168. return "vec4";
  169. case AttributeType.MAT2:
  170. return "mat2";
  171. case AttributeType.MAT3:
  172. return "mat3";
  173. case AttributeType.MAT4:
  174. return "mat4";
  175. //>>includeStart('debug', pragmas.debug);
  176. default:
  177. throw new RuntimeError.DeveloperError("attributeType is not a valid value.");
  178. //>>includeEnd('debug');
  179. }
  180. };
  181. var AttributeType$1 = Object.freeze(AttributeType);
  182. const RIGHT_SHIFT = 1.0 / 256.0;
  183. const LEFT_SHIFT = 256.0;
  184. /**
  185. * Attribute compression and decompression functions.
  186. *
  187. * @namespace AttributeCompression
  188. *
  189. * @private
  190. */
  191. const AttributeCompression = {};
  192. /**
  193. * Encodes a normalized vector into 2 SNORM values in the range of [0-rangeMax] following the 'oct' encoding.
  194. *
  195. * Oct encoding is a compact representation of unit length vectors.
  196. * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors",
  197. * Cigolle et al 2014: {@link http://jcgt.org/published/0003/02/01/}
  198. *
  199. * @param {Cartesian3} vector The normalized vector to be compressed into 2 component 'oct' encoding.
  200. * @param {Cartesian2} result The 2 component oct-encoded unit length vector.
  201. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  202. * @returns {Cartesian2} The 2 component oct-encoded unit length vector.
  203. *
  204. * @exception {DeveloperError} vector must be normalized.
  205. *
  206. * @see AttributeCompression.octDecodeInRange
  207. */
  208. AttributeCompression.octEncodeInRange = function (vector, rangeMax, result) {
  209. //>>includeStart('debug', pragmas.debug);
  210. RuntimeError.Check.defined("vector", vector);
  211. RuntimeError.Check.defined("result", result);
  212. const magSquared = Matrix2.Cartesian3.magnitudeSquared(vector);
  213. if (Math.abs(magSquared - 1.0) > ComponentDatatype.CesiumMath.EPSILON6) {
  214. throw new RuntimeError.DeveloperError("vector must be normalized.");
  215. }
  216. //>>includeEnd('debug');
  217. result.x =
  218. vector.x / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  219. result.y =
  220. vector.y / (Math.abs(vector.x) + Math.abs(vector.y) + Math.abs(vector.z));
  221. if (vector.z < 0) {
  222. const x = result.x;
  223. const y = result.y;
  224. result.x = (1.0 - Math.abs(y)) * ComponentDatatype.CesiumMath.signNotZero(x);
  225. result.y = (1.0 - Math.abs(x)) * ComponentDatatype.CesiumMath.signNotZero(y);
  226. }
  227. result.x = ComponentDatatype.CesiumMath.toSNorm(result.x, rangeMax);
  228. result.y = ComponentDatatype.CesiumMath.toSNorm(result.y, rangeMax);
  229. return result;
  230. };
  231. /**
  232. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding.
  233. *
  234. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  235. * @param {Cartesian2} result The 2 byte oct-encoded unit length vector.
  236. * @returns {Cartesian2} The 2 byte oct-encoded unit length vector.
  237. *
  238. * @exception {DeveloperError} vector must be normalized.
  239. *
  240. * @see AttributeCompression.octEncodeInRange
  241. * @see AttributeCompression.octDecode
  242. */
  243. AttributeCompression.octEncode = function (vector, result) {
  244. return AttributeCompression.octEncodeInRange(vector, 255, result);
  245. };
  246. const octEncodeScratch = new Matrix2.Cartesian2();
  247. const uint8ForceArray = new Uint8Array(1);
  248. function forceUint8(value) {
  249. uint8ForceArray[0] = value;
  250. return uint8ForceArray[0];
  251. }
  252. /**
  253. * @param {Cartesian3} vector The normalized vector to be compressed into 4 byte 'oct' encoding.
  254. * @param {Cartesian4} result The 4 byte oct-encoded unit length vector.
  255. * @returns {Cartesian4} The 4 byte oct-encoded unit length vector.
  256. *
  257. * @exception {DeveloperError} vector must be normalized.
  258. *
  259. * @see AttributeCompression.octEncodeInRange
  260. * @see AttributeCompression.octDecodeFromCartesian4
  261. */
  262. AttributeCompression.octEncodeToCartesian4 = function (vector, result) {
  263. AttributeCompression.octEncodeInRange(vector, 65535, octEncodeScratch);
  264. result.x = forceUint8(octEncodeScratch.x * RIGHT_SHIFT);
  265. result.y = forceUint8(octEncodeScratch.x);
  266. result.z = forceUint8(octEncodeScratch.y * RIGHT_SHIFT);
  267. result.w = forceUint8(octEncodeScratch.y);
  268. return result;
  269. };
  270. /**
  271. * Decodes a unit-length vector in 'oct' encoding to a normalized 3-component vector.
  272. *
  273. * @param {Number} x The x component of the oct-encoded unit length vector.
  274. * @param {Number} y The y component of the oct-encoded unit length vector.
  275. * @param {Number} rangeMax The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits.
  276. * @param {Cartesian3} result The decoded and normalized vector
  277. * @returns {Cartesian3} The decoded and normalized vector.
  278. *
  279. * @exception {DeveloperError} x and y must be unsigned normalized integers between 0 and rangeMax.
  280. *
  281. * @see AttributeCompression.octEncodeInRange
  282. */
  283. AttributeCompression.octDecodeInRange = function (x, y, rangeMax, result) {
  284. //>>includeStart('debug', pragmas.debug);
  285. RuntimeError.Check.defined("result", result);
  286. if (x < 0 || x > rangeMax || y < 0 || y > rangeMax) {
  287. throw new RuntimeError.DeveloperError(
  288. `x and y must be unsigned normalized integers between 0 and ${rangeMax}`
  289. );
  290. }
  291. //>>includeEnd('debug');
  292. result.x = ComponentDatatype.CesiumMath.fromSNorm(x, rangeMax);
  293. result.y = ComponentDatatype.CesiumMath.fromSNorm(y, rangeMax);
  294. result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y));
  295. if (result.z < 0.0) {
  296. const oldVX = result.x;
  297. result.x = (1.0 - Math.abs(result.y)) * ComponentDatatype.CesiumMath.signNotZero(oldVX);
  298. result.y = (1.0 - Math.abs(oldVX)) * ComponentDatatype.CesiumMath.signNotZero(result.y);
  299. }
  300. return Matrix2.Cartesian3.normalize(result, result);
  301. };
  302. /**
  303. * Decodes a unit-length vector in 2 byte 'oct' encoding to a normalized 3-component vector.
  304. *
  305. * @param {Number} x The x component of the oct-encoded unit length vector.
  306. * @param {Number} y The y component of the oct-encoded unit length vector.
  307. * @param {Cartesian3} result The decoded and normalized vector.
  308. * @returns {Cartesian3} The decoded and normalized vector.
  309. *
  310. * @exception {DeveloperError} x and y must be an unsigned normalized integer between 0 and 255.
  311. *
  312. * @see AttributeCompression.octDecodeInRange
  313. */
  314. AttributeCompression.octDecode = function (x, y, result) {
  315. return AttributeCompression.octDecodeInRange(x, y, 255, result);
  316. };
  317. /**
  318. * Decodes a unit-length vector in 4 byte 'oct' encoding to a normalized 3-component vector.
  319. *
  320. * @param {Cartesian4} encoded The oct-encoded unit length vector.
  321. * @param {Cartesian3} result The decoded and normalized vector.
  322. * @returns {Cartesian3} The decoded and normalized vector.
  323. *
  324. * @exception {DeveloperError} x, y, z, and w must be unsigned normalized integers between 0 and 255.
  325. *
  326. * @see AttributeCompression.octDecodeInRange
  327. * @see AttributeCompression.octEncodeToCartesian4
  328. */
  329. AttributeCompression.octDecodeFromCartesian4 = function (encoded, result) {
  330. //>>includeStart('debug', pragmas.debug);
  331. RuntimeError.Check.typeOf.object("encoded", encoded);
  332. RuntimeError.Check.typeOf.object("result", result);
  333. //>>includeEnd('debug');
  334. const x = encoded.x;
  335. const y = encoded.y;
  336. const z = encoded.z;
  337. const w = encoded.w;
  338. //>>includeStart('debug', pragmas.debug);
  339. if (
  340. x < 0 ||
  341. x > 255 ||
  342. y < 0 ||
  343. y > 255 ||
  344. z < 0 ||
  345. z > 255 ||
  346. w < 0 ||
  347. w > 255
  348. ) {
  349. throw new RuntimeError.DeveloperError(
  350. "x, y, z, and w must be unsigned normalized integers between 0 and 255"
  351. );
  352. }
  353. //>>includeEnd('debug');
  354. const xOct16 = x * LEFT_SHIFT + y;
  355. const yOct16 = z * LEFT_SHIFT + w;
  356. return AttributeCompression.octDecodeInRange(xOct16, yOct16, 65535, result);
  357. };
  358. /**
  359. * Packs an oct encoded vector into a single floating-point number.
  360. *
  361. * @param {Cartesian2} encoded The oct encoded vector.
  362. * @returns {Number} The oct encoded vector packed into a single float.
  363. *
  364. */
  365. AttributeCompression.octPackFloat = function (encoded) {
  366. //>>includeStart('debug', pragmas.debug);
  367. RuntimeError.Check.defined("encoded", encoded);
  368. //>>includeEnd('debug');
  369. return 256.0 * encoded.x + encoded.y;
  370. };
  371. const scratchEncodeCart2 = new Matrix2.Cartesian2();
  372. /**
  373. * Encodes a normalized vector into 2 SNORM values in the range of [0-255] following the 'oct' encoding and
  374. * stores those values in a single float-point number.
  375. *
  376. * @param {Cartesian3} vector The normalized vector to be compressed into 2 byte 'oct' encoding.
  377. * @returns {Number} The 2 byte oct-encoded unit length vector.
  378. *
  379. * @exception {DeveloperError} vector must be normalized.
  380. */
  381. AttributeCompression.octEncodeFloat = function (vector) {
  382. AttributeCompression.octEncode(vector, scratchEncodeCart2);
  383. return AttributeCompression.octPackFloat(scratchEncodeCart2);
  384. };
  385. /**
  386. * Decodes a unit-length vector in 'oct' encoding packed in a floating-point number to a normalized 3-component vector.
  387. *
  388. * @param {Number} value The oct-encoded unit length vector stored as a single floating-point number.
  389. * @param {Cartesian3} result The decoded and normalized vector
  390. * @returns {Cartesian3} The decoded and normalized vector.
  391. *
  392. */
  393. AttributeCompression.octDecodeFloat = function (value, result) {
  394. //>>includeStart('debug', pragmas.debug);
  395. RuntimeError.Check.defined("value", value);
  396. //>>includeEnd('debug');
  397. const temp = value / 256.0;
  398. const x = Math.floor(temp);
  399. const y = (temp - x) * 256.0;
  400. return AttributeCompression.octDecode(x, y, result);
  401. };
  402. /**
  403. * Encodes three normalized vectors into 6 SNORM values in the range of [0-255] following the 'oct' encoding and
  404. * packs those into two floating-point numbers.
  405. *
  406. * @param {Cartesian3} v1 A normalized vector to be compressed.
  407. * @param {Cartesian3} v2 A normalized vector to be compressed.
  408. * @param {Cartesian3} v3 A normalized vector to be compressed.
  409. * @param {Cartesian2} result The 'oct' encoded vectors packed into two floating-point numbers.
  410. * @returns {Cartesian2} The 'oct' encoded vectors packed into two floating-point numbers.
  411. *
  412. */
  413. AttributeCompression.octPack = function (v1, v2, v3, result) {
  414. //>>includeStart('debug', pragmas.debug);
  415. RuntimeError.Check.defined("v1", v1);
  416. RuntimeError.Check.defined("v2", v2);
  417. RuntimeError.Check.defined("v3", v3);
  418. RuntimeError.Check.defined("result", result);
  419. //>>includeEnd('debug');
  420. const encoded1 = AttributeCompression.octEncodeFloat(v1);
  421. const encoded2 = AttributeCompression.octEncodeFloat(v2);
  422. const encoded3 = AttributeCompression.octEncode(v3, scratchEncodeCart2);
  423. result.x = 65536.0 * encoded3.x + encoded1;
  424. result.y = 65536.0 * encoded3.y + encoded2;
  425. return result;
  426. };
  427. /**
  428. * Decodes three unit-length vectors in 'oct' encoding packed into a floating-point number to a normalized 3-component vector.
  429. *
  430. * @param {Cartesian2} packed The three oct-encoded unit length vectors stored as two floating-point number.
  431. * @param {Cartesian3} v1 One decoded and normalized vector.
  432. * @param {Cartesian3} v2 One decoded and normalized vector.
  433. * @param {Cartesian3} v3 One decoded and normalized vector.
  434. */
  435. AttributeCompression.octUnpack = function (packed, v1, v2, v3) {
  436. //>>includeStart('debug', pragmas.debug);
  437. RuntimeError.Check.defined("packed", packed);
  438. RuntimeError.Check.defined("v1", v1);
  439. RuntimeError.Check.defined("v2", v2);
  440. RuntimeError.Check.defined("v3", v3);
  441. //>>includeEnd('debug');
  442. let temp = packed.x / 65536.0;
  443. const x = Math.floor(temp);
  444. const encodedFloat1 = (temp - x) * 65536.0;
  445. temp = packed.y / 65536.0;
  446. const y = Math.floor(temp);
  447. const encodedFloat2 = (temp - y) * 65536.0;
  448. AttributeCompression.octDecodeFloat(encodedFloat1, v1);
  449. AttributeCompression.octDecodeFloat(encodedFloat2, v2);
  450. AttributeCompression.octDecode(x, y, v3);
  451. };
  452. /**
  453. * Pack texture coordinates into a single float. The texture coordinates will only preserve 12 bits of precision.
  454. *
  455. * @param {Cartesian2} textureCoordinates The texture coordinates to compress. Both coordinates must be in the range 0.0-1.0.
  456. * @returns {Number} The packed texture coordinates.
  457. *
  458. */
  459. AttributeCompression.compressTextureCoordinates = function (
  460. textureCoordinates
  461. ) {
  462. //>>includeStart('debug', pragmas.debug);
  463. RuntimeError.Check.defined("textureCoordinates", textureCoordinates);
  464. //>>includeEnd('debug');
  465. // Move x and y to the range 0-4095;
  466. const x = (textureCoordinates.x * 4095.0) | 0;
  467. const y = (textureCoordinates.y * 4095.0) | 0;
  468. return 4096.0 * x + y;
  469. };
  470. /**
  471. * Decompresses texture coordinates that were packed into a single float.
  472. *
  473. * @param {Number} compressed The compressed texture coordinates.
  474. * @param {Cartesian2} result The decompressed texture coordinates.
  475. * @returns {Cartesian2} The modified result parameter.
  476. *
  477. */
  478. AttributeCompression.decompressTextureCoordinates = function (
  479. compressed,
  480. result
  481. ) {
  482. //>>includeStart('debug', pragmas.debug);
  483. RuntimeError.Check.defined("compressed", compressed);
  484. RuntimeError.Check.defined("result", result);
  485. //>>includeEnd('debug');
  486. const temp = compressed / 4096.0;
  487. const xZeroTo4095 = Math.floor(temp);
  488. result.x = xZeroTo4095 / 4095.0;
  489. result.y = (compressed - xZeroTo4095 * 4096) / 4095;
  490. return result;
  491. };
  492. function zigZagDecode(value) {
  493. return (value >> 1) ^ -(value & 1);
  494. }
  495. /**
  496. * Decodes delta and ZigZag encoded vertices. This modifies the buffers in place.
  497. *
  498. * @param {Uint16Array} uBuffer The buffer view of u values.
  499. * @param {Uint16Array} vBuffer The buffer view of v values.
  500. * @param {Uint16Array} [heightBuffer] The buffer view of height values.
  501. *
  502. * @see {@link https://github.com/CesiumGS/quantized-mesh|quantized-mesh-1.0 terrain format}
  503. */
  504. AttributeCompression.zigZagDeltaDecode = function (
  505. uBuffer,
  506. vBuffer,
  507. heightBuffer
  508. ) {
  509. //>>includeStart('debug', pragmas.debug);
  510. RuntimeError.Check.defined("uBuffer", uBuffer);
  511. RuntimeError.Check.defined("vBuffer", vBuffer);
  512. RuntimeError.Check.typeOf.number.equals(
  513. "uBuffer.length",
  514. "vBuffer.length",
  515. uBuffer.length,
  516. vBuffer.length
  517. );
  518. if (defaultValue.defined(heightBuffer)) {
  519. RuntimeError.Check.typeOf.number.equals(
  520. "uBuffer.length",
  521. "heightBuffer.length",
  522. uBuffer.length,
  523. heightBuffer.length
  524. );
  525. }
  526. //>>includeEnd('debug');
  527. const count = uBuffer.length;
  528. let u = 0;
  529. let v = 0;
  530. let height = 0;
  531. for (let i = 0; i < count; ++i) {
  532. u += zigZagDecode(uBuffer[i]);
  533. v += zigZagDecode(vBuffer[i]);
  534. uBuffer[i] = u;
  535. vBuffer[i] = v;
  536. if (defaultValue.defined(heightBuffer)) {
  537. height += zigZagDecode(heightBuffer[i]);
  538. heightBuffer[i] = height;
  539. }
  540. }
  541. };
  542. /**
  543. * Dequantizes a quantized typed array into a floating point typed array.
  544. *
  545. * @see {@link https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data}
  546. *
  547. * @param {Int8Array|Uint8Array|Int16Array|Uint16Array|Int32Array|Uint32Array} typedArray The typed array for the quantized data.
  548. * @param {ComponentDatatype} componentDatatype The component datatype of the quantized data.
  549. * @param {AttributeType} type The attribute type of the quantized data.
  550. * @param {Number} count The number of attributes referenced in the dequantized array.
  551. *
  552. * @returns {Float32Array} The dequantized array.
  553. */
  554. AttributeCompression.dequantize = function (
  555. typedArray,
  556. componentDatatype,
  557. type,
  558. count
  559. ) {
  560. //>>includeStart('debug', pragmas.debug);
  561. RuntimeError.Check.defined("typedArray", typedArray);
  562. RuntimeError.Check.defined("componentDatatype", componentDatatype);
  563. RuntimeError.Check.defined("type", type);
  564. RuntimeError.Check.defined("count", count);
  565. //>>includeEnd('debug');
  566. const componentsPerAttribute = AttributeType$1.getNumberOfComponents(type);
  567. let divisor;
  568. switch (componentDatatype) {
  569. case ComponentDatatype.ComponentDatatype.BYTE:
  570. divisor = 127.0;
  571. break;
  572. case ComponentDatatype.ComponentDatatype.UNSIGNED_BYTE:
  573. divisor = 255.0;
  574. break;
  575. case ComponentDatatype.ComponentDatatype.SHORT:
  576. divisor = 32767.0;
  577. break;
  578. case ComponentDatatype.ComponentDatatype.UNSIGNED_SHORT:
  579. divisor = 65535.0;
  580. break;
  581. case ComponentDatatype.ComponentDatatype.INT:
  582. divisor = 2147483647.0;
  583. break;
  584. case ComponentDatatype.ComponentDatatype.UNSIGNED_INT:
  585. divisor = 4294967295.0;
  586. break;
  587. //>>includeStart('debug', pragmas.debug);
  588. default:
  589. throw new RuntimeError.DeveloperError(
  590. `Cannot dequantize component datatype: ${componentDatatype}`
  591. );
  592. //>>includeEnd('debug');
  593. }
  594. const dequantizedTypedArray = new Float32Array(
  595. count * componentsPerAttribute
  596. );
  597. for (let i = 0; i < count; i++) {
  598. for (let j = 0; j < componentsPerAttribute; j++) {
  599. const index = i * componentsPerAttribute + j;
  600. dequantizedTypedArray[index] = Math.max(
  601. typedArray[index] / divisor,
  602. -1.0
  603. );
  604. }
  605. }
  606. return dequantizedTypedArray;
  607. };
  608. /**
  609. * Decode RGB565-encoded colors into a floating point typed array containing
  610. * normalized RGB values.
  611. *
  612. * @param {Uint16Array} typedArray Array of RGB565 values
  613. * @param {Float32Array} [result] Array to store the normalized VEC3 result
  614. */
  615. AttributeCompression.decodeRGB565 = function (typedArray, result) {
  616. //>>includeStart('debug', pragmas.debug);
  617. RuntimeError.Check.defined("typedArray", typedArray);
  618. const expectedLength = typedArray.length * 3;
  619. if (defaultValue.defined(result)) {
  620. RuntimeError.Check.typeOf.number.equals(
  621. "result.length",
  622. "typedArray.length * 3",
  623. result.length,
  624. expectedLength
  625. );
  626. }
  627. //>>includeEnd('debug');
  628. const count = typedArray.length;
  629. if (!defaultValue.defined(result)) {
  630. result = new Float32Array(count * 3);
  631. }
  632. const mask5 = (1 << 5) - 1;
  633. const mask6 = (1 << 6) - 1;
  634. const normalize5 = 1.0 / 31.0;
  635. const normalize6 = 1.0 / 63.0;
  636. for (let i = 0; i < count; i++) {
  637. const value = typedArray[i];
  638. const red = value >> 11;
  639. const green = (value >> 5) & mask6;
  640. const blue = value & mask5;
  641. const offset = 3 * i;
  642. result[offset] = red * normalize5;
  643. result[offset + 1] = green * normalize6;
  644. result[offset + 2] = blue * normalize5;
  645. }
  646. return result;
  647. };
  648. exports.AttributeCompression = AttributeCompression;
  649. }));