decodeDraco.js 11 KB

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