AttributeCompression-f9f6c717.js 23 KB

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