TinyUSDZLoader.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { Loader } from "three"; // or https://cdn.jsdelivr.net/npm/three/build/three.module.js';
  2. // WASM module of TinyUSDZ.
  3. import initTinyUSDZNative from "./tinyusdz.js";
  4. class FetchAssetResolver {
  5. constructor() {
  6. this.assetCache = new Map();
  7. }
  8. async resolveAsync(uri) {
  9. try {
  10. const response = await fetch(uri);
  11. if (!response.ok) {
  12. throw new Error(`Failed to fetch asset: ${uri}`);
  13. }
  14. const data = await response.arrayBuffer();
  15. //console.log(`Fetched asset ${uri} successfully, size: ${data.byteLength} bytes`);
  16. this.assetCache.set(uri, data);
  17. return Promise.resolve([uri, data]);
  18. } catch (error) {
  19. console.error(`Error resolving asset ${uri}:`, error);
  20. throw error;
  21. }
  22. }
  23. getAsset(uri) {
  24. if (this.assetCache.has(uri)) {
  25. return this.assetCache.get(uri);
  26. } else {
  27. console.warn(`Asset not found in cache: ${uri}`);
  28. return null;
  29. }
  30. }
  31. hasAsset(uri) {
  32. return this.assetCache.has(uri);
  33. }
  34. setAsset(uri, data) {
  35. this.assetCache.set(uri, data);
  36. }
  37. clearCache() {
  38. this.assetCache.clear();
  39. }
  40. }
  41. // TODO
  42. //
  43. // Polish API
  44. //
  45. class TinyUSDZLoader extends Loader {
  46. constructor(manager) {
  47. super(manager);
  48. this.native_ = null;
  49. this.assetResolver_ = null;
  50. this.imageCache = {};
  51. this.textureCache = {};
  52. }
  53. // 初始化 native 模块(直接用普通 wasm,不用压缩)
  54. async init(options = {}) {
  55. if (!this.native_) {
  56. console.log("Initializing TinyUSDZ native module...");
  57. // 直接 import,不传 wasmBinary(让 Emscripten 自己 fetch /tinyusdz.wasm)
  58. this.native_ = await initTinyUSDZNative();
  59. if (!this.native_) {
  60. throw new Error("Failed to initialize TinyUSDZ native module.");
  61. }
  62. console.log("Native module initialized");
  63. }
  64. return this;
  65. }
  66. // load 方法保持不变(内部会调用 init)
  67. load(url, onLoad, onProgress, onError) {
  68. const scope = this;
  69. const initPromise = this.native_ ? Promise.resolve() : this.init();
  70. initPromise
  71. .then(() => fetch(url))
  72. .then((response) => response.arrayBuffer())
  73. .then((usd_data) => {
  74. const usd_binary = new Uint8Array(usd_data);
  75. scope.parse(usd_binary, url, onLoad, onError);
  76. })
  77. .catch((error) => {
  78. console.error("TinyUSDZLoader error:", error);
  79. if (onError) onError(error);
  80. });
  81. }
  82. // parse 方法保持不变
  83. parse(binary, filePath, onLoad, onError) {
  84. if (!this.native_) {
  85. const err = new Error("Native module not initialized");
  86. console.error(err);
  87. if (onError) onError(err);
  88. return;
  89. }
  90. const usd = new this.native_.TinyUSDZLoaderNative();
  91. const ok = usd.loadFromBinary(binary, filePath);
  92. if (!ok) {
  93. const err = new Error("Failed to load USD from binary", { cause: usd.error() });
  94. if (onError) onError(err);
  95. } else {
  96. onLoad(usd);
  97. }
  98. }
  99. // 其他方法(如 loadAsLayer)可以保留或根据需要删减
  100. }
  101. export { TinyUSDZLoader, FetchAssetResolver };