decodeDraco.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * @license
  3. * Cesium - https://github.com/CesiumGS/cesium
  4. * Version 1.97
  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(['./ComponentDatatype-e06f4e16', './defaultValue-a6eb9f34', './IndexDatatype-c2232ebd', './RuntimeError-1088cc64', './createTaskProcessorWorker', './WebGLConstants-d81b330d'], (function (ComponentDatatype, defaultValue, IndexDatatype, RuntimeError, createTaskProcessorWorker, WebGLConstants) { 'use strict';
  26. /* global require */
  27. let draco;
  28. function decodeIndexArray(dracoGeometry, dracoDecoder) {
  29. const numPoints = dracoGeometry.num_points();
  30. const numFaces = dracoGeometry.num_faces();
  31. const faceIndices = new draco.DracoInt32Array();
  32. const numIndices = numFaces * 3;
  33. const indexArray = IndexDatatype.IndexDatatype.createTypedArray(numPoints, numIndices);
  34. let offset = 0;
  35. for (let i = 0; i < numFaces; ++i) {
  36. dracoDecoder.GetFaceFromMesh(dracoGeometry, i, faceIndices);
  37. indexArray[offset + 0] = faceIndices.GetValue(0);
  38. indexArray[offset + 1] = faceIndices.GetValue(1);
  39. indexArray[offset + 2] = faceIndices.GetValue(2);
  40. offset += 3;
  41. }
  42. draco.destroy(faceIndices);
  43. return {
  44. typedArray: indexArray,
  45. numberOfIndices: numIndices,
  46. };
  47. }
  48. function decodeQuantizedDracoTypedArray(
  49. dracoGeometry,
  50. dracoDecoder,
  51. dracoAttribute,
  52. quantization,
  53. vertexArrayLength
  54. ) {
  55. let vertexArray;
  56. let attributeData;
  57. if (quantization.quantizationBits <= 8) {
  58. attributeData = new draco.DracoUInt8Array();
  59. vertexArray = new Uint8Array(vertexArrayLength);
  60. dracoDecoder.GetAttributeUInt8ForAllPoints(
  61. dracoGeometry,
  62. dracoAttribute,
  63. attributeData
  64. );
  65. } else {
  66. attributeData = new draco.DracoUInt16Array();
  67. vertexArray = new Uint16Array(vertexArrayLength);
  68. dracoDecoder.GetAttributeUInt16ForAllPoints(
  69. dracoGeometry,
  70. dracoAttribute,
  71. attributeData
  72. );
  73. }
  74. for (let i = 0; i < vertexArrayLength; ++i) {
  75. vertexArray[i] = attributeData.GetValue(i);
  76. }
  77. draco.destroy(attributeData);
  78. return vertexArray;
  79. }
  80. function decodeDracoTypedArray(
  81. dracoGeometry,
  82. dracoDecoder,
  83. dracoAttribute,
  84. vertexArrayLength
  85. ) {
  86. let vertexArray;
  87. let attributeData;
  88. // Some attribute types are casted down to 32 bit since Draco only returns 32 bit values
  89. switch (dracoAttribute.data_type()) {
  90. case 1:
  91. case 11: // DT_INT8 or DT_BOOL
  92. attributeData = new draco.DracoInt8Array();
  93. vertexArray = new Int8Array(vertexArrayLength);
  94. dracoDecoder.GetAttributeInt8ForAllPoints(
  95. dracoGeometry,
  96. dracoAttribute,
  97. attributeData
  98. );
  99. break;
  100. case 2: // DT_UINT8
  101. attributeData = new draco.DracoUInt8Array();
  102. vertexArray = new Uint8Array(vertexArrayLength);
  103. dracoDecoder.GetAttributeUInt8ForAllPoints(
  104. dracoGeometry,
  105. dracoAttribute,
  106. attributeData
  107. );
  108. break;
  109. case 3: // DT_INT16
  110. attributeData = new draco.DracoInt16Array();
  111. vertexArray = new Int16Array(vertexArrayLength);
  112. dracoDecoder.GetAttributeInt16ForAllPoints(
  113. dracoGeometry,
  114. dracoAttribute,
  115. attributeData
  116. );
  117. break;
  118. case 4: // DT_UINT16
  119. attributeData = new draco.DracoUInt16Array();
  120. vertexArray = new Uint16Array(vertexArrayLength);
  121. dracoDecoder.GetAttributeUInt16ForAllPoints(
  122. dracoGeometry,
  123. dracoAttribute,
  124. attributeData
  125. );
  126. break;
  127. case 5:
  128. case 7: // DT_INT32 or DT_INT64
  129. attributeData = new draco.DracoInt32Array();
  130. vertexArray = new Int32Array(vertexArrayLength);
  131. dracoDecoder.GetAttributeInt32ForAllPoints(
  132. dracoGeometry,
  133. dracoAttribute,
  134. attributeData
  135. );
  136. break;
  137. case 6:
  138. case 8: // DT_UINT32 or DT_UINT64
  139. attributeData = new draco.DracoUInt32Array();
  140. vertexArray = new Uint32Array(vertexArrayLength);
  141. dracoDecoder.GetAttributeUInt32ForAllPoints(
  142. dracoGeometry,
  143. dracoAttribute,
  144. attributeData
  145. );
  146. break;
  147. case 9:
  148. case 10: // DT_FLOAT32 or DT_FLOAT64
  149. attributeData = new draco.DracoFloat32Array();
  150. vertexArray = new Float32Array(vertexArrayLength);
  151. dracoDecoder.GetAttributeFloatForAllPoints(
  152. dracoGeometry,
  153. dracoAttribute,
  154. attributeData
  155. );
  156. break;
  157. }
  158. for (let i = 0; i < vertexArrayLength; ++i) {
  159. vertexArray[i] = attributeData.GetValue(i);
  160. }
  161. draco.destroy(attributeData);
  162. return vertexArray;
  163. }
  164. function decodeAttribute(dracoGeometry, dracoDecoder, dracoAttribute) {
  165. const numPoints = dracoGeometry.num_points();
  166. const numComponents = dracoAttribute.num_components();
  167. let quantization;
  168. let transform = new draco.AttributeQuantizationTransform();
  169. if (transform.InitFromAttribute(dracoAttribute)) {
  170. const minValues = new Array(numComponents);
  171. for (let i = 0; i < numComponents; ++i) {
  172. minValues[i] = transform.min_value(i);
  173. }
  174. quantization = {
  175. quantizationBits: transform.quantization_bits(),
  176. minValues: minValues,
  177. range: transform.range(),
  178. octEncoded: false,
  179. };
  180. }
  181. draco.destroy(transform);
  182. transform = new draco.AttributeOctahedronTransform();
  183. if (transform.InitFromAttribute(dracoAttribute)) {
  184. quantization = {
  185. quantizationBits: transform.quantization_bits(),
  186. octEncoded: true,
  187. };
  188. }
  189. draco.destroy(transform);
  190. const vertexArrayLength = numPoints * numComponents;
  191. let vertexArray;
  192. if (defaultValue.defined(quantization)) {
  193. vertexArray = decodeQuantizedDracoTypedArray(
  194. dracoGeometry,
  195. dracoDecoder,
  196. dracoAttribute,
  197. quantization,
  198. vertexArrayLength
  199. );
  200. } else {
  201. vertexArray = decodeDracoTypedArray(
  202. dracoGeometry,
  203. dracoDecoder,
  204. dracoAttribute,
  205. vertexArrayLength
  206. );
  207. }
  208. const componentDatatype = ComponentDatatype.ComponentDatatype.fromTypedArray(vertexArray);
  209. return {
  210. array: vertexArray,
  211. data: {
  212. componentsPerAttribute: numComponents,
  213. componentDatatype: componentDatatype,
  214. byteOffset: dracoAttribute.byte_offset(),
  215. byteStride:
  216. ComponentDatatype.ComponentDatatype.getSizeInBytes(componentDatatype) * numComponents,
  217. normalized: dracoAttribute.normalized(),
  218. quantization: quantization,
  219. },
  220. };
  221. }
  222. function decodePointCloud(parameters) {
  223. const dracoDecoder = new draco.Decoder();
  224. if (parameters.dequantizeInShader) {
  225. dracoDecoder.SkipAttributeTransform(draco.POSITION);
  226. dracoDecoder.SkipAttributeTransform(draco.NORMAL);
  227. }
  228. const buffer = new draco.DecoderBuffer();
  229. buffer.Init(parameters.buffer, parameters.buffer.length);
  230. const geometryType = dracoDecoder.GetEncodedGeometryType(buffer);
  231. if (geometryType !== draco.POINT_CLOUD) {
  232. throw new RuntimeError.RuntimeError("Draco geometry type must be POINT_CLOUD.");
  233. }
  234. const dracoPointCloud = new draco.PointCloud();
  235. const decodingStatus = dracoDecoder.DecodeBufferToPointCloud(
  236. buffer,
  237. dracoPointCloud
  238. );
  239. if (!decodingStatus.ok() || dracoPointCloud.ptr === 0) {
  240. throw new RuntimeError.RuntimeError(
  241. `Error decoding draco point cloud: ${decodingStatus.error_msg()}`
  242. );
  243. }
  244. draco.destroy(buffer);
  245. const result = {};
  246. const properties = parameters.properties;
  247. for (const propertyName in properties) {
  248. if (properties.hasOwnProperty(propertyName)) {
  249. let dracoAttribute;
  250. if (propertyName === "POSITION" || propertyName === "NORMAL") {
  251. const dracoAttributeId = dracoDecoder.GetAttributeId(
  252. dracoPointCloud,
  253. draco[propertyName]
  254. );
  255. dracoAttribute = dracoDecoder.GetAttribute(
  256. dracoPointCloud,
  257. dracoAttributeId
  258. );
  259. } else {
  260. const attributeId = properties[propertyName];
  261. dracoAttribute = dracoDecoder.GetAttributeByUniqueId(
  262. dracoPointCloud,
  263. attributeId
  264. );
  265. }
  266. result[propertyName] = decodeAttribute(
  267. dracoPointCloud,
  268. dracoDecoder,
  269. dracoAttribute
  270. );
  271. }
  272. }
  273. draco.destroy(dracoPointCloud);
  274. draco.destroy(dracoDecoder);
  275. return result;
  276. }
  277. function decodePrimitive(parameters) {
  278. const dracoDecoder = new draco.Decoder();
  279. // Skip all parameter types except generic
  280. const attributesToSkip = ["POSITION", "NORMAL", "COLOR", "TEX_COORD"];
  281. if (parameters.dequantizeInShader) {
  282. for (let i = 0; i < attributesToSkip.length; ++i) {
  283. dracoDecoder.SkipAttributeTransform(draco[attributesToSkip[i]]);
  284. }
  285. }
  286. const bufferView = parameters.bufferView;
  287. const buffer = new draco.DecoderBuffer();
  288. buffer.Init(parameters.array, bufferView.byteLength);
  289. const geometryType = dracoDecoder.GetEncodedGeometryType(buffer);
  290. if (geometryType !== draco.TRIANGULAR_MESH) {
  291. throw new RuntimeError.RuntimeError("Unsupported draco mesh geometry type.");
  292. }
  293. const dracoGeometry = new draco.Mesh();
  294. const decodingStatus = dracoDecoder.DecodeBufferToMesh(buffer, dracoGeometry);
  295. if (!decodingStatus.ok() || dracoGeometry.ptr === 0) {
  296. throw new RuntimeError.RuntimeError(
  297. `Error decoding draco mesh geometry: ${decodingStatus.error_msg()}`
  298. );
  299. }
  300. draco.destroy(buffer);
  301. const attributeData = {};
  302. const compressedAttributes = parameters.compressedAttributes;
  303. for (const attributeName in compressedAttributes) {
  304. if (compressedAttributes.hasOwnProperty(attributeName)) {
  305. const compressedAttribute = compressedAttributes[attributeName];
  306. const dracoAttribute = dracoDecoder.GetAttributeByUniqueId(
  307. dracoGeometry,
  308. compressedAttribute
  309. );
  310. attributeData[attributeName] = decodeAttribute(
  311. dracoGeometry,
  312. dracoDecoder,
  313. dracoAttribute
  314. );
  315. }
  316. }
  317. const result = {
  318. indexArray: decodeIndexArray(dracoGeometry, dracoDecoder),
  319. attributeData: attributeData,
  320. };
  321. draco.destroy(dracoGeometry);
  322. draco.destroy(dracoDecoder);
  323. return result;
  324. }
  325. function decode(parameters) {
  326. if (defaultValue.defined(parameters.bufferView)) {
  327. return decodePrimitive(parameters);
  328. }
  329. return decodePointCloud(parameters);
  330. }
  331. function initWorker(dracoModule) {
  332. draco = dracoModule;
  333. self.onmessage = createTaskProcessorWorker(decode);
  334. self.postMessage(true);
  335. }
  336. function decodeDraco(event) {
  337. const data = event.data;
  338. // Expect the first message to be to load a web assembly module
  339. const wasmConfig = data.webAssemblyConfig;
  340. if (defaultValue.defined(wasmConfig)) {
  341. // Require and compile WebAssembly module, or use fallback if not supported
  342. return require([wasmConfig.modulePath], function (dracoModule) {
  343. if (defaultValue.defined(wasmConfig.wasmBinaryFile)) {
  344. if (!defaultValue.defined(dracoModule)) {
  345. dracoModule = self.DracoDecoderModule;
  346. }
  347. dracoModule(wasmConfig).then(function (compiledModule) {
  348. initWorker(compiledModule);
  349. });
  350. } else {
  351. initWorker(dracoModule());
  352. }
  353. });
  354. }
  355. }
  356. return decodeDraco;
  357. }));