智慧水务管理系统 - 精河县供水工程综合管理平台

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import Check from "../Core/Check.js";
  2. import ComponentDatatype from "../Core/ComponentDatatype.js";
  3. import Frozen from "../Core/Frozen.js";
  4. import defined from "../Core/defined.js";
  5. import DracoLoader from "./DracoLoader.js";
  6. import ResourceLoader from "./ResourceLoader.js";
  7. import ResourceLoaderState from "./ResourceLoaderState.js";
  8. import VertexAttributeSemantic from "./VertexAttributeSemantic.js";
  9. /**
  10. * Load a draco buffer from a glTF.
  11. * <p>
  12. * Implements the {@link ResourceLoader} interface.
  13. * </p>
  14. *
  15. * @private
  16. */
  17. class GltfDracoLoader extends ResourceLoader {
  18. /**
  19. * @param {object} options Object with the following properties:
  20. * @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
  21. * @param {object} options.gltf The glTF JSON.
  22. * @param {object} options.primitive The primitive containing the Draco extension.
  23. * @param {object} options.draco The Draco extension object.
  24. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  25. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  26. * @param {string} [options.cacheKey] The cache key of the resource.
  27. */
  28. constructor(options) {
  29. super();
  30. options = options ?? Frozen.EMPTY_OBJECT;
  31. const resourceCache = options.resourceCache;
  32. const gltf = options.gltf;
  33. const primitive = options.primitive;
  34. const draco = options.draco;
  35. const gltfResource = options.gltfResource;
  36. const baseResource = options.baseResource;
  37. const cacheKey = options.cacheKey;
  38. //>>includeStart('debug', pragmas.debug);
  39. Check.typeOf.func("options.resourceCache", resourceCache);
  40. Check.typeOf.object("options.gltf", gltf);
  41. Check.typeOf.object("options.primitive", primitive);
  42. Check.typeOf.object("options.draco", draco);
  43. Check.typeOf.object("options.gltfResource", gltfResource);
  44. Check.typeOf.object("options.baseResource", baseResource);
  45. //>>includeEnd('debug');
  46. this._resourceCache = resourceCache;
  47. this._gltfResource = gltfResource;
  48. this._baseResource = baseResource;
  49. this._gltf = gltf;
  50. this._primitive = primitive;
  51. this._draco = draco;
  52. this._cacheKey = cacheKey;
  53. this._bufferViewLoader = undefined;
  54. this._bufferViewTypedArray = undefined;
  55. this._decodePromise = undefined;
  56. this._decodedData = undefined;
  57. this._state = ResourceLoaderState.UNLOADED;
  58. this._promise = undefined;
  59. this._dracoError = undefined;
  60. }
  61. /**
  62. * The cache key of the resource.
  63. *
  64. *
  65. * @type {string}
  66. * @readonly
  67. * @private
  68. */
  69. get cacheKey() {
  70. return this._cacheKey;
  71. }
  72. /**
  73. * The decoded data.
  74. *
  75. *
  76. * @type {object}
  77. * @readonly
  78. * @private
  79. */
  80. get decodedData() {
  81. return this._decodedData;
  82. }
  83. /**
  84. * Loads the resource.
  85. * @returns {Promise<GltfDracoLoader>} A promise which resolves to the loader when the resource loading is completed.
  86. * @private
  87. */
  88. async load() {
  89. if (defined(this._promise)) {
  90. return this._promise;
  91. }
  92. this._state = ResourceLoaderState.LOADING;
  93. this._promise = loadResources(this);
  94. return this._promise;
  95. }
  96. /**
  97. * Processes the resource until it becomes ready.
  98. *
  99. * @param {FrameState} frameState The frame state.
  100. * @private
  101. */
  102. process(frameState) {
  103. //>>includeStart('debug', pragmas.debug);
  104. Check.typeOf.object("frameState", frameState);
  105. //>>includeEnd('debug');
  106. if (this._state === ResourceLoaderState.READY) {
  107. return true;
  108. }
  109. if (this._state !== ResourceLoaderState.PROCESSING) {
  110. return false;
  111. }
  112. if (defined(this._dracoError)) {
  113. handleError(this, this._dracoError);
  114. }
  115. if (!defined(this._bufferViewTypedArray)) {
  116. // Not ready to decode the Draco buffer
  117. return false;
  118. }
  119. if (defined(this._decodePromise)) {
  120. // Currently decoding
  121. return false;
  122. }
  123. const draco = this._draco;
  124. const primitive = this._primitive;
  125. const gltf = this._gltf;
  126. const bufferViews = gltf.bufferViews;
  127. const bufferViewId = draco.bufferView;
  128. const bufferView = bufferViews[bufferViewId];
  129. const compressedAttributes = draco.attributes;
  130. // Skip de-quantization transform if present for floating point attributes.
  131. // They will stay quantized in memory and be dequantized in the shader.
  132. const attributesToSkipTransform = [];
  133. for (const attribute in primitive.attributes) {
  134. if (primitive.attributes.hasOwnProperty(attribute)) {
  135. const dracoAttributeType = getDracoAttributeType(attribute);
  136. if (defined(dracoAttributeType)) {
  137. const accessor = gltf.accessors[primitive.attributes[attribute]];
  138. if (accessor.componentType === ComponentDatatype.FLOAT) {
  139. if (!attributesToSkipTransform.includes(dracoAttributeType)) {
  140. attributesToSkipTransform.push(dracoAttributeType);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. const decodeOptions = {
  147. // Need to make a copy of the typed array otherwise the underlying
  148. // ArrayBuffer may be accessed on both the worker and the main thread. This
  149. // leads to errors such as "ArrayBuffer at index 0 is already detached".
  150. // PERFORMANCE_IDEA: Look into SharedArrayBuffer to get around this.
  151. array: new Uint8Array(this._bufferViewTypedArray),
  152. bufferView: bufferView,
  153. compressedAttributes: compressedAttributes,
  154. dequantizeInShader: true,
  155. attributesToSkipTransform: attributesToSkipTransform,
  156. };
  157. const decodePromise = DracoLoader.decodeBufferView(decodeOptions);
  158. if (!defined(decodePromise)) {
  159. // Cannot schedule task this frame
  160. return false;
  161. }
  162. this._decodePromise = processDecode(this, decodePromise);
  163. }
  164. /**
  165. * Unloads the resource.
  166. * @private
  167. */
  168. unload() {
  169. if (defined(this._bufferViewLoader)) {
  170. this._resourceCache.unload(this._bufferViewLoader);
  171. }
  172. this._bufferViewLoader = undefined;
  173. this._bufferViewTypedArray = undefined;
  174. this._decodedData = undefined;
  175. this._gltf = undefined;
  176. this._primitive = undefined;
  177. }
  178. }
  179. async function loadResources(loader) {
  180. const resourceCache = loader._resourceCache;
  181. try {
  182. const bufferViewLoader = resourceCache.getBufferViewLoader({
  183. gltf: loader._gltf,
  184. bufferViewId: loader._draco.bufferView,
  185. gltfResource: loader._gltfResource,
  186. baseResource: loader._baseResource,
  187. });
  188. loader._bufferViewLoader = bufferViewLoader;
  189. await bufferViewLoader.load();
  190. if (loader.isDestroyed()) {
  191. return;
  192. }
  193. loader._bufferViewTypedArray = bufferViewLoader.typedArray;
  194. loader._state = ResourceLoaderState.PROCESSING;
  195. return loader;
  196. } catch (error) {
  197. if (loader.isDestroyed()) {
  198. return;
  199. }
  200. handleError(loader, error);
  201. }
  202. }
  203. function handleError(dracoLoader, error) {
  204. dracoLoader.unload();
  205. dracoLoader._state = ResourceLoaderState.FAILED;
  206. const errorMessage = "Failed to load Draco";
  207. throw dracoLoader.getError(errorMessage, error);
  208. }
  209. async function processDecode(loader, decodePromise) {
  210. try {
  211. const results = await decodePromise;
  212. if (loader.isDestroyed()) {
  213. return;
  214. }
  215. // Unload everything except the decoded data
  216. loader.unload();
  217. loader._decodedData = {
  218. indices: results.indexArray,
  219. vertexAttributes: results.attributeData,
  220. };
  221. loader._state = ResourceLoaderState.READY;
  222. return loader._baseResource;
  223. } catch (error) {
  224. if (loader.isDestroyed()) {
  225. return;
  226. }
  227. // Capture this error so it can be thrown on the next `process` call
  228. loader._dracoError = error;
  229. }
  230. }
  231. const SemanticToDracoAttributeType = {};
  232. SemanticToDracoAttributeType[VertexAttributeSemantic.POSITION] = "POSITION";
  233. SemanticToDracoAttributeType[VertexAttributeSemantic.NORMAL] = "NORMAL";
  234. SemanticToDracoAttributeType[VertexAttributeSemantic.COLOR] = "COLOR";
  235. SemanticToDracoAttributeType[VertexAttributeSemantic.TEXCOORD] = "TEX_COORD";
  236. function getDracoAttributeType(attribute) {
  237. for (const semantic in SemanticToDracoAttributeType) {
  238. if (SemanticToDracoAttributeType.hasOwnProperty(semantic)) {
  239. if (attribute.startsWith(semantic)) {
  240. return SemanticToDracoAttributeType[semantic];
  241. }
  242. }
  243. }
  244. return undefined;
  245. }
  246. export default GltfDracoLoader;