AttributeCompression-d0b97a83.js 24 KB

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