AttributeCompression-f202be44.js 24 KB

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