Skip to content

Commit

Permalink
glTF Loader: Allow to async return after all resources are loaded (#1029
Browse files Browse the repository at this point in the history
)
  • Loading branch information
georgios-uber committed Apr 3, 2019
1 parent 8055588 commit 7e7ea78
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion modules/addons/src/gltf/gltf-scenegraph-loader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/* global window */
import {GLTFParser} from '@loaders.gl/gltf';
import {DracoDecoder} from '@loaders.gl/draco';
import createGLTFObjects from './create-gltf-objects';

async function waitWhileCondition(condition) {
while (condition()) {
await new Promise(resolve => window.requestAnimationFrame(resolve));
}
}

async function parse(data, options, uri, loader) {
const gltfParser = new GLTFParser();
const gltf = await gltfParser.parse(data, {
Expand All @@ -10,7 +17,25 @@ async function parse(data, options, uri, loader) {
DracoDecoder
});

return Object.assign({gltfParser, gltf}, createGLTFObjects(options.gl, gltf, options));
const gltfObjects = createGLTFObjects(options.gl, gltf, options);

if (options.waitForFullLoad) {
const remaining = [];

gltfObjects.scenes.forEach(scene => {
scene.traverse(model => {
Object.values(model.model.program.uniforms).forEach(uniform => {
if (uniform.loaded === false) {
remaining.push(uniform);
}
});
});
});

await waitWhileCondition(() => remaining.some(uniform => !uniform.loaded));
}

return Object.assign({gltfParser, gltf}, gltfObjects);
}

export const GLBScenegraphLoader = {
Expand Down

0 comments on commit 7e7ea78

Please sign in to comment.