12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490 |
- /**
- * @author Kai Salmen / https://kaisalmen.de
- * Development repository: https://github.com/kaisalmen/WWOBJLoader
- */
- 'use strict';
- 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;
- }
- };
- /**
- * Logging wrapper for console.
- * @class
- *
- * @param {boolean} enabled=true Tell if logger is enabled.
- * @param {boolean} debug=false Toggle debug logging.
- */
- THREE.LoaderSupport.ConsoleLogger = (function () {
- function ConsoleLogger( enabled, debug ) {
- this.enabled = enabled !== false;
- this.debug = debug === true;
- }
- /**
- * Enable or disable debug logging.
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @param {boolean} debug True or False
- */
- ConsoleLogger.prototype.setDebug = function ( debug ) {
- this.debug = debug === true;
- };
- /**
- * Returns if is enabled and debug.
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @returns {boolean}
- */
- ConsoleLogger.prototype.isDebug = function () {
- return this.isEnabled() && this.debug;
- };
- /**
- * Enable or disable info, debug and time logging.
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @param {boolean} enabled True or False
- */
- ConsoleLogger.prototype.setEnabled = function ( enabled ) {
- this.enabled = enabled === true;
- };
- /**
- * Returns if is enabled.
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @returns {boolean}
- */
- ConsoleLogger.prototype.isEnabled = function () {
- return this.enabled;
- };
- /**
- * Log a debug message if enabled and debug is set.
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @param {string} message Message to log
- */
- ConsoleLogger.prototype.logDebug = function ( message ) {
- if ( this.enabled && this.debug ) console.info( message );
- };
- /**
- * Log an info message if enabled.
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @param {string} message Message to log
- */
- ConsoleLogger.prototype.logInfo = function ( message ) {
- if ( this.enabled ) console.info( message );
- };
- /**
- * Log a warn message (always).
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @param {string} message Message to log
- */
- ConsoleLogger.prototype.logWarn = function ( message ) {
- console.warn( message );
- };
- /**
- * Log an error message (always).
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @param {string} message Message to log
- */
- ConsoleLogger.prototype.logError = function ( message ) {
- console.error( message );
- };
- /**
- * Start time measurement with provided id.
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @param {string} id Time identification
- */
- ConsoleLogger.prototype.logTimeStart = function ( id ) {
- if ( this.enabled ) console.time( id );
- };
- /**
- * Stop time measurement started with provided id.
- * @memberOf THREE.LoaderSupport.ConsoleLogger
- *
- * @param {string} id Time identification
- */
- ConsoleLogger.prototype.logTimeEnd = function ( id ) {
- if ( this.enabled ) console.timeEnd( id );
- };
- return ConsoleLogger;
- })();
- /**
- * Callbacks utilized by loaders and builder.
- * @class
- */
- THREE.LoaderSupport.Callbacks = (function () {
- var Validator = THREE.LoaderSupport.Validator;
- function Callbacks() {
- this.onProgress = null;
- this.onMeshAlter = null;
- this.onLoad = 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 );
- };
- 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 = 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 (String)
- * @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.modelName = Validator.verifyInput( modelName, '' );
- this.resources = [];
- this.streamMeshesTo = null;
- this.materialPerSmoothingGroup = false;
- this.useIndices = false;
- this.disregardNormals = false;
- this.callbacks = new THREE.LoaderSupport.Callbacks();
- this.crossOrigin;
- this.useAsync = false;
- }
- /**
- * Set the node where the loaded objects will be attached directly.
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @param {THREE.Object3D} streamMeshesTo Object already attached to scenegraph where new meshes will be attached to
- */
- PrepData.prototype.setStreamMeshesTo = function ( streamMeshesTo ) {
- this.streamMeshesTo = Validator.verifyInput( streamMeshesTo, null );
- };
- /**
- * Tells whether a material shall be created per smoothing group.
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @param {boolean} materialPerSmoothingGroup=false
- */
- PrepData.prototype.setMaterialPerSmoothingGroup = function ( materialPerSmoothingGroup ) {
- this.materialPerSmoothingGroup = materialPerSmoothingGroup === true;
- };
- /**
- * Tells whether indices should be used
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @param {boolean} useIndices=false
- */
- PrepData.prototype.setUseIndices = function ( useIndices ) {
- this.useIndices = useIndices === true;
- };
- /**
- * Tells whether normals should be completely disregarded and regenerated.
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @param {boolean} disregardNormals=false
- */
- PrepData.prototype.setDisregardNormals = function ( disregardNormals ) {
- this.disregardNormals = disregardNormals === true;
- };
- /**
- * Returns all callbacks as {@link THREE.LoaderSupport.Callbacks}
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @returns {THREE.LoaderSupport.Callbacks}
- */
- PrepData.prototype.getCallbacks = function () {
- return this.callbacks;
- };
- /**
- * Sets the CORS string to be used.
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @param {string} crossOrigin CORS value
- */
- PrepData.prototype.setCrossOrigin = function ( crossOrigin ) {
- this.crossOrigin = crossOrigin;
- };
- /**
- * Add a resource description.
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @param {THREE.LoaderSupport.ResourceDescriptor}
- */
- PrepData.prototype.addResource = function ( resource ) {
- this.resources.push( resource );
- };
- /**
- * If true uses async loading with worker, if false loads data synchronously.
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @param {boolean} useAsync
- */
- PrepData.prototype.setUseAsync = function ( useAsync ) {
- this.useAsync = useAsync === true;
- };
- /**
- * Clones this object and returns it afterwards.
- * @memberOf THREE.LoaderSupport.PrepData
- *
- * @returns {@link THREE.LoaderSupport.PrepData}
- */
- PrepData.prototype.clone = function () {
- var clone = new THREE.LoaderSupport.PrepData( this.modelName );
- clone.resources = this.resources;
- clone.streamMeshesTo = this.streamMeshesTo;
- clone.materialPerSmoothingGroup = this.materialPerSmoothingGroup;
- clone.useIndices = this.useIndices;
- clone.disregardNormals = this.disregardNormals;
- clone.callbacks = this.callbacks;
- clone.crossOrigin = this.crossOrigin;
- clone.useAsync = this.useAsync;
- return clone;
- };
- return PrepData;
- })();
- /**
- * Builds one or many THREE.Mesh from one raw set of Arraybuffers, materialGroup descriptions and further parameters.
- * Supports vertex, vertexColor, normal, uv and index buffers.
- * @class
- */
- THREE.LoaderSupport.Builder = (function () {
- var LOADER_BUILDER_VERSION = '1.1.0';
- var Validator = THREE.LoaderSupport.Validator;
- var ConsoleLogger = THREE.LoaderSupport.ConsoleLogger;
- function Builder( logger ) {
- this.logger = Validator.verifyInput( logger, new ConsoleLogger() );
- this.logger.logInfo( 'Using THREE.LoaderSupport.Builder version: ' + LOADER_BUILDER_VERSION );
- this.callbacks = new THREE.LoaderSupport.Callbacks();
- this.materials = [];
- }
- /**
- * Set materials loaded by any supplier of an Array of {@link THREE.Material}.
- * @memberOf THREE.LoaderSupport.Builder
- *
- * @param {THREE.Material[]} materials Array of {@link THREE.Material}
- */
- Builder.prototype.setMaterials = function ( materials ) {
- var payload = {
- cmd: 'materialData',
- materials: {
- materialCloneInstructions: null,
- serializedMaterials: null,
- runtimeMaterials: materials
- }
- };
- this.updateMaterials( payload );
- };
- Builder.prototype._setCallbacks = function ( callbackOnProgress, callbackOnMeshAlter, callbackOnLoad ) {
- this.callbacks.setCallbackOnProgress( callbackOnProgress );
- this.callbacks.setCallbackOnMeshAlter( callbackOnMeshAlter );
- this.callbacks.setCallbackOnLoad( callbackOnLoad );
- };
- /**
- * Delegates processing of the payload (mesh building or material update) to the corresponding functions (BW-compatibility).
- * @memberOf THREE.LoaderSupport.Builder
- *
- * @param {Object} payload Raw Mesh or Material descriptions.
- * @returns {THREE.Mesh[]} mesh Array of {@link THREE.Mesh} or null in case of material update
- */
- Builder.prototype.processPayload = function ( payload ) {
- if ( payload.cmd === 'meshData' ) {
- return this.buildMeshes( payload );
- } else if ( payload.cmd === 'materialData' ) {
- this.updateMaterials( payload );
- return null;
- }
- };
- /**
- * Builds one or multiple meshes from the data described in the payload (buffers, params, material info).
- * @memberOf THREE.LoaderSupport.Builder
- *
- * @param {Object} meshPayload Raw mesh description (buffers, params, materials) used to build one to many meshes.
- * @returns {THREE.Mesh[]} mesh Array of {@link THREE.Mesh}
- */
- Builder.prototype.buildMeshes = function ( meshPayload ) {
- var meshName = meshPayload.params.meshName;
- var bufferGeometry = new THREE.BufferGeometry();
- bufferGeometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( meshPayload.buffers.vertices ), 3 ) );
- if ( Validator.isValid( meshPayload.buffers.indices ) ) {
- bufferGeometry.setIndex( new THREE.BufferAttribute( new Uint32Array( meshPayload.buffers.indices ), 1 ));
- }
- var haveVertexColors = Validator.isValid( meshPayload.buffers.colors );
- if ( haveVertexColors ) {
- bufferGeometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( meshPayload.buffers.colors ), 3 ) );
- }
- if ( Validator.isValid( meshPayload.buffers.normals ) ) {
- bufferGeometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( meshPayload.buffers.normals ), 3 ) );
- } else {
- bufferGeometry.computeVertexNormals();
- }
- if ( Validator.isValid( meshPayload.buffers.uvs ) ) {
- bufferGeometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( meshPayload.buffers.uvs ), 2 ) );
- }
- var material, materialName, key;
- var materialNames = meshPayload.materials.materialNames;
- var createMultiMaterial = meshPayload.materials.multiMaterial;
- var multiMaterials = [];
- for ( key in materialNames ) {
- materialName = materialNames[ key ];
- material = this.materials[ materialName ];
- if ( createMultiMaterial ) multiMaterials.push( material );
- }
- if ( createMultiMaterial ) {
- material = multiMaterials;
- var materialGroups = meshPayload.materials.materialGroups;
- var materialGroup;
- for ( key in materialGroups ) {
- materialGroup = materialGroups[ key ];
- bufferGeometry.addGroup( materialGroup.start, materialGroup.count, materialGroup.index );
- }
- }
- var meshes = [];
- var mesh;
- var callbackOnMeshAlter = this.callbacks.onMeshAlter;
- var callbackOnMeshAlterResult;
- var useOrgMesh = true;
- if ( Validator.isValid( callbackOnMeshAlter ) ) {
- callbackOnMeshAlterResult = callbackOnMeshAlter(
- {
- detail: {
- meshName: meshName,
- bufferGeometry: bufferGeometry,
- material: material
- }
- }
- );
- if ( Validator.isValid( callbackOnMeshAlterResult ) ) {
- if ( ! callbackOnMeshAlterResult.isDisregardMesh() && callbackOnMeshAlterResult.providesAlteredMeshes() ) {
- for ( var i in callbackOnMeshAlterResult.meshes ) {
- meshes.push( callbackOnMeshAlterResult.meshes[ i ] );
- }
- }
- useOrgMesh = false;
- }
- }
- if ( useOrgMesh ) {
- mesh = new THREE.Mesh( bufferGeometry, material );
- mesh.name = meshName;
- meshes.push( mesh );
- }
- var progressMessage;
- if ( Validator.isValid( meshes ) && meshes.length > 0 ) {
- var meshNames = [];
- for ( var i in meshes ) {
- mesh = meshes[ i ];
- meshNames[ i ] = mesh.name;
- }
- progressMessage = 'Adding mesh(es) (' + meshNames.length + ': ' + meshNames + ') from input mesh: ' + meshName;
- progressMessage += ' (' + ( meshPayload.progress.numericalValue * 100 ).toFixed( 2 ) + '%)';
- } else {
- progressMessage = 'Not adding mesh: ' + meshName;
- progressMessage += ' (' + ( meshPayload.progress.numericalValue * 100 ).toFixed( 2 ) + '%)';
- }
- var callbackOnProgress = this.callbacks.onProgress;
- if ( Validator.isValid( callbackOnProgress ) ) {
- var event = new CustomEvent( 'BuilderEvent', {
- detail: {
- type: 'progress',
- modelName: meshPayload.params.meshName,
- text: progressMessage,
- numericalValue: meshPayload.progress.numericalValue
- }
- } );
- callbackOnProgress( event );
- }
- return meshes;
- };
- /**
- * Updates the materials with contained material objects (sync) or from alteration instructions (async).
- * @memberOf THREE.LoaderSupport.Builder
- *
- * @param {Object} materialPayload Material update instructions
- */
- Builder.prototype.updateMaterials = function ( materialPayload ) {
- var material, materialName;
- var materialCloneInstructions = materialPayload.materials.materialCloneInstructions;
- if ( Validator.isValid( materialCloneInstructions ) ) {
- var materialNameOrg = materialCloneInstructions.materialNameOrg;
- var materialOrg = this.materials[ materialNameOrg ];
- material = materialOrg.clone();
- materialName = materialCloneInstructions.materialName;
- material.name = materialName;
- var materialProperties = materialCloneInstructions.materialProperties;
- for ( var key in materialProperties ) {
- if ( material.hasOwnProperty( key ) && materialProperties.hasOwnProperty( key ) ) material[ key ] = materialProperties[ key ];
- }
- this.materials[ materialName ] = material;
- }
- var materials = materialPayload.materials.serializedMaterials;
- if ( Validator.isValid( materials ) && Object.keys( materials ).length > 0 ) {
- var loader = new THREE.MaterialLoader();
- var materialJson;
- for ( materialName in materials ) {
- materialJson = materials[ materialName ];
- if ( Validator.isValid( materialJson ) ) {
- material = loader.parse( materialJson );
- this.logger.logInfo( 'De-serialized material with name "' + materialName + '" will be added.' );
- this.materials[ materialName ] = material;
- }
- }
- }
- materials = materialPayload.materials.runtimeMaterials;
- if ( Validator.isValid( materials ) && Object.keys( materials ).length > 0 ) {
- for ( materialName in materials ) {
- material = materials[ materialName ];
- this.logger.logInfo( 'Material with name "' + materialName + '" will be added.' );
- this.materials[ materialName ] = material;
- }
- }
- };
- /**
- * Returns the mapping object of material name and corresponding jsonified material.
- *
- * @returns {Object} Map of Materials in JSON representation
- */
- Builder.prototype.getMaterialsJSON = function () {
- var materialsJSON = {};
- var material;
- for ( var materialName in this.materials ) {
- material = this.materials[ materialName ];
- materialsJSON[ materialName ] = material.toJSON();
- }
- return materialsJSON;
- };
- /**
- * Returns the mapping object of material name and corresponding material.
- *
- * @returns {Object} Map of {@link THREE.Material}
- */
- Builder.prototype.getMaterials = function () {
- return this.materials;
- };
- return Builder;
- })();
- /**
- * Base class to be used by loaders.
- * @class
- *
- * @param {THREE.DefaultLoadingManager} [manager] The loadingManager for the loader to use. Default is {@link THREE.DefaultLoadingManager}
- * @param {THREE.LoaderSupport.ConsoleLogger} logger logger to be used
- */
- THREE.LoaderSupport.LoaderBase = (function () {
- var Validator = THREE.LoaderSupport.Validator;
- var ConsoleLogger = THREE.LoaderSupport.ConsoleLogger;
- function LoaderBase( manager, logger ) {
- this.manager = Validator.verifyInput( manager, THREE.DefaultLoadingManager );
- this.logger = Validator.verifyInput( logger, new ConsoleLogger() );
- this.modelName = '';
- this.instanceNo = 0;
- this.path = '';
- this.useIndices = false;
- this.disregardNormals = false;
- this.loaderRootNode = new THREE.Group();
- this.builder = new THREE.LoaderSupport.Builder( this.logger );
- this._createDefaultMaterials();
- this.callbacks = new THREE.LoaderSupport.Callbacks();
- };
- LoaderBase.prototype._createDefaultMaterials = function () {
- var defaultMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
- defaultMaterial.name = 'defaultMaterial';
- var vertexColorMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
- vertexColorMaterial.name = 'vertexColorMaterial';
- vertexColorMaterial.vertexColors = THREE.VertexColors;
- var runtimeMaterials = {};
- runtimeMaterials[ defaultMaterial.name ] = defaultMaterial;
- runtimeMaterials[ vertexColorMaterial.name ] = vertexColorMaterial;
- this.builder.updateMaterials(
- {
- cmd: 'materialData',
- materials: {
- materialCloneInstructions: null,
- serializedMaterials: null,
- runtimeMaterials: runtimeMaterials
- }
- }
- );
- };
- LoaderBase.prototype._applyPrepData = function ( prepData ) {
- if ( Validator.isValid( prepData ) ) {
- this.setModelName( prepData.modelName );
- this.setStreamMeshesTo( prepData.streamMeshesTo );
- this.builder.setMaterials( prepData.materials );
- this.setUseIndices( prepData.useIndices );
- this.setDisregardNormals( prepData.disregardNormals );
- this._setCallbacks( prepData.getCallbacks().onProgress, prepData.getCallbacks().onMeshAlter, prepData.getCallbacks().onLoad );
- }
- };
- LoaderBase.prototype._setCallbacks = function ( callbackOnProgress, callbackOnMeshAlter, callbackOnLoad ) {
- this.callbacks.setCallbackOnProgress( callbackOnProgress );
- this.callbacks.setCallbackOnMeshAlter( callbackOnMeshAlter );
- this.callbacks.setCallbackOnLoad( callbackOnLoad );
- this.builder._setCallbacks( callbackOnProgress, callbackOnMeshAlter, callbackOnLoad );
- };
- /**
- * Provides access to console logging wrapper.
- *
- * @returns {THREE.LoaderSupport.ConsoleLogger}
- */
- LoaderBase.prototype.getLogger = function () {
- return this.logger;
- };
- /**
- * Set the name of the model.
- * @memberOf THREE.LoaderSupport.LoaderBase
- *
- * @param {string} modelName
- */
- LoaderBase.prototype.setModelName = function ( modelName ) {
- this.modelName = Validator.verifyInput( modelName, this.modelName );
- };
- /**
- * The URL of the base path.
- * @memberOf THREE.LoaderSupport.LoaderBase
- *
- * @param {string} path URL
- */
- LoaderBase.prototype.setPath = function ( path ) {
- this.path = Validator.verifyInput( path, this.path );
- };
- /**
- * Set the node where the loaded objects will be attached directly.
- * @memberOf THREE.LoaderSupport.LoaderBase
- *
- * @param {THREE.Object3D} streamMeshesTo Object already attached to scenegraph where new meshes will be attached to
- */
- LoaderBase.prototype.setStreamMeshesTo = function ( streamMeshesTo ) {
- this.loaderRootNode = Validator.verifyInput( streamMeshesTo, this.loaderRootNode );
- };
- /**
- * Set materials loaded by MTLLoader or any other supplier of an Array of {@link THREE.Material}.
- * @memberOf THREE.LoaderSupport.LoaderBase
- *
- * @param {THREE.Material[]} materials Array of {@link THREE.Material}
- */
- LoaderBase.prototype.setMaterials = function ( materials ) {
- this.builder.setMaterials( materials );
- };
- /**
- * Instructs loaders to create indexed {@link THREE.BufferGeometry}.
- * @memberOf THREE.LoaderSupport.LoaderBase
- *
- * @param {boolean} useIndices=false
- */
- LoaderBase.prototype.setUseIndices = function ( useIndices ) {
- this.useIndices = useIndices === true;
- };
- /**
- * Tells whether normals should be completely disregarded and regenerated.
- * @memberOf THREE.LoaderSupport.LoaderBase
- *
- * @param {boolean} disregardNormals=false
- */
- LoaderBase.prototype.setDisregardNormals = function ( disregardNormals ) {
- this.disregardNormals = disregardNormals === true;
- };
- /**
- * Announce feedback which is give to the registered callbacks.
- * @memberOf THREE.LoaderSupport.LoaderBase
- * @private
- *
- * @param {string} type The type of event
- * @param {string} text Textual description of the event
- * @param {number} numericalValue Numerical value describing the progress
- */
- LoaderBase.prototype.onProgress = function ( type, text, numericalValue ) {
- var content = Validator.isValid( text ) ? text: '';
- var event = {
- detail: {
- type: type,
- modelName: this.modelName,
- instanceNo: this.instanceNo,
- text: content,
- numericalValue: numericalValue
- }
- };
- if ( Validator.isValid( this.callbacks.onProgress ) ) this.callbacks.onProgress( event );
- this.logger.logDebug( content );
- };
- return LoaderBase;
- })();
- /**
- * Default implementation of the WorkerRunner responsible for creation and configuration of the parser within the worker.
- *
- * @class
- */
- THREE.LoaderSupport.WorkerRunnerRefImpl = (function () {
- function WorkerRunnerRefImpl() {
- var scope = this;
- var scopedRunner = function( event ) {
- scope.processMessage( event.data );
- };
- self.addEventListener( 'message', scopedRunner, false );
- }
- /**
- * Applies values from parameter object via set functions or via direct assignment.
- * @memberOf THREE.LoaderSupport.WorkerRunnerRefImpl
- *
- * @param {Object} parser The parser instance
- * @param {Object} params The parameter object
- */
- WorkerRunnerRefImpl.prototype.applyProperties = function ( parser, params ) {
- var property, funcName, values;
- for ( property in params ) {
- funcName = 'set' + property.substring( 0, 1 ).toLocaleUpperCase() + property.substring( 1 );
- values = params[ property ];
- if ( typeof parser[ funcName ] === 'function' ) {
- parser[ funcName ]( values );
- } else if ( parser.hasOwnProperty( property ) ) {
- parser[ property ] = values;
- }
- }
- };
- /**
- * Configures the Parser implementation according the supplied configuration object.
- * @memberOf THREE.LoaderSupport.WorkerRunnerRefImpl
- *
- * @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
- */
- WorkerRunnerRefImpl.prototype.processMessage = function ( payload ) {
- var logger = new ConsoleLogger();
- if ( Validator.isValid( payload.logger ) ) {
- logger.setEnabled( payload.logger.enabled );
- logger.setDebug( payload.logger.debug );
- }
- if ( payload.cmd === 'run' ) {
- var callbacks = {
- callbackBuilder: function ( payload ) {
- self.postMessage( payload );
- },
- callbackProgress: function ( text ) {
- logger.logDebug( 'WorkerRunner: progress: ' + text );
- }
- };
- // Parser is expected to be named as such
- var parser = new Parser( logger );
- this.applyProperties( parser, payload.params );
- this.applyProperties( parser, payload.materials );
- this.applyProperties( parser, callbacks );
- parser.workerScope = self;
- parser.parse( payload.data.input, payload.data.options );
- logger.logInfo( 'WorkerRunner: Run complete!' );
- callbacks.callbackBuilder( {
- cmd: 'complete',
- msg: 'WorkerRunner completed run.'
- } );
- } else {
- logger.logError( 'WorkerRunner: Received unknown command: ' + payload.cmd );
- }
- };
- return WorkerRunnerRefImpl;
- })();
- /**
- * This class provides means to transform existing parser code into a web worker. It defines a simple communication protocol
- * which allows to configure the worker and receive raw mesh data during execution.
- * @class
- *
- * @param {THREE.LoaderSupport.ConsoleLogger} logger logger to be used
- */
- THREE.LoaderSupport.WorkerSupport = (function () {
- var WORKER_SUPPORT_VERSION = '1.1.1';
- var Validator = THREE.LoaderSupport.Validator;
- function WorkerSupport( logger ) {
- this.logger = Validator.verifyInput( logger, new THREE.LoaderSupport.ConsoleLogger() );
- this.logger.logInfo( 'Using THREE.LoaderSupport.WorkerSupport version: ' + WORKER_SUPPORT_VERSION );
- // check worker support first
- if ( window.Worker === undefined ) throw "This browser does not support web workers!";
- if ( window.Blob === undefined ) throw "This browser does not support Blob!";
- if ( typeof window.URL.createObjectURL !== 'function' ) throw "This browser does not support Object creation from URL!";
- this.worker = null;
- this.workerCode = null;
- this.loading = true;
- this.queuedMessage = null;
- this.running = false;
- this.terminateRequested = false;
- this.callbacks = {
- builder: null,
- onLoad: null
- };
- }
- /**
- * Validate the status of worker code and the derived worker.
- * @memberOf THREE.LoaderSupport.WorkerSupport
- *
- * @param {Function} functionCodeBuilder Function that is invoked with funcBuildObject and funcBuildSingelton that allows stringification of objects and singletons.
- * @param {boolean} forceWorkerReload Force re-build of the worker code.
- * @param {String[]} libLocations URL of libraries that shall be added to worker code relative to libPath
- * @param {String} libPath Base path used for loading libraries
- * @param {THREE.LoaderSupport.WorkerRunnerRefImpl} runnerImpl The default worker parser wrapper implementation (communication and execution). An extended class could be passed here.
- */
- WorkerSupport.prototype.validate = function ( functionCodeBuilder, forceWorkerReload, libLocations, libPath, runnerImpl ) {
- this.running = false;
- if ( forceWorkerReload ) {
- this.worker = null;
- this.workerCode = null;
- this.loading = true;
- this.queuedMessage = null;
- this.callbacks.builder = null;
- this.callbacks.onLoad = null;
- }
- if ( ! Validator.isValid( this.worker ) ) {
- this.logger.logInfo( 'WorkerSupport: Building worker code...' );
- this.logger.logTimeStart( 'buildWebWorkerCode' );
- var workerRunner;
- if ( Validator.isValid( runnerImpl ) ) {
- this.logger.logInfo( 'WorkerSupport: Using "' + runnerImpl.name + '" as Runncer class for worker.' );
- workerRunner = runnerImpl;
- } else {
- this.logger.logInfo( 'WorkerSupport: Using DEFAULT "THREE.LoaderSupport.WorkerRunnerRefImpl" as Runncer class for worker.' );
- workerRunner = THREE.LoaderSupport.WorkerRunnerRefImpl;
- }
- var scope = this;
- var buildWorkerCode = function ( baseWorkerCode ) {
- scope.workerCode = baseWorkerCode;
- if ( workerRunner == THREE.LoaderSupport.WorkerRunnerRefImpl ) {
- scope.workerCode += buildObject( 'Validator', THREE.LoaderSupport.Validator );
- scope.workerCode += buildSingelton( 'ConsoleLogger', 'ConsoleLogger', THREE.LoaderSupport.ConsoleLogger );
- }
- scope.workerCode += functionCodeBuilder( buildObject, buildSingelton );
- scope.workerCode += buildSingelton( workerRunner.name, workerRunner.name, workerRunner );
- scope.workerCode += 'new ' + workerRunner.name + '();\n\n';
- var blob = new Blob( [ scope.workerCode ], { type: 'application/javascript' } );
- scope.worker = new Worker( window.URL.createObjectURL( blob ) );
- scope.logger.logTimeEnd( 'buildWebWorkerCode' );
- var receiveWorkerMessage = function ( e ) {
- var payload = e.data;
- switch ( payload.cmd ) {
- case 'meshData':
- case 'materialData':
- case 'imageData':
- scope.callbacks.builder( payload );
- break;
- case 'complete':
- scope.callbacks.onLoad( payload.msg );
- scope.running = false;
- if ( scope.terminateRequested ) {
- scope.logger.logInfo( 'WorkerSupport [' + workerRunner + ']: Run is complete. Terminating application on request!' );
- scope.terminateWorker();
- }
- break;
- case 'error':
- scope.logger.logError( 'WorkerSupport [' + workerRunner + ']: Reported error: ' + payload.msg );
- break;
- default:
- scope.logger.logError( 'WorkerSupport [' + workerRunner + ']: Received unknown command: ' + payload.cmd );
- break;
- }
- };
- scope.worker.addEventListener( 'message', receiveWorkerMessage, false );
- scope.loading = false;
- scope._postMessage();
- };
- if ( Validator.isValid( libLocations ) && libLocations.length > 0 ) {
- var libsContent = '';
- var loadAllLibraries = function ( path, locations ) {
- if ( locations.length === 0 ) {
- buildWorkerCode( libsContent );
- } else {
- var loadedLib = function ( contentAsString ) {
- libsContent += contentAsString;
- loadAllLibraries( path, locations );
- };
- var fileLoader = new THREE.FileLoader();
- fileLoader.setPath( path );
- fileLoader.setResponseType( 'text' );
- fileLoader.load( locations[ 0 ], loadedLib );
- locations.shift();
- }
- };
- loadAllLibraries( libPath, libLocations );
- } else {
- buildWorkerCode( '' );
- }
- }
- };
- /**
- * Terminate the worker and the code.
- * @memberOf THREE.LoaderSupport.WorkerSupport
- */
- WorkerSupport.prototype.terminateWorker = function () {
- if ( Validator.isValid( this.worker ) ) {
- this.worker.terminate();
- }
- this.worker = null;
- this.workerCode = null;
- };
- /**
- * Specify functions that should be build when new raw mesh data becomes available and when the parser is finished.
- * @memberOf THREE.LoaderSupport.WorkerSupport
- *
- * @param {Function} builder The builder function. Default is {@link THREE.LoaderSupport.Builder}.
- * @param {Function} onLoad The function that is called when parsing is complete.
- */
- WorkerSupport.prototype.setCallbacks = function ( builder, onLoad ) {
- this.callbacks = {
- builder: builder,
- onLoad: onLoad
- };
- };
- var buildObject = function ( fullName, object ) {
- var objectString = fullName + ' = {\n';
- var part;
- for ( var name in object ) {
- part = object[ name ];
- if ( typeof( part ) === 'string' || part instanceof String ) {
- part = part.replace( '\n', '\\n' );
- part = part.replace( '\r', '\\r' );
- objectString += '\t' + name + ': "' + part + '",\n';
- } else if ( part instanceof Array ) {
- objectString += '\t' + name + ': [' + part + '],\n';
- } else if ( Number.isInteger( part ) ) {
- objectString += '\t' + name + ': ' + part + ',\n';
- } else if ( typeof part === 'function' ) {
- objectString += '\t' + name + ': ' + part + ',\n';
- }
- }
- objectString += '}\n\n';
- return objectString;
- };
- var buildSingelton = function ( fullName, internalName, object ) {
- var objectString = fullName + ' = (function () {\n\n';
- objectString += '\t' + object.prototype.constructor.toString() + '\n\n';
- objectString = objectString.replace( object.name, internalName );
- var funcString;
- var objectPart;
- for ( var name in object.prototype ) {
- objectPart = object.prototype[ name ];
- if ( typeof objectPart === 'function' ) {
- funcString = objectPart.toString();
- objectString += '\t' + internalName + '.prototype.' + name + ' = ' + funcString + ';\n\n';
- }
- }
- objectString += '\treturn ' + internalName + ';\n';
- objectString += '})();\n\n';
- return objectString;
- };
- /**
- * Request termination of worker once parser is finished.
- * @memberOf THREE.LoaderSupport.WorkerSupport
- *
- * @param {boolean} terminateRequested True or false.
- */
- WorkerSupport.prototype.setTerminateRequested = function ( terminateRequested ) {
- this.terminateRequested = terminateRequested === true;
- };
- /**
- * Runs the parser with the provided configuration.
- * @memberOf THREE.LoaderSupport.WorkerSupport
- *
- * @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
- */
- WorkerSupport.prototype.run = function ( payload ) {
- if ( ! Validator.isValid( this.callbacks.builder ) ) throw 'Unable to run as no "builder" callback is set.';
- if ( ! Validator.isValid( this.callbacks.onLoad ) ) throw 'Unable to run as no "onLoad" callback is set.';
- if ( Validator.isValid( this.worker ) || this.loading ) {
- if ( payload.cmd !== 'run' ) payload.cmd = 'run';
- this.queuedMessage = payload;
- this.running = true;
- this._postMessage();
- }
- };
- WorkerSupport.prototype._postMessage = function () {
- if ( ! this.loading && Validator.isValid( this.queuedMessage ) ) {
- this.worker.postMessage( this.queuedMessage );
- }
- };
- return WorkerSupport;
- })();
- /**
- * Orchestrate loading of multiple OBJ files/data from an instruction queue with a configurable amount of workers (1-16).
- * Workflow:
- * prepareWorkers
- * enqueueForRun
- * processQueue
- * deregister
- *
- * @class
- *
- * @param {string} classDef Class definition to be used for construction
- * @param {THREE.LoaderSupport.ConsoleLogger} logger logger to be used
- */
- THREE.LoaderSupport.WorkerDirector = (function () {
- var LOADER_WORKER_DIRECTOR_VERSION = '2.0.0';
- var Validator = THREE.LoaderSupport.Validator;
- var MAX_WEB_WORKER = 16;
- var MAX_QUEUE_SIZE = 8192;
- function WorkerDirector( classDef, logger ) {
- this.logger = Validator.verifyInput( logger, new THREE.LoaderSupport.ConsoleLogger() );
- this.logger.logInfo( 'Using THREE.LoaderSupport.WorkerDirector version: ' + LOADER_WORKER_DIRECTOR_VERSION );
- this.maxQueueSize = MAX_QUEUE_SIZE ;
- this.maxWebWorkers = MAX_WEB_WORKER;
- this.crossOrigin = null;
- if ( ! Validator.isValid( classDef ) ) throw 'Provided invalid classDef: ' + classDef;
- this.workerDescription = {
- classDef: classDef,
- globalCallbacks: {},
- workerSupports: []
- };
- this.objectsCompleted = 0;
- this.instructionQueue = [];
- }
- /**
- * Returns the maximum length of the instruction queue.
- * @memberOf THREE.LoaderSupport.WorkerDirector
- *
- * @returns {number}
- */
- WorkerDirector.prototype.getMaxQueueSize = function () {
- return this.maxQueueSize;
- };
- /**
- * Returns the maximum number of workers.
- * @memberOf THREE.LoaderSupport.WorkerDirector
- *
- * @returns {number}
- */
- WorkerDirector.prototype.getMaxWebWorkers = function () {
- return this.maxWebWorkers;
- };
- /**
- * Sets the CORS string to be used.
- * @memberOf THREE.LoaderSupport.WorkerDirector
- *
- * @param {string} crossOrigin CORS value
- */
- WorkerDirector.prototype.setCrossOrigin = function ( crossOrigin ) {
- this.crossOrigin = crossOrigin;
- };
- /**
- * Create or destroy workers according limits. Set the name and register callbacks for dynamically created web workers.
- * @memberOf THREE.LoaderSupport.WorkerDirector
- *
- * @param {THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks} globalCallbacks Register global callbacks used by all web workers
- * @param {number} maxQueueSize Set the maximum size of the instruction queue (1-1024)
- * @param {number} maxWebWorkers Set the maximum amount of workers (1-16)
- */
- WorkerDirector.prototype.prepareWorkers = function ( globalCallbacks, maxQueueSize, maxWebWorkers ) {
- if ( Validator.isValid( globalCallbacks ) ) this.workerDescription.globalCallbacks = globalCallbacks;
- this.maxQueueSize = Math.min( maxQueueSize, MAX_QUEUE_SIZE );
- this.maxWebWorkers = Math.min( maxWebWorkers, MAX_WEB_WORKER );
- this.objectsCompleted = 0;
- this.instructionQueue = [];
- var start = this.workerDescription.workerSupports.length;
- var i;
- if ( start < this.maxWebWorkers ) {
- for ( i = start; i < this.maxWebWorkers; i++ ) {
- this.workerDescription.workerSupports[ i ] = {
- workerSupport: new THREE.LoaderSupport.WorkerSupport( this.logger ),
- loader: null
- };
- }
- } else {
- for ( i = start - 1; i >= this.maxWebWorkers; i-- ) {
- this.workerDescription.workerSupports[ i ].workerSupport.setRequestTerminate( true );
- this.workerDescription.workerSupports.pop();
- }
- }
- };
- /**
- * Store run instructions in internal instructionQueue.
- * @memberOf THREE.LoaderSupport.WorkerDirector
- *
- * @param {THREE.LoaderSupport.PrepData} prepData
- */
- WorkerDirector.prototype.enqueueForRun = function ( prepData ) {
- if ( this.instructionQueue.length < this.maxQueueSize ) {
- this.instructionQueue.push( prepData );
- }
- };
- /**
- * Process the instructionQueue until it is depleted.
- * @memberOf THREE.LoaderSupport.WorkerDirector
- */
- WorkerDirector.prototype.processQueue = function () {
- if ( this.instructionQueue.length === 0 ) return;
- var length = Math.min( this.maxWebWorkers, this.instructionQueue.length );
- for ( var i = 0; i < length; i++ ) {
- this._kickWorkerRun( this.instructionQueue[ 0 ], i );
- this.instructionQueue.shift();
- }
- };
- WorkerDirector.prototype._kickWorkerRun = function( prepData, workerInstanceNo ) {
- var scope = this;
- var directorOnLoad = function ( event ) {
- scope.objectsCompleted++;
- var nextPrepData = scope.instructionQueue[ 0 ];
- if ( Validator.isValid( nextPrepData ) ) {
- scope.instructionQueue.shift();
- scope.logger.logInfo( '\nAssigning next item from queue to worker (queue length: ' + scope.instructionQueue.length + ')\n\n' );
- scope._kickWorkerRun( nextPrepData, event.detail.instanceNo );
- } else if ( scope.instructionQueue.length === 0 ) {
- scope.deregister();
- }
- };
- var prepDataCallbacks = prepData.getCallbacks();
- var globalCallbacks = this.workerDescription.globalCallbacks;
- var wrapperOnLoad = function ( event ) {
- if ( Validator.isValid( globalCallbacks.onLoad ) ) globalCallbacks.onLoad( event );
- if ( Validator.isValid( prepDataCallbacks.onLoad ) ) prepDataCallbacks.onLoad( event );
- directorOnLoad( event );
- };
- var wrapperOnProgress = function ( event ) {
- if ( Validator.isValid( globalCallbacks.onProgress ) ) globalCallbacks.onProgress( event );
- if ( Validator.isValid( prepDataCallbacks.onProgress ) ) prepDataCallbacks.onProgress( event );
- };
- var wrapperOnMeshAlter = function ( event ) {
- if ( Validator.isValid( globalCallbacks.onMeshAlter ) ) globalCallbacks.onMeshAlter( event );
- if ( Validator.isValid( prepDataCallbacks.onMeshAlter ) ) prepDataCallbacks.onMeshAlter( event );
- };
- var supportTuple = this.workerDescription.workerSupports[ workerInstanceNo ];
- supportTuple.loader = this._buildLoader( workerInstanceNo );
- var updatedCallbacks = new THREE.LoaderSupport.Callbacks();
- updatedCallbacks.setCallbackOnLoad( wrapperOnLoad );
- updatedCallbacks.setCallbackOnProgress( wrapperOnProgress );
- updatedCallbacks.setCallbackOnMeshAlter( wrapperOnMeshAlter );
- prepData.callbacks = updatedCallbacks;
- supportTuple.loader.run( prepData, supportTuple.workerSupport );
- };
- WorkerDirector.prototype._buildLoader = function ( instanceNo ) {
- var classDef = this.workerDescription.classDef;
- var loader = Object.create( classDef.prototype );
- this.workerDescription.classDef.call( loader, null, this.logger );
- // verify that all required functions are implemented
- if ( ! loader.hasOwnProperty( 'instanceNo' ) ) throw classDef.name + ' has no property "instanceNo".';
- loader.instanceNo = instanceNo;
- if ( ! loader.hasOwnProperty( 'workerSupport' ) ) {
- throw classDef.name + ' has no property "workerSupport".';
- } else if ( ! classDef.workerSupport instanceof THREE.LoaderSupport.WorkerSupport ) {
- throw classDef.name + '.workerSupport is not of type "THREE.LoaderSupport.WorkerSupport".';
- }
- if ( typeof loader.run !== 'function' ) throw classDef.name + ' has no function "run".';
- return loader;
- };
- /**
- * Terminate all workers.
- * @memberOf THREE.LoaderSupport.WorkerDirector
- */
- WorkerDirector.prototype.deregister = function () {
- this.logger.logInfo( 'WorkerDirector received the deregister call. Terminating all workers!' );
- for ( var i = 0, length = this.workerDescription.workerSupports.length; i < length; i++ ) {
- var supportTuple = this.workerDescription.workerSupports[ i ];
- supportTuple.workerSupport.setTerminateRequested( true );
- this.logger.logInfo( 'Requested termination of worker.' );
- var loaderCallbacks = supportTuple.loader.callbacks;
- if ( Validator.isValid( loaderCallbacks.onProgress ) ) loaderCallbacks.onProgress( { detail: { text: '' } } );
- }
- this.workerDescription.workerSupports = [];
- this.instructionQueue = [];
- };
- return WorkerDirector;
- })();
|