qd-changjing/public/leaflet/libs/three/plugins/loaders/LoaderCommons.js

336 lines
9.7 KiB
JavaScript

if ( THREE.LoaderSupport === undefined ) { THREE.LoaderSupport = {} }
/**
* Validation functions.
* @class
*/
THREE.LoaderSupport.Validator = {
/**
* If given input is null or undefined, false is returned otherwise true.
*
* @param input Can be anything
* @returns {boolean}
*/
isValid: function( input ) {
return ( input !== null && input !== undefined );
},
/**
* If given input is null or undefined, the defaultValue is returned otherwise the given input.
*
* @param input Can be anything
* @param defaultValue Can be anything
* @returns {*}
*/
verifyInput: function( input, defaultValue ) {
return ( input === null || input === undefined ) ? defaultValue : input;
}
};
/**
* Callbacks utilized by loaders and builders.
* @class
*/
THREE.LoaderSupport.Callbacks = (function () {
var Validator = THREE.LoaderSupport.Validator;
function Callbacks() {
this.onProgress = null;
this.onMeshAlter = null;
this.onLoad = null;
this.onLoadMaterials = null;
}
/**
* Register callback function that is invoked by internal function "announceProgress" to print feedback.
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnProgress Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnProgress = function ( callbackOnProgress ) {
this.onProgress = Validator.verifyInput( callbackOnProgress, this.onProgress );
};
/**
* Register callback function that is called every time a mesh was loaded.
* Use {@link THREE.LoaderSupport.LoadedMeshUserOverride} for alteration instructions (geometry, material or disregard mesh).
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnMeshAlter Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnMeshAlter = function ( callbackOnMeshAlter ) {
this.onMeshAlter = Validator.verifyInput( callbackOnMeshAlter, this.onMeshAlter );
};
/**
* Register callback function that is called once loading of the complete OBJ file is completed.
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnLoad Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnLoad = function ( callbackOnLoad ) {
this.onLoad = Validator.verifyInput( callbackOnLoad, this.onLoad );
};
/**
* Register callback function that is called when materials have been loaded.
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnLoadMaterials Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnLoadMaterials = function ( callbackOnLoadMaterials ) {
this.onLoadMaterials = Validator.verifyInput( callbackOnLoadMaterials, this.onLoadMaterials );
};
return Callbacks;
})();
/**
* Object to return by callback onMeshAlter. Used to disregard a certain mesh or to return one to many meshes.
* @class
*
* @param {boolean} disregardMesh=false Tell implementation to completely disregard this mesh
* @param {boolean} disregardMesh=false Tell implementation that mesh(es) have been altered or added
*/
THREE.LoaderSupport.LoadedMeshUserOverride = (function () {
function LoadedMeshUserOverride( disregardMesh, alteredMesh ) {
this.disregardMesh = disregardMesh === true;
this.alteredMesh = alteredMesh === true;
this.meshes = [];
}
/**
* Add a mesh created within callback.
*
* @memberOf THREE.OBJLoader2.LoadedMeshUserOverride
*
* @param {THREE.Mesh} mesh
*/
LoadedMeshUserOverride.prototype.addMesh = function ( mesh ) {
this.meshes.push( mesh );
this.alteredMesh = true;
};
/**
* Answers if mesh shall be disregarded completely.
*
* @returns {boolean}
*/
LoadedMeshUserOverride.prototype.isDisregardMesh = function () {
return this.disregardMesh;
};
/**
* Answers if new mesh(es) were created.
*
* @returns {boolean}
*/
LoadedMeshUserOverride.prototype.providesAlteredMeshes = function () {
return this.alteredMesh;
};
return LoadedMeshUserOverride;
})();
/**
* A resource description used by {@link THREE.LoaderSupport.PrepData} and others.
* @class
*
* @param {string} url URL to the file
* @param {string} extension The file extension (type)
*/
THREE.LoaderSupport.ResourceDescriptor = (function () {
var Validator = THREE.LoaderSupport.Validator;
function ResourceDescriptor( url, extension ) {
var urlParts = url.split( '/' );
if ( urlParts.length < 2 ) {
this.path = null;
this.name = url;
this.url = url;
} else {
this.path = Validator.verifyInput( urlParts.slice( 0, urlParts.length - 1).join( '/' ) + '/', null );
this.name = Validator.verifyInput( urlParts[ urlParts.length - 1 ], null );
this.url = url;
}
this.extension = Validator.verifyInput( extension, "default" );
this.extension = this.extension.trim();
this.content = null;
}
/**
* Set the content of this resource
* @memberOf THREE.LoaderSupport.ResourceDescriptor
*
* @param {Object} content The file content as arraybuffer or text
*/
ResourceDescriptor.prototype.setContent = function ( content ) {
this.content = Validator.verifyInput( content, null );
};
return ResourceDescriptor;
})();
/**
* Configuration instructions to be used by run method.
* @class
*/
THREE.LoaderSupport.PrepData = (function () {
var Validator = THREE.LoaderSupport.Validator;
function PrepData( modelName ) {
this.logging = {
enabled: true,
debug: false
};
this.modelName = Validator.verifyInput( modelName, '' );
this.resources = [];
this.callbacks = new THREE.LoaderSupport.Callbacks();
}
/**
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
* @memberOf THREE.LoaderSupport.PrepData
*
* @param {boolean} enabled True or false.
* @param {boolean} debug True or false.
*/
PrepData.prototype.setLogging = function ( enabled, debug ) {
this.logging.enabled = enabled === true;
this.logging.debug = debug === true;
};
/**
* Returns all callbacks as {@link THREE.LoaderSupport.Callbacks}
* @memberOf THREE.LoaderSupport.PrepData
*
* @returns {THREE.LoaderSupport.Callbacks}
*/
PrepData.prototype.getCallbacks = function () {
return this.callbacks;
};
/**
* Add a resource description.
* @memberOf THREE.LoaderSupport.PrepData
*
* @param {THREE.LoaderSupport.ResourceDescriptor} Adds a {@link THREE.LoaderSupport.ResourceDescriptor}
*/
PrepData.prototype.addResource = function ( resource ) {
this.resources.push( resource );
};
/**
* Clones this object and returns it afterwards. Callbacks and resources are not cloned deep (references!).
* @memberOf THREE.LoaderSupport.PrepData
*
* @returns {@link THREE.LoaderSupport.PrepData}
*/
PrepData.prototype.clone = function () {
var clone = new THREE.LoaderSupport.PrepData( this.modelName );
clone.logging.enabled = this.logging.enabled;
clone.logging.debug = this.logging.debug;
clone.resources = this.resources;
clone.callbacks = this.callbacks;
var property, value;
for ( property in this ) {
value = this[ property ];
if ( ! clone.hasOwnProperty( property ) && typeof this[ property ] !== 'function' ) {
clone[ property ] = value;
}
}
return clone;
};
/**
* Identify files or content of interest from an Array of {@link THREE.LoaderSupport.ResourceDescriptor}.
* @memberOf THREE.LoaderSupport.PrepData
*
* @param {THREE.LoaderSupport.ResourceDescriptor[]} resources Array of {@link THREE.LoaderSupport.ResourceDescriptor}
* @param Object fileDesc Object describing which resources are of interest (ext, type (string or UInt8Array) and ignore (boolean))
* @returns {{}} Object with each "ext" and the corresponding {@link THREE.LoaderSupport.ResourceDescriptor}
*/
PrepData.prototype.checkResourceDescriptorFiles = function ( resources, fileDesc ) {
var resource, triple, i, found;
var result = {};
for ( var index in resources ) {
resource = resources[ index ];
found = false;
if ( ! Validator.isValid( resource.name ) ) continue;
if ( Validator.isValid( resource.content ) ) {
for ( i = 0; i < fileDesc.length && !found; i++ ) {
triple = fileDesc[ i ];
if ( resource.extension.toLowerCase() === triple.ext.toLowerCase() ) {
if ( triple.ignore ) {
found = true;
} else if ( triple.type === "ArrayBuffer" ) {
// fast-fail on bad type
if ( ! ( resource.content instanceof ArrayBuffer || resource.content instanceof Uint8Array ) ) throw 'Provided content is not of type ArrayBuffer! Aborting...';
result[ triple.ext ] = resource;
found = true;
} else if ( triple.type === "String" ) {
if ( ! ( typeof( resource.content ) === 'string' || resource.content instanceof String) ) throw 'Provided content is not of type String! Aborting...';
result[ triple.ext ] = resource;
found = true;
}
}
}
if ( !found ) throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
} else {
// fast-fail on bad type
if ( ! ( typeof( resource.name ) === 'string' || resource.name instanceof String ) ) throw 'Provided file is not properly defined! Aborting...';
for ( i = 0; i < fileDesc.length && !found; i++ ) {
triple = fileDesc[ i ];
if ( resource.extension.toLowerCase() === triple.ext.toLowerCase() ) {
if ( ! triple.ignore ) result[ triple.ext ] = resource;
found = true;
}
}
if ( !found ) throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
}
}
return result;
};
return PrepData;
})();