decodeDraco.js 12 KB

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