12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388 |
- /**
- * @author Kai Salmen / https://kaisalmen.de
- * Development repository: https://github.com/kaisalmen/WWOBJLoader
- */
- 'use strict';
- if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} }
- /**
- * Use this class to load OBJ data from files or to parse OBJ data from an arraybuffer
- * @class
- *
- * @param {THREE.DefaultLoadingManager} [manager] The loadingManager for the loader to use. Default is {@link THREE.DefaultLoadingManager}
- */
- THREE.OBJLoader2 = (function () {
- var OBJLOADER2_VERSION = '2.0.1';
- var Commons = THREE.LoaderSupport.Commons;
- var Validator = THREE.LoaderSupport.Validator;
- var ConsoleLogger = THREE.LoaderSupport.ConsoleLogger;
- OBJLoader2.prototype = Object.create( THREE.LoaderSupport.Commons.prototype );
- OBJLoader2.prototype.constructor = OBJLoader2;
- function OBJLoader2( logger, manager ) {
- THREE.LoaderSupport.Commons.call( this, logger, manager );
- this.logger.logInfo( 'Using THREE.OBJLoader2 version: ' + OBJLOADER2_VERSION );
- this.materialPerSmoothingGroup = false;
- this.fileLoader = Validator.verifyInput( this.fileLoader, new THREE.FileLoader( this.manager ) );
- this.workerSupport = null;
- this.terminateWorkerOnLoad = true;
- };
- /**
- * Tells whether a material shall be created per smoothing group.
- * @memberOf THREE.OBJLoader2
- *
- * @param {boolean} materialPerSmoothingGroup=false
- */
- OBJLoader2.prototype.setMaterialPerSmoothingGroup = function ( materialPerSmoothingGroup ) {
- this.materialPerSmoothingGroup = materialPerSmoothingGroup === true;
- };
- /**
- * Use this convenient method to load an OBJ file at the given URL. Per default the fileLoader uses an arraybuffer.
- * @memberOf THREE.OBJLoader2
- *
- * @param {string} url URL of the file to load
- * @param {callback} onLoad Called after loading was successfully completed
- * @param {callback} onProgress Called to report progress of loading. The argument will be the XMLHttpRequest instance, which contains {integer total} and {integer loaded} bytes.
- * @param {callback} onError Called after an error occurred during loading
- * @param {callback} onMeshAlter Called after a new mesh raw data becomes available to allow alteration
- * @param {boolean} useAsync If true uses async loading with worker, if false loads data synchronously
- */
- OBJLoader2.prototype.load = function ( url, onLoad, onProgress, onError, onMeshAlter, useAsync ) {
- var scope = this;
- if ( ! Validator.isValid( onProgress ) ) {
- var numericalValueRef = 0;
- var numericalValue = 0;
- onProgress = function ( event ) {
- if ( ! event.lengthComputable ) return;
- numericalValue = event.loaded / event.total;
- if ( numericalValue > numericalValueRef ) {
- numericalValueRef = numericalValue;
- var output = 'Download of "' + url + '": ' + ( numericalValue * 100 ).toFixed( 2 ) + '%';
- scope.onProgress( 'progressLoad', output, numericalValue );
- }
- };
- }
- if ( ! Validator.isValid( onError ) ) {
- onError = function ( event ) {
- var output = 'Error occurred while downloading "' + url + '"';
- scope.logger.logError( output + ': ' + event );
- scope.onProgress( 'error', output, -1 );
- };
- }
- this.fileLoader.setPath( this.path );
- this.fileLoader.setResponseType( 'arraybuffer' );
- this.fileLoader.load( url, function ( content ) {
- if ( useAsync ) {
- scope.parseAsync( content, onLoad );
- } else {
- scope._setCallbacks( null, onMeshAlter, null );
- onLoad(
- {
- detail: {
- loaderRootNode: scope.parse( content ),
- modelName: scope.modelName,
- instanceNo: scope.instanceNo
- }
- }
- );
- }
- }, onProgress, onError );
- };
- /**
- * Run the loader according the provided instructions.
- * @memberOf THREE.OBJLoader2
- *
- * @param {THREE.LoaderSupport.PrepData} prepData All parameters and resources required for execution
- * @param {THREE.LoaderSupport.WorkerSupport} [workerSupportExternal] Use pre-existing WorkerSupport
- */
- OBJLoader2.prototype.run = function ( prepData, workerSupportExternal ) {
- this._applyPrepData( prepData );
- var available = this._checkFiles( prepData.resources );
- if ( Validator.isValid( workerSupportExternal ) ) {
- this.terminateWorkerOnLoad = false;
- this.workerSupport = workerSupportExternal;
- this.logger = workerSupportExternal.logger;
- } else {
- this.terminateWorkerOnLoad = true;
- }
- var scope = this;
- var onMaterialsLoaded = function ( materials ) {
- scope.builder.setMaterials( materials );
- if ( Validator.isValid( available.obj.content ) ) {
- if ( prepData.useAsync ) {
- scope.parseAsync( available.obj.content, scope.callbacks.onLoad );
- } else {
- scope.parse( available.obj.content );
- }
- } else {
- scope.setPath( available.obj.path );
- scope.load( available.obj.name, scope.callbacks.onLoad, null, null, scope.callbacks.onMeshAlter, prepData.useAsync );
- }
- };
- this._loadMtl( available.mtl, onMaterialsLoaded, prepData.crossOrigin );
- };
- OBJLoader2.prototype._applyPrepData = function ( prepData ) {
- THREE.LoaderSupport.Commons.prototype._applyPrepData.call( this, prepData );
- if ( Validator.isValid( prepData ) ) {
- this.setMaterialPerSmoothingGroup( prepData.materialPerSmoothingGroup );
- }
- };
- /**
- * Parses OBJ data synchronously from arraybuffer or string.
- * @memberOf THREE.OBJLoader2
- *
- * @param {arraybuffer|string} content OBJ data as Uint8Array or String
- */
- OBJLoader2.prototype.parse = function ( content ) {
- this.logger.logTimeStart( 'OBJLoader2 parse: ' + this.modelName );
- var parser = new Parser( this.logger );
- parser.setMaterialPerSmoothingGroup( this.materialPerSmoothingGroup );
- parser.setUseIndices( this.useIndices );
- parser.setDisregardNormals( this.disregardNormals );
- parser.setMaterialNames( this.builder.materialNames );
- var scope = this;
- var onMeshLoaded = function ( payload ) {
- var meshes = scope.builder.buildMeshes( payload );
- var mesh;
- for ( var i in meshes ) {
- mesh = meshes[ i ];
- scope.loaderRootNode.add( mesh );
- }
- };
- parser.setCallbackBuilder( onMeshLoaded );
- var onProgressScoped = function ( text, numericalValue ) {
- scope.onProgress( 'progressParse', text, numericalValue );
- };
- parser.setCallbackProgress( onProgressScoped );
- if ( content instanceof ArrayBuffer || content instanceof Uint8Array ) {
- this.logger.logInfo( 'Parsing arrayBuffer...' );
- parser.parse( content );
- } else if ( typeof( content ) === 'string' || content instanceof String ) {
- this.logger.logInfo( 'Parsing text...' );
- parser.parseText( content );
- } else {
- throw 'Provided content was neither of type String nor Uint8Array! Aborting...';
- }
- this.logger.logTimeEnd( 'OBJLoader2 parse: ' + this.modelName );
- return this.loaderRootNode;
- };
- /**
- * Parses OBJ content asynchronously from arraybuffer.
- * @memberOf THREE.OBJLoader2
- *
- * @param {arraybuffer} content OBJ data as Uint8Array
- * @param {callback} onLoad Called after worker successfully completed loading
- */
- OBJLoader2.prototype.parseAsync = function ( content, onLoad ) {
- this.logger.logTimeStart( 'OBJLoader2 parseAsync: ' + this.modelName );
- var scope = this;
- var scopedOnLoad = function () {
- onLoad(
- {
- detail: {
- loaderRootNode: scope.loaderRootNode,
- modelName: scope.modelName,
- instanceNo: scope.instanceNo
- }
- }
- );
- if ( scope.terminateWorkerOnLoad ) scope.workerSupport.terminateWorker();
- scope.logger.logTimeEnd( 'OBJLoader2 parseAsync: ' + scope.modelName );
- };
- var scopedOnMeshLoaded = function ( payload ) {
- var meshes = scope.builder.buildMeshes( payload );
- var mesh;
- for ( var i in meshes ) {
- mesh = meshes[ i ];
- scope.loaderRootNode.add( mesh );
- }
- };
- this.workerSupport = Validator.verifyInput( this.workerSupport, new THREE.LoaderSupport.WorkerSupport( this.logger ) );
- var buildCode = function ( funcBuildObject, funcBuildSingelton ) {
- var workerCode = '';
- workerCode += '/**\n';
- workerCode += ' * This code was constructed by OBJLoader2 buildWorkerCode.\n';
- workerCode += ' */\n\n';
- workerCode += funcBuildSingelton( 'Commons', 'Commons', Commons );
- workerCode += funcBuildObject( 'Consts', Consts );
- workerCode += funcBuildObject( 'Validator', Validator );
- workerCode += funcBuildSingelton( 'ConsoleLogger', 'ConsoleLogger', ConsoleLogger );
- workerCode += funcBuildSingelton( 'Parser', 'Parser', Parser );
- workerCode += funcBuildSingelton( 'RawMesh', 'RawMesh', RawMesh );
- workerCode += funcBuildSingelton( 'RawMeshSubGroup', 'RawMeshSubGroup', RawMeshSubGroup );
- return workerCode;
- };
- this.workerSupport.validate( buildCode, false );
- this.workerSupport.setCallbacks( scopedOnMeshLoaded, scopedOnLoad );
- this.workerSupport.run(
- {
- cmd: 'run',
- params: {
- materialPerSmoothingGroup: this.materialPerSmoothingGroup,
- useIndices: this.useIndices,
- disregardNormals: this.disregardNormals
- },
- logger: {
- debug: this.logger.debug,
- enabled: this.logger.enabled
- },
- materials: {
- materialNames: this.builder.materialNames
- },
- buffers: {
- input: content
- }
- },
- [ content.buffer ]
- );
- };
- /**
- * Constants used by THREE.OBJLoader2
- */
- var Consts = {
- CODE_LF: 10,
- CODE_CR: 13,
- CODE_SPACE: 32,
- CODE_SLASH: 47,
- STRING_LF: '\n',
- STRING_CR: '\r',
- STRING_SPACE: ' ',
- STRING_SLASH: '/',
- LINE_F: 'f',
- LINE_G: 'g',
- LINE_L: 'l',
- LINE_O: 'o',
- LINE_S: 's',
- LINE_V: 'v',
- LINE_VT: 'vt',
- LINE_VN: 'vn',
- LINE_MTLLIB: 'mtllib',
- LINE_USEMTL: 'usemtl'
- };
- /**
- * Parse OBJ data either from ArrayBuffer or string
- * @class
- */
- var Parser = (function () {
- function Parser( logger ) {
- this.callbackProgress = null;
- this.callbackBuilder = null;
- this.materialNames = [];
- this.rawMesh = null;
- this.materialPerSmoothingGroup = false;
- this.useIndices = false;
- this.disregardNormals = false;
- this.inputObjectCount = 1;
- this.outputObjectCount = 1;
- this.counts = {
- vertices: 0,
- faces: 0,
- doubleIndicesCount: 0
- };
- this.logger = logger;
- this.totalBytes = 0;
- };
- Parser.prototype.setMaterialPerSmoothingGroup = function ( materialPerSmoothingGroup ) {
- this.materialPerSmoothingGroup = materialPerSmoothingGroup;
- };
- Parser.prototype.setUseIndices = function ( useIndices ) {
- this.useIndices = useIndices;
- };
- Parser.prototype.setDisregardNormals = function ( disregardNormals ) {
- this.disregardNormals = disregardNormals;
- };
- Parser.prototype.setMaterialNames = function ( materialNames ) {
- this.materialNames = Validator.verifyInput( materialNames, this.materialNames );
- this.materialNames = Validator.verifyInput( this.materialNames, [] );
- };
- Parser.prototype.setCallbackBuilder = function ( callbackBuilder ) {
- this.callbackBuilder = callbackBuilder;
- if ( ! Validator.isValid( this.callbackBuilder ) ) throw 'Unable to run as no "builder" callback is set.';
- };
- Parser.prototype.setCallbackProgress = function ( callbackProgress ) {
- this.callbackProgress = callbackProgress;
- };
- Parser.prototype.configure = function () {
- this.rawMesh = new RawMesh( this.materialPerSmoothingGroup, this.useIndices, this.disregardNormals );
- if ( this.logger.isEnabled() ) {
- var matNames = ( this.materialNames.length > 0 ) ? '\n\tmaterialNames:\n\t\t- ' + this.materialNames.join( '\n\t\t- ' ) : '\n\tmaterialNames: None';
- var printedConfig = 'OBJLoader2.Parser configuration:'
- + matNames
- + '\n\tmaterialPerSmoothingGroup: ' + this.materialPerSmoothingGroup
- + '\n\tuseIndices: ' + this.useIndices
- + '\n\tdisregardNormals: ' + this.disregardNormals
- + '\n\tcallbackBuilderName: ' + this.callbackBuilder.name
- + '\n\tcallbackProgressName: ' + this.callbackProgress.name;
- this.logger.logInfo( printedConfig );
- }
- };
- /**
- * Parse the provided arraybuffer
- * @memberOf Parser
- *
- * @param {Uint8Array} arrayBuffer OBJ data as Uint8Array
- */
- Parser.prototype.parse = function ( arrayBuffer ) {
- this.logger.logTimeStart( 'OBJLoader2.Parser.parse' );
- this.configure();
- var arrayBufferView = new Uint8Array( arrayBuffer );
- var length = arrayBufferView.byteLength;
- this.totalBytes = length;
- var buffer = new Array( 128 );
- var bufferPointer = 0;
- var slashSpacePattern = new Array( 16 );
- var slashSpacePatternPointer = 0;
- var reachedFaces = false;
- var code;
- var word = '';
- var i = 0;
- for ( ; i < length; i++ ) {
- code = arrayBufferView[ i ];
- switch ( code ) {
- case Consts.CODE_SPACE:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- slashSpacePattern[ slashSpacePatternPointer++ ] = 0;
- word = '';
- break;
- case Consts.CODE_SLASH:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- slashSpacePattern[ slashSpacePatternPointer++ ] = 1;
- word = '';
- break;
- case Consts.CODE_LF:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- word = '';
- reachedFaces = this.processLine( buffer, bufferPointer, slashSpacePattern, slashSpacePatternPointer, reachedFaces, i );
- bufferPointer = 0;
- slashSpacePatternPointer = 0;
- break;
- case Consts.CODE_CR:
- break;
- default:
- word += String.fromCharCode( code );
- break;
- }
- }
- this.finalize( i );
- this.logger.logTimeEnd( 'OBJLoader2.Parser.parse' );
- };
- /**
- * Parse the provided text
- * @memberOf Parser
- *
- * @param {string} text OBJ data as string
- */
- Parser.prototype.parseText = function ( text ) {
- this.logger.logTimeStart( 'OBJLoader2.Parser.parseText' );
- this.configure();
- var length = text.length;
- this.totalBytes = length;
- var buffer = new Array( 128 );
- var bufferPointer = 0;
- var slashSpacePattern = new Array( 16 );
- var slashSpacePatternPointer = 0;
- var reachedFaces = false;
- var char;
- var word = '';
- var i = 0;
- for ( ; i < length; i++ ) {
- char = text[ i ];
- switch ( char ) {
- case Consts.STRING_SPACE:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- slashSpacePattern[ slashSpacePatternPointer++ ] = 0;
- word = '';
- break;
- case Consts.STRING_SLASH:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- slashSpacePattern[ slashSpacePatternPointer++ ] = 1;
- word = '';
- break;
- case Consts.STRING_LF:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- word = '';
- reachedFaces = this.processLine( buffer, bufferPointer, slashSpacePattern, slashSpacePatternPointer, reachedFaces, i );
- bufferPointer = 0;
- slashSpacePatternPointer = 0;
- break;
- case Consts.STRING_CR:
- break;
- default:
- word += char;
- }
- }
- this.finalize( i );
- this.logger.logTimeEnd( 'OBJLoader2.Parser.parseText' );
- };
- Parser.prototype.processLine = function ( buffer, bufferPointer, slashSpacePattern, slashSpacePatternPointer, reachedFaces, currentByte ) {
- if ( bufferPointer < 1 ) return reachedFaces;
- var countSlashes = function ( slashSpacePattern, slashSpacePatternPointer ) {
- var slashesCount = 0;
- for ( var i = 0; i < slashSpacePatternPointer; i++ ) {
- slashesCount += slashSpacePattern[ i ];
- }
- return slashesCount;
- };
- var concatStringBuffer = function ( buffer, bufferPointer, slashSpacePattern ) {
- var concatBuffer = '';
- if ( bufferPointer === 2 ) {
- concatBuffer = buffer[ 1 ];
- } else {
- var bufferLength = bufferPointer - 1;
- for ( var i = 1; i < bufferLength; i++ ) {
- concatBuffer += buffer[ i ] + ( slashSpacePattern[ i ] === 0 ? ' ' : '/' );
- }
- concatBuffer += buffer[ bufferLength ];
- }
- return concatBuffer;
- };
- var flushStringBuffer = function ( buffer, bufferPointer ) {
- for ( var i = 0; i < bufferPointer; i++ ) {
- buffer[ i ] = '';
- }
- };
- switch ( buffer[ 0 ] ) {
- case Consts.LINE_V:
- // object complete instance required if reached faces already (= reached next block of v)
- if ( reachedFaces ) {
- if ( this.rawMesh.colors.length > 0 && this.rawMesh.colors.length !== this.rawMesh.vertices.length ) {
- throw 'Vertex Colors were detected, but vertex count and color count do not match!';
- }
- this.processCompletedObject( null, this.rawMesh.groupName, currentByte );
- reachedFaces = false;
- }
- if ( bufferPointer === 4 ) {
- this.rawMesh.pushVertex( buffer )
- } else {
- this.rawMesh.pushVertexAndVertextColors( buffer );
- }
- break;
- case Consts.LINE_VT:
- this.rawMesh.pushUv( buffer );
- break;
- case Consts.LINE_VN:
- this.rawMesh.pushNormal( buffer );
- break;
- case Consts.LINE_F:
- reachedFaces = true;
- this.rawMesh.processFaces( buffer, bufferPointer, countSlashes( slashSpacePattern, slashSpacePatternPointer ) );
- break;
- case Consts.LINE_L:
- this.rawMesh.processLines( buffer, bufferPointer, countSlashes( slashSpacePattern, slashSpacePatternPointer ) );
- break;
- case Consts.LINE_S:
- this.rawMesh.pushSmoothingGroup( buffer[ 1 ] );
- flushStringBuffer( buffer, bufferPointer );
- break;
- case Consts.LINE_G:
- this.processCompletedGroup( concatStringBuffer( buffer, bufferPointer, slashSpacePattern ), currentByte );
- flushStringBuffer( buffer, bufferPointer );
- break;
- case Consts.LINE_O:
- if ( this.rawMesh.vertices.length > 0 ) {
- this.processCompletedObject( concatStringBuffer( buffer, bufferPointer, slashSpacePattern ), null, currentByte );
- reachedFaces = false;
- } else {
- this.rawMesh.pushObject( concatStringBuffer( buffer, bufferPointer, slashSpacePattern ) );
- }
- flushStringBuffer( buffer, bufferPointer );
- break;
- case Consts.LINE_MTLLIB:
- this.rawMesh.pushMtllib( concatStringBuffer( buffer, bufferPointer, slashSpacePattern ) );
- flushStringBuffer( buffer, bufferPointer );
- break;
- case Consts.LINE_USEMTL:
- this.rawMesh.pushUsemtl( concatStringBuffer( buffer, bufferPointer, slashSpacePattern ) );
- flushStringBuffer( buffer, bufferPointer );
- break;
- default:
- break;
- }
- return reachedFaces;
- };
- Parser.prototype.createRawMeshReport = function ( rawMesh , inputObjectCount ) {
- var report = rawMesh.createReport( inputObjectCount );
- return 'Input Object number: ' + inputObjectCount +
- '\n\tObject name: ' + report.objectName +
- '\n\tGroup name: ' + report.groupName +
- '\n\tMtllib name: ' + report.mtllibName +
- '\n\tVertex count: ' + report.vertexCount +
- '\n\tNormal count: ' + report.normalCount +
- '\n\tUV count: ' + report.uvCount +
- '\n\tSmoothingGroup count: ' + report.smoothingGroupCount +
- '\n\tMaterial count: ' + report.mtlCount +
- '\n\tReal RawMeshSubGroup count: ' + report.subGroups;
- };
- Parser.prototype.processCompletedObject = function ( objectName, groupName, currentByte ) {
- var result = this.rawMesh.finalize();
- if ( Validator.isValid( result ) ) {
- this.inputObjectCount++;
- if ( this.logger.isDebug() ) this.logger.logDebug( this.createRawMeshReport( this.rawMesh, this.inputObjectCount ) );
- this.buildMesh( result, currentByte );
- var progressBytesPercent = currentByte / this.totalBytes;
- this.callbackProgress( 'Completed object: ' + objectName + ' Total progress: ' + ( progressBytesPercent * 100 ).toFixed( 2 ) + '%', progressBytesPercent );
- }
- this.rawMesh = this.rawMesh.newInstanceFromObject( objectName, groupName );
- };
- Parser.prototype.processCompletedGroup = function ( groupName, currentByte ) {
- var result = this.rawMesh.finalize();
- if ( Validator.isValid( result ) ) {
- this.inputObjectCount++;
- if ( this.logger.isDebug() ) this.logger.logDebug( this.createRawMeshReport( this.rawMesh, this.inputObjectCount ) );
- this.buildMesh( result, currentByte );
- var progressBytesPercent = currentByte / this.totalBytes;
- this.callbackProgress( 'Completed group: ' + groupName + ' Total progress: ' + ( progressBytesPercent * 100 ).toFixed( 2 ) + '%', progressBytesPercent );
- this.rawMesh = this.rawMesh.newInstanceFromGroup( groupName );
- } else {
- // if a group was set that did not lead to object creation in finalize, then the group name has to be updated
- this.rawMesh.pushGroup( groupName );
- }
- };
- Parser.prototype.finalize = function ( currentByte ) {
- this.logger.logInfo( 'Global output object count: ' + this.outputObjectCount );
- var result = Validator.isValid( this.rawMesh ) ? this.rawMesh.finalize() : null;
- if ( Validator.isValid( result ) ) {
- this.inputObjectCount++;
- if ( this.logger.isDebug() ) this.logger.logDebug( this.createRawMeshReport( this.rawMesh, this.inputObjectCount ) );
- this.buildMesh( result, currentByte );
- if ( this.logger.isEnabled() ) {
- var parserFinalReport = 'Overall counts: ' +
- '\n\tVertices: ' + this.counts.vertices +
- '\n\tFaces: ' + this.counts.faces +
- '\n\tMultiple definitions: ' + this.counts.doubleIndicesCount;
- this.logger.logInfo( parserFinalReport );
- }
- var progressBytesPercent = currentByte / this.totalBytes;
- this.callbackProgress( 'Completed Parsing: 100.00%', progressBytesPercent );
- }
- };
- /**
- * RawObjectDescriptions are transformed to too intermediate format that is forwarded to the Builder.
- * It is ensured that rawObjectDescriptions only contain objects with vertices (no need to check).
- *
- * @param result
- */
- Parser.prototype.buildMesh = function ( result, currentByte ) {
- var rawObjectDescriptions = result.subGroups;
- var vertexFA = new Float32Array( result.absoluteVertexCount );
- this.counts.vertices += result.absoluteVertexCount / 3;
- this.counts.faces += result.faceCount;
- this.counts.doubleIndicesCount += result.doubleIndicesCount;
- var indexUA = ( result.absoluteIndexCount > 0 ) ? new Uint32Array( result.absoluteIndexCount ) : null;
- var colorFA = ( result.absoluteColorCount > 0 ) ? new Float32Array( result.absoluteColorCount ) : null;
- var normalFA = ( result.absoluteNormalCount > 0 ) ? new Float32Array( result.absoluteNormalCount ) : null;
- var uvFA = ( result.absoluteUvCount > 0 ) ? new Float32Array( result.absoluteUvCount ) : null;
- var rawObjectDescription;
- var materialDescription;
- var materialDescriptions = [];
- var createMultiMaterial = ( rawObjectDescriptions.length > 1 );
- var materialIndex = 0;
- var materialIndexMapping = [];
- var selectedMaterialIndex;
- var materialGroup;
- var materialGroups = [];
- var vertexFAOffset = 0;
- var indexUAOffset = 0;
- var colorFAOffset = 0;
- var normalFAOffset = 0;
- var uvFAOffset = 0;
- var materialGroupOffset = 0;
- var materialGroupLength = 0;
- for ( var oodIndex in rawObjectDescriptions ) {
- if ( ! rawObjectDescriptions.hasOwnProperty( oodIndex ) ) continue;
- rawObjectDescription = rawObjectDescriptions[ oodIndex ];
- materialDescription = {
- name: rawObjectDescription.materialName,
- flat: false,
- default: false
- };
- if ( this.materialNames[ materialDescription.name ] === null ) {
- materialDescription.default = true;
- this.logger.logWarn( 'object_group "' + rawObjectDescription.objectName + '_' +
- rawObjectDescription.groupName +
- '" was defined without material! Assigning "defaultMaterial".' );
- }
- // Attach '_flat' to materialName in case flat shading is needed due to smoothingGroup 0
- if ( rawObjectDescription.smoothingGroup === 0 ) materialDescription.flat = true;
- if ( createMultiMaterial ) {
- // re-use material if already used before. Reduces materials array size and eliminates duplicates
- selectedMaterialIndex = materialIndexMapping[ materialDescription.name ];
- if ( ! selectedMaterialIndex ) {
- selectedMaterialIndex = materialIndex;
- materialIndexMapping[ materialDescription.name ] = materialIndex;
- materialDescriptions.push( materialDescription );
- materialIndex++;
- }
- materialGroupLength = this.useIndices ? rawObjectDescription.indices.length : rawObjectDescription.vertices.length / 3;
- materialGroup = {
- start: materialGroupOffset,
- count: materialGroupLength,
- index: selectedMaterialIndex
- };
- materialGroups.push( materialGroup );
- materialGroupOffset += materialGroupLength;
- } else {
- materialDescriptions.push( materialDescription );
- }
- vertexFA.set( rawObjectDescription.vertices, vertexFAOffset );
- vertexFAOffset += rawObjectDescription.vertices.length;
- if ( indexUA ) {
- indexUA.set( rawObjectDescription.indices, indexUAOffset );
- indexUAOffset += rawObjectDescription.indices.length;
- }
- if ( colorFA ) {
- colorFA.set( rawObjectDescription.colors, colorFAOffset );
- colorFAOffset += rawObjectDescription.colors.length;
- }
- if ( normalFA ) {
- normalFA.set( rawObjectDescription.normals, normalFAOffset );
- normalFAOffset += rawObjectDescription.normals.length;
- }
- if ( uvFA ) {
- uvFA.set( rawObjectDescription.uvs, uvFAOffset );
- uvFAOffset += rawObjectDescription.uvs.length;
- }
- if ( this.logger.isDebug() ) {
- var materialIndexLine = Validator.isValid( selectedMaterialIndex ) ? '\n\t\tmaterialIndex: ' + selectedMaterialIndex : '';
- var createdReport = 'Output Object no.: ' + this.outputObjectCount +
- '\n\t\tobjectName: ' + rawObjectDescription.objectName +
- '\n\t\tgroupName: ' + rawObjectDescription.groupName +
- '\n\t\tmaterialName: ' + rawObjectDescription.materialName +
- materialIndexLine +
- '\n\t\tsmoothingGroup: ' + rawObjectDescription.smoothingGroup +
- '\n\t\t#vertices: ' + rawObjectDescription.vertices.length / 3 +
- '\n\t\t#indices: ' + rawObjectDescription.indices.length +
- '\n\t\t#colors: ' + rawObjectDescription.colors.length / 3 +
- '\n\t\t#uvs: ' + rawObjectDescription.uvs.length / 2 +
- '\n\t\t#normals: ' + rawObjectDescription.normals.length / 3;
- this.logger.logDebug( createdReport );
- }
- }
- this.outputObjectCount++;
- this.callbackBuilder(
- {
- cmd: 'meshData',
- progress: {
- numericalValue: currentByte / this.totalBytes
- },
- params: {
- meshName: result.name
- },
- materials: {
- multiMaterial: createMultiMaterial,
- materialDescriptions: materialDescriptions,
- materialGroups: materialGroups
- },
- buffers: {
- vertices: vertexFA,
- indices: indexUA,
- colors: colorFA,
- normals: normalFA,
- uvs: uvFA
- }
- },
- [ vertexFA.buffer ],
- Validator.isValid( indexUA ) ? [ indexUA.buffer ] : null,
- Validator.isValid( colorFA ) ? [ colorFA.buffer ] : null,
- Validator.isValid( normalFA ) ? [ normalFA.buffer ] : null,
- Validator.isValid( uvFA ) ? [ uvFA.buffer ] : null
- );
- };
- return Parser;
- })();
- /**
- * {@link RawMesh} is only used by {@link Parser}.
- * The user of OBJLoader2 does not need to care about this class.
- * It is defined publicly for inclusion in web worker based OBJ loader ({@link THREE.OBJLoader2.WWOBJLoader2})
- */
- var RawMesh = (function () {
- function RawMesh( materialPerSmoothingGroup, useIndices, disregardNormals, objectName, groupName, activeMtlName ) {
- this.globalVertexOffset = 1;
- this.globalUvOffset = 1;
- this.globalNormalOffset = 1;
- this.vertices = [];
- this.colors = [];
- this.normals = [];
- this.uvs = [];
- // faces are stored according combined index of group, material and smoothingGroup (0 or not)
- this.activeMtlName = Validator.verifyInput( activeMtlName, '' );
- this.objectName = Validator.verifyInput( objectName, '' );
- this.groupName = Validator.verifyInput( groupName, '' );
- this.mtllibName = '';
- this.smoothingGroup = {
- splitMaterials: materialPerSmoothingGroup === true,
- normalized: -1,
- real: -1
- };
- this.useIndices = useIndices === true;
- this.disregardNormals = disregardNormals === true;
- this.mtlCount = 0;
- this.smoothingGroupCount = 0;
- this.subGroups = [];
- this.subGroupInUse = null;
- // this default index is required as it is possible to define faces without 'g' or 'usemtl'
- this.pushSmoothingGroup( 1 );
- this.doubleIndicesCount = 0;
- this.faceCount = 0;
- }
- RawMesh.prototype.newInstanceFromObject = function ( objectName, groupName ) {
- var newRawObject = new RawMesh( this.smoothingGroup.splitMaterials, this.useIndices, this.disregardNormals, objectName, groupName, this.activeMtlName );
- // move indices forward
- newRawObject.globalVertexOffset = this.globalVertexOffset + this.vertices.length / 3;
- newRawObject.globalUvOffset = this.globalUvOffset + this.uvs.length / 2;
- newRawObject.globalNormalOffset = this.globalNormalOffset + this.normals.length / 3;
- return newRawObject;
- };
- RawMesh.prototype.newInstanceFromGroup = function ( groupName ) {
- var newRawObject = new RawMesh( this.smoothingGroup.splitMaterials, this.useIndices, this.disregardNormals, this.objectName, groupName, this.activeMtlName );
- // keep current buffers and indices forward
- newRawObject.vertices = this.vertices;
- newRawObject.colors = this.colors;
- newRawObject.uvs = this.uvs;
- newRawObject.normals = this.normals;
- newRawObject.globalVertexOffset = this.globalVertexOffset;
- newRawObject.globalUvOffset = this.globalUvOffset;
- newRawObject.globalNormalOffset = this.globalNormalOffset;
- return newRawObject;
- };
- RawMesh.prototype.pushVertex = function ( buffer ) {
- this.vertices.push( parseFloat( buffer[ 1 ] ) );
- this.vertices.push( parseFloat( buffer[ 2 ] ) );
- this.vertices.push( parseFloat( buffer[ 3 ] ) );
- };
- RawMesh.prototype.pushVertexAndVertextColors = function ( buffer ) {
- this.vertices.push( parseFloat( buffer[ 1 ] ) );
- this.vertices.push( parseFloat( buffer[ 2 ] ) );
- this.vertices.push( parseFloat( buffer[ 3 ] ) );
- this.colors.push( parseFloat( buffer[ 4 ] ) );
- this.colors.push( parseFloat( buffer[ 5 ] ) );
- this.colors.push( parseFloat( buffer[ 6 ] ) );
- };
- RawMesh.prototype.pushUv = function ( buffer ) {
- this.uvs.push( parseFloat( buffer[ 1 ] ) );
- this.uvs.push( parseFloat( buffer[ 2 ] ) );
- };
- RawMesh.prototype.pushNormal = function ( buffer ) {
- this.normals.push( parseFloat( buffer[ 1 ] ) );
- this.normals.push( parseFloat( buffer[ 2 ] ) );
- this.normals.push( parseFloat( buffer[ 3 ] ) );
- };
- RawMesh.prototype.pushObject = function ( objectName ) {
- this.objectName = Validator.verifyInput( objectName, '' );
- };
- RawMesh.prototype.pushMtllib = function ( mtllibName ) {
- this.mtllibName = Validator.verifyInput( mtllibName, '' );
- };
- RawMesh.prototype.pushGroup = function ( groupName ) {
- this.groupName = Validator.verifyInput( groupName, '' );
- };
- RawMesh.prototype.pushUsemtl = function ( mtlName ) {
- if ( this.activeMtlName === mtlName || ! Validator.isValid( mtlName ) ) return;
- this.activeMtlName = mtlName;
- this.mtlCount++;
- this.verifyIndex();
- };
- RawMesh.prototype.pushSmoothingGroup = function ( smoothingGroup ) {
- var smoothingGroupInt = parseInt( smoothingGroup );
- if ( isNaN( smoothingGroupInt ) ) {
- smoothingGroupInt = smoothingGroup === "off" ? 0 : 1;
- }
- var smoothCheck = this.smoothingGroup.normalized;
- this.smoothingGroup.normalized = this.smoothingGroup.splitMaterials ? smoothingGroupInt : ( smoothingGroupInt === 0 ) ? 0 : 1;
- this.smoothingGroup.real = smoothingGroupInt;
- if ( smoothCheck !== smoothingGroupInt ) {
- this.smoothingGroupCount++;
- this.verifyIndex();
- }
- };
- RawMesh.prototype.verifyIndex = function () {
- var index = this.activeMtlName + '|' + this.smoothingGroup.normalized;
- this.subGroupInUse = this.subGroups[ index ];
- if ( ! Validator.isValid( this.subGroupInUse ) ) {
- this.subGroupInUse = new RawMeshSubGroup( this.objectName, this.groupName, this.activeMtlName, this.smoothingGroup.normalized );
- this.subGroups[ index ] = this.subGroupInUse;
- }
- };
- RawMesh.prototype.processFaces = function ( buffer, bufferPointer, slashesCount ) {
- var bufferLength = bufferPointer - 1;
- var i, length;
- // "f vertex ..."
- if ( slashesCount === 0 ) {
- for ( i = 2, length = bufferLength - 1; i < length; i ++ ) {
- this.buildFace( buffer[ 1 ] );
- this.buildFace( buffer[ i ] );
- this.buildFace( buffer[ i + 1 ] );
- }
- // "f vertex/uv ..."
- } else if ( bufferLength === slashesCount * 2 ) {
- for ( i = 3, length = bufferLength - 2; i < length; i += 2 ) {
- this.buildFace( buffer[ 1 ], buffer[ 2 ] );
- this.buildFace( buffer[ i ], buffer[ i + 1 ] );
- this.buildFace( buffer[ i + 2 ], buffer[ i + 3 ] );
- }
- // "f vertex/uv/normal ..."
- } else if ( bufferLength * 2 === slashesCount * 3 ) {
- for ( i = 4, length = bufferLength - 3; i < length; i += 3 ) {
- this.buildFace( buffer[ 1 ], buffer[ 2 ], buffer[ 3 ] );
- this.buildFace( buffer[ i ], buffer[ i + 1 ], buffer[ i + 2 ] );
- this.buildFace( buffer[ i + 3 ], buffer[ i + 4 ], buffer[ i + 5 ] );
- }
- // "f vertex//normal ..."
- } else {
- for ( i = 3, length = bufferLength - 2; i < length; i += 2 ) {
- this.buildFace( buffer[ 1 ], undefined, buffer[ 2 ] );
- this.buildFace( buffer[ i ], undefined, buffer[ i + 1 ] );
- this.buildFace( buffer[ i + 2 ], undefined, buffer[ i + 3 ] );
- }
- }
- };
- RawMesh.prototype.buildFace = function ( faceIndexV, faceIndexU, faceIndexN ) {
- var sgiu = this.subGroupInUse;
- if ( this.disregardNormals ) faceIndexN = undefined;
- var scope = this;
- var updateRawObjectDescriptionInUse = function () {
- var indexPointerV = ( parseInt( faceIndexV ) - scope.globalVertexOffset ) * 3;
- var indexPointerC = scope.colors.length > 0 ? indexPointerV : null;
- var vertices = sgiu.vertices;
- vertices.push( scope.vertices[ indexPointerV++ ] );
- vertices.push( scope.vertices[ indexPointerV++ ] );
- vertices.push( scope.vertices[ indexPointerV ] );
- if ( indexPointerC !== null ) {
- var colors = sgiu.colors;
- colors.push( scope.colors[ indexPointerC++ ] );
- colors.push( scope.colors[ indexPointerC++ ] );
- colors.push( scope.colors[ indexPointerC ] );
- }
- if ( faceIndexU ) {
- var indexPointerU = ( parseInt( faceIndexU ) - scope.globalUvOffset ) * 2;
- var uvs = sgiu.uvs;
- uvs.push( scope.uvs[ indexPointerU++ ] );
- uvs.push( scope.uvs[ indexPointerU ] );
- }
- if ( faceIndexN ) {
- var indexPointerN = ( parseInt( faceIndexN ) - scope.globalNormalOffset ) * 3;
- var normals = sgiu.normals;
- normals.push( scope.normals[ indexPointerN++ ] );
- normals.push( scope.normals[ indexPointerN++ ] );
- normals.push( scope.normals[ indexPointerN ] );
- }
- };
- if ( this.useIndices ) {
- var mappingName = faceIndexV + ( faceIndexU ? '_' + faceIndexU : '_n' ) + ( faceIndexN ? '_' + faceIndexN : '_n' );
- var indicesPointer = sgiu.indexMappings[ mappingName ];
- if ( Validator.isValid( indicesPointer ) ) {
- this.doubleIndicesCount++;
- } else {
- indicesPointer = sgiu.vertices.length / 3;
- updateRawObjectDescriptionInUse();
- sgiu.indexMappings[ mappingName ] = indicesPointer;
- sgiu.indexMappingsCount++;
- }
- sgiu.indices.push( indicesPointer );
- } else {
- updateRawObjectDescriptionInUse();
- }
- this.faceCount++;
- };
- /*
- * Support for lines with or without texture. First element in indexArray is the line identification
- * 0: "f vertex/uv vertex/uv ..."
- * 1: "f vertex vertex ..."
- */
- RawMesh.prototype.processLines = function ( buffer, bufferPointer, slashCount ) {
- var i = 1;
- var length;
- var bufferLength = bufferPointer - 1;
- if ( bufferLength === slashCount * 2 ) {
- for ( length = bufferLength - 2; i < length; i += 2 ) {
- this.vertices.push( parseInt( buffer[ i ] ) );
- this.uvs.push( parseInt( buffer[ i + 1 ] ) );
- }
- } else {
- for ( length = bufferLength - 1; i < length; i ++ ) {
- this.vertices.push( parseInt( buffer[ i ] ) );
- }
- }
- };
- /**
- * Clear any empty rawObjectDescription and calculate absolute vertex, normal and uv counts
- */
- RawMesh.prototype.finalize = function () {
- var rawObjectDescriptionsTemp = [];
- var rawObjectDescription;
- var absoluteVertexCount = 0;
- var absoluteIndexMappingsCount = 0;
- var absoluteIndexCount = 0;
- var absoluteColorCount = 0;
- var absoluteNormalCount = 0;
- var absoluteUvCount = 0;
- var indices;
- for ( var name in this.subGroups ) {
- rawObjectDescription = this.subGroups[ name ];
- if ( rawObjectDescription.vertices.length > 0 ) {
- indices = rawObjectDescription.indices;
- if ( indices.length > 0 && absoluteIndexMappingsCount > 0 ) {
- for ( var i in indices ) indices[ i ] = indices[ i ] + absoluteIndexMappingsCount;
- }
- rawObjectDescriptionsTemp.push( rawObjectDescription );
- absoluteVertexCount += rawObjectDescription.vertices.length;
- absoluteIndexMappingsCount += rawObjectDescription.indexMappingsCount;
- absoluteIndexCount += rawObjectDescription.indices.length;
- absoluteColorCount += rawObjectDescription.colors.length;
- absoluteUvCount += rawObjectDescription.uvs.length;
- absoluteNormalCount += rawObjectDescription.normals.length;
- }
- }
- // do not continue if no result
- var result = null;
- if ( rawObjectDescriptionsTemp.length > 0 ) {
- result = {
- name: this.groupName !== '' ? this.groupName : this.objectName,
- subGroups: rawObjectDescriptionsTemp,
- absoluteVertexCount: absoluteVertexCount,
- absoluteIndexCount: absoluteIndexCount,
- absoluteColorCount: absoluteColorCount,
- absoluteNormalCount: absoluteNormalCount,
- absoluteUvCount: absoluteUvCount,
- faceCount: this.faceCount,
- doubleIndicesCount: this.doubleIndicesCount
- };
- }
- return result;
- };
- RawMesh.prototype.createReport = function () {
- var report = {
- objectName: this.objectName,
- groupName: this.groupName,
- mtllibName: this.mtllibName,
- vertexCount: this.vertices.length / 3,
- normalCount: this.normals.length / 3,
- uvCount: this.uvs.length / 2,
- smoothingGroupCount: this.smoothingGroupCount,
- mtlCount: this.mtlCount,
- subGroups: this.subGroups.length
- };
- return report;
- };
- return RawMesh;
- })();
- /**
- * Descriptive information and data (vertices, normals, uvs) to passed on to mesh building function.
- * @class
- *
- * @param {string} objectName Name of the mesh
- * @param {string} groupName Name of the group
- * @param {string} materialName Name of the material
- * @param {number} smoothingGroup Normalized smoothingGroup (0: flat shading, 1: smooth shading)
- */
- var RawMeshSubGroup = (function () {
- function RawMeshSubGroup( objectName, groupName, materialName, smoothingGroup ) {
- this.objectName = objectName;
- this.groupName = groupName;
- this.materialName = materialName;
- this.smoothingGroup = smoothingGroup;
- this.vertices = [];
- this.indexMappingsCount = 0;
- this.indexMappings = [];
- this.indices = [];
- this.colors = [];
- this.uvs = [];
- this.normals = [];
- }
- return RawMeshSubGroup;
- })();
- OBJLoader2.prototype._checkFiles = function ( resources ) {
- var resource;
- var result = {
- mtl: null,
- obj: null
- };
- for ( var index in resources ) {
- resource = resources[ index ];
- if ( ! Validator.isValid( resource.name ) ) continue;
- if ( Validator.isValid( resource.content ) ) {
- if ( resource.extension === 'OBJ' ) {
- // fast-fail on bad type
- if ( ! ( resource.content instanceof Uint8Array ) ) throw 'Provided content is not of type arraybuffer! Aborting...';
- result.obj = resource;
- } else if ( resource.extension === 'MTL' && Validator.isValid( resource.name ) ) {
- if ( ! ( typeof( resource.content ) === 'string' || resource.content instanceof String ) ) throw 'Provided content is not of type String! Aborting...';
- result.mtl = resource;
- } else if ( resource.extension === "ZIP" ) {
- // ignore
- } else {
- 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...';
- if ( resource.extension === 'OBJ' ) {
- result.obj = resource;
- } else if ( resource.extension === 'MTL' ) {
- result.mtl = resource;
- } else if ( resource.extension === "ZIP" ) {
- // ignore
- } else {
- throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
- }
- }
- }
- return result;
- };
- /**
- * Utility method for loading an mtl file according resource description.
- * @memberOf THREE.OBJLoader2
- *
- * @param {string} url URL to the file
- * @param {string} name The name of the object
- * @param {Object} content The file content as arraybuffer or text
- * @param {function} callbackOnLoad
- * @param {string} [crossOrigin] CORS value
- */
- OBJLoader2.prototype.loadMtl = function ( url, name, content, callbackOnLoad, crossOrigin ) {
- var resource = new THREE.LoaderSupport.ResourceDescriptor( url, 'MTL' );
- resource.setContent( content );
- this._loadMtl( resource, callbackOnLoad, crossOrigin );
- };
- /**
- * Utility method for loading an mtl file according resource description.
- * @memberOf THREE.OBJLoader2
- *
- * @param {THREE.LoaderSupport.ResourceDescriptor} resource
- * @param {function} callbackOnLoad
- * @param {string} [crossOrigin] CORS value
- */
- OBJLoader2.prototype._loadMtl = function ( resource, callbackOnLoad, crossOrigin ) {
- if ( Validator.isValid( resource ) ) this.logger.logTimeStart( 'Loading MTL: ' + resource.name );
- var materials = [];
- var scope = this;
- var processMaterials = function ( materialCreator ) {
- var materialCreatorMaterials = [];
- if ( Validator.isValid( materialCreator ) ) {
- materialCreator.preload();
- materialCreatorMaterials = materialCreator.materials;
- for ( var materialName in materialCreatorMaterials ) {
- if ( materialCreatorMaterials.hasOwnProperty( materialName ) ) {
- materials[ materialName ] = materialCreatorMaterials[ materialName ];
- }
- }
- }
- if ( Validator.isValid( resource ) ) scope.logger.logTimeEnd( 'Loading MTL: ' + resource.name );
- callbackOnLoad( materials );
- };
- var mtlLoader = new THREE.MTLLoader();
- crossOrigin = Validator.verifyInput( crossOrigin, 'anonymous' );
- mtlLoader.setCrossOrigin( crossOrigin );
- // fast-fail
- if ( ! Validator.isValid( resource ) || ( ! Validator.isValid( resource.content ) && ! Validator.isValid( resource.url ) ) ) {
- processMaterials();
- } else {
- mtlLoader.setPath( resource.path );
- if ( Validator.isValid( resource.content ) ) {
- processMaterials( Validator.isValid( resource.content ) ? mtlLoader.parse( resource.content ) : null );
- } else if ( Validator.isValid( resource.url ) ) {
- var onError = function ( event ) {
- var output = 'Error occurred while downloading "' + resource.url + '"';
- this.logger.logError( output + ': ' + event );
- throw output;
- };
- mtlLoader.load( resource.name, processMaterials, undefined, onError );
- }
- }
- };
- return OBJLoader2;
- })();
|