galacean / engine

A typescript interactive engine, support 2D, 3D, animation, physics, built on WebGL and glTF.
https://galacean.antgroup.com/engine
MIT License
4.25k stars 301 forks source link

How to use custom loader #2385

Open zbx99 opened 4 weeks ago

zbx99 commented 4 weeks ago

I try to write my own custom loader like this.https://galacean.antgroup.com/engine/docs/assets/custom/ ,but I cannot use my own custom loader.Here is my loader.

export class ZipLoader extends Loader<GLTFResource>{
  load(
    item: LoadItem,
    resourceManager: ResourceManager
  ): AssetPromise<GLTFResource> {
    // 读取zip压缩文件,然后传回GLTFResource
    if (item.url) {
      fs.readFile(item.url!, (err, data) => {
        if (err) throw err;
        // 使用JSZip解压文件
        JSZip.loadAsync(data).then((zip) => {
          zip.forEach((relativePath,zipEntry)=>{
            if (/\.(gltf|glb)$/i.test(zipEntry.name)) {
              zipEntry.async("base64").then((file)=>{
                 return resourceManager.load<GLTFResource>(file);
              })
            }
          })
        });
      });
    }
    return new AssetPromise<GLTFResource>((resolve, reject, setTaskCompleteProgress, setTaskDetailProgress) => {})
  }
}
    const baseUrl = "assets/Building/output"
    // 加载ZIP文件
    let self = this;
    this.engine.resourceManager.load<GALACEAN.GLTFResource>({
      type:"ZIP",
      url:baseUrl+index+".zip"
    }).then((asset) => {
      console.log(asset)
    })
  }
cptbtptpbcptdtptp commented 3 weeks ago

You need to call resourceLoader before use to tell the engine that your loader is required to load this file. image

cptbtptpbcptdtptp commented 3 weeks ago

You can write like this:

@resourceLoader("ZIP", ["zip"])
export class ZipLoader extends Loader<GLTFResource>{