From dc8233c1d5559e98c2d83b70c6da62a5dd1b7d9e Mon Sep 17 00:00:00 2001 From: Robert Long Date: Tue, 17 Jul 2018 13:34:12 -0700 Subject: [PATCH 1/2] Add lazy option to GLTFLoader. --- docs/examples/loaders/GLTFLoader.html | 28 +++++++++++++++++++++++++++ examples/js/loaders/GLTFLoader.js | 16 +++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/docs/examples/loaders/GLTFLoader.html b/docs/examples/loaders/GLTFLoader.html index dbb7988bf2b57d..f85d115fd48743 100644 --- a/docs/examples/loaders/GLTFLoader.html +++ b/docs/examples/loaders/GLTFLoader.html @@ -156,6 +156,34 @@

[method:null setDRACOLoader]( [param:DRACOLoader dracoLoader] )

Refer to this [link:https://github.com/mrdoob/three.js/tree/dev/examples/js/libs/draco#readme readme] for the details of Draco and its decoder.

+

[method:null setLazy]( [param:Boolean value] )

+

+ [page:String value] — When true, [page:Function parse] and [page:Function load] will return an object with the parser property set. +

+

+ When lazy loading is enabled and the provided url points to a .gltf file [page:Function load] will only download the .gltf file. [page:Function parse] and [page:Function load] will not call parser.parse() or parser.getDependency(). Examples: +

+ + var loader = new THREE.GLTFLoader(); + + loader.setLazy(true); + + loader.load('foo.gltf', function ( gltf ) { + + var parser = gltf.parser; + + // Modify the glTF before calling parse + parser.json.node[ 3 ].extensions = { EXT_foo: { bufferView: 3 } }; + + // Load part of the gltf + parser.getDependency( "bufferView", 1 ).then( createNavMesh ); + + // Load the entire glTF + parser.parse( function ( scene, scenes, cameras, animations, json ) { } ); + + } ); + +

[method:null parse]( [param:ArrayBuffer data], [param:String path], [param:Function onLoad], [param:Function onError] )

[page:ArrayBuffer data] — glTF asset to parse, as an ArrayBuffer or JSON string.
diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 8946d295adebc2..9175cd47696f2e 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -12,6 +12,7 @@ THREE.GLTFLoader = ( function () { this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager; this.dracoLoader = null; + this.lazy = false; } @@ -76,6 +77,13 @@ THREE.GLTFLoader = ( function () { }, + setLazy: function ( lazy ) { + + this.lazy = lazy; + return this; + + }, + parse: function ( data, path, onLoad, onError ) { var content; @@ -172,6 +180,14 @@ THREE.GLTFLoader = ( function () { } ); + if ( this.lazy ) { + + onLoad( { parser: parser } ); + + return; + + } + parser.parse( function ( scene, scenes, cameras, animations, json ) { var glTF = { From 77afa8cb776225332fcf24980b9fcc973387cb8f Mon Sep 17 00:00:00 2001 From: Robert Long Date: Wed, 25 Jul 2018 11:25:24 -0700 Subject: [PATCH 2/2] Call markDefs when using lazy flag. --- examples/js/loaders/GLTFLoader.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index 9175cd47696f2e..6e279560ae0d7e 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/examples/js/loaders/GLTFLoader.js @@ -182,6 +182,9 @@ THREE.GLTFLoader = ( function () { if ( this.lazy ) { + // Mark the special nodes/meshes in json for efficient parse + this.markDefs(); + onLoad( { parser: parser } ); return;