decodeDraco.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* This file is automatically rebuilt by the Cesium build process. */
  2. define(['./when-e6985d2a', './Check-24cae389', './Math-392d0035', './RuntimeError-61701d3e', './WebGLConstants-34c08bc0', './ComponentDatatype-cb08e294', './IndexDatatype-1be7d1f8', './createTaskProcessorWorker'], function (when, Check, _Math, RuntimeError, WebGLConstants, ComponentDatatype, IndexDatatype, createTaskProcessorWorker) { 'use strict';
  3. /* global require */
  4. var draco;
  5. function decodeIndexArray(dracoGeometry, dracoDecoder) {
  6. var numPoints = dracoGeometry.num_points();
  7. var numFaces = dracoGeometry.num_faces();
  8. var faceIndices = new draco.DracoInt32Array();
  9. var numIndices = numFaces * 3;
  10. var indexArray = IndexDatatype.IndexDatatype.createTypedArray(numPoints, numIndices);
  11. var offset = 0;
  12. for (var 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. var vertexArray;
  33. var 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 (var 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. var vertexArray;
  64. var 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 (var 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. var numPoints = dracoGeometry.num_points();
  143. var numComponents = dracoAttribute.num_components();
  144. var quantization;
  145. var transform = new draco.AttributeQuantizationTransform();
  146. if (transform.InitFromAttribute(dracoAttribute)) {
  147. var minValues = new Array(numComponents);
  148. for (var 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. var vertexArrayLength = numPoints * numComponents;
  168. var vertexArray;
  169. if (when.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. var 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. var dracoDecoder = new draco.Decoder();
  201. if (parameters.dequantizeInShader) {
  202. dracoDecoder.SkipAttributeTransform(draco.POSITION);
  203. dracoDecoder.SkipAttributeTransform(draco.NORMAL);
  204. }
  205. var buffer = new draco.DecoderBuffer();
  206. buffer.Init(parameters.buffer, parameters.buffer.length);
  207. var geometryType = dracoDecoder.GetEncodedGeometryType(buffer);
  208. if (geometryType !== draco.POINT_CLOUD) {
  209. throw new RuntimeError.RuntimeError("Draco geometry type must be POINT_CLOUD.");
  210. }
  211. var dracoPointCloud = new draco.PointCloud();
  212. var 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. var result = {};
  223. var properties = parameters.properties;
  224. for (var propertyName in properties) {
  225. if (properties.hasOwnProperty(propertyName)) {
  226. var attributeId = properties[propertyName];
  227. var dracoAttribute = dracoDecoder.GetAttributeByUniqueId(
  228. dracoPointCloud,
  229. attributeId
  230. );
  231. result[propertyName] = decodeAttribute(
  232. dracoPointCloud,
  233. dracoDecoder,
  234. dracoAttribute
  235. );
  236. }
  237. }
  238. draco.destroy(dracoPointCloud);
  239. draco.destroy(dracoDecoder);
  240. return result;
  241. }
  242. function decodePrimitive(parameters) {
  243. var dracoDecoder = new draco.Decoder();
  244. // Skip all parameter types except generic
  245. var attributesToSkip = ["POSITION", "NORMAL", "COLOR", "TEX_COORD"];
  246. if (parameters.dequantizeInShader) {
  247. for (var i = 0; i < attributesToSkip.length; ++i) {
  248. dracoDecoder.SkipAttributeTransform(draco[attributesToSkip[i]]);
  249. }
  250. }
  251. var bufferView = parameters.bufferView;
  252. var buffer = new draco.DecoderBuffer();
  253. buffer.Init(parameters.array, bufferView.byteLength);
  254. var geometryType = dracoDecoder.GetEncodedGeometryType(buffer);
  255. if (geometryType !== draco.TRIANGULAR_MESH) {
  256. throw new RuntimeError.RuntimeError("Unsupported draco mesh geometry type.");
  257. }
  258. var dracoGeometry = new draco.Mesh();
  259. var decodingStatus = dracoDecoder.DecodeBufferToMesh(buffer, dracoGeometry);
  260. if (!decodingStatus.ok() || dracoGeometry.ptr === 0) {
  261. throw new RuntimeError.RuntimeError(
  262. "Error decoding draco mesh geometry: " + decodingStatus.error_msg()
  263. );
  264. }
  265. draco.destroy(buffer);
  266. var attributeData = {};
  267. var compressedAttributes = parameters.compressedAttributes;
  268. for (var attributeName in compressedAttributes) {
  269. if (compressedAttributes.hasOwnProperty(attributeName)) {
  270. var compressedAttribute = compressedAttributes[attributeName];
  271. var dracoAttribute = dracoDecoder.GetAttributeByUniqueId(
  272. dracoGeometry,
  273. compressedAttribute
  274. );
  275. attributeData[attributeName] = decodeAttribute(
  276. dracoGeometry,
  277. dracoDecoder,
  278. dracoAttribute
  279. );
  280. }
  281. }
  282. var result = {
  283. indexArray: decodeIndexArray(dracoGeometry, dracoDecoder),
  284. attributeData: attributeData,
  285. };
  286. draco.destroy(dracoGeometry);
  287. draco.destroy(dracoDecoder);
  288. return result;
  289. }
  290. function decode(parameters) {
  291. if (when.defined(parameters.primitive)) {
  292. return decodePrimitive(parameters);
  293. }
  294. return decodePointCloud(parameters);
  295. }
  296. function initWorker(dracoModule) {
  297. draco = dracoModule;
  298. self.onmessage = createTaskProcessorWorker(decode);
  299. self.postMessage(true);
  300. }
  301. function decodeDraco(event) {
  302. var data = event.data;
  303. // Expect the first message to be to load a web assembly module
  304. var wasmConfig = data.webAssemblyConfig;
  305. if (when.defined(wasmConfig)) {
  306. // Require and compile WebAssembly module, or use fallback if not supported
  307. return require([wasmConfig.modulePath], function (dracoModule) {
  308. if (when.defined(wasmConfig.wasmBinaryFile)) {
  309. if (!when.defined(dracoModule)) {
  310. dracoModule = self.DracoDecoderModule;
  311. }
  312. dracoModule(wasmConfig).then(function (compiledModule) {
  313. initWorker(compiledModule);
  314. });
  315. } else {
  316. initWorker(dracoModule());
  317. }
  318. });
  319. }
  320. }
  321. return decodeDraco;
  322. });