123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341 |
- /**
- * @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.0-dev';
- var Validator = THREE.LoaderSupport.Validator;
- var Commons = THREE.LoaderSupport.Commons;
- OBJLoader2.prototype = Object.create( THREE.LoaderSupport.Commons.prototype );
- OBJLoader2.prototype.constructor = OBJLoader2;
- function OBJLoader2( manager ) {
- THREE.LoaderSupport.Commons.call( this, manager );
- console.log( "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 Default is false
- */
- OBJLoader2.prototype.setMaterialPerSmoothingGroup = function ( materialPerSmoothingGroup ) {
- this.materialPerSmoothingGroup = materialPerSmoothingGroup === true;
- };
- /**
- * Sets debug mode for the parser
- * @memberOf THREE.OBJLoader2
- *
- * @param {boolean} enabled
- */
- OBJLoader2.prototype.setDebug = function ( enabled ) {
- THREE.LoaderSupport.Commons.prototype.setDebug.call( this, enabled );
- };
- /**
- * 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
- * @param {boolean} useAsync Set this to use async loading
- */
- OBJLoader2.prototype.load = function ( url, onLoad, onProgress, onError, onMeshAlter, useAsync ) {
- var scope = this;
- if ( ! Validator.isValid( onProgress ) ) {
- var refPercentComplete = 0;
- var percentComplete = 0;
- onProgress = function ( event ) {
- if ( ! event.lengthComputable ) return;
- percentComplete = Math.round( event.loaded / event.total * 100 );
- if ( percentComplete > refPercentComplete ) {
- refPercentComplete = percentComplete;
- var output = 'Download of "' + url + '": ' + percentComplete + '%';
- console.log( output );
- scope.onProgress( output );
- }
- };
- }
- if ( ! Validator.isValid( onError ) ) {
- onError = function ( event ) {
- var output = 'Error occurred while downloading "' + url + '"';
- console.error( output + ': ' + event );
- scope.onProgress( output );
- };
- }
- 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( scope.parse( content ), scope.modelName, 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;
- } 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 content synchronously.
- * @memberOf THREE.OBJLoader2
- *
- * @param content
- */
- OBJLoader2.prototype.parse = function ( content ) {
- console.time( 'OBJLoader2 parse: ' + this.modelName );
- var parser = new Parser();
- parser.setMaterialPerSmoothingGroup( this.materialPerSmoothingGroup );
- parser.setUseIndices( this.useIndices );
- parser.setDisregardNormals( this.disregardNormals );
- parser.setMaterialNames( this.builder.materialNames );
- parser.setDebug( this.debug );
- 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 ( message ) {
- scope.onProgress( message );
- };
- parser.setCallbackProgress( onProgressScoped );
- if ( content instanceof ArrayBuffer || content instanceof Uint8Array ) {
- console.log( 'Parsing arrayBuffer...' );
- parser.parse( content );
- } else if ( typeof( content ) === 'string' || content instanceof String ) {
- console.log( 'Parsing text...' );
- parser.parseText( content );
- } else {
- throw 'Provided content was neither of type String nor Uint8Array! Aborting...';
- }
- console.timeEnd( 'OBJLoader2 parse: ' + this.modelName );
- return this.loaderRootNode;
- };
- /**
- * Parses OBJ content asynchronously.
- * @memberOf THREE.OBJLoader2
- *
- * @param {arraybuffer} content
- * @param {callback} onLoad
- */
- OBJLoader2.prototype.parseAsync = function ( content, onLoad ) {
- console.time( 'OBJLoader2 parseAsync: ' + this.modelName);
- var scope = this;
- var scopedOnLoad = function ( message ) {
- onLoad( scope.loaderRootNode, scope.modelName, scope.instanceNo, message );
- if ( scope.terminateWorkerOnLoad ) scope.workerSupport.terminateWorker();
- console.timeEnd( '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() );
- 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( '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: {
- debug: this.debug,
- materialPerSmoothingGroup: this.materialPerSmoothingGroup,
- useIndices: this.useIndices,
- disregardNormals: this.disregardNormals
- },
- 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() {
- this.callbackProgress = null;
- this.callbackBuilder = null;
- this.materialNames = [];
- this.debug = false;
- 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
- };
- };
- Parser.prototype.setDebug = function ( debug ) {
- if ( debug === true || debug === false ) this.debug = debug;
- };
- Parser.prototype.configure = function () {
- this.rawMesh = new RawMesh( this.materialPerSmoothingGroup, this.useIndices, this.disregardNormals );
- var matNames = ( this.materialNames.length > 0 ) ? '\n\tmaterialNames:\n\t\t- ' + this.materialNames.join( '\n\t\t- ' ) : '\n\tmaterialNames: None';
- var printConfig = 'OBJLoader2.Parser configuration:'
- + '\n\tdebug: ' + this.debug
- + matNames
- + '\n\tmaterialPerSmoothingGroup: ' + this.materialPerSmoothingGroup
- + '\n\tuseIndices: ' + this.useIndices
- + '\n\tdisregardNormals: ' +this.disregardNormals;
- console.log( printConfig );
- };
- 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;
- };
- /**
- * Parse the provided arraybuffer
- * @memberOf Parser
- *
- * @param {Uint8Array} arrayBuffer OBJ data as Uint8Array
- */
- Parser.prototype.parse = function ( arrayBuffer ) {
- console.time( 'OBJLoader2.Parser.parse' );
- this.configure();
- var arrayBufferView = new Uint8Array( arrayBuffer );
- var length = arrayBufferView.byteLength;
- var buffer = new Array( 128 );
- var bufferPointer = 0;
- var slashesCount = 0;
- var reachedFaces = false;
- var code;
- var word = '';
- for ( var i = 0; i < length; i++ ) {
- code = arrayBufferView[ i ];
- switch ( code ) {
- case Consts.CODE_SPACE:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- word = '';
- break;
- case Consts.CODE_SLASH:
- slashesCount++;
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- word = '';
- break;
- case Consts.CODE_LF:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- word = '';
- reachedFaces = this.processLine( buffer, bufferPointer, slashesCount, reachedFaces );
- bufferPointer = 0;
- slashesCount = 0;
- break;
- case Consts.CODE_CR:
- break;
- default:
- word += String.fromCharCode( code );
- break;
- }
- }
- this.finalize();
- console.timeEnd( 'OBJLoader2.Parser.parse' );
- };
- /**
- * Parse the provided text
- * @memberOf Parser
- *
- * @param {string} text OBJ data as string
- */
- Parser.prototype.parseText = function ( text ) {
- console.time( 'OBJLoader2.Parser.parseText' );
- this.configure();
- var length = text.length;
- var buffer = new Array( 128 );
- var bufferPointer = 0;
- var slashesCount = 0;
- var reachedFaces = false;
- var char;
- var word = '';
- for ( var i = 0; i < length; i++ ) {
- char = text[ i ];
- switch ( char ) {
- case Consts.STRING_SPACE:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- word = '';
- break;
- case Consts.STRING_SLASH:
- slashesCount++;
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- word = '';
- break;
- case Consts.STRING_LF:
- if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
- word = '';
- reachedFaces = this.processLine( buffer, bufferPointer, slashesCount, reachedFaces );
- bufferPointer = 0;
- slashesCount = 0;
- break;
- case Consts.STRING_CR:
- break;
- default:
- word += char;
- }
- }
- this.finalize();
- console.timeEnd( 'OBJLoader2.Parser.parseText' );
- };
- Parser.prototype.processLine = function ( buffer, bufferPointer, slashesCount, reachedFaces ) {
- if ( bufferPointer < 1 ) return reachedFaces;
- var bufferLength = bufferPointer - 1;
- var concatBuffer;
- 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 );
- reachedFaces = false;
- }
- if ( bufferLength === 3 ) {
- 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, slashesCount );
- break;
- case Consts.LINE_L:
- if ( bufferLength === slashesCount * 2 ) {
- this.rawMesh.buildLineVvt( buffer );
- } else {
- this.rawMesh.buildLineV( buffer );
- }
- break;
- case Consts.LINE_S:
- this.rawMesh.pushSmoothingGroup( buffer[ 1 ] );
- this.flushStringBuffer( buffer, bufferPointer );
- break;
- case Consts.LINE_G:
- concatBuffer = bufferLength > 1 ? buffer.slice( 1, bufferPointer ).join( ' ' ) : buffer[ 1 ];
- this.processCompletedGroup( concatBuffer );
- this.flushStringBuffer( buffer, bufferPointer );
- break;
- case Consts.LINE_O:
- concatBuffer = bufferLength > 1 ? buffer.slice( 1, bufferPointer ).join( ' ' ) : buffer[ 1 ];
- if ( this.rawMesh.vertices.length > 0 ) {
- this.processCompletedObject( concatBuffer, null );
- reachedFaces = false;
- } else {
- this.rawMesh.pushObject( concatBuffer );
- }
- this.flushStringBuffer( buffer, bufferPointer );
- break;
- case Consts.LINE_MTLLIB:
- concatBuffer = bufferLength > 1 ? buffer.slice( 1, bufferPointer ).join( ' ' ) : buffer[ 1 ];
- this.rawMesh.pushMtllib( concatBuffer );
- this.flushStringBuffer( buffer, bufferPointer );
- break;
- case Consts.LINE_USEMTL:
- concatBuffer = bufferLength > 1 ? buffer.slice( 1, bufferPointer ).join( ' ' ) : buffer[ 1 ];
- this.rawMesh.pushUsemtl( concatBuffer );
- this.flushStringBuffer( buffer, bufferPointer );
- break;
- default:
- break;
- }
- return reachedFaces;
- };
- Parser.prototype.flushStringBuffer = function ( buffer, bufferLength ) {
- for ( var i = 0; i < bufferLength; i++ ) {
- buffer[ i ] = '';
- }
- };
- Parser.prototype.processCompletedObject = function ( objectName, groupName ) {
- var result = this.rawMesh.finalize( this.debug );
- if ( Validator.isValid( result ) ) {
- this.inputObjectCount++;
- if ( this.debug ) this.rawMesh.createReport( this.inputObjectCount, true );
- var message = this.buildMesh( result, this.inputObjectCount );
- this.onProgress( message );
- }
- this.rawMesh = this.rawMesh.newInstanceFromObject( objectName, groupName );
- };
- Parser.prototype.processCompletedGroup = function ( groupName ) {
- var result = this.rawMesh.finalize();
- if ( Validator.isValid( result ) ) {
- this.inputObjectCount++;
- if ( this.debug ) this.rawMesh.createReport( this.inputObjectCount, true );
- var message = this.buildMesh( result, this.inputObjectCount );
- this.onProgress( message );
- 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 () {
- console.log( 'Global output object count: ' + this.outputObjectCount );
- var result = Validator.isValid( this.rawMesh ) ? this.rawMesh.finalize() : null;
- if ( Validator.isValid( result ) ) {
- this.inputObjectCount++;
- if ( this.debug ) this.rawMesh.createReport( this.inputObjectCount, true );
- var message = this.buildMesh( result, this.inputObjectCount );
- console.log(
- 'Overall counts: ' +
- '\n\tVertices: ' + this.counts.vertices,
- '\n\tFaces: ' + this.counts.faces,
- '\n\tMultiple definitions: ' + this.counts.doubleIndicesCount
- );
- this.onProgress( message );
- }
- };
- Parser.prototype.onProgress = function ( text ) {
- if ( Validator.isValid( text ) && Validator.isValid( this.callbackProgress) ) this.callbackProgress( text );
- };
- /**
- * 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 ) {
- 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;
- console.warn( '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.debug ) this.printReport( rawObjectDescription, selectedMaterialIndex );
- }
- this.outputObjectCount++;
- this.callbackBuilder(
- {
- cmd: 'meshData',
- 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
- );
- };
- Parser.prototype.printReport = function ( rawObjectDescription, selectedMaterialIndex ) {
- var materialIndexLine = Validator.isValid( selectedMaterialIndex ) ? '\n\tmaterialIndex: ' + selectedMaterialIndex : '';
- console.log(
- '\tOutput Object no.: ' + this.outputObjectCount +
- '\n\tobjectName: ' + rawObjectDescription.objectName +
- '\n\tgroupName: ' + rawObjectDescription.groupName +
- '\n\tmaterialName: ' + rawObjectDescription.materialName +
- materialIndexLine +
- '\n\tsmoothingGroup: ' + rawObjectDescription.smoothingGroup +
- '\n\t#vertices: ' + rawObjectDescription.vertices.length / 3 +
- '\n\t#indices: ' + rawObjectDescription.indices.length +
- '\n\t#colors: ' + rawObjectDescription.colors.length / 3 +
- '\n\t#uvs: ' + rawObjectDescription.uvs.length / 2 +
- '\n\t#normals: ' + rawObjectDescription.normals.length / 3
- );
- };
- 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.buildLineVvt = function ( lineArray ) {
- for ( var i = 1, length = lineArray.length; i < length; i ++ ) {
- this.vertices.push( parseInt( lineArray[ i ] ) );
- this.uvs.push( parseInt( lineArray[ i ] ) );
- }
- };
- RawMesh.prototype.buildLineV = function ( lineArray ) {
- for ( var i = 1, length = lineArray.length; i < length; i++ ) {
- this.vertices.push( parseInt( lineArray[ 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 ( inputObjectCount, printDirectly ) {
- 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
- };
- if ( printDirectly ) {
- console.log( '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
- );
- }
- 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 ) ) console.time( 'Loading MTL: ' + resource.name );
- var materials = [];
- 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 ) ) console.timeEnd( '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 + '"';
- console.error( output + ': ' + event );
- throw output;
- };
- mtlLoader.load( resource.name, processMaterials, undefined, onError );
- }
- }
- };
- return OBJLoader2;
- })();
|