123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218 |
- import { LoaderUtils } from '../../../../build/three.module.js';
- import { LWO2Parser } from './LWO2Parser.js';
- import { LWO3Parser } from './LWO3Parser.js';
- 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 LWO2Parser( this );
- while ( ! this.reader.endOfFile() ) this.parser.parseBlock();
- } else if ( this.tree.format === 'LWO3' ) {
- this.parser = new 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 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( LoaderUtils.decodeText( new Uint8Array( buffer, from, to ) ) );
- }
- export { IFFParser };
|