Skip to content

Commit

Permalink
enhance: process GLTFBufferParser and GLTFTextureParser pipelines…
Browse files Browse the repository at this point in the history
… in parallel.
  • Loading branch information
zhuxudong committed Jul 4, 2023
1 parent 1d15f56 commit ce566c6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/loader/src/gltf/parser/GLTFBufferParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class GLTFBufferParser extends GLTFParser {
}).then((glTF: IGLTF) => {
context.glTF = glTF;

return Promise.all(
const buffersPromise = Promise.all(
glTF.buffers.map((buffer: IBuffer) => {
const absoluteUrl = Utils.resolveAbsoluteUrl(url, buffer.uri);
restoreBufferRequests.push(new BufferRequestInfo(absoluteUrl, requestConfig));
Expand All @@ -40,6 +40,12 @@ export class GLTFBufferParser extends GLTFParser {
).then((buffers: ArrayBuffer[]) => {
context.buffers = buffers;
});
// If the textures are all urls, process `GLTFBufferParser` and `GLTFTextureParser` pipelines in parallel.
if (glTF.textures && glTF.textures.every((texture) => glTF.images[texture.source])) {
context.buffersPromise = buffersPromise;
} else {
return buffersPromise;
}
});
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/loader/src/gltf/parser/GLTFParserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { IGLTF } from "../GLTFSchema";
export class GLTFParserContext {
glTF: IGLTF;
buffers: ArrayBuffer[];
buffersPromise?: Promise<void>;
glTFResource: GLTFResource;
keepMeshData: boolean;
hasSkinned: boolean = false;
Expand Down
11 changes: 9 additions & 2 deletions packages/loader/src/gltf/parser/GLTFTextureParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class GLTFTextureParser extends GLTFParser {
};

parse(context: GLTFParserContext): AssetPromise<Texture2D[]> {
const { glTFResource, glTF, buffers } = context;
const { glTFResource, glTF, buffers, buffersPromise } = context;
const { engine, url } = glTFResource;

if (glTF.textures) {
Expand Down Expand Up @@ -66,7 +66,14 @@ export class GLTFTextureParser extends GLTFParser {
texturesPromiseInfo.resolve(textures);
})
.catch(texturesPromiseInfo.reject);
return texturesPromiseInfo.promise;

if (buffersPromise) {
return (<AssetPromise<void>>buffersPromise).then(() => {
return texturesPromiseInfo.promise;
});
} else {
return texturesPromiseInfo.promise;
}
}
}

Expand Down

0 comments on commit ce566c6

Please sign in to comment.