Skip to content

Commit bb008e7

Browse files
committed
V2.3.1: Extracted load and checkResourceDescriptorFiles to LoaderBase. Updated documentation.
1 parent d706faa commit bb008e7

File tree

4 files changed

+184
-150
lines changed

4 files changed

+184
-150
lines changed

docs/examples/loaders/LoaderSupport.html

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,35 @@ <h3>LoaderBase( [page:LoadingManager manager], [page:LoaderSupport.ConsoleLogger
405405
[page:LoaderSupport.ConsoleLogger logger] - logger to be used
406406
</div>
407407
<div>
408-
Base class to be used by loaders.
408+
Base class to be used by Loaders that provide load, parse, parseAsync and run
409409
</div>
410410

411411

412412
<h2>Methods</h2>
413-
413+
414+
<h3>[method:null load]( [page:String url], [page:Function onLoad], [page:Function onProgress], [page:Function onError], [page:Function onMeshAlter], [page:boolean useAsync] )</h3>
415+
<div>
416+
[page:String url] - A string containing the path/URL of the file to be loaded.<br>
417+
[page:Function onLoad] - A function to be called after loading is successfully completed. The function receives loaded [page:Object3D] as an argument.<br>
418+
[page:Function onProgress] - (optional) A function to be called while the loading is in progress. The argument will be the XMLHttpRequest instance, which contains [page:Integer total] and [page:Integer loaded] bytes.<br>
419+
[page:Function onError] - (optional) A function to be called if an error occurs during loading. The function receives the error as an argument.<br>
420+
[page:Function onMeshAlter] - (optional) A function to be called after a new mesh raw data becomes available for alteration.<br>
421+
[page:boolean useAsync] - (optional) If true, uses async loading with worker, if false loads data synchronously.
422+
</div>
423+
<div>
424+
Use this convenient method to load a file at the given URL. By default the fileLoader uses an ArrayBuffer.
425+
</div>
426+
427+
<h3>[method:ConsoleLogger checkResourceDescriptorFiles] ( [page:THREE.LoaderSupport.ResourceDescriptor resources], [page:Object fileDesc] )</h3>
428+
<div>
429+
[page:THREE.LoaderSupport.ResourceDescriptor resources] - Array of {@link THREE.LoaderSupport.ResourceDescriptor}
430+
[page:Object fileDesc] - Object describing which resources are of interest (ext, type (string or UInt8Array) and ignore (boolean))
431+
</div>
432+
<div>
433+
Identify files or content of interest from an Array of {@link THREE.LoaderSupport.ResourceDescriptor}. Returns Object with each "ext" and the corresponding {@link THREE.LoaderSupport.ResourceDescriptor}
434+
</div>
435+
436+
414437
<h3>[method:ConsoleLogger getLogger] ()</h3>
415438
<div>
416439
Returns [page:LoaderSupport.ConsoleLogger].

docs/examples/loaders/OBJLoader2.html

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h2>Constructor</h2>
4242

4343
<h3>[name]( [page:LoadingManager manager], [page:LoaderSupport.ConsoleLogger logger] )</h3>
4444
<div>
45-
[page:LoadingManager manager] - The [page:LoadingManager loadingManager] for the loader to use. Default is [page:LoadingManager THREE.DefaultLoadingManager].
45+
[page:LoadingManager manager] - The [page:LoadingManager loadingManager] for the loader to use. Default is [page:LoadingManager THREE.DefaultLoadingManager].<br>
4646
[page:LoaderSupport.ConsoleLogger logger] - logger to be used
4747
</div>
4848
<div>
@@ -71,19 +71,8 @@ <h3>[method:Object3D parseAsync]( [page:arraybuffer content], [page:Function onL
7171
</div>
7272

7373

74-
<h3>[method:null load]( [page:String url], [page:Function onLoad], [page:Function onProgress], [page:Function onError], [page:Function onMeshAlter], [page:boolean useAsync] )</h3>
75-
<div>
76-
[page:String url] - A string containing the path/URL of the <em>.obj</em> file.<br>
77-
[page:Function onLoad] - A function to be called after loading is successfully completed. The function receives loaded [page:Object3D] as an argument.<br>
78-
[page:Function onProgress] - (optional) A function to be called while the loading is in progress. The argument will be the XMLHttpRequest instance, which contains [page:Integer total] and [page:Integer loaded] bytes.<br>
79-
[page:Function onError] - (optional) A function to be called if an error occurs during loading. The function receives the error as an argument.<br>
80-
[page:Function onMeshAlter] - (optional) A function to be called after a new mesh raw data becomes available for alteration.<br>
81-
[page:boolean useAsync] - (optional) If true, uses async loading with worker, if false loads data synchronously.
82-
</div>
83-
<div>
84-
Use this convenient method to load an OBJ file at the given URL. By default the fileLoader uses an arraybuffer.
85-
</div>
86-
74+
<h3>[method:null load] => [page:LoaderSupport.LoaderBase.load]</h3>
75+
8776

8877
<h3>[method:null run]( [page:LoaderSupport.PrepData params], [page:LoaderSupport.WorkerSupport workerSupportExternal] )</h3>
8978
<div>
@@ -94,7 +83,10 @@ <h3>[method:null run]( [page:LoaderSupport.PrepData params], [page:LoaderSupport
9483
Run the loader according the provided instructions.
9584
</div>
9685

97-
86+
87+
<h3>[method:Object checkResourceDescriptorFiles] => [page:LoaderSupport.LoaderBase.checkResourceDescriptorFiles]</h3>
88+
89+
9890
<h3>[method:null setMaterialPerSmoothingGroup] ( [page:boolean materialPerSmoothingGroup] )</h3>
9991
<div>
10092
[page:boolean materialPerSmoothingGroup]

examples/js/loaders/LoaderSupport.js

Lines changed: 142 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ THREE.LoaderSupport.Builder = (function () {
802802
})();
803803

804804
/**
805-
* Base class to be used by loaders.
805+
* Base class to be used by Loaders that provide load, parse, parseAsync and run
806806
* @class
807807
*
808808
* @param {THREE.DefaultLoadingManager} [manager] The loadingManager for the loader to use. Default is {@link THREE.DefaultLoadingManager}
@@ -817,6 +817,9 @@ THREE.LoaderSupport.LoaderBase = (function () {
817817
this.manager = Validator.verifyInput( manager, THREE.DefaultLoadingManager );
818818
this.logger = Validator.verifyInput( logger, new ConsoleLogger() );
819819

820+
this.fileLoader = new THREE.FileLoader( this.manager );
821+
this.fileLoader.setResponseType( 'arraybuffer' );
822+
820823
this.modelName = '';
821824
this.instanceNo = 0;
822825
this.path = '';
@@ -826,7 +829,7 @@ THREE.LoaderSupport.LoaderBase = (function () {
826829
this.loaderRootNode = new THREE.Group();
827830
this.builder = new THREE.LoaderSupport.Builder( this.logger );
828831
this.callbacks = new THREE.LoaderSupport.Callbacks();
829-
};
832+
}
830833

831834
LoaderBase.prototype._applyPrepData = function ( prepData ) {
832835
if ( Validator.isValid( prepData ) ) {
@@ -945,6 +948,141 @@ THREE.LoaderSupport.LoaderBase = (function () {
945948
this.logger.logDebug( content );
946949
};
947950

951+
/**
952+
* Use this convenient method to load a file at the given URL. By default the fileLoader uses an ArrayBuffer.
953+
* @memberOf THREE.LoaderSupport.LoaderBase
954+
*
955+
* @param {string} url A string containing the path/URL of the file to be loaded.
956+
* @param {callback} onLoad A function to be called after loading is successfully completed. The function receives loaded Object3D as an argument.
957+
* @param {callback} [onProgress] A function to be called while the loading is in progress. The argument will be the XMLHttpRequest instance, which contains total and Integer bytes.
958+
* @param {callback} [onError] A function to be called if an error occurs during loading. The function receives the error as an argument.
959+
* @param {callback} [onMeshAlter] A function to be called after a new mesh raw data becomes available for alteration.
960+
* @param {boolean} [useAsync] If true, uses async loading with worker, if false loads data synchronously.
961+
*/
962+
LoaderBase.prototype.load = function ( url, onLoad, onProgress, onError, onMeshAlter, useAsync ) {
963+
var scope = this;
964+
if ( ! Validator.isValid( onProgress ) ) {
965+
var numericalValueRef = 0;
966+
var numericalValue = 0;
967+
onProgress = function ( event ) {
968+
if ( ! event.lengthComputable ) return;
969+
970+
numericalValue = event.loaded / event.total;
971+
if ( numericalValue > numericalValueRef ) {
972+
973+
numericalValueRef = numericalValue;
974+
var output = 'Download of "' + url + '": ' + ( numericalValue * 100 ).toFixed( 2 ) + '%';
975+
scope.onProgress( 'progressLoad', output, numericalValue );
976+
977+
}
978+
};
979+
}
980+
981+
if ( ! Validator.isValid( onError ) ) {
982+
onError = function ( event ) {
983+
var output = 'Error occurred while downloading "' + url + '"';
984+
scope.logger.logError( output + ': ' + event );
985+
scope.onProgress( 'error', output, -1 );
986+
};
987+
}
988+
989+
this.fileLoader.setPath( this.path );
990+
this.fileLoader.load( url, function ( content ) {
991+
if ( useAsync ) {
992+
993+
scope.parseAsync( content, onLoad );
994+
995+
} else {
996+
997+
var callbacks = new THREE.LoaderSupport.Callbacks();
998+
callbacks.setCallbackOnMeshAlter( onMeshAlter );
999+
scope._setCallbacks( callbacks );
1000+
onLoad(
1001+
{
1002+
detail: {
1003+
loaderRootNode: scope.parse( content ),
1004+
modelName: scope.modelName,
1005+
instanceNo: scope.instanceNo
1006+
}
1007+
}
1008+
);
1009+
1010+
}
1011+
1012+
}, onProgress, onError );
1013+
1014+
};
1015+
1016+
/**
1017+
* Identify files or content of interest from an Array of {@link THREE.LoaderSupport.ResourceDescriptor}.
1018+
*
1019+
* @param {THREE.LoaderSupport.ResourceDescriptor[]} resources Array of {@link THREE.LoaderSupport.ResourceDescriptor}
1020+
* @param Object fileDesc Object describing which resources are of interest (ext, type (string or UInt8Array) and ignore (boolean))
1021+
* @returns {{}} Object with each "ext" and the corresponding {@link THREE.LoaderSupport.ResourceDescriptor}
1022+
*/
1023+
LoaderBase.prototype.checkResourceDescriptorFiles = function ( resources, fileDesc ) {
1024+
var resource, triple, i, found;
1025+
var result = {};
1026+
1027+
for ( var index in resources ) {
1028+
1029+
resource = resources[ index ];
1030+
found = false;
1031+
if ( ! Validator.isValid( resource.name ) ) continue;
1032+
if ( Validator.isValid( resource.content ) ) {
1033+
1034+
for ( i = 0; i < fileDesc.length && !found; i++ ) {
1035+
1036+
triple = fileDesc[ i ];
1037+
if ( resource.extension.toLowerCase() === triple.ext.toLowerCase() ) {
1038+
1039+
if ( triple.ignore ) {
1040+
1041+
found = true;
1042+
1043+
} else if ( triple.type === "Uint8Array" ) {
1044+
1045+
// fast-fail on bad type
1046+
if ( ! ( resource.content instanceof Uint8Array ) ) throw 'Provided content is not of type arraybuffer! Aborting...';
1047+
result[ triple.ext ] = resource;
1048+
found = true;
1049+
1050+
} else if ( triple.type === "String" ) {
1051+
1052+
if ( ! (typeof(resource.content) === 'string' || resource.content instanceof String) ) throw 'Provided content is not of type String! Aborting...';
1053+
result[ triple.ext ] = resource;
1054+
found = true;
1055+
1056+
}
1057+
1058+
}
1059+
1060+
}
1061+
if ( !found ) throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
1062+
1063+
} else {
1064+
1065+
// fast-fail on bad type
1066+
if ( ! ( typeof( resource.name ) === 'string' || resource.name instanceof String ) ) throw 'Provided file is not properly defined! Aborting...';
1067+
for ( i = 0; i < fileDesc.length && !found; i++ ) {
1068+
1069+
triple = fileDesc[ i ];
1070+
if ( resource.extension.toLowerCase() === triple.ext.toLowerCase() ) {
1071+
1072+
if ( ! triple.ignore ) result[ triple.ext ] = resource;
1073+
found = true;
1074+
1075+
}
1076+
1077+
}
1078+
if ( !found ) throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
1079+
1080+
}
1081+
}
1082+
1083+
return result;
1084+
};
1085+
9481086
return LoaderBase;
9491087
})();
9501088

@@ -1226,12 +1364,12 @@ THREE.LoaderSupport.WorkerSupport = (function () {
12261364

12271365
if ( Validator.isValid( runnerImpl ) ) {
12281366

1229-
this.logger.logInfo( 'WorkerSupport: Using "' + runnerImpl.name + '" as Runncer class for worker.' );
1367+
this.logger.logInfo( 'WorkerSupport: Using "' + runnerImpl.name + '" as Runner class for worker.' );
12301368

12311369
} else {
12321370

12331371
runnerImpl = THREE.LoaderSupport.WorkerRunnerRefImpl;
1234-
this.logger.logInfo( 'WorkerSupport: Using DEFAULT "THREE.LoaderSupport.WorkerRunnerRefImpl" as Runncer class for worker.' );
1372+
this.logger.logInfo( 'WorkerSupport: Using DEFAULT "THREE.LoaderSupport.WorkerRunnerRefImpl" as Runner class for worker.' );
12351373

12361374
}
12371375

0 commit comments

Comments
 (0)