12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148 |
- ( function () {
-
- function IFFParser() {
- this.debugger = new Debugger();
- }
- IFFParser.prototype = {
- constructor: IFFParser,
- parse: function ( buffer ) {
- this.reader = new DataViewReader( buffer );
- this.tree = {
- materials: {},
- layers: [],
- tags: [],
- textures: []
- };
- this.currentLayer = this.tree;
- this.currentForm = this.tree;
- this.parseTopForm();
- if ( this.tree.format === undefined ) return;
- if ( this.tree.format === 'LWO2' ) {
- this.parser = new THREE.LWO2Parser( this );
- while ( ! this.reader.endOfFile() ) this.parser.parseBlock();
- } else if ( this.tree.format === 'LWO3' ) {
- this.parser = new THREE.LWO3Parser( this );
- while ( ! this.reader.endOfFile() ) this.parser.parseBlock();
- }
- this.debugger.offset = this.reader.offset;
- this.debugger.closeForms();
- return this.tree;
- },
- parseTopForm() {
- this.debugger.offset = this.reader.offset;
- var topForm = this.reader.getIDTag();
- if ( topForm !== 'FORM' ) {
- console.warn( 'LWOLoader: Top-level FORM missing.' );
- return;
- }
- var length = this.reader.getUint32();
- this.debugger.dataOffset = this.reader.offset;
- this.debugger.length = length;
- var type = this.reader.getIDTag();
- if ( type === 'LWO2' ) {
- this.tree.format = type;
- } else if ( type === 'LWO3' ) {
- this.tree.format = type;
- }
- this.debugger.node = 0;
- this.debugger.nodeID = type;
- this.debugger.log();
- return;
- },
-
-
-
-
-
- parseForm( length ) {
- var type = this.reader.getIDTag();
- switch ( type ) {
-
-
- case 'ISEQ':
- case 'ANIM':
- case 'STCC':
- case 'VPVL':
- case 'VPRM':
- case 'NROT':
- case 'WRPW':
- case 'WRPH':
- case 'FUNC':
- case 'FALL':
- case 'OPAC':
- case 'GRAD':
- case 'ENVS':
- case 'VMOP':
- case 'VMBG':
- case 'OMAX':
- case 'STEX':
- case 'CKBG':
- case 'CKEY':
- case 'VMLA':
- case 'VMLB':
- this.debugger.skipped = true;
- this.skipForm( length );
- break;
-
-
- case 'META':
- case 'NNDS':
- case 'NODS':
- case 'NDTA':
- case 'ADAT':
- case 'AOVS':
- case 'BLOK':
- case 'IBGC':
- case 'IOPC':
- case 'IIMG':
- case 'TXTR':
-
- this.debugger.length = 4;
- this.debugger.skipped = true;
- break;
- case 'IFAL':
- case 'ISCL':
- case 'IPOS':
- case 'IROT':
- case 'IBMP':
- case 'IUTD':
- case 'IVTD':
- this.parseTextureNodeAttribute( type );
- break;
- case 'ENVL':
- this.parseEnvelope( length );
- break;
-
- case 'CLIP':
- if ( this.tree.format === 'LWO2' ) {
- this.parseForm( length );
- } else {
- this.parseClip( length );
- }
- break;
- case 'STIL':
- this.parseImage();
- break;
- case 'XREF':
-
- this.reader.skip( 8 );
- this.currentForm.referenceTexture = {
- index: this.reader.getUint32(),
- refName: this.reader.getString()
- };
- break;
-
- case 'IMST':
- this.parseImageStateForm( length );
- break;
-
- case 'SURF':
- this.parseSurfaceForm( length );
- break;
- case 'VALU':
-
- this.parseValueForm( length );
- break;
- case 'NTAG':
- this.parseSubNode( length );
- break;
- case 'ATTR':
- case 'SATR':
-
- this.setupForm( 'attributes', length );
- break;
- case 'NCON':
- this.parseConnections( length );
- break;
- case 'SSHA':
- this.parentForm = this.currentForm;
- this.currentForm = this.currentSurface;
- this.setupForm( 'surfaceShader', length );
- break;
- case 'SSHD':
- this.setupForm( 'surfaceShaderData', length );
- break;
- case 'ENTR':
-
- this.parseEntryForm( length );
- break;
-
- case 'IMAP':
- this.parseImageMap( length );
- break;
- case 'TAMP':
- this.parseXVAL( 'amplitude', length );
- break;
-
- case 'TMAP':
- this.setupForm( 'textureMap', length );
- break;
- case 'CNTR':
- this.parseXVAL3( 'center', length );
- break;
- case 'SIZE':
- this.parseXVAL3( 'scale', length );
- break;
- case 'ROTA':
- this.parseXVAL3( 'rotation', length );
- break;
- default:
- this.parseUnknownForm( type, length );
- }
- this.debugger.node = 0;
- this.debugger.nodeID = type;
- this.debugger.log();
- },
- setupForm( type, length ) {
- if ( ! this.currentForm ) this.currentForm = this.currentNode;
- this.currentFormEnd = this.reader.offset + length;
- this.parentForm = this.currentForm;
- if ( ! this.currentForm[ type ] ) {
- this.currentForm[ type ] = {};
- this.currentForm = this.currentForm[ type ];
- } else {
-
- console.warn( 'LWOLoader: form already exists on parent: ', type, this.currentForm );
- this.currentForm = this.currentForm[ type ];
- }
- },
- skipForm( length ) {
- this.reader.skip( length - 4 );
- },
- parseUnknownForm( type, length ) {
- console.warn( 'LWOLoader: unknown FORM encountered: ' + type, length );
- printBuffer( this.reader.dv.buffer, this.reader.offset, length - 4 );
- this.reader.skip( length - 4 );
- },
- parseSurfaceForm( length ) {
- this.reader.skip( 8 );
- var name = this.reader.getString();
- var surface = {
- attributes: {},
-
- connections: {},
- name: name,
- inputName: name,
- nodes: {},
- source: this.reader.getString()
- };
- this.tree.materials[ name ] = surface;
- this.currentSurface = surface;
- this.parentForm = this.tree.materials;
- this.currentForm = surface;
- this.currentFormEnd = this.reader.offset + length;
- },
- parseSurfaceLwo2( length ) {
- var name = this.reader.getString();
- var surface = {
- attributes: {},
-
- connections: {},
- name: name,
- nodes: {},
- source: this.reader.getString()
- };
- this.tree.materials[ name ] = surface;
- this.currentSurface = surface;
- this.parentForm = this.tree.materials;
- this.currentForm = surface;
- this.currentFormEnd = this.reader.offset + length;
- },
- parseSubNode( length ) {
-
-
-
- this.reader.skip( 8 );
- var name = this.reader.getString();
- var node = {
- name: name
- };
- this.currentForm = node;
- this.currentNode = node;
- this.currentFormEnd = this.reader.offset + length;
- },
-
- parseConnections( length ) {
- this.currentFormEnd = this.reader.offset + length;
- this.parentForm = this.currentForm;
- this.currentForm = this.currentSurface.connections;
- },
-
- parseEntryForm( length ) {
- this.reader.skip( 8 );
- var name = this.reader.getString();
- this.currentForm = this.currentNode.attributes;
- this.setupForm( name, length );
- },
-
-
- parseValueForm() {
- this.reader.skip( 8 );
- var valueType = this.reader.getString();
- if ( valueType === 'double' ) {
- this.currentForm.value = this.reader.getUint64();
- } else if ( valueType === 'int' ) {
- this.currentForm.value = this.reader.getUint32();
- } else if ( valueType === 'vparam' ) {
- this.reader.skip( 24 );
- this.currentForm.value = this.reader.getFloat64();
- } else if ( valueType === 'vparam3' ) {
- this.reader.skip( 24 );
- this.currentForm.value = this.reader.getFloat64Array( 3 );
- }
- },
-
-
- parseImageStateForm() {
- this.reader.skip( 8 );
- this.currentForm.mipMapLevel = this.reader.getFloat32();
- },
-
- parseImageMap( length ) {
- this.currentFormEnd = this.reader.offset + length;
- this.parentForm = this.currentForm;
- if ( ! this.currentForm.maps ) this.currentForm.maps = [];
- var map = {};
- this.currentForm.maps.push( map );
- this.currentForm = map;
- this.reader.skip( 10 );
- },
- parseTextureNodeAttribute( type ) {
- this.reader.skip( 28 );
- this.reader.skip( 20 );
- switch ( type ) {
- case 'ISCL':
- this.currentNode.scale = this.reader.getFloat32Array( 3 );
- break;
- case 'IPOS':
- this.currentNode.position = this.reader.getFloat32Array( 3 );
- break;
- case 'IROT':
- this.currentNode.rotation = this.reader.getFloat32Array( 3 );
- break;
- case 'IFAL':
- this.currentNode.falloff = this.reader.getFloat32Array( 3 );
- break;
- case 'IBMP':
- this.currentNode.amplitude = this.reader.getFloat32();
- break;
- case 'IUTD':
- this.currentNode.uTiles = this.reader.getFloat32();
- break;
- case 'IVTD':
- this.currentNode.vTiles = this.reader.getFloat32();
- break;
- }
- this.reader.skip( 2 );
- },
-
- parseEnvelope( length ) {
- this.reader.skip( length - 4 );
- },
-
-
-
-
-
- parseClip( length ) {
- var tag = this.reader.getIDTag();
- if ( tag === 'FORM' ) {
- this.reader.skip( 16 );
- this.currentNode.fileName = this.reader.getString();
- return;
- }
- this.reader.setOffset( this.reader.offset - 4 );
- this.currentFormEnd = this.reader.offset + length;
- this.parentForm = this.currentForm;
- this.reader.skip( 8 );
- var texture = {
- index: this.reader.getUint32()
- };
- this.tree.textures.push( texture );
- this.currentForm = texture;
- },
- parseClipLwo2( length ) {
- var texture = {
- index: this.reader.getUint32(),
- fileName: ''
- };
- while ( true ) {
- var tag = this.reader.getIDTag();
- var n_length = this.reader.getUint16();
- if ( tag === 'STIL' ) {
- texture.fileName = this.reader.getString();
- break;
- }
- if ( n_length >= length ) {
- break;
- }
- }
- this.tree.textures.push( texture );
- this.currentForm = texture;
- },
- parseImage() {
- this.reader.skip( 8 );
- this.currentForm.fileName = this.reader.getString();
- },
- parseXVAL( type, length ) {
- var endOffset = this.reader.offset + length - 4;
- this.reader.skip( 8 );
- this.currentForm[ type ] = this.reader.getFloat32();
- this.reader.setOffset( endOffset );
- },
- parseXVAL3( type, length ) {
- var endOffset = this.reader.offset + length - 4;
- this.reader.skip( 8 );
- this.currentForm[ type ] = {
- x: this.reader.getFloat32(),
- y: this.reader.getFloat32(),
- z: this.reader.getFloat32()
- };
- this.reader.setOffset( endOffset );
- },
-
-
- parseObjectTag() {
- if ( ! this.tree.objectTags ) this.tree.objectTags = {};
- this.tree.objectTags[ this.reader.getIDTag() ] = {
- tagString: this.reader.getString()
- };
- },
-
-
- parseLayer( length ) {
- var layer = {
- number: this.reader.getUint16(),
- flags: this.reader.getUint16(),
-
- pivot: this.reader.getFloat32Array( 3 ),
-
- name: this.reader.getString()
- };
- this.tree.layers.push( layer );
- this.currentLayer = layer;
- var parsedLength = 16 + stringOffset( this.currentLayer.name );
-
- this.currentLayer.parent = parsedLength < length ? this.reader.getUint16() : - 1;
- },
-
-
-
- parsePoints( length ) {
- this.currentPoints = [];
- for ( var i = 0; i < length / 4; i += 3 ) {
-
- this.currentPoints.push( this.reader.getFloat32(), this.reader.getFloat32(), - this.reader.getFloat32() );
- }
- },
-
-
-
-
-
-
-
-
-
- parseVertexMapping( length, discontinuous ) {
- var finalOffset = this.reader.offset + length;
- var channelName = this.reader.getString();
- if ( this.reader.offset === finalOffset ) {
-
- this.currentForm.UVChannel = channelName;
- return;
- }
- this.reader.setOffset( this.reader.offset - stringOffset( channelName ) );
- var type = this.reader.getIDTag();
- this.reader.getUint16();
- var name = this.reader.getString();
- var remainingLength = length - 6 - stringOffset( name );
- switch ( type ) {
- case 'TXUV':
- this.parseUVMapping( name, finalOffset, discontinuous );
- break;
- case 'MORF':
- case 'SPOT':
- this.parseMorphTargets( name, finalOffset, type );
- break;
-
- case 'APSL':
- case 'NORM':
- case 'WGHT':
- case 'MNVW':
- case 'PICK':
- case 'RGB ':
- case 'RGBA':
- this.reader.skip( remainingLength );
- break;
- default:
- console.warn( 'LWOLoader: unknown vertex map type: ' + type );
- this.reader.skip( remainingLength );
- }
- },
- parseUVMapping( name, finalOffset, discontinuous ) {
- var uvIndices = [];
- var polyIndices = [];
- var uvs = [];
- while ( this.reader.offset < finalOffset ) {
- uvIndices.push( this.reader.getVariableLengthIndex() );
- if ( discontinuous ) polyIndices.push( this.reader.getVariableLengthIndex() );
- uvs.push( this.reader.getFloat32(), this.reader.getFloat32() );
- }
- if ( discontinuous ) {
- if ( ! this.currentLayer.discontinuousUVs ) this.currentLayer.discontinuousUVs = {};
- this.currentLayer.discontinuousUVs[ name ] = {
- uvIndices: uvIndices,
- polyIndices: polyIndices,
- uvs: uvs
- };
- } else {
- if ( ! this.currentLayer.uvs ) this.currentLayer.uvs = {};
- this.currentLayer.uvs[ name ] = {
- uvIndices: uvIndices,
- uvs: uvs
- };
- }
- },
- parseMorphTargets( name, finalOffset, type ) {
- var indices = [];
- var points = [];
- type = type === 'MORF' ? 'relative' : 'absolute';
- while ( this.reader.offset < finalOffset ) {
- indices.push( this.reader.getVariableLengthIndex() );
- points.push( this.reader.getFloat32(), this.reader.getFloat32(), - this.reader.getFloat32() );
- }
- if ( ! this.currentLayer.morphTargets ) this.currentLayer.morphTargets = {};
- this.currentLayer.morphTargets[ name ] = {
- indices: indices,
- points: points,
- type: type
- };
- },
-
-
- parsePolygonList( length ) {
- var finalOffset = this.reader.offset + length;
- var type = this.reader.getIDTag();
- var indices = [];
- var polygonDimensions = [];
- while ( this.reader.offset < finalOffset ) {
- var numverts = this.reader.getUint16();
- numverts = numverts & 1023;
- polygonDimensions.push( numverts );
- for ( var j = 0; j < numverts; j ++ ) indices.push( this.reader.getVariableLengthIndex() );
- }
- var geometryData = {
- type: type,
- vertexIndices: indices,
- polygonDimensions: polygonDimensions,
- points: this.currentPoints
- };
- if ( polygonDimensions[ 0 ] === 1 ) geometryData.type = 'points'; else if ( polygonDimensions[ 0 ] === 2 ) geometryData.type = 'lines';
- this.currentLayer.geometry = geometryData;
- },
-
-
- parseTagStrings( length ) {
- this.tree.tags = this.reader.getStringArray( length );
- },
-
-
- parsePolygonTagMapping( length ) {
- var finalOffset = this.reader.offset + length;
- var type = this.reader.getIDTag();
- if ( type === 'SURF' ) this.parseMaterialIndices( finalOffset ); else {
-
- this.reader.skip( length - 4 );
- }
- },
- parseMaterialIndices( finalOffset ) {
-
- this.currentLayer.geometry.materialIndices = [];
- while ( this.reader.offset < finalOffset ) {
- var polygonIndex = this.reader.getVariableLengthIndex();
- var materialIndex = this.reader.getUint16();
- this.currentLayer.geometry.materialIndices.push( polygonIndex, materialIndex );
- }
- },
- parseUnknownCHUNK( blockID, length ) {
- console.warn( 'LWOLoader: unknown chunk type: ' + blockID + ' length: ' + length );
-
- var data = this.reader.getString( length );
- this.currentForm[ blockID ] = data;
- }
- };
- function DataViewReader( buffer ) {
- this.dv = new DataView( buffer );
- this.offset = 0;
- }
- DataViewReader.prototype = {
- constructor: DataViewReader,
- size: function () {
- return this.dv.buffer.byteLength;
- },
- setOffset( offset ) {
- if ( offset > 0 && offset < this.dv.buffer.byteLength ) {
- this.offset = offset;
- } else {
- console.error( 'LWOLoader: invalid buffer offset' );
- }
- },
- endOfFile: function () {
- if ( this.offset >= this.size() ) return true;
- return false;
- },
- skip: function ( length ) {
- this.offset += length;
- },
- getUint8: function () {
- var value = this.dv.getUint8( this.offset );
- this.offset += 1;
- return value;
- },
- getUint16: function () {
- var value = this.dv.getUint16( this.offset );
- this.offset += 2;
- return value;
- },
- getInt32: function () {
- var value = this.dv.getInt32( this.offset, false );
- this.offset += 4;
- return value;
- },
- getUint32: function () {
- var value = this.dv.getUint32( this.offset, false );
- this.offset += 4;
- return value;
- },
- getUint64: function () {
- var low, high;
- high = this.getUint32();
- low = this.getUint32();
- return high * 0x100000000 + low;
- },
- getFloat32: function () {
- var value = this.dv.getFloat32( this.offset, false );
- this.offset += 4;
- return value;
- },
- getFloat32Array: function ( size ) {
- var a = [];
- for ( var i = 0; i < size; i ++ ) {
- a.push( this.getFloat32() );
- }
- return a;
- },
- getFloat64: function () {
- var value = this.dv.getFloat64( this.offset, this.littleEndian );
- this.offset += 8;
- return value;
- },
- getFloat64Array: function ( size ) {
- var a = [];
- for ( var i = 0; i < size; i ++ ) {
- a.push( this.getFloat64() );
- }
- return a;
- },
-
-
-
-
-
-
- getVariableLengthIndex() {
- var firstByte = this.getUint8();
- if ( firstByte === 255 ) {
- return this.getUint8() * 65536 + this.getUint8() * 256 + this.getUint8();
- }
- return firstByte * 256 + this.getUint8();
- },
-
- getIDTag() {
- return this.getString( 4 );
- },
- getString: function ( size ) {
- if ( size === 0 ) return;
- var a = [];
- if ( size ) {
- for ( var i = 0; i < size; i ++ ) {
- a[ i ] = this.getUint8();
- }
- } else {
- var currentChar;
- var len = 0;
- while ( currentChar !== 0 ) {
- currentChar = this.getUint8();
- if ( currentChar !== 0 ) a.push( currentChar );
- len ++;
- }
- if ( ! isEven( len + 1 ) ) this.getUint8();
- }
- return THREE.LoaderUtils.decodeText( new Uint8Array( a ) );
- },
- getStringArray: function ( size ) {
- var a = this.getString( size );
- a = a.split( '\0' );
- return a.filter( Boolean );
- }
- };
- function Debugger() {
- this.active = false;
- this.depth = 0;
- this.formList = [];
- }
- Debugger.prototype = {
- constructor: Debugger,
- enable: function () {
- this.active = true;
- },
- log: function () {
- if ( ! this.active ) return;
- var nodeType;
- switch ( this.node ) {
- case 0:
- nodeType = 'FORM';
- break;
- case 1:
- nodeType = 'CHK';
- break;
- case 2:
- nodeType = 'S-CHK';
- break;
- }
- console.log( '| '.repeat( this.depth ) + nodeType, this.nodeID, `( ${this.offset} ) -> ( ${this.dataOffset + this.length} )`, this.node == 0 ? ' {' : '', this.skipped ? 'SKIPPED' : '', this.node == 0 && this.skipped ? '}' : '' );
- if ( this.node == 0 && ! this.skipped ) {
- this.depth += 1;
- this.formList.push( this.dataOffset + this.length );
- }
- this.skipped = false;
- },
- closeForms: function () {
- if ( ! this.active ) return;
- for ( var i = this.formList.length - 1; i >= 0; i -- ) {
- if ( this.offset >= this.formList[ i ] ) {
- this.depth -= 1;
- console.log( '| '.repeat( this.depth ) + '}' );
- this.formList.splice( - 1, 1 );
- }
- }
- }
- };
- function isEven( num ) {
- return num % 2;
- }
-
- function stringOffset( string ) {
- return string.length + 1 + ( isEven( string.length + 1 ) ? 1 : 0 );
- }
-
- function printBuffer( buffer, from, to ) {
- console.log( THREE.LoaderUtils.decodeText( new Uint8Array( buffer, from, to ) ) );
- }
- THREE.IFFParser = IFFParser;
- } )();
|