Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lazy option to GLTFLoader. #14492

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/examples/loaders/GLTFLoader.html
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,34 @@ <h3>[method:null setDRACOLoader]( [param:DRACOLoader dracoLoader] )</h3>
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.
</p>

<h3>[method:null setLazy]( [param:Boolean value] )</h3>
<p>
[page:String value] — When true, [page:Function parse] and [page:Function load] will return an object with the <em>parser</em> property set.
</p>
<p>
When lazy loading is enabled and the provided url points to a <em>.gltf</em> file [page:Function load] will only download the <em>.gltf</em> file. [page:Function parse] and [page:Function load] will not call <em>parser.parse()</em> or <em>parser.getDependency()</em>. Examples:
</p>
<code>
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 ) { } );

} );
</code>

<h3>[method:null parse]( [param:ArrayBuffer data], [param:String path], [param:Function onLoad], [param:Function onError] )</h3>
<p>
[page:ArrayBuffer data] — glTF asset to parse, as an ArrayBuffer or <em>JSON</em> string.<br />
Expand Down
16 changes: 16 additions & 0 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ THREE.GLTFLoader = ( function () {

this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
this.dracoLoader = null;
this.lazy = false;

}

Expand Down Expand Up @@ -76,6 +77,13 @@ THREE.GLTFLoader = ( function () {

},

setLazy: function ( lazy ) {

this.lazy = lazy;
return this;

},

parse: function ( data, path, onLoad, onError ) {

var content;
Expand Down Expand Up @@ -172,6 +180,14 @@ THREE.GLTFLoader = ( function () {

} );

if ( this.lazy ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also some pre-processing in parser.parse() that still needs to happen here — specifically parser.markDefs().


onLoad( { parser: parser } );

return;

}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you want to also return the json object here? I imagine users will want to use that to access extensions data, or to list the available materials.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The json object is available via parser.json. I think this lines up well with the non-lazy method.

When specifying lazy:

onLoad( { parser: parser } )

When using the default non-lazy method:

onLoad( {
  scene: scene,
  scenes: scenes,
  cameras: cameras,
  animations: animations,
  asset: json.asset,
  parser: parser,
  userData: {}
} )


parser.parse( function ( scene, scenes, cameras, animations, json ) {

var glTF = {
Expand Down