|
@@ -1,7 +1,10 @@
|
|
|
/**
|
|
|
+ * @version 1.1.1
|
|
|
+ *
|
|
|
* @author Lewy Blue https://github.com/looeee
|
|
|
+ * @author Guilherme Avila https://github/sciecode
|
|
|
*
|
|
|
- * Load files in LWO3 and LWO2 format
|
|
|
+ * @desc Load files in LWO3 and LWO2 format on Three.js
|
|
|
*
|
|
|
* LWO3 format specification:
|
|
|
* http://static.lightwave3d.com/sdk/2018/html/filefmts/lwo3.html
|
|
@@ -9,7 +12,10 @@
|
|
|
* LWO2 format specification:
|
|
|
* http://static.lightwave3d.com/sdk/2018/html/filefmts/lwo2.html
|
|
|
*
|
|
|
- */
|
|
|
+ * Development and test repository:
|
|
|
+ * https://github.com/threejs/lwoloader
|
|
|
+ *
|
|
|
+ **/
|
|
|
|
|
|
import {
|
|
|
AddOperation,
|
|
@@ -30,6 +36,7 @@ import {
|
|
|
LoaderUtils,
|
|
|
Mesh,
|
|
|
MeshPhongMaterial,
|
|
|
+ MeshPhysicalMaterial,
|
|
|
MeshStandardMaterial,
|
|
|
MirroredRepeatWrapping,
|
|
|
Points,
|
|
@@ -39,2442 +46,3041 @@ import {
|
|
|
Vector2
|
|
|
} from "../../../build/three.module.js";
|
|
|
|
|
|
-var LWOLoader = ( function () {
|
|
|
+function LWO2Parser( IFFParser ) {
|
|
|
|
|
|
- var lwoTree;
|
|
|
+ this.IFF = IFFParser;
|
|
|
|
|
|
- function LWOLoader( manager ) {
|
|
|
+}
|
|
|
|
|
|
- this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
|
|
|
+LWO2Parser.prototype = {
|
|
|
|
|
|
- }
|
|
|
+ constructor: LWO2Parser,
|
|
|
|
|
|
- LWOLoader.prototype = {
|
|
|
+ parseBlock: function () {
|
|
|
|
|
|
- constructor: LWOLoader,
|
|
|
+ this.IFF.debugger.offset = this.IFF.reader.offset;
|
|
|
+ this.IFF.debugger.closeForms();
|
|
|
|
|
|
- crossOrigin: 'anonymous',
|
|
|
+ var blockID = this.IFF.reader.getIDTag();
|
|
|
+ var length = this.IFF.reader.getUint32(); // size of data in bytes
|
|
|
+ if ( length > this.IFF.reader.dv.byteLength - this.IFF.reader.offset ) {
|
|
|
|
|
|
- load: function ( url, onLoad, onProgress, onError ) {
|
|
|
+ this.IFF.reader.offset -= 4;
|
|
|
+ length = this.IFF.reader.getUint16();
|
|
|
|
|
|
- var self = this;
|
|
|
+ }
|
|
|
|
|
|
- var path = ( self.path === undefined ) ? LoaderUtils.extractUrlBase( url ) : self.path;
|
|
|
+ this.IFF.debugger.dataOffset = this.IFF.reader.offset;
|
|
|
+ this.IFF.debugger.length = length;
|
|
|
+
|
|
|
+ // Data types may be found in either LWO2 OR LWO3 spec
|
|
|
+ switch ( blockID ) {
|
|
|
+
|
|
|
+ case 'FORM': // form blocks may consist of sub -chunks or sub-forms
|
|
|
+ this.IFF.parseForm( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // SKIPPED CHUNKS
|
|
|
+ // if break; is called directly, the position in the lwoTree is not created
|
|
|
+ // any sub chunks and forms are added to the parent form instead
|
|
|
+ // MISC skipped
|
|
|
+ case 'ICON': // Thumbnail Icon Image
|
|
|
+ case 'VMPA': // Vertex Map Parameter
|
|
|
+ case 'BBOX': // bounding box
|
|
|
+ // case 'VMMD':
|
|
|
+ // case 'VTYP':
|
|
|
+
|
|
|
+ // normal maps can be specified, normally on models imported from other applications. Currently ignored
|
|
|
+ case 'NORM':
|
|
|
+
|
|
|
+ // ENVL FORM skipped
|
|
|
+ case 'PRE ':
|
|
|
+ case 'POST':
|
|
|
+ case 'KEY ':
|
|
|
+ case 'SPAN':
|
|
|
+
|
|
|
+ // CLIP FORM skipped
|
|
|
+ case 'TIME':
|
|
|
+ case 'CLRS':
|
|
|
+ case 'CLRA':
|
|
|
+ case 'FILT':
|
|
|
+ case 'DITH':
|
|
|
+ case 'CONT':
|
|
|
+ case 'BRIT':
|
|
|
+ case 'SATR':
|
|
|
+ case 'HUE ':
|
|
|
+ case 'GAMM':
|
|
|
+ case 'NEGA':
|
|
|
+ case 'IFLT':
|
|
|
+ case 'PFLT':
|
|
|
+
|
|
|
+ // Image Map Layer skipped
|
|
|
+ case 'PROJ':
|
|
|
+ case 'AXIS':
|
|
|
+ case 'AAST':
|
|
|
+ case 'PIXB':
|
|
|
+ case 'AUVO':
|
|
|
+ case 'STCK':
|
|
|
+
|
|
|
+ // Procedural Textures skipped
|
|
|
+ case 'PROC':
|
|
|
+ case 'VALU':
|
|
|
+ case 'FUNC':
|
|
|
+
|
|
|
+ // Gradient Textures skipped
|
|
|
+ case 'PNAM':
|
|
|
+ case 'INAM':
|
|
|
+ case 'GRST':
|
|
|
+ case 'GREN':
|
|
|
+ case 'GRPT':
|
|
|
+ case 'FKEY':
|
|
|
+ case 'IKEY':
|
|
|
+
|
|
|
+ // Texture Mapping Form skipped
|
|
|
+ case 'CSYS':
|
|
|
+
|
|
|
+ // Surface CHUNKs skipped
|
|
|
+ case 'OPAQ': // top level 'opacity' checkbox
|
|
|
+ case 'CMAP': // clip map
|
|
|
+
|
|
|
+ // Surface node CHUNKS skipped
|
|
|
+ // These mainly specify the node editor setup in LW
|
|
|
+ case 'NLOC':
|
|
|
+ case 'NZOM':
|
|
|
+ case 'NVER':
|
|
|
+ case 'NSRV':
|
|
|
+ case 'NVSK': // unknown
|
|
|
+ case 'NCRD':
|
|
|
+ case 'WRPW': // image wrap w ( for cylindrical and spherical projections)
|
|
|
+ case 'WRPH': // image wrap h
|
|
|
+ case 'NMOD':
|
|
|
+ case 'NPRW':
|
|
|
+ case 'NPLA':
|
|
|
+ case 'NODS':
|
|
|
+ case 'VERS':
|
|
|
+ case 'ENUM':
|
|
|
+ case 'TAG ':
|
|
|
+ case 'OPAC':
|
|
|
+
|
|
|
+ // Car Material CHUNKS
|
|
|
+ case 'CGMD':
|
|
|
+ case 'CGTY':
|
|
|
+ case 'CGST':
|
|
|
+ case 'CGEN':
|
|
|
+ case 'CGTS':
|
|
|
+ case 'CGTE':
|
|
|
+ case 'OSMP':
|
|
|
+ case 'OMDE':
|
|
|
+ case 'OUTR':
|
|
|
+ case 'FLAG':
|
|
|
+
|
|
|
+ case 'TRNL':
|
|
|
+ case 'GLOW':
|
|
|
+ case 'GVAL': // glow intensity
|
|
|
+ case 'SHRP':
|
|
|
+ case 'RFOP':
|
|
|
+ case 'RSAN':
|
|
|
+ case 'TROP':
|
|
|
+ case 'RBLR':
|
|
|
+ case 'TBLR':
|
|
|
+ case 'CLRH':
|
|
|
+ case 'CLRF':
|
|
|
+ case 'ADTR':
|
|
|
+ case 'LINE':
|
|
|
+ case 'ALPH':
|
|
|
+ case 'VCOL':
|
|
|
+ case 'ENAB':
|
|
|
+ this.IFF.debugger.skipped = true;
|
|
|
+ this.IFF.reader.skip( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'SURF':
|
|
|
+ this.IFF.parseSurfaceLwo2( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'CLIP':
|
|
|
+ this.IFF.parseClipLwo2( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Texture node chunks (not in spec)
|
|
|
+ case 'IPIX': // usePixelBlending
|
|
|
+ case 'IMIP': // useMipMaps
|
|
|
+ case 'IMOD': // imageBlendingMode
|
|
|
+ case 'AMOD': // unknown
|
|
|
+ case 'IINV': // imageInvertAlpha
|
|
|
+ case 'INCR': // imageInvertColor
|
|
|
+ case 'IAXS': // imageAxis ( for non-UV maps)
|
|
|
+ case 'IFOT': // imageFallofType
|
|
|
+ case 'ITIM': // timing for animated textures
|
|
|
+ case 'IWRL':
|
|
|
+ case 'IUTI':
|
|
|
+ case 'IINX':
|
|
|
+ case 'IINY':
|
|
|
+ case 'IINZ':
|
|
|
+ case 'IREF': // possibly a VX for reused texture nodes
|
|
|
+ if ( length === 4 ) this.IFF.currentNode[ blockID ] = this.IFF.reader.getInt32();
|
|
|
+ else this.IFF.reader.skip( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'OTAG':
|
|
|
+ this.IFF.parseObjectTag();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'LAYR':
|
|
|
+ this.IFF.parseLayer( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'PNTS':
|
|
|
+ this.IFF.parsePoints( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'VMAP':
|
|
|
+ this.IFF.parseVertexMapping( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'AUVU':
|
|
|
+ case 'AUVN':
|
|
|
+ this.IFF.reader.skip( length - 1 );
|
|
|
+ this.IFF.reader.getVariableLengthIndex(); // VX
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'POLS':
|
|
|
+ this.IFF.parsePolygonList( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TAGS':
|
|
|
+ this.IFF.parseTagStrings( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'PTAG':
|
|
|
+ this.IFF.parsePolygonTagMapping( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'VMAD':
|
|
|
+ this.IFF.parseVertexMapping( length, true );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Misc CHUNKS
|
|
|
+ case 'DESC': // Description Line
|
|
|
+ this.IFF.currentForm.description = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TEXT':
|
|
|
+ case 'CMNT':
|
|
|
+ case 'NCOM':
|
|
|
+ this.IFF.currentForm.comment = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Envelope Form
|
|
|
+ case 'NAME':
|
|
|
+ this.IFF.currentForm.channelName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Image Map Layer
|
|
|
+ case 'WRAP':
|
|
|
+ this.IFF.currentForm.wrap = { w: this.IFF.reader.getUint16(), h: this.IFF.reader.getUint16() };
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IMAG':
|
|
|
+ var index = this.IFF.reader.getVariableLengthIndex();
|
|
|
+ this.IFF.currentForm.imageIndex = index;
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Texture Mapping Form
|
|
|
+ case 'OREF':
|
|
|
+ this.IFF.currentForm.referenceObject = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'ROID':
|
|
|
+ this.IFF.currentForm.referenceObjectID = this.IFF.reader.getUint32();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Surface Blocks
|
|
|
+ case 'SSHN':
|
|
|
+ this.IFF.currentSurface.surfaceShaderName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'AOVN':
|
|
|
+ this.IFF.currentSurface.surfaceCustomAOVName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Nodal Blocks
|
|
|
+ case 'NSTA':
|
|
|
+ this.IFF.currentForm.disabled = this.IFF.reader.getUint16();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'NRNM':
|
|
|
+ this.IFF.currentForm.realName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'NNME':
|
|
|
+ this.IFF.currentForm.refName = this.IFF.reader.getString();
|
|
|
+ this.IFF.currentSurface.nodes[ this.IFF.currentForm.refName ] = this.IFF.currentForm;
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Nodal Blocks : connections
|
|
|
+ case 'INME':
|
|
|
+ if ( ! this.IFF.currentForm.nodeName ) this.IFF.currentForm.nodeName = [];
|
|
|
+ this.IFF.currentForm.nodeName.push( this.IFF.reader.getString() );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IINN':
|
|
|
+ if ( ! this.IFF.currentForm.inputNodeName ) this.IFF.currentForm.inputNodeName = [];
|
|
|
+ this.IFF.currentForm.inputNodeName.push( this.IFF.reader.getString() );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IINM':
|
|
|
+ if ( ! this.IFF.currentForm.inputName ) this.IFF.currentForm.inputName = [];
|
|
|
+ this.IFF.currentForm.inputName.push( this.IFF.reader.getString() );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IONM':
|
|
|
+ if ( ! this.IFF.currentForm.inputOutputName ) this.IFF.currentForm.inputOutputName = [];
|
|
|
+ this.IFF.currentForm.inputOutputName.push( this.IFF.reader.getString() );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'FNAM':
|
|
|
+ this.IFF.currentForm.fileName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'CHAN': // NOTE: ENVL Forms may also have CHAN chunk, however ENVL is currently ignored
|
|
|
+ if ( length === 4 ) this.IFF.currentForm.textureChannel = this.IFF.reader.getIDTag();
|
|
|
+ else this.IFF.reader.skip( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // LWO2 Spec chunks: these are needed since the SURF FORMs are often in LWO2 format
|
|
|
+ case 'SMAN':
|
|
|
+ var maxSmoothingAngle = this.IFF.reader.getFloat32();
|
|
|
+ this.IFF.currentSurface.attributes.smooth = ( maxSmoothingAngle < 0 ) ? false : true;
|
|
|
+ break;
|
|
|
+
|
|
|
+ // LWO2: Basic Surface Parameters
|
|
|
+ case 'COLR':
|
|
|
+ this.IFF.currentSurface.attributes.Color = { value: this.IFF.reader.getFloat32Array( 3 ) };
|
|
|
+ this.IFF.reader.skip( 2 ); // VX: envelope
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'LUMI':
|
|
|
+ this.IFF.currentSurface.attributes.Luminosity = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'SPEC':
|
|
|
+ this.IFF.currentSurface.attributes.Specular = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'DIFF':
|
|
|
+ this.IFF.currentSurface.attributes.Diffuse = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'REFL':
|
|
|
+ this.IFF.currentSurface.attributes.Reflection = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'GLOS':
|
|
|
+ this.IFF.currentSurface.attributes.Glossiness = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TRAN':
|
|
|
+ this.IFF.currentSurface.attributes.opacity = this.IFF.reader.getFloat32();
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'BUMP':
|
|
|
+ this.IFF.currentSurface.attributes.bumpStrength = this.IFF.reader.getFloat32();
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'SIDE':
|
|
|
+ this.IFF.currentSurface.attributes.side = this.IFF.reader.getUint16();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'RIMG':
|
|
|
+ this.IFF.currentSurface.attributes.reflectionMap = this.IFF.reader.getVariableLengthIndex();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'RIND':
|
|
|
+ this.IFF.currentSurface.attributes.refractiveIndex = this.IFF.reader.getFloat32();
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TIMG':
|
|
|
+ this.IFF.currentSurface.attributes.refractionMap = this.IFF.reader.getVariableLengthIndex();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IMAP':
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TMAP':
|
|
|
+ this.IFF.debugger.skipped = true;
|
|
|
+ this.IFF.reader.skip( length ); // needs implementing
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IUVI': // uv channel name
|
|
|
+ this.IFF.currentNode.UVChannel = this.IFF.reader.getString( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IUTL': // widthWrappingMode: 0 = Reset, 1 = Repeat, 2 = Mirror, 3 = Edge
|
|
|
+ this.IFF.currentNode.widthWrappingMode = this.IFF.reader.getUint32();
|
|
|
+ break;
|
|
|
+ case 'IVTL': // heightWrappingMode
|
|
|
+ this.IFF.currentNode.heightWrappingMode = this.IFF.reader.getUint32();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // LWO2 USE
|
|
|
+ case 'BLOK':
|
|
|
+ // skip
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ this.IFF.parseUnknownCHUNK( blockID, length );
|
|
|
|
|
|
- // give the mesh a default name based on the filename
|
|
|
- var modelName = url.split( path ).pop().split( '.' )[ 0 ];
|
|
|
+ }
|
|
|
|
|
|
- var loader = new FileLoader( this.manager );
|
|
|
- loader.setPath( self.path );
|
|
|
- loader.setResponseType( 'arraybuffer' );
|
|
|
+ if ( blockID != 'FORM' ) {
|
|
|
|
|
|
- loader.load( url, function ( buffer ) {
|
|
|
+ this.IFF.debugger.node = 1;
|
|
|
+ this.IFF.debugger.nodeID = blockID;
|
|
|
+ this.IFF.debugger.log();
|
|
|
|
|
|
- // console.time( 'Total parsing: ' );
|
|
|
- onLoad( self.parse( buffer, path, modelName ) );
|
|
|
- // console.timeEnd( 'Total parsing: ' );
|
|
|
+ }
|
|
|
|
|
|
- }, onProgress, onError );
|
|
|
+ if ( this.IFF.reader.offset >= this.IFF.currentFormEnd ) {
|
|
|
|
|
|
- },
|
|
|
+ this.IFF.currentForm = this.IFF.parentForm;
|
|
|
|
|
|
- setCrossOrigin: function ( value ) {
|
|
|
+ }
|
|
|
|
|
|
- this.crossOrigin = value;
|
|
|
- return this;
|
|
|
+ }
|
|
|
|
|
|
- },
|
|
|
+};
|
|
|
+
|
|
|
+function LWO3Parser( IFFParser ) {
|
|
|
+
|
|
|
+ this.IFF = IFFParser;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+LWO3Parser.prototype = {
|
|
|
+
|
|
|
+ constructor: LWO3Parser,
|
|
|
+
|
|
|
+ parseBlock: function () {
|
|
|
+
|
|
|
+ this.IFF.debugger.offset = this.IFF.reader.offset;
|
|
|
+ this.IFF.debugger.closeForms();
|
|
|
+
|
|
|
+ var blockID = this.IFF.reader.getIDTag();
|
|
|
+ var length = this.IFF.reader.getUint32(); // size of data in bytes
|
|
|
+
|
|
|
+ this.IFF.debugger.dataOffset = this.IFF.reader.offset;
|
|
|
+ this.IFF.debugger.length = length;
|
|
|
+
|
|
|
+ // Data types may be found in either LWO2 OR LWO3 spec
|
|
|
+ switch ( blockID ) {
|
|
|
+
|
|
|
+ case 'FORM': // form blocks may consist of sub -chunks or sub-forms
|
|
|
+ this.IFF.parseForm( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // SKIPPED CHUNKS
|
|
|
+ // MISC skipped
|
|
|
+ case 'ICON': // Thumbnail Icon Image
|
|
|
+ case 'VMPA': // Vertex Map Parameter
|
|
|
+ case 'BBOX': // bounding box
|
|
|
+ // case 'VMMD':
|
|
|
+ // case 'VTYP':
|
|
|
+
|
|
|
+ // normal maps can be specified, normally on models imported from other applications. Currently ignored
|
|
|
+ case 'NORM':
|
|
|
+
|
|
|
+ // ENVL FORM skipped
|
|
|
+ case 'PRE ':
|
|
|
+ case 'POST':
|
|
|
+ case 'KEY ':
|
|
|
+ case 'SPAN':
|
|
|
+
|
|
|
+ // CLIP FORM skipped
|
|
|
+ case 'TIME':
|
|
|
+ case 'CLRS':
|
|
|
+ case 'CLRA':
|
|
|
+ case 'FILT':
|
|
|
+ case 'DITH':
|
|
|
+ case 'CONT':
|
|
|
+ case 'BRIT':
|
|
|
+ case 'SATR':
|
|
|
+ case 'HUE ':
|
|
|
+ case 'GAMM':
|
|
|
+ case 'NEGA':
|
|
|
+ case 'IFLT':
|
|
|
+ case 'PFLT':
|
|
|
+
|
|
|
+ // Image Map Layer skipped
|
|
|
+ case 'PROJ':
|
|
|
+ case 'AXIS':
|
|
|
+ case 'AAST':
|
|
|
+ case 'PIXB':
|
|
|
+ case 'STCK':
|
|
|
+
|
|
|
+ // Procedural Textures skipped
|
|
|
+ case 'VALU':
|
|
|
+
|
|
|
+ // Gradient Textures skipped
|
|
|
+ case 'PNAM':
|
|
|
+ case 'INAM':
|
|
|
+ case 'GRST':
|
|
|
+ case 'GREN':
|
|
|
+ case 'GRPT':
|
|
|
+ case 'FKEY':
|
|
|
+ case 'IKEY':
|
|
|
+
|
|
|
+ // Texture Mapping Form skipped
|
|
|
+ case 'CSYS':
|
|
|
+
|
|
|
+ // Surface CHUNKs skipped
|
|
|
+ case 'OPAQ': // top level 'opacity' checkbox
|
|
|
+ case 'CMAP': // clip map
|
|
|
+
|
|
|
+ // Surface node CHUNKS skipped
|
|
|
+ // These mainly specify the node editor setup in LW
|
|
|
+ case 'NLOC':
|
|
|
+ case 'NZOM':
|
|
|
+ case 'NVER':
|
|
|
+ case 'NSRV':
|
|
|
+ case 'NCRD':
|
|
|
+ case 'NMOD':
|
|
|
+ case 'NSEL':
|
|
|
+ case 'NPRW':
|
|
|
+ case 'NPLA':
|
|
|
+ case 'VERS':
|
|
|
+ case 'ENUM':
|
|
|
+ case 'TAG ':
|
|
|
+
|
|
|
+ // Car Material CHUNKS
|
|
|
+ case 'CGMD':
|
|
|
+ case 'CGTY':
|
|
|
+ case 'CGST':
|
|
|
+ case 'CGEN':
|
|
|
+ case 'CGTS':
|
|
|
+ case 'CGTE':
|
|
|
+ case 'OSMP':
|
|
|
+ case 'OMDE':
|
|
|
+ case 'OUTR':
|
|
|
+ case 'FLAG':
|
|
|
+
|
|
|
+ case 'TRNL':
|
|
|
+ case 'GLOS':
|
|
|
+ case 'SHRP':
|
|
|
+ case 'RFOP':
|
|
|
+ case 'RSAN':
|
|
|
+ case 'TROP':
|
|
|
+ case 'RBLR':
|
|
|
+ case 'TBLR':
|
|
|
+ case 'CLRH':
|
|
|
+ case 'CLRF':
|
|
|
+ case 'ADTR':
|
|
|
+ case 'GLOW':
|
|
|
+ case 'LINE':
|
|
|
+ case 'ALPH':
|
|
|
+ case 'VCOL':
|
|
|
+ case 'ENAB':
|
|
|
+ this.IFF.debugger.skipped = true;
|
|
|
+ this.IFF.reader.skip( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Texture node chunks (not in spec)
|
|
|
+ case 'IPIX': // usePixelBlending
|
|
|
+ case 'IMIP': // useMipMaps
|
|
|
+ case 'IMOD': // imageBlendingMode
|
|
|
+ case 'AMOD': // unknown
|
|
|
+ case 'IINV': // imageInvertAlpha
|
|
|
+ case 'INCR': // imageInvertColor
|
|
|
+ case 'IAXS': // imageAxis ( for non-UV maps)
|
|
|
+ case 'IFOT': // imageFallofType
|
|
|
+ case 'ITIM': // timing for animated textures
|
|
|
+ case 'IWRL':
|
|
|
+ case 'IUTI':
|
|
|
+ case 'IINX':
|
|
|
+ case 'IINY':
|
|
|
+ case 'IINZ':
|
|
|
+ case 'IREF': // possibly a VX for reused texture nodes
|
|
|
+ if ( length === 4 ) this.IFF.currentNode[ blockID ] = this.IFF.reader.getInt32();
|
|
|
+ else this.IFF.reader.skip( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'OTAG':
|
|
|
+ this.IFF.parseObjectTag();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'LAYR':
|
|
|
+ this.IFF.parseLayer( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'PNTS':
|
|
|
+ this.IFF.parsePoints( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'VMAP':
|
|
|
+ this.IFF.parseVertexMapping( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'POLS':
|
|
|
+ this.IFF.parsePolygonList( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TAGS':
|
|
|
+ this.IFF.parseTagStrings( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'PTAG':
|
|
|
+ this.IFF.parsePolygonTagMapping( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'VMAD':
|
|
|
+ this.IFF.parseVertexMapping( length, true );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Misc CHUNKS
|
|
|
+ case 'DESC': // Description Line
|
|
|
+ this.IFF.currentForm.description = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TEXT':
|
|
|
+ case 'CMNT':
|
|
|
+ case 'NCOM':
|
|
|
+ this.IFF.currentForm.comment = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Envelope Form
|
|
|
+ case 'NAME':
|
|
|
+ this.IFF.currentForm.channelName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Image Map Layer
|
|
|
+ case 'WRAP':
|
|
|
+ this.IFF.currentForm.wrap = { w: this.IFF.reader.getUint16(), h: this.IFF.reader.getUint16() };
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IMAG':
|
|
|
+ var index = this.IFF.reader.getVariableLengthIndex();
|
|
|
+ this.IFF.currentForm.imageIndex = index;
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Texture Mapping Form
|
|
|
+ case 'OREF':
|
|
|
+ this.IFF.currentForm.referenceObject = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'ROID':
|
|
|
+ this.IFF.currentForm.referenceObjectID = this.IFF.reader.getUint32();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Surface Blocks
|
|
|
+ case 'SSHN':
|
|
|
+ this.IFF.currentSurface.surfaceShaderName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'AOVN':
|
|
|
+ this.IFF.currentSurface.surfaceCustomAOVName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Nodal Blocks
|
|
|
+ case 'NSTA':
|
|
|
+ this.IFF.currentForm.disabled = this.IFF.reader.getUint16();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'NRNM':
|
|
|
+ this.IFF.currentForm.realName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'NNME':
|
|
|
+ this.IFF.currentForm.refName = this.IFF.reader.getString();
|
|
|
+ this.IFF.currentSurface.nodes[ this.IFF.currentForm.refName ] = this.IFF.currentForm;
|
|
|
+ break;
|
|
|
+
|
|
|
+ // Nodal Blocks : connections
|
|
|
+ case 'INME':
|
|
|
+ if ( ! this.IFF.currentForm.nodeName ) this.IFF.currentForm.nodeName = [];
|
|
|
+ this.IFF.currentForm.nodeName.push( this.IFF.reader.getString() );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IINN':
|
|
|
+ if ( ! this.IFF.currentForm.inputNodeName ) this.IFF.currentForm.inputNodeName = [];
|
|
|
+ this.IFF.currentForm.inputNodeName.push( this.IFF.reader.getString() );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IINM':
|
|
|
+ if ( ! this.IFF.currentForm.inputName ) this.IFF.currentForm.inputName = [];
|
|
|
+ this.IFF.currentForm.inputName.push( this.IFF.reader.getString() );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IONM':
|
|
|
+ if ( ! this.IFF.currentForm.inputOutputName ) this.IFF.currentForm.inputOutputName = [];
|
|
|
+ this.IFF.currentForm.inputOutputName.push( this.IFF.reader.getString() );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'FNAM':
|
|
|
+ this.IFF.currentForm.fileName = this.IFF.reader.getString();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'CHAN': // NOTE: ENVL Forms may also have CHAN chunk, however ENVL is currently ignored
|
|
|
+ if ( length === 4 ) this.IFF.currentForm.textureChannel = this.IFF.reader.getIDTag();
|
|
|
+ else this.IFF.reader.skip( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // LWO2 Spec chunks: these are needed since the SURF FORMs are often in LWO2 format
|
|
|
+ case 'SMAN':
|
|
|
+ var maxSmoothingAngle = this.IFF.reader.getFloat32();
|
|
|
+ this.IFF.currentSurface.attributes.smooth = ( maxSmoothingAngle < 0 ) ? false : true;
|
|
|
+ break;
|
|
|
+
|
|
|
+ // LWO2: Basic Surface Parameters
|
|
|
+ case 'COLR':
|
|
|
+ this.IFF.currentSurface.attributes.Color = { value: this.IFF.reader.getFloat32Array( 3 ) };
|
|
|
+ this.IFF.reader.skip( 2 ); // VX: envelope
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'LUMI':
|
|
|
+ this.IFF.currentSurface.attributes.Luminosity = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'SPEC':
|
|
|
+ this.IFF.currentSurface.attributes.Specular = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'DIFF':
|
|
|
+ this.IFF.currentSurface.attributes.Diffuse = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'REFL':
|
|
|
+ this.IFF.currentSurface.attributes.Reflection = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'GLOS':
|
|
|
+ this.IFF.currentSurface.attributes.Glossiness = { value: this.IFF.reader.getFloat32() };
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TRAN':
|
|
|
+ this.IFF.currentSurface.attributes.opacity = this.IFF.reader.getFloat32();
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'BUMP':
|
|
|
+ this.IFF.currentSurface.attributes.bumpStrength = this.IFF.reader.getFloat32();
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'SIDE':
|
|
|
+ this.IFF.currentSurface.attributes.side = this.IFF.reader.getUint16();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'RIMG':
|
|
|
+ this.IFF.currentSurface.attributes.reflectionMap = this.IFF.reader.getVariableLengthIndex();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'RIND':
|
|
|
+ this.IFF.currentSurface.attributes.refractiveIndex = this.IFF.reader.getFloat32();
|
|
|
+ this.IFF.reader.skip( 2 );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'TIMG':
|
|
|
+ this.IFF.currentSurface.attributes.refractionMap = this.IFF.reader.getVariableLengthIndex();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IMAP':
|
|
|
+ this.IFF.currentSurface.attributes.imageMapIndex = this.IFF.reader.getUint32();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IUVI': // uv channel name
|
|
|
+ this.IFF.currentNode.UVChannel = this.IFF.reader.getString( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IUTL': // widthWrappingMode: 0 = Reset, 1 = Repeat, 2 = Mirror, 3 = Edge
|
|
|
+ this.IFF.currentNode.widthWrappingMode = this.IFF.reader.getUint32();
|
|
|
+ break;
|
|
|
+ case 'IVTL': // heightWrappingMode
|
|
|
+ this.IFF.currentNode.heightWrappingMode = this.IFF.reader.getUint32();
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ this.IFF.parseUnknownCHUNK( blockID, length );
|
|
|
|
|
|
- setPath: function ( value ) {
|
|
|
+ }
|
|
|
|
|
|
- this.path = value;
|
|
|
- return this;
|
|
|
+ if ( blockID != 'FORM' ) {
|
|
|
|
|
|
- },
|
|
|
+ this.IFF.debugger.node = 1;
|
|
|
+ this.IFF.debugger.nodeID = blockID;
|
|
|
+ this.IFF.debugger.log();
|
|
|
|
|
|
- setResourcePath: function ( value ) {
|
|
|
+ }
|
|
|
|
|
|
- this.resourcePath = value;
|
|
|
- return this;
|
|
|
+ if ( this.IFF.reader.offset >= this.IFF.currentFormEnd ) {
|
|
|
|
|
|
- },
|
|
|
+ this.IFF.currentForm = this.IFF.parentForm;
|
|
|
|
|
|
- parse: function ( iffBuffer, path, modelName ) {
|
|
|
+ }
|
|
|
|
|
|
- lwoTree = new IFFParser().parse( iffBuffer );
|
|
|
+ }
|
|
|
|
|
|
- // console.log( 'lwoTree', lwoTree );
|
|
|
+};
|
|
|
|
|
|
- var textureLoader = new TextureLoader( this.manager ).setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
|
|
|
+/**
|
|
|
+ * === IFFParser ===
|
|
|
+ * - Parses data from the IFF buffer.
|
|
|
+ * - LWO3 files are in IFF format and can contain the following data types, referred to by shorthand codes
|
|
|
+ *
|
|
|
+ * ATOMIC DATA TYPES
|
|
|
+ * ID Tag - 4x 7 bit uppercase ASCII chars: ID4
|
|
|
+ * signed integer, 1, 2, or 4 byte length: I1, I2, I4
|
|
|
+ * unsigned integer, 1, 2, or 4 byte length: U1, U2, U4
|
|
|
+ * float, 4 byte length: F4
|
|
|
+ * string, series of ASCII chars followed by null byte (If the length of the string including the null terminating byte is odd, an extra null is added so that the data that follows will begin on an even byte boundary): S0
|
|
|
+ *
|
|
|
+ * COMPOUND DATA TYPES
|
|
|
+ * Variable-length Index (index into an array or collection): U2 or U4 : VX
|
|
|
+ * Color (RGB): F4 + F4 + F4: COL12
|
|
|
+ * Coordinate (x, y, z): F4 + F4 + F4: VEC12
|
|
|
+ * Percentage F4 data type from 0->1 with 1 = 100%: FP4
|
|
|
+ * Angle in radian F4: ANG4
|
|
|
+ * Filename (string) S0: FNAM0
|
|
|
+ * XValue F4 + index (VX) + optional envelope( ENVL ): XVAL
|
|
|
+ * XValue vector VEC12 + index (VX) + optional envelope( ENVL ): XVAL3
|
|
|
+ *
|
|
|
+ * The IFF file is arranged in chunks:
|
|
|
+ * CHUNK = ID4 + length (U4) + length X bytes of data + optional 0 pad byte
|
|
|
+ * optional 0 pad byte is there to ensure chunk ends on even boundary, not counted in size
|
|
|
+ *
|
|
|
+ * COMPOUND DATA TYPES
|
|
|
+ * - Chunks are combined in Forms (collections of chunks)
|
|
|
+ * - FORM = string 'FORM' (ID4) + length (U4) + type (ID4) + optional ( CHUNK | FORM )
|
|
|
+ * - CHUNKS and FORMS are collectively referred to as blocks
|
|
|
+ * - The entire file is contained in one top level FORM
|
|
|
+ *
|
|
|
+ **/
|
|
|
|
|
|
- return new LWOTreeParser( textureLoader ).parse( modelName );
|
|
|
+function IFFParser( ) {
|
|
|
|
|
|
- }
|
|
|
+ this.debugger = new Debugger();
|
|
|
+ // this.debugger.enable(); // un-comment to log IFF hierarchy.
|
|
|
|
|
|
- };
|
|
|
+}
|
|
|
|
|
|
- // Parse the lwoTree object
|
|
|
- function LWOTreeParser( textureLoader ) {
|
|
|
+IFFParser.prototype = {
|
|
|
|
|
|
- this.textureLoader = textureLoader;
|
|
|
+ constructor: IFFParser,
|
|
|
|
|
|
- }
|
|
|
+ parse: function ( buffer ) {
|
|
|
|
|
|
- LWOTreeParser.prototype = {
|
|
|
+ this.reader = new DataViewReader( buffer );
|
|
|
|
|
|
- constructor: LWOTreeParser,
|
|
|
+ this.tree = {
|
|
|
+ materials: {},
|
|
|
+ layers: [],
|
|
|
+ tags: [],
|
|
|
+ textures: [],
|
|
|
+ };
|
|
|
|
|
|
- parse: function ( modelName ) {
|
|
|
+ // start out at the top level to add any data before first layer is encountered
|
|
|
+ this.currentLayer = this.tree;
|
|
|
+ this.currentForm = this.tree;
|
|
|
|
|
|
- this.materials = new MaterialParser( this.textureLoader ).parse();
|
|
|
- this.defaultLayerName = modelName;
|
|
|
+ this.parseTopForm();
|
|
|
|
|
|
- this.meshes = this.parseLayers();
|
|
|
+ if ( this.tree.format === undefined ) return;
|
|
|
|
|
|
- return {
|
|
|
- materials: this.materials,
|
|
|
- meshes: this.meshes,
|
|
|
- };
|
|
|
+ if ( this.tree.format === 'LWO2' ) {
|
|
|
|
|
|
- },
|
|
|
+ this.parser = new LWO2Parser( this );
|
|
|
+ while ( ! this.reader.endOfFile() ) this.parser.parseBlock();
|
|
|
|
|
|
- parseLayers() {
|
|
|
+ } else if ( this.tree.format === 'LWO3' ) {
|
|
|
|
|
|
- // array of all meshes for building hierarchy
|
|
|
- var meshes = [];
|
|
|
+ this.parser = new LWO3Parser( this );
|
|
|
+ while ( ! this.reader.endOfFile() ) this.parser.parseBlock();
|
|
|
|
|
|
- // final array containing meshes with scene graph hierarchy set up
|
|
|
- var finalMeshes = [];
|
|
|
+ }
|
|
|
|
|
|
- var geometryParser = new GeometryParser();
|
|
|
+ this.debugger.offset = this.reader.offset;
|
|
|
+ this.debugger.closeForms();
|
|
|
|
|
|
- var self = this;
|
|
|
- lwoTree.layers.forEach( function ( layer ) {
|
|
|
+ return this.tree;
|
|
|
|
|
|
- var geometry = geometryParser.parse( layer.geometry, layer );
|
|
|
+ },
|
|
|
|
|
|
- var mesh = self.parseMesh( geometry, layer );
|
|
|
+ parseTopForm() {
|
|
|
|
|
|
- meshes[ layer.number ] = mesh;
|
|
|
+ this.debugger.offset = this.reader.offset;
|
|
|
|
|
|
- if ( layer.parent === - 1 ) finalMeshes.push( mesh );
|
|
|
- else meshes[ layer.parent ].add( mesh );
|
|
|
+ var topForm = this.reader.getIDTag();
|
|
|
|
|
|
+ if ( topForm !== 'FORM' ) {
|
|
|
|
|
|
- } );
|
|
|
+ console.warn( "LWOLoader: Top-level FORM missing." );
|
|
|
+ return;
|
|
|
|
|
|
- this.applyPivots( finalMeshes );
|
|
|
+ }
|
|
|
|
|
|
- return finalMeshes;
|
|
|
+ var length = this.reader.getUint32();
|
|
|
|
|
|
- },
|
|
|
+ this.debugger.dataOffset = this.reader.offset;
|
|
|
+ this.debugger.length = length;
|
|
|
|
|
|
- parseMesh( geometry, layer ) {
|
|
|
+ var type = this.reader.getIDTag();
|
|
|
|
|
|
- var mesh;
|
|
|
+ if ( type === 'LWO2' ) {
|
|
|
|
|
|
- var materials = this.getMaterials( geometry.userData.matNames, layer.geometry.type );
|
|
|
+ this.tree.format = type;
|
|
|
|
|
|
- this.duplicateUVs( geometry, materials );
|
|
|
+ } else if ( type === 'LWO3' ) {
|
|
|
|
|
|
- if ( layer.geometry.type === 'points' ) mesh = new Points( geometry, materials );
|
|
|
- else if ( layer.geometry.type === 'lines' ) mesh = new LineSegments( geometry, materials );
|
|
|
- else mesh = new Mesh( geometry, materials );
|
|
|
+ this.tree.format = type;
|
|
|
|
|
|
- if ( layer.name ) mesh.name = layer.name;
|
|
|
- else mesh.name = this.defaultLayerName + '_layer_' + layer.number;
|
|
|
+ }
|
|
|
|
|
|
- mesh.userData.pivot = layer.pivot;
|
|
|
+ this.debugger.node = 0;
|
|
|
+ this.debugger.nodeID = type;
|
|
|
+ this.debugger.log();
|
|
|
+
|
|
|
+ return;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ ///
|
|
|
+ // FORM PARSING METHODS
|
|
|
+ ///
|
|
|
+
|
|
|
+ // Forms are organisational and can contain any number of sub chunks and sub forms
|
|
|
+ // FORM ::= 'FORM'[ID4], length[U4], type[ID4], ( chunk[CHUNK] | form[FORM] ) * }
|
|
|
+ parseForm( length ) {
|
|
|
+
|
|
|
+ var type = this.reader.getIDTag();
|
|
|
+
|
|
|
+ switch ( type ) {
|
|
|
+
|
|
|
+ // SKIPPED FORMS
|
|
|
+ // if skipForm( length ) is called, the entire form and any sub forms and chunks are skipped
|
|
|
+
|
|
|
+ case 'ISEQ': // Image sequence
|
|
|
+ case 'ANIM': // plug in animation
|
|
|
+ case 'STCC': // Color-cycling Still
|
|
|
+ case 'VPVL':
|
|
|
+ case 'VPRM':
|
|
|
+ case 'NROT':
|
|
|
+ case 'WRPW': // image wrap w ( for cylindrical and spherical projections)
|
|
|
+ case 'WRPH': // image wrap h
|
|
|
+ case 'FUNC':
|
|
|
+ case 'FALL':
|
|
|
+ case 'OPAC':
|
|
|
+ case 'GRAD': // gradient texture
|
|
|
+ case 'ENVS':
|
|
|
+ case 'VMOP':
|
|
|
+ case 'VMBG':
|
|
|
+
|
|
|
+ // Car Material FORMS
|
|
|
+ case 'OMAX':
|
|
|
+ case 'STEX':
|
|
|
+ case 'CKBG':
|
|
|
+ case 'CKEY':
|
|
|
+ case 'VMLA':
|
|
|
+ case 'VMLB':
|
|
|
+ this.debugger.skipped = true;
|
|
|
+ this.skipForm( length ); // not currently supported
|
|
|
+ break;
|
|
|
+
|
|
|
+ // if break; is called directly, the position in the lwoTree is not created
|
|
|
+ // any sub chunks and forms are added to the parent form instead
|
|
|
+ case 'META':
|
|
|
+ case 'NNDS':
|
|
|
+ case 'NODS':
|
|
|
+ case 'NDTA':
|
|
|
+ case 'ADAT':
|
|
|
+ case 'AOVS':
|
|
|
+ case 'BLOK':
|
|
|
+
|
|
|
+ // used by texture nodes
|
|
|
+ case 'IBGC': // imageBackgroundColor
|
|
|
+ case 'IOPC': // imageOpacity
|
|
|
+ case 'IIMG': // hold reference to image path
|
|
|
+ case 'TXTR':
|
|
|
+ // this.setupForm( type, length );
|
|
|
+ this.debugger.length = 4;
|
|
|
+ this.debugger.skipped = true;
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'IFAL': // imageFallof
|
|
|
+ case 'ISCL': // imageScale
|
|
|
+ case 'IPOS': // imagePosition
|
|
|
+ case 'IROT': // imageRotation
|
|
|
+ case 'IBMP':
|
|
|
+ case 'IUTD':
|
|
|
+ case 'IVTD':
|
|
|
+ this.parseTextureNodeAttribute( type );
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'ENVL':
|
|
|
+ this.parseEnvelope( length );
|
|
|
+ break;
|
|
|
+
|
|
|
+ // CLIP FORM AND SUB FORMS
|
|
|
+
|
|
|
+ case 'CLIP':
|
|
|
+ if ( this.tree.format === 'LWO2' ) {
|
|
|
|
|
|
- return mesh;
|
|
|
+ this.parseForm( length );
|
|
|
|
|
|
- },
|
|
|
+ } else {
|
|
|
|
|
|
- // TODO: may need to be reversed in z to convert LWO to three.js coordinates
|
|
|
- applyPivots( meshes ) {
|
|
|
+ this.parseClip( length );
|
|
|
|
|
|
- meshes.forEach( function ( mesh ) {
|
|
|
+ }
|
|
|
+ break;
|
|
|
|
|
|
- mesh.traverse( function ( child ) {
|
|
|
+ case 'STIL':
|
|
|
+ this.parseImage();
|
|
|
+ break;
|
|
|
|
|
|
- var pivot = child.userData.pivot;
|
|
|
+ case 'XREF': // clone of another STIL
|
|
|
+ this.reader.skip( 8 ); // unknown
|
|
|
+ this.currentForm.referenceTexture = {
|
|
|
+ index: this.reader.getUint32(),
|
|
|
+ refName: this.reader.getString() // internal unique ref
|
|
|
+ };
|
|
|
+ break;
|
|
|
|
|
|
- child.position.x += pivot[ 0 ];
|
|
|
- child.position.y += pivot[ 1 ];
|
|
|
- child.position.z += pivot[ 2 ];
|
|
|
+ // Not in spec, used by texture nodes
|
|
|
|
|
|
- if ( child.parent ) {
|
|
|
+ case 'IMST':
|
|
|
+ this.parseImageStateForm( length );
|
|
|
+ break;
|
|
|
|
|
|
- var parentPivot = child.parent.userData.pivot;
|
|
|
+ // SURF FORM AND SUB FORMS
|
|
|
|
|
|
- child.position.x -= parentPivot[ 0 ];
|
|
|
- child.position.y -= parentPivot[ 1 ];
|
|
|
- child.position.z -= parentPivot[ 2 ];
|
|
|
+ case 'SURF':
|
|
|
+ this.parseSurfaceForm( length );
|
|
|
+ break;
|
|
|
|
|
|
- }
|
|
|
+ case 'VALU': // Not in spec
|
|
|
+ this.parseValueForm( length );
|
|
|
+ break;
|
|
|
|
|
|
- } );
|
|
|
+ case 'NTAG':
|
|
|
+ this.parseSubNode( length );
|
|
|
+ break;
|
|
|
|
|
|
- } );
|
|
|
+ case 'ATTR': // BSDF Node Attributes
|
|
|
+ case 'SATR': // Standard Node Attributes
|
|
|
+ this.setupForm( 'attributes', length );
|
|
|
+ break;
|
|
|
|
|
|
- },
|
|
|
+ case 'NCON':
|
|
|
+ this.parseConnections( length );
|
|
|
+ break;
|
|
|
|
|
|
- getMaterials( namesArray, type ) {
|
|
|
+ case 'SSHA':
|
|
|
+ this.parentForm = this.currentForm;
|
|
|
+ this.currentForm = this.currentSurface;
|
|
|
+ this.setupForm( 'surfaceShader', length );
|
|
|
+ break;
|
|
|
|
|
|
- var materials = [];
|
|
|
+ case 'SSHD':
|
|
|
+ this.setupForm( 'surfaceShaderData', length );
|
|
|
+ break;
|
|
|
|
|
|
- var self = this;
|
|
|
+ case 'ENTR': // Not in spec
|
|
|
+ this.parseEntryForm( length );
|
|
|
+ break;
|
|
|
|
|
|
- namesArray.forEach( function ( name, i ) {
|
|
|
+ // Image Map Layer
|
|
|
|
|
|
- materials[ i ] = self.getMaterialByName( name );
|
|
|
+ case 'IMAP':
|
|
|
+ this.parseImageMap( length );
|
|
|
+ break;
|
|
|
|
|
|
- } );
|
|
|
+ case 'TAMP':
|
|
|
+ this.parseXVAL( 'amplitude', length );
|
|
|
+ break;
|
|
|
|
|
|
- // convert materials to line or point mats if required
|
|
|
- if ( type === 'points' || type === 'lines' ) {
|
|
|
+ //Texture Mapping Form
|
|
|
|
|
|
- materials.forEach( function ( mat, i ) {
|
|
|
+ case 'TMAP':
|
|
|
+ this.setupForm( 'textureMap', length );
|
|
|
+ break;
|
|
|
|
|
|
- var spec = {
|
|
|
- color: mat.color,
|
|
|
- };
|
|
|
+ case 'CNTR':
|
|
|
+ this.parseXVAL3( 'center', length );
|
|
|
+ break;
|
|
|
|
|
|
- if ( type === 'points' ) {
|
|
|
+ case 'SIZE':
|
|
|
+ this.parseXVAL3( 'scale', length );
|
|
|
+ break;
|
|
|
|
|
|
- spec.size = 0.1;
|
|
|
- spec.map = mat.map;
|
|
|
- spec.morphTargets = mat.morphTargets;
|
|
|
- materials[ i ] = new PointsMaterial( spec );
|
|
|
+ case 'ROTA':
|
|
|
+ this.parseXVAL3( 'rotation', length );
|
|
|
+ break;
|
|
|
|
|
|
- } else if ( type === 'lines' ) {
|
|
|
+ default:
|
|
|
+ this.parseUnknownForm( type, length );
|
|
|
|
|
|
- materials[ i ] = new LineBasicMaterial( spec );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ this.debugger.node = 0;
|
|
|
+ this.debugger.nodeID = type;
|
|
|
+ this.debugger.log();
|
|
|
|
|
|
- } );
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ setupForm( type, length ) {
|
|
|
|
|
|
- // if there is only one material, return that directly instead of array
|
|
|
- var filtered = materials.filter( Boolean );
|
|
|
- if ( filtered.length === 1 ) return filtered[ 0 ];
|
|
|
+ if ( ! this.currentForm ) this.currentForm = this.currentNode;
|
|
|
|
|
|
- return materials;
|
|
|
+ this.currentFormEnd = this.reader.offset + length;
|
|
|
+ this.parentForm = this.currentForm;
|
|
|
|
|
|
- },
|
|
|
+ if ( ! this.currentForm[ type ] ) {
|
|
|
|
|
|
- getMaterialByName( name ) {
|
|
|
+ this.currentForm[ type ] = {};
|
|
|
+ this.currentForm = this.currentForm[ type ];
|
|
|
|
|
|
- return this.materials.filter( function ( m ) {
|
|
|
|
|
|
- return m.name === name;
|
|
|
+ } else {
|
|
|
|
|
|
- } )[ 0 ];
|
|
|
+ // should never see this unless there's a bug in the reader
|
|
|
+ console.warn( 'LWOLoader: form already exists on parent: ', type, this.currentForm );
|
|
|
|
|
|
- },
|
|
|
+ this.currentForm = this.currentForm[ type ];
|
|
|
|
|
|
- // If the material has an aoMap, duplicate UVs
|
|
|
- duplicateUVs( geometry, materials ) {
|
|
|
+ }
|
|
|
|
|
|
- var duplicateUVs = false;
|
|
|
|
|
|
- if ( ! Array.isArray( materials ) ) {
|
|
|
+ },
|
|
|
|
|
|
- if ( materials.aoMap ) duplicateUVs = true;
|
|
|
+ skipForm( length ) {
|
|
|
|
|
|
- } else {
|
|
|
+ this.reader.skip( length - 4 );
|
|
|
|
|
|
- materials.forEach( function ( material ) {
|
|
|
+ },
|
|
|
|
|
|
- if ( material.aoMap ) duplicateUVs = true;
|
|
|
+ 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 );
|
|
|
|
|
|
- if ( ! duplicateUVs ) return;
|
|
|
+ },
|
|
|
|
|
|
- geometry.addAttribute( 'uv2', new BufferAttribute( geometry.attributes.uv.array, 2 ) );
|
|
|
+ parseSurfaceForm( length ) {
|
|
|
|
|
|
- },
|
|
|
+ this.reader.skip( 8 ); // unknown Uint32 x2
|
|
|
|
|
|
- };
|
|
|
+ var name = this.reader.getString();
|
|
|
|
|
|
- function MaterialParser( textureLoader ) {
|
|
|
+ var surface = {
|
|
|
+ attributes: {}, // LWO2 style non-node attributes will go here
|
|
|
+ connections: {},
|
|
|
+ name: name,
|
|
|
+ inputName: name,
|
|
|
+ nodes: {},
|
|
|
+ source: this.reader.getString(),
|
|
|
+ };
|
|
|
|
|
|
- this.textureLoader = textureLoader;
|
|
|
+ this.tree.materials[ name ] = surface;
|
|
|
+ this.currentSurface = surface;
|
|
|
|
|
|
- }
|
|
|
+ this.parentForm = this.tree.materials;
|
|
|
+ this.currentForm = surface;
|
|
|
+ this.currentFormEnd = this.reader.offset + length;
|
|
|
|
|
|
- MaterialParser.prototype = {
|
|
|
+ },
|
|
|
|
|
|
- constructor: MaterialParser,
|
|
|
+ parseSurfaceLwo2( length ) {
|
|
|
|
|
|
- parse: function () {
|
|
|
+ var name = this.reader.getString();
|
|
|
|
|
|
- var materials = [];
|
|
|
- this.textures = {};
|
|
|
+ var surface = {
|
|
|
+ attributes: {}, // LWO2 style non-node attributes will go here
|
|
|
+ connections: {},
|
|
|
+ name: name,
|
|
|
+ nodes: {},
|
|
|
+ source: this.reader.getString(),
|
|
|
+ };
|
|
|
|
|
|
- for ( var name in lwoTree.materials ) {
|
|
|
+ this.tree.materials[ name ] = surface;
|
|
|
+ this.currentSurface = surface;
|
|
|
|
|
|
- if ( lwoTree.format === 'LWO3' ) {
|
|
|
+ this.parentForm = this.tree.materials;
|
|
|
+ this.currentForm = surface;
|
|
|
+ this.currentFormEnd = this.reader.offset + length;
|
|
|
|
|
|
- materials.push( this.parseMaterial( lwoTree.materials[ name ], name, lwoTree.textures ) );
|
|
|
+ },
|
|
|
|
|
|
- } else if ( lwoTree.format === 'LWO2' ) {
|
|
|
+ parseSubNode( length ) {
|
|
|
|
|
|
- materials.push( this.parseMaterialLwo2( lwoTree.materials[ name ], name /*, lwoTree.textures */ ) );
|
|
|
+ // parse the NRNM CHUNK of the subnode FORM to get
|
|
|
+ // a meaningful name for the subNode
|
|
|
+ // some subnodes can be renamed, but Input and Surface cannot
|
|
|
|
|
|
- }
|
|
|
+ this.reader.skip( 8 ); // NRNM + length
|
|
|
+ var name = this.reader.getString();
|
|
|
|
|
|
- }
|
|
|
+ var node = {
|
|
|
+ name: name
|
|
|
+ };
|
|
|
+ this.currentForm = node;
|
|
|
+ this.currentNode = node;
|
|
|
|
|
|
- return materials;
|
|
|
+ this.currentFormEnd = this.reader.offset + length;
|
|
|
|
|
|
- },
|
|
|
|
|
|
- parseMaterial( materialData, name, textures ) {
|
|
|
+ },
|
|
|
|
|
|
- var params = {
|
|
|
- name: name,
|
|
|
- side: this.getSide( materialData.attributes ),
|
|
|
- flatShading: this.getSmooth( materialData.attributes ),
|
|
|
- };
|
|
|
+ // collect attributes from all nodes at the top level of a surface
|
|
|
+ parseConnections( length ) {
|
|
|
|
|
|
- var connections = this.parseConnections( materialData.connections, materialData.nodes );
|
|
|
+ this.currentFormEnd = this.reader.offset + length;
|
|
|
+ this.parentForm = this.currentForm;
|
|
|
|
|
|
- var maps = this.parseTextureNodes( connections.maps );
|
|
|
+ this.currentForm = this.currentSurface.connections;
|
|
|
|
|
|
- this.parseAttributeImageMaps( connections.attributes, textures, maps, materialData.maps );
|
|
|
+ },
|
|
|
|
|
|
- var attributes = this.parseAttributes( connections.attributes, maps );
|
|
|
+ // surface node attribute data, e.g. specular, roughness etc
|
|
|
+ parseEntryForm( length ) {
|
|
|
|
|
|
- this.parseEnvMap( connections, maps, attributes );
|
|
|
+ this.reader.skip( 8 ); // NAME + length
|
|
|
+ var name = this.reader.getString();
|
|
|
+ this.currentForm = this.currentNode.attributes;
|
|
|
|
|
|
- params = Object.assign( maps, params );
|
|
|
- params = Object.assign( params, attributes );
|
|
|
+ this.setupForm( name, length );
|
|
|
|
|
|
- var materialCtor = connections.attributes.Roughness ? MeshStandardMaterial : MeshPhongMaterial;
|
|
|
+ },
|
|
|
|
|
|
- return new materialCtor( params );
|
|
|
+ // parse values from material - doesn't match up to other LWO3 data types
|
|
|
+ // sub form of entry form
|
|
|
+ parseValueForm() {
|
|
|
|
|
|
- },
|
|
|
+ this.reader.skip( 8 ); // unknown + length
|
|
|
|
|
|
- parseMaterialLwo2( materialData, name /*, textures */ ) {
|
|
|
+ var valueType = this.reader.getString();
|
|
|
|
|
|
- var params = {
|
|
|
- name: name,
|
|
|
- side: this.getSide( materialData.attributes ),
|
|
|
- flatShading: this.getSmooth( materialData.attributes ),
|
|
|
- };
|
|
|
+ if ( valueType === 'double' ) {
|
|
|
|
|
|
- var attributes = this.parseAttributes( materialData.attributes, {} );
|
|
|
- params = Object.assign( params, attributes );
|
|
|
- return new MeshPhongMaterial( params );
|
|
|
+ this.currentForm.value = this.reader.getUint64();
|
|
|
|
|
|
- },
|
|
|
+ } else if ( valueType === 'int' ) {
|
|
|
|
|
|
- // Note: converting from left to right handed coords by switching x -> -x in vertices, and
|
|
|
- // then switching mat FrontSide -> BackSide
|
|
|
- // NB: this means that FrontSide and BackSide have been switched!
|
|
|
- getSide( attributes ) {
|
|
|
+ this.currentForm.value = this.reader.getUint32();
|
|
|
|
|
|
- if ( ! attributes.side ) return BackSide;
|
|
|
+ } else if ( valueType === 'vparam' ) {
|
|
|
|
|
|
- switch ( attributes.side ) {
|
|
|
+ this.reader.skip( 24 );
|
|
|
+ this.currentForm.value = this.reader.getFloat64();
|
|
|
|
|
|
- case 0:
|
|
|
- case 1:
|
|
|
- return BackSide;
|
|
|
- case 2: return FrontSide;
|
|
|
- case 3: return DoubleSide;
|
|
|
+ } else if ( valueType === 'vparam3' ) {
|
|
|
|
|
|
- }
|
|
|
+ this.reader.skip( 24 );
|
|
|
+ this.currentForm.value = this.reader.getFloat64Array( 3 );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- getSmooth( attributes ) {
|
|
|
+ },
|
|
|
|
|
|
- if ( ! attributes.smooth ) return true;
|
|
|
- return ! attributes.smooth;
|
|
|
+ // holds various data about texture node image state
|
|
|
+ // Data other thanmipMapLevel unknown
|
|
|
+ parseImageStateForm() {
|
|
|
|
|
|
- },
|
|
|
+ this.reader.skip( 8 ); // unknown
|
|
|
|
|
|
- parseConnections( connections, nodes ) {
|
|
|
+ this.currentForm.mipMapLevel = this.reader.getFloat32();
|
|
|
|
|
|
- var materialConnections = {
|
|
|
- maps: {}
|
|
|
- };
|
|
|
+ },
|
|
|
|
|
|
- var inputName = connections.inputName;
|
|
|
- var inputNodeName = connections.inputNodeName;
|
|
|
- var nodeName = connections.nodeName;
|
|
|
+ // LWO2 style image data node OR LWO3 textures defined at top level in editor (not as SURF node)
|
|
|
+ parseImageMap( length ) {
|
|
|
|
|
|
- var self = this;
|
|
|
- inputName.forEach( function ( name, index ) {
|
|
|
+ this.currentFormEnd = this.reader.offset + length;
|
|
|
+ this.parentForm = this.currentForm;
|
|
|
|
|
|
- if ( name === 'Material' ) {
|
|
|
+ if ( ! this.currentForm.maps ) this.currentForm.maps = [];
|
|
|
|
|
|
- var matNode = self.getNodeByRefName( inputNodeName[ index ], nodes );
|
|
|
- materialConnections.attributes = matNode.attributes;
|
|
|
- materialConnections.envMap = matNode.fileName;
|
|
|
- materialConnections.name = inputNodeName[ index ];
|
|
|
+ var map = {};
|
|
|
+ this.currentForm.maps.push( map );
|
|
|
+ this.currentForm = map;
|
|
|
|
|
|
- }
|
|
|
+ this.reader.skip( 10 ); // unknown, could be an issue if it contains a VX
|
|
|
|
|
|
- } );
|
|
|
+ },
|
|
|
|
|
|
- nodeName.forEach( function ( name, index ) {
|
|
|
+ parseTextureNodeAttribute( type ) {
|
|
|
|
|
|
- if ( name === materialConnections.name ) {
|
|
|
+ this.reader.skip( 28 ); // FORM + length + VPRM + unknown + Uint32 x2 + float32
|
|
|
|
|
|
- materialConnections.maps[ inputName[ index ] ] = self.getNodeByRefName( inputNodeName[ index ], nodes );
|
|
|
+ this.reader.skip( 20 ); // FORM + length + VPVL + float32 + Uint32
|
|
|
|
|
|
- }
|
|
|
+ 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;
|
|
|
|
|
|
- return materialConnections;
|
|
|
+ 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;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- getNodeByRefName( refName, nodes ) {
|
|
|
+ this.reader.skip( 2 ); // unknown
|
|
|
|
|
|
- for ( var name in nodes ) {
|
|
|
|
|
|
- if ( nodes[ name ].refName === refName ) return nodes[ name ];
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ // ENVL forms are currently ignored
|
|
|
+ parseEnvelope( length ) {
|
|
|
|
|
|
- },
|
|
|
+ this.reader.skip( length - 4 ); // skipping entirely for now
|
|
|
|
|
|
- parseTextureNodes( textureNodes ) {
|
|
|
+ },
|
|
|
|
|
|
- var maps = {};
|
|
|
+ ///
|
|
|
+ // CHUNK PARSING METHODS
|
|
|
+ ///
|
|
|
|
|
|
- for ( var name in textureNodes ) {
|
|
|
+ // clips can either be defined inside a surface node, or at the top
|
|
|
+ // level and they have a different format in each case
|
|
|
+ parseClip( length ) {
|
|
|
|
|
|
- var node = textureNodes[ name ];
|
|
|
- var path = node.fileName;
|
|
|
+ var tag = this.reader.getIDTag();
|
|
|
|
|
|
- if ( ! path ) return;
|
|
|
+ // inside surface node
|
|
|
+ if ( tag === 'FORM' ) {
|
|
|
|
|
|
- var texture = this.loadTexture( path );
|
|
|
+ this.reader.skip( 16 );
|
|
|
|
|
|
- if ( node.widthWrappingMode !== undefined ) texture.wrapS = this.getWrappingType( node.widthWrappingMode );
|
|
|
- if ( node.heightWrappingMode !== undefined ) texture.wrapT = this.getWrappingType( node.heightWrappingMode );
|
|
|
+ this.currentNode.fileName = this.reader.getString();
|
|
|
|
|
|
- switch ( name ) {
|
|
|
+ return;
|
|
|
|
|
|
- case 'Color':
|
|
|
- maps.map = texture;
|
|
|
- break;
|
|
|
- case 'Roughness':
|
|
|
- maps.roughnessMap = texture;
|
|
|
- maps.roughness = 0.5;
|
|
|
- break;
|
|
|
- case 'Specular':
|
|
|
- maps.specularMap = texture;
|
|
|
- maps.specular = 0xffffff;
|
|
|
- break;
|
|
|
- case 'Luminous':
|
|
|
- maps.emissiveMap = texture;
|
|
|
- maps.emissive = 0x808080;
|
|
|
- break;
|
|
|
- case 'Metallic':
|
|
|
- maps.metalnessMap = texture;
|
|
|
- maps.metalness = 0.5;
|
|
|
- break;
|
|
|
- case 'Transparency':
|
|
|
- case 'Alpha':
|
|
|
- maps.alphaMap = texture;
|
|
|
- maps.transparent = true;
|
|
|
- break;
|
|
|
- case 'Normal':
|
|
|
- maps.normalMap = texture;
|
|
|
- if ( node.amplitude !== undefined ) maps.normalScale = new Vector2( node.amplitude, node.amplitude );
|
|
|
- break;
|
|
|
- case 'Bump':
|
|
|
- maps.bumpMap = texture;
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ // otherwise top level
|
|
|
+ this.reader.setOffset( this.reader.offset - 4 );
|
|
|
+
|
|
|
+ this.currentFormEnd = this.reader.offset + length;
|
|
|
+ this.parentForm = this.currentForm;
|
|
|
+
|
|
|
+ this.reader.skip( 8 ); // unknown
|
|
|
+
|
|
|
+ var texture = {
|
|
|
+ index: this.reader.getUint32()
|
|
|
+ };
|
|
|
+ this.tree.textures.push( texture );
|
|
|
+ this.currentForm = texture;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ parseClipLwo2( length ) {
|
|
|
+
|
|
|
+ var texture = {
|
|
|
+ index: this.reader.getUint32(),
|
|
|
+ fileName: ""
|
|
|
+ };
|
|
|
+
|
|
|
+ // seach STIL block
|
|
|
+ while ( true ) {
|
|
|
+
|
|
|
+ var tag = this.reader.getIDTag();
|
|
|
+ var n_length = this.reader.getUint16();
|
|
|
+ if ( tag === 'STIL' ) {
|
|
|
+
|
|
|
+ texture.fileName = this.reader.getString();
|
|
|
+ break;
|
|
|
|
|
|
}
|
|
|
|
|
|
- // LWO BSDF materials can have both spec and rough, but this is not valid in three
|
|
|
- if ( maps.roughnessMap && maps.specularMap ) delete maps.specularMap;
|
|
|
-
|
|
|
- return maps;
|
|
|
-
|
|
|
- },
|
|
|
-
|
|
|
- // maps can also be defined on individual material attributes, parse those here
|
|
|
- // This occurs on Standard (Phong) surfaces
|
|
|
- parseAttributeImageMaps( attributes, textures, maps ) {
|
|
|
-
|
|
|
- for ( var name in attributes ) {
|
|
|
-
|
|
|
- var attribute = attributes[ name ];
|
|
|
-
|
|
|
- if ( attribute.maps ) {
|
|
|
-
|
|
|
- var mapData = attribute.maps[ 0 ];
|
|
|
-
|
|
|
- var path = this.getTexturePathByIndex( mapData.imageIndex, textures );
|
|
|
- if ( ! path ) return;
|
|
|
-
|
|
|
- var texture = this.loadTexture( path );
|
|
|
-
|
|
|
- if ( mapData.wrap !== undefined ) texture.wrapS = this.getWrappingType( mapData.wrap.w );
|
|
|
- if ( mapData.wrap !== undefined ) texture.wrapT = this.getWrappingType( mapData.wrap.h );
|
|
|
-
|
|
|
- switch ( name ) {
|
|
|
-
|
|
|
- case 'Color':
|
|
|
- maps.map = texture;
|
|
|
- break;
|
|
|
- case 'Diffuse':
|
|
|
- maps.aoMap = texture;
|
|
|
- break;
|
|
|
- case 'Roughness':
|
|
|
- maps.roughnessMap = texture;
|
|
|
- maps.roughness = 1;
|
|
|
- break;
|
|
|
- case 'Specular':
|
|
|
- maps.specularMap = texture;
|
|
|
- maps.specular = 0xffffff;
|
|
|
- break;
|
|
|
- case 'Luminosity':
|
|
|
- maps.emissiveMap = texture;
|
|
|
- maps.emissive = 0x808080;
|
|
|
- break;
|
|
|
- case 'Metallic':
|
|
|
- maps.metalnessMap = texture;
|
|
|
- maps.metalness = 1;
|
|
|
- break;
|
|
|
- case 'Transparency':
|
|
|
- case 'Alpha':
|
|
|
- maps.alphaMap = texture;
|
|
|
- maps.transparent = true;
|
|
|
- break;
|
|
|
- case 'Normal':
|
|
|
- maps.normalMap = texture;
|
|
|
- break;
|
|
|
- case 'Bump':
|
|
|
- maps.bumpMap = texture;
|
|
|
- break;
|
|
|
-
|
|
|
- }
|
|
|
+ if ( n_length >= length ) {
|
|
|
|
|
|
- }
|
|
|
+ break;
|
|
|
|
|
|
}
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- parseAttributes( attributes, maps ) {
|
|
|
+ this.tree.textures.push( texture );
|
|
|
+ this.currentForm = texture;
|
|
|
|
|
|
- var params = {};
|
|
|
+ },
|
|
|
|
|
|
- // don't use color data if color map is present
|
|
|
- if ( attributes.Color && ! maps.map ) {
|
|
|
+ parseImage() {
|
|
|
|
|
|
- params.color = new Color().fromArray( attributes.Color.value );
|
|
|
+ this.reader.skip( 8 ); // unknown
|
|
|
+ this.currentForm.fileName = this.reader.getString();
|
|
|
|
|
|
- } else params.color = new Color();
|
|
|
+ },
|
|
|
|
|
|
+ parseXVAL( type, length ) {
|
|
|
|
|
|
- if ( attributes.Transparency && attributes.Transparency.value !== 0 ) {
|
|
|
+ var endOffset = this.reader.offset + length - 4;
|
|
|
+ this.reader.skip( 8 );
|
|
|
|
|
|
- params.opacity = 1 - attributes.Transparency.value;
|
|
|
- params.transparent = true;
|
|
|
+ this.currentForm[ type ] = this.reader.getFloat32();
|
|
|
|
|
|
- }
|
|
|
+ this.reader.setOffset( endOffset ); // set end offset directly to skip optional envelope
|
|
|
|
|
|
- if ( attributes[ 'Bump Height' ] ) params.bumpScale = attributes[ 'Bump Height' ].value * 0.1;
|
|
|
+ },
|
|
|
|
|
|
- if ( attributes[ 'Refraction Index' ] ) params.refractionRatio = 1 / attributes[ 'Refraction Index' ].value;
|
|
|
+ parseXVAL3( type, length ) {
|
|
|
|
|
|
- this.parseStandardAttributes( params, attributes, maps );
|
|
|
- this.parsePhongAttributes( params, attributes, maps );
|
|
|
+ var endOffset = this.reader.offset + length - 4;
|
|
|
+ this.reader.skip( 8 );
|
|
|
|
|
|
- return params;
|
|
|
+ this.currentForm[ type ] = {
|
|
|
+ x: this.reader.getFloat32(),
|
|
|
+ y: this.reader.getFloat32(),
|
|
|
+ z: this.reader.getFloat32(),
|
|
|
+ };
|
|
|
|
|
|
- },
|
|
|
+ this.reader.setOffset( endOffset );
|
|
|
|
|
|
- parseStandardAttributes( params, attributes, maps ) {
|
|
|
+ },
|
|
|
|
|
|
- if ( attributes.Luminous && attributes.Luminous.value !== 0 && attributes[ 'Luminous Color' ] ) {
|
|
|
+ // Tags associated with an object
|
|
|
+ // OTAG { type[ID4], tag-string[S0] }
|
|
|
+ parseObjectTag() {
|
|
|
|
|
|
- var emissiveColor = attributes[ 'Luminous Color' ].value.map( function ( val ) {
|
|
|
+ if ( ! this.tree.objectTags ) this.tree.objectTags = {};
|
|
|
|
|
|
- return val * attributes.Luminous.value;
|
|
|
+ this.tree.objectTags[ this.reader.getIDTag() ] = {
|
|
|
+ tagString: this.reader.getString()
|
|
|
+ };
|
|
|
|
|
|
- } );
|
|
|
+ },
|
|
|
|
|
|
- params.emissive = new Color().fromArray( emissiveColor );
|
|
|
+ // Signals the start of a new layer. All the data chunks which follow will be included in this layer until another layer chunk is encountered.
|
|
|
+ // LAYR: number[U2], flags[U2], pivot[VEC12], name[S0], parent[U2]
|
|
|
+ parseLayer( length ) {
|
|
|
|
|
|
- }
|
|
|
- if ( attributes.Roughness && ! maps.roughnessMap ) params.roughness = attributes.Roughness.value;
|
|
|
- if ( attributes.Metallic && ! maps.metalnessMap ) params.metalness = attributes.Metallic.value;
|
|
|
+ var layer = {
|
|
|
+ number: this.reader.getUint16(),
|
|
|
+ flags: this.reader.getUint16(), // If the least significant bit of flags is set, the layer is hidden.
|
|
|
+ pivot: this.reader.getFloat32Array( 3 ), // Note: this seems to be superflous, as the geometry is translated when pivot is present
|
|
|
+ name: this.reader.getString(),
|
|
|
+ };
|
|
|
|
|
|
- },
|
|
|
+ this.tree.layers.push( layer );
|
|
|
+ this.currentLayer = layer;
|
|
|
|
|
|
- parsePhongAttributes( params, attributes, maps ) {
|
|
|
+ var parsedLength = 16 + stringOffset( this.currentLayer.name ); // index ( 2 ) + flags( 2 ) + pivot( 12 ) + stringlength
|
|
|
|
|
|
- if ( attributes.Diffuse ) params.color.multiplyScalar( attributes.Diffuse.value );
|
|
|
+ // if we have not reached then end of the layer block, there must be a parent defined
|
|
|
+ this.currentLayer.parent = ( parsedLength < length ) ? this.reader.getUint16() : - 1; // omitted or -1 for no parent
|
|
|
|
|
|
- if ( attributes.Reflection ) {
|
|
|
+ },
|
|
|
|
|
|
- params.reflectivity = attributes.Reflection.value;
|
|
|
- params.combine = AddOperation;
|
|
|
+ // VEC12 * ( F4 + F4 + F4 ) array of x,y,z vectors
|
|
|
+ // Converting from left to right handed coordinate system:
|
|
|
+ // x -> -x and switch material FrontSide -> BackSide
|
|
|
+ parsePoints( length ) {
|
|
|
|
|
|
- }
|
|
|
+ this.currentPoints = [];
|
|
|
+ for ( var i = 0; i < length / 4; i += 3 ) {
|
|
|
|
|
|
- if ( attributes.Luminosity && ! maps.emissiveMap ) params.emissive = new Color().setScalar( attributes.Luminosity.value );
|
|
|
+ // z -> -z to match three.js right handed coords
|
|
|
+ this.currentPoints.push( this.reader.getFloat32(), this.reader.getFloat32(), - this.reader.getFloat32() );
|
|
|
|
|
|
- if ( attributes.Glossiness !== undefined ) params.shininess = 5 + Math.pow( attributes.Glossiness.value * 7, 6 );
|
|
|
+ }
|
|
|
|
|
|
- // parse specular if there is no roughness - we will interpret the material as 'Phong' in this case
|
|
|
- if ( ! attributes.Roughness && attributes.Specular && ! maps.specularMap ) params.specular = new Color().setScalar( attributes.Specular.value * 1.5 );
|
|
|
+ },
|
|
|
|
|
|
- },
|
|
|
+ // parse VMAP or VMAD
|
|
|
+ // Associates a set of floating-point vectors with a set of points.
|
|
|
+ // VMAP: { type[ID4], dimension[U2], name[S0], ( vert[VX], value[F4] # dimension ) * }
|
|
|
|
|
|
- parseEnvMap( connections, maps, attributes ) {
|
|
|
+ // VMAD Associates a set of floating-point vectors with the vertices of specific polygons.
|
|
|
+ // Similar to VMAP UVs, but associates with polygon vertices rather than points
|
|
|
+ // to solve to problem of UV seams: VMAD chunks are paired with VMAPs of the same name,
|
|
|
+ // if they exist. The vector values in the VMAD will then replace those in the
|
|
|
+ // corresponding VMAP, but only for calculations involving the specified polygons.
|
|
|
+ // VMAD { type[ID4], dimension[U2], name[S0], ( vert[VX], poly[VX], value[F4] # dimension ) * }
|
|
|
+ parseVertexMapping( length, discontinuous ) {
|
|
|
|
|
|
- if ( connections.envMap ) {
|
|
|
+ var finalOffset = this.reader.offset + length;
|
|
|
|
|
|
- var envMap = this.loadTexture( connections.envMap );
|
|
|
+ var channelName = this.reader.getString();
|
|
|
|
|
|
- if ( attributes.transparent && attributes.opacity < 0.999 ) {
|
|
|
+ if ( this.reader.offset === finalOffset ) {
|
|
|
|
|
|
- envMap.mapping = EquirectangularRefractionMapping;
|
|
|
+ // then we are in a texture node and the VMAP chunk is just a reference to a UV channel name
|
|
|
+ this.currentForm.UVChannel = channelName;
|
|
|
+ return;
|
|
|
|
|
|
- // Reflectivity and refraction mapping don't work well together in Phong materials
|
|
|
- if ( attributes.reflectivity !== undefined ) {
|
|
|
+ }
|
|
|
|
|
|
- delete attributes.reflectivity;
|
|
|
- delete attributes.combine;
|
|
|
+ // otherwise reset to initial length and parse normal VMAP CHUNK
|
|
|
+ this.reader.setOffset( this.reader.offset - stringOffset( channelName ) );
|
|
|
+
|
|
|
+ var type = this.reader.getIDTag();
|
|
|
+
|
|
|
+ this.reader.getUint16(); // dimension
|
|
|
+ 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 ); // can't be discontinuous
|
|
|
+ break;
|
|
|
+ // unsupported VMAPs
|
|
|
+ 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 );
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- if ( attributes.metalness !== undefined ) {
|
|
|
+ },
|
|
|
|
|
|
- delete attributes.metalness;
|
|
|
+ parseUVMapping( name, finalOffset, discontinuous ) {
|
|
|
|
|
|
- }
|
|
|
+ var uvIndices = [];
|
|
|
+ var polyIndices = [];
|
|
|
+ var uvs = [];
|
|
|
|
|
|
- } else envMap.mapping = EquirectangularReflectionMapping;
|
|
|
+ while ( this.reader.offset < finalOffset ) {
|
|
|
|
|
|
- maps.envMap = envMap;
|
|
|
+ uvIndices.push( this.reader.getVariableLengthIndex() );
|
|
|
|
|
|
- }
|
|
|
+ if ( discontinuous ) polyIndices.push( this.reader.getVariableLengthIndex() );
|
|
|
|
|
|
- },
|
|
|
+ uvs.push( this.reader.getFloat32(), this.reader.getFloat32() );
|
|
|
|
|
|
- // get texture defined at top level by its index
|
|
|
- getTexturePathByIndex( index ) {
|
|
|
+ }
|
|
|
|
|
|
- var fileName = '';
|
|
|
+ if ( discontinuous ) {
|
|
|
|
|
|
- if ( ! lwoTree.textures ) return fileName;
|
|
|
+ if ( ! this.currentLayer.discontinuousUVs ) this.currentLayer.discontinuousUVs = {};
|
|
|
|
|
|
- lwoTree.textures.forEach( function ( texture ) {
|
|
|
+ this.currentLayer.discontinuousUVs[ name ] = {
|
|
|
+ uvIndices: uvIndices,
|
|
|
+ polyIndices: polyIndices,
|
|
|
+ uvs: uvs,
|
|
|
+ };
|
|
|
|
|
|
- if ( texture.index === index ) fileName = texture.fileName;
|
|
|
+ } else {
|
|
|
|
|
|
- } );
|
|
|
+ if ( ! this.currentLayer.uvs ) this.currentLayer.uvs = {};
|
|
|
|
|
|
- return fileName;
|
|
|
+ this.currentLayer.uvs[ name ] = {
|
|
|
+ uvIndices: uvIndices,
|
|
|
+ uvs: uvs,
|
|
|
+ };
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- loadTexture( path ) {
|
|
|
+ },
|
|
|
|
|
|
- if ( ! path ) return null;
|
|
|
+ parseMorphTargets( name, finalOffset, type ) {
|
|
|
|
|
|
- return this.textureLoader.load( this.cleanPath( path ) );
|
|
|
+ var indices = [];
|
|
|
+ var points = [];
|
|
|
|
|
|
- },
|
|
|
+ type = ( type === 'MORF' ) ? 'relative' : 'absolute';
|
|
|
|
|
|
- // Lightwave expects textures to be in folder called Images relative
|
|
|
- // to the model
|
|
|
- // Otherwise, the full absolute path is stored: D://some_directory/textures/bumpMap.png
|
|
|
- // In this case, we'll strip out everything and load 'bumpMap.png' from the same directory as the model
|
|
|
- cleanPath( path ) {
|
|
|
+ while ( this.reader.offset < finalOffset ) {
|
|
|
|
|
|
- if ( path.toLowerCase().indexOf( 'images' ) === 0 ) return './' + path;
|
|
|
- return path.split( '/' ).pop().split( '\\' ).pop();
|
|
|
+ indices.push( this.reader.getVariableLengthIndex() );
|
|
|
+ // z -> -z to match three.js right handed coords
|
|
|
+ points.push( this.reader.getFloat32(), this.reader.getFloat32(), - this.reader.getFloat32() );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- // 0 = Reset, 1 = Repeat, 2 = Mirror, 3 = Edge
|
|
|
- getWrappingType( num ) {
|
|
|
+ if ( ! this.currentLayer.morphTargets ) this.currentLayer.morphTargets = {};
|
|
|
|
|
|
- switch ( num ) {
|
|
|
+ this.currentLayer.morphTargets[ name ] = {
|
|
|
+ indices: indices,
|
|
|
+ points: points,
|
|
|
+ type: type,
|
|
|
+ };
|
|
|
|
|
|
- case 0:
|
|
|
- console.warn( 'LWOLoader: "Reset" texture wrapping type is not supported in three.js' );
|
|
|
- return ClampToEdgeWrapping;
|
|
|
- case 1: return RepeatWrapping;
|
|
|
- case 2: return MirroredRepeatWrapping;
|
|
|
- case 3: return ClampToEdgeWrapping;
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ // A list of polygons for the current layer.
|
|
|
+ // POLS { type[ID4], ( numvert+flags[U2], vert[VX] # numvert ) * }
|
|
|
+ parsePolygonList( length ) {
|
|
|
|
|
|
- },
|
|
|
+ var finalOffset = this.reader.offset + length;
|
|
|
+ var type = this.reader.getIDTag();
|
|
|
|
|
|
- getType( nodeData ) {
|
|
|
+ var indices = [];
|
|
|
|
|
|
- if ( nodeData.roughness ) return 'Standard';
|
|
|
- return 'Phong';
|
|
|
+ // hold a list of polygon sizes, to be split up later
|
|
|
+ var polygonDimensions = [];
|
|
|
|
|
|
- },
|
|
|
+ while ( this.reader.offset < finalOffset ) {
|
|
|
|
|
|
- };
|
|
|
+ var numverts = this.reader.getUint16();
|
|
|
|
|
|
- function GeometryParser() {}
|
|
|
+ //var flags = numverts & 64512; // 6 high order bits are flags - ignoring for now
|
|
|
+ numverts = numverts & 1023; // remaining ten low order bits are vertex num
|
|
|
+ polygonDimensions.push( numverts );
|
|
|
|
|
|
- GeometryParser.prototype = {
|
|
|
+ for ( var j = 0; j < numverts; j ++ ) indices.push( this.reader.getVariableLengthIndex() );
|
|
|
|
|
|
- constructor: GeometryParser,
|
|
|
+ }
|
|
|
|
|
|
- parse( geoData, layer ) {
|
|
|
+ var geometryData = {
|
|
|
+ type: type,
|
|
|
+ vertexIndices: indices,
|
|
|
+ polygonDimensions: polygonDimensions,
|
|
|
+ points: this.currentPoints
|
|
|
+ };
|
|
|
|
|
|
- var geometry = new BufferGeometry();
|
|
|
+ // Note: assuming that all polys will be lines or points if the first is
|
|
|
+ if ( polygonDimensions[ 0 ] === 1 ) geometryData.type = 'points';
|
|
|
+ else if ( polygonDimensions[ 0 ] === 2 ) geometryData.type = 'lines';
|
|
|
|
|
|
- geometry.addAttribute( 'position', new Float32BufferAttribute( geoData.points, 3 ) );
|
|
|
+ this.currentLayer.geometry = geometryData;
|
|
|
|
|
|
- var indices = this.splitIndices( geoData.vertexIndices, geoData.polygonDimensions );
|
|
|
- geometry.setIndex( indices );
|
|
|
+ },
|
|
|
|
|
|
- this.parseGroups( geometry, geoData );
|
|
|
+ // Lists the tag strings that can be associated with polygons by the PTAG chunk.
|
|
|
+ // TAGS { tag-string[S0] * }
|
|
|
+ parseTagStrings( length ) {
|
|
|
|
|
|
- geometry.computeVertexNormals();
|
|
|
+ this.tree.tags = this.reader.getStringArray( length );
|
|
|
|
|
|
- this.parseUVs( geometry, layer, indices );
|
|
|
- this.parseMorphTargets( geometry, layer, indices );
|
|
|
+ },
|
|
|
|
|
|
- // TODO: z may need to be reversed to account for coordinate system change
|
|
|
- geometry.translate( - layer.pivot[ 0 ], - layer.pivot[ 1 ], - layer.pivot[ 2 ] );
|
|
|
+ // Associates tags of a given type with polygons in the most recent POLS chunk.
|
|
|
+ // PTAG { type[ID4], ( poly[VX], tag[U2] ) * }
|
|
|
+ parsePolygonTagMapping( length ) {
|
|
|
|
|
|
- // var userData = geometry.userData;
|
|
|
- // geometry = geometry.toNonIndexed()
|
|
|
- // geometry.userData = userData;
|
|
|
+ var finalOffset = this.reader.offset + length;
|
|
|
+ var type = this.reader.getIDTag();
|
|
|
+ if ( type === 'SURF' ) this.parseMaterialIndices( finalOffset );
|
|
|
+ else { //PART, SMGP, COLR not supported
|
|
|
|
|
|
- return geometry;
|
|
|
+ this.reader.skip( length - 4 );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- // split quads into tris
|
|
|
- splitIndices( indices, polygonDimensions ) {
|
|
|
+ },
|
|
|
|
|
|
- var remappedIndices = [];
|
|
|
+ parseMaterialIndices( finalOffset ) {
|
|
|
|
|
|
- var i = 0;
|
|
|
- polygonDimensions.forEach( function ( dim ) {
|
|
|
+ // array holds polygon index followed by material index
|
|
|
+ this.currentLayer.geometry.materialIndices = [];
|
|
|
|
|
|
- if ( dim < 4 ) {
|
|
|
+ while ( this.reader.offset < finalOffset ) {
|
|
|
|
|
|
- for ( var k = 0; k < dim; k ++ ) remappedIndices.push( indices[ i + k ] );
|
|
|
+ var polygonIndex = this.reader.getVariableLengthIndex();
|
|
|
+ var materialIndex = this.reader.getUint16();
|
|
|
|
|
|
- } else if ( dim === 4 ) {
|
|
|
+ this.currentLayer.geometry.materialIndices.push( polygonIndex, materialIndex );
|
|
|
|
|
|
- remappedIndices.push(
|
|
|
- indices[ i ],
|
|
|
- indices[ i + 1 ],
|
|
|
- indices[ i + 2 ],
|
|
|
+ }
|
|
|
|
|
|
- indices[ i ],
|
|
|
- indices[ i + 2 ],
|
|
|
- indices[ i + 3 ]
|
|
|
+ },
|
|
|
|
|
|
- );
|
|
|
+ parseUnknownCHUNK( blockID, length ) {
|
|
|
|
|
|
- } else if ( dim > 4 ) {
|
|
|
+ console.warn( 'LWOLoader: unknown chunk type: ' + blockID + ' length: ' + length );
|
|
|
|
|
|
- for ( var k = 1; k < dim - 1; k ++ ) {
|
|
|
+ // print the chunk plus some bytes padding either side
|
|
|
+ // printBuffer( this.reader.dv.buffer, this.reader.offset - 20, length + 40 );
|
|
|
|
|
|
- remappedIndices.push( indices[ i ], indices[ i + k ], indices[ i + k + 1 ] );
|
|
|
+ var data = this.reader.getString( length );
|
|
|
|
|
|
- }
|
|
|
+ this.currentForm[ blockID ] = data;
|
|
|
|
|
|
- console.warn( 'LWOLoader: polygons with greater than 4 sides are not supported' );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+};
|
|
|
|
|
|
- i += dim;
|
|
|
+function DataViewReader( buffer ) {
|
|
|
|
|
|
- } );
|
|
|
+ this.dv = new DataView( buffer );
|
|
|
+ this.offset = 0;
|
|
|
|
|
|
- return remappedIndices;
|
|
|
+}
|
|
|
|
|
|
- },
|
|
|
+DataViewReader.prototype = {
|
|
|
|
|
|
- // NOTE: currently ignoring poly indices and assuming that they are intelligently ordered
|
|
|
- parseGroups( geometry, geoData ) {
|
|
|
+ constructor: DataViewReader,
|
|
|
|
|
|
- var tags = lwoTree.tags;
|
|
|
- var matNames = [];
|
|
|
+ size: function () {
|
|
|
|
|
|
- var elemSize = 3;
|
|
|
- if ( geoData.type === 'lines' ) elemSize = 2;
|
|
|
- if ( geoData.type === 'points' ) elemSize = 1;
|
|
|
+ return this.dv.buffer.byteLength;
|
|
|
|
|
|
- var remappedIndices = this.splitMaterialIndices( geoData.polygonDimensions, geoData.materialIndices );
|
|
|
+ },
|
|
|
|
|
|
- var indexNum = 0; // create new indices in numerical order
|
|
|
- var indexPairs = {}; // original indices mapped to numerical indices
|
|
|
+ setOffset( offset ) {
|
|
|
|
|
|
- var prevMaterialIndex;
|
|
|
+ if ( offset > 0 && offset < this.dv.buffer.byteLength ) {
|
|
|
|
|
|
- var prevStart = 0;
|
|
|
- var currentCount = 0;
|
|
|
+ this.offset = offset;
|
|
|
|
|
|
- for ( var i = 0; i < remappedIndices.length; i += 2 ) {
|
|
|
+ } else {
|
|
|
|
|
|
- var materialIndex = remappedIndices[ i + 1 ];
|
|
|
+ console.error( 'LWOLoader: invalid buffer offset' );
|
|
|
|
|
|
- if ( i === 0 ) matNames[ indexNum ] = tags[ materialIndex ];
|
|
|
+ }
|
|
|
|
|
|
- if ( prevMaterialIndex === undefined ) prevMaterialIndex = materialIndex;
|
|
|
+ },
|
|
|
|
|
|
- if ( materialIndex !== prevMaterialIndex ) {
|
|
|
+ endOfFile: function () {
|
|
|
|
|
|
- var currentIndex;
|
|
|
- if ( indexPairs[ tags[ prevMaterialIndex ] ] ) {
|
|
|
+ if ( this.offset >= this.size() ) return true;
|
|
|
+ return false;
|
|
|
|
|
|
- currentIndex = indexPairs[ tags[ prevMaterialIndex ] ];
|
|
|
+ },
|
|
|
|
|
|
- } else {
|
|
|
+ skip: function ( length ) {
|
|
|
|
|
|
- currentIndex = indexNum;
|
|
|
- indexPairs[ tags[ prevMaterialIndex ] ] = indexNum;
|
|
|
- matNames[ indexNum ] = tags[ prevMaterialIndex ];
|
|
|
- indexNum ++;
|
|
|
+ this.offset += length;
|
|
|
|
|
|
- }
|
|
|
+ },
|
|
|
|
|
|
- geometry.addGroup( prevStart, currentCount, currentIndex );
|
|
|
+ getUint8: function () {
|
|
|
|
|
|
- prevStart += currentCount;
|
|
|
+ var value = this.dv.getUint8( this.offset );
|
|
|
+ this.offset += 1;
|
|
|
+ return value;
|
|
|
|
|
|
- prevMaterialIndex = materialIndex;
|
|
|
- currentCount = 0;
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ getUint16: function () {
|
|
|
|
|
|
- currentCount += elemSize;
|
|
|
+ var value = this.dv.getUint16( this.offset );
|
|
|
+ this.offset += 2;
|
|
|
+ return value;
|
|
|
|
|
|
- }
|
|
|
+ },
|
|
|
|
|
|
- // the loop above doesn't add the last group, do that here.
|
|
|
- if ( geometry.groups.length > 0 ) {
|
|
|
+ getInt32: function () {
|
|
|
|
|
|
- var currentIndex;
|
|
|
- if ( indexPairs[ tags[ materialIndex ] ] ) {
|
|
|
+ var value = this.dv.getInt32( this.offset, false );
|
|
|
+ this.offset += 4;
|
|
|
+ return value;
|
|
|
|
|
|
- currentIndex = indexPairs[ tags[ materialIndex ] ];
|
|
|
+ },
|
|
|
|
|
|
- } else {
|
|
|
+ getUint32: function () {
|
|
|
|
|
|
- currentIndex = indexNum;
|
|
|
- indexPairs[ tags[ materialIndex ] ] = indexNum;
|
|
|
- matNames[ indexNum ] = tags[ materialIndex ];
|
|
|
+ var value = this.dv.getUint32( this.offset, false );
|
|
|
+ this.offset += 4;
|
|
|
+ return value;
|
|
|
|
|
|
- }
|
|
|
+ },
|
|
|
|
|
|
- geometry.addGroup( prevStart, currentCount, currentIndex );
|
|
|
+ getUint64: function () {
|
|
|
|
|
|
- }
|
|
|
+ var low, high;
|
|
|
|
|
|
- // Mat names from TAGS chunk, used to build up an array of materials for this geometry
|
|
|
- geometry.userData.matNames = matNames;
|
|
|
+ high = this.getUint32();
|
|
|
+ low = this.getUint32();
|
|
|
+ return high * 0x100000000 + low;
|
|
|
|
|
|
- },
|
|
|
+ },
|
|
|
|
|
|
- splitMaterialIndices( polygonDimensions, indices ) {
|
|
|
+ getFloat32: function () {
|
|
|
|
|
|
- var remappedIndices = [];
|
|
|
+ var value = this.dv.getFloat32( this.offset, false );
|
|
|
+ this.offset += 4;
|
|
|
+ return value;
|
|
|
|
|
|
- polygonDimensions.forEach( function ( dim, i ) {
|
|
|
+ },
|
|
|
|
|
|
- if ( dim <= 3 ) {
|
|
|
+ getFloat32Array: function ( size ) {
|
|
|
|
|
|
- remappedIndices.push( indices[ i * 2 ], indices[ i * 2 + 1 ] );
|
|
|
+ var a = [];
|
|
|
|
|
|
- } else if ( dim === 4 ) {
|
|
|
+ for ( var i = 0; i < size; i ++ ) {
|
|
|
|
|
|
- remappedIndices.push( indices[ i * 2 ], indices[ i * 2 + 1 ], indices[ i * 2 ], indices[ i * 2 + 1 ] );
|
|
|
+ a.push( this.getFloat32() );
|
|
|
|
|
|
- } else {
|
|
|
+ }
|
|
|
|
|
|
- // ignore > 4 for now
|
|
|
- for ( var k = 0; k < dim - 2; k ++ ) {
|
|
|
+ return a;
|
|
|
|
|
|
- remappedIndices.push( indices[ i * 2 ], indices[ i * 2 + 1 ] );
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ getFloat64: function () {
|
|
|
|
|
|
- }
|
|
|
+ var value = this.dv.getFloat64( this.offset, this.littleEndian );
|
|
|
+ this.offset += 8;
|
|
|
+ return value;
|
|
|
|
|
|
- } );
|
|
|
+ },
|
|
|
|
|
|
- return remappedIndices;
|
|
|
+ getFloat64Array: function ( size ) {
|
|
|
|
|
|
- },
|
|
|
+ var a = [];
|
|
|
|
|
|
- // UV maps:
|
|
|
- // 1: are defined via index into an array of points, not into a geometry
|
|
|
- // - the geometry is also defined by an index into this array, but the indexes may not match
|
|
|
- // 2: there can be any number of UV maps for a single geometry. Here these are combined,
|
|
|
- // with preference given to the first map encountered
|
|
|
- // 3: UV maps can be partial - that is, defined for only a part of the geometry
|
|
|
- // 4: UV maps can be VMAP or VMAD (discontinuous, to allow for seams). In practice, most
|
|
|
- // UV maps are defined as partially VMAP and partially VMAD
|
|
|
- // VMADs are currently not supported
|
|
|
- parseUVs( geometry, layer ) {
|
|
|
+ for ( var i = 0; i < size; i ++ ) {
|
|
|
|
|
|
- // start by creating a UV map set to zero for the whole geometry
|
|
|
- var remappedUVs = Array.from( Array( geometry.attributes.position.count * 2 ), function () {
|
|
|
+ a.push( this.getFloat64() );
|
|
|
|
|
|
- return 0;
|
|
|
+ }
|
|
|
|
|
|
- } );
|
|
|
+ return a;
|
|
|
|
|
|
- for ( var name in layer.uvs ) {
|
|
|
+ },
|
|
|
|
|
|
- var uvs = layer.uvs[ name ].uvs;
|
|
|
- var uvIndices = layer.uvs[ name ].uvIndices;
|
|
|
+ // get variable-length index data type
|
|
|
+ // VX ::= index[U2] | (index + 0xFF000000)[U4]
|
|
|
+ // If the index value is less than 65,280 (0xFF00),then VX === U2
|
|
|
+ // otherwise VX === U4 with bits 24-31 set
|
|
|
+ // When reading an index, if the first byte encountered is 255 (0xFF), then
|
|
|
+ // the four-byte form is being used and the first byte should be discarded or masked out.
|
|
|
+ getVariableLengthIndex() {
|
|
|
|
|
|
- uvIndices.forEach( function ( i, j ) {
|
|
|
+ var firstByte = this.getUint8();
|
|
|
|
|
|
- remappedUVs[ i * 2 ] = uvs[ j * 2 ];
|
|
|
- remappedUVs[ i * 2 + 1 ] = uvs[ j * 2 + 1 ];
|
|
|
+ if ( firstByte === 255 ) {
|
|
|
|
|
|
- } );
|
|
|
+ return this.getUint8() * 65536 + this.getUint8() * 256 + this.getUint8();
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- geometry.addAttribute( 'uv', new Float32BufferAttribute( remappedUVs, 2 ) );
|
|
|
+ return firstByte * 256 + this.getUint8();
|
|
|
|
|
|
- },
|
|
|
+ },
|
|
|
|
|
|
- parseMorphTargets( geometry, layer ) {
|
|
|
+ // An ID tag is a sequence of 4 bytes containing 7-bit ASCII values
|
|
|
+ getIDTag() {
|
|
|
|
|
|
- var num = 0;
|
|
|
- for ( var name in layer.morphTargets ) {
|
|
|
+ return this.getString( 4 );
|
|
|
|
|
|
- var remappedPoints = geometry.attributes.position.array.slice();
|
|
|
+ },
|
|
|
|
|
|
- if ( ! geometry.morphAttributes.position ) geometry.morphAttributes.position = [];
|
|
|
+ getString: function ( size ) {
|
|
|
|
|
|
- var morphPoints = layer.morphTargets[ name ].points;
|
|
|
- var morphIndices = layer.morphTargets[ name ].indices;
|
|
|
- var type = layer.morphTargets[ name ].type;
|
|
|
+ if ( size === 0 ) return;
|
|
|
|
|
|
- morphIndices.forEach( function ( i, j ) {
|
|
|
+ // note: safari 9 doesn't support Uint8Array.indexOf; create intermediate array instead
|
|
|
+ var a = [];
|
|
|
|
|
|
- if ( type === 'relative' ) {
|
|
|
+ if ( size ) {
|
|
|
|
|
|
- remappedPoints[ i * 3 ] += morphPoints[ j * 3 ];
|
|
|
- remappedPoints[ i * 3 + 1 ] += morphPoints[ j * 3 + 1 ];
|
|
|
- remappedPoints[ i * 3 + 2 ] += morphPoints[ j * 3 + 2 ];
|
|
|
+ for ( var i = 0; i < size; i ++ ) {
|
|
|
|
|
|
- } else {
|
|
|
+ a[ i ] = this.getUint8();
|
|
|
|
|
|
- remappedPoints[ i * 3 ] = morphPoints[ j * 3 ];
|
|
|
- remappedPoints[ i * 3 + 1 ] = morphPoints[ j * 3 + 1 ];
|
|
|
- remappedPoints[ i * 3 + 2 ] = morphPoints[ j * 3 + 2 ];
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
|
|
|
- } );
|
|
|
+ var currentChar;
|
|
|
+ var len = 0;
|
|
|
|
|
|
- geometry.morphAttributes.position[ num ] = new Float32BufferAttribute( remappedPoints, 3 );
|
|
|
- geometry.morphAttributes.position[ num ].name = name;
|
|
|
+ while ( currentChar !== 0 ) {
|
|
|
|
|
|
- num ++;
|
|
|
+ currentChar = this.getUint8();
|
|
|
+ if ( currentChar !== 0 ) a.push( currentChar );
|
|
|
+ len ++;
|
|
|
|
|
|
}
|
|
|
|
|
|
- },
|
|
|
-
|
|
|
- };
|
|
|
-
|
|
|
- // parse data from the IFF buffer.
|
|
|
- // LWO3 files are in IFF format and can contain the following data types, referred to by shorthand codes
|
|
|
- //
|
|
|
- // ATOMIC DATA TYPES
|
|
|
- // ID Tag - 4x 7 bit uppercase ASCII chars: ID4
|
|
|
- // signed integer, 1, 2, or 4 byte length: I1, I2, I4
|
|
|
- // unsigned integer, 1, 2, or 4 byte length: U1, U2, U4
|
|
|
- // float, 4 byte length: F4
|
|
|
- // string, series of ASCII chars followed by null byte (If the length of the string including the null terminating byte is odd, an extra null is added so that the data that follows will begin on an even byte boundary): S0
|
|
|
- //
|
|
|
- // COMPOUND DATA TYPES
|
|
|
- // Variable-length Index (index into an array or collection): U2 or U4 : VX
|
|
|
- // Color (RGB): F4 + F4 + F4: COL12
|
|
|
- // Coordinate (x, y, z): F4 + F4 + F4: VEC12
|
|
|
- // Percentage F4 data type from 0->1 with 1 = 100%: FP4
|
|
|
- // Angle in radian F4: ANG4
|
|
|
- // Filename (string) S0: FNAM0
|
|
|
- // XValue F4 + index (VX) + optional envelope( ENVL ): XVAL
|
|
|
- // XValue vector VEC12 + index (VX) + optional envelope( ENVL ): XVAL3
|
|
|
- //
|
|
|
- // The IFF file is arranged in chunks:
|
|
|
- // CHUNK = ID4 + length (U4) + length X bytes of data + optional 0 pad byte
|
|
|
- // optional 0 pad byte is there to ensure chunk ends on even boundary, not counted in size
|
|
|
-
|
|
|
- // Chunks are combined in Forms (collections of chunks)
|
|
|
- // FORM = string 'FORM' (ID4) + length (U4) + type (ID4) + optional ( CHUNK | FORM )
|
|
|
-
|
|
|
- // CHUNKS and FORMS are collectively referred to as blocks
|
|
|
-
|
|
|
- // The entire file is contained in one top level FORM
|
|
|
- function IFFParser() {}
|
|
|
-
|
|
|
- IFFParser.prototype = {
|
|
|
-
|
|
|
- constructor: IFFParser,
|
|
|
-
|
|
|
- parse: function ( buffer ) {
|
|
|
-
|
|
|
- // dump the whole buffer as a string for testing
|
|
|
- // printBuffer( buffer );
|
|
|
-
|
|
|
- this.reader = new DataViewReader( buffer );
|
|
|
-
|
|
|
- this.tree = {
|
|
|
- materials: {},
|
|
|
- layers: [],
|
|
|
- tags: [],
|
|
|
- textures: [],
|
|
|
- };
|
|
|
+ if ( ! isEven( len + 1 ) ) this.getUint8(); // if string with terminating nullbyte is uneven, extra nullbyte is added
|
|
|
|
|
|
- // start out at the top level to add any data before first layer is encountered
|
|
|
- this.currentLayer = this.tree;
|
|
|
- this.currentForm = this.tree;
|
|
|
+ }
|
|
|
|
|
|
- // parse blocks until end of file is reached
|
|
|
- while ( ! this.reader.endOfFile() ) this.parseBlock();
|
|
|
+ return LoaderUtils.decodeText( new Uint8Array( a ) );
|
|
|
|
|
|
- return this.tree;
|
|
|
+ },
|
|
|
|
|
|
- },
|
|
|
+ getStringArray: function ( size ) {
|
|
|
|
|
|
- parseBlock() {
|
|
|
+ var a = this.getString( size );
|
|
|
+ a = a.split( '\0' );
|
|
|
|
|
|
- var blockID = this.reader.getIDTag();
|
|
|
- var length = this.reader.getUint32(); // size of data in bytes
|
|
|
- if ( this.tree.format === 'LWO2' && length > this.reader.dv.byteLength - this.reader.offset ) {
|
|
|
+ return a.filter( Boolean ); // return array with any empty strings removed
|
|
|
|
|
|
- this.reader.offset -= 4;
|
|
|
- length = this.reader.getUint16();
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+};
|
|
|
|
|
|
+// ************** DEBUGGER **************
|
|
|
|
|
|
- // Data types may be found in either LWO2 OR LWO3 spec
|
|
|
- switch ( blockID ) {
|
|
|
+function Debugger( ) {
|
|
|
|
|
|
- case 'FORM': // form blocks may consist of sub -chunks or sub-forms
|
|
|
- this.parseForm( length );
|
|
|
- break;
|
|
|
+ this.active = false;
|
|
|
+ this.depth = 0;
|
|
|
+ this.formList = [];
|
|
|
|
|
|
- // SKIPPED CHUNKS
|
|
|
-
|
|
|
- // MISC skipped
|
|
|
- case 'ICON': // Thumbnail Icon Image
|
|
|
- case 'VMPA': // Vertex Map Parameter
|
|
|
- case 'BBOX': // bounding box
|
|
|
- // case 'VMMD':
|
|
|
- // case 'VTYP':
|
|
|
-
|
|
|
- // normal maps can be specified, normally on models imported from other applications. Currently ignored
|
|
|
- case 'NORM':
|
|
|
-
|
|
|
- // ENVL FORM skipped
|
|
|
- case 'PRE ':
|
|
|
- case 'POST':
|
|
|
- case 'KEY ':
|
|
|
- case 'SPAN':
|
|
|
-
|
|
|
- // CLIP FORM skipped
|
|
|
- case 'TIME':
|
|
|
- case 'CLRS':
|
|
|
- case 'CLRA':
|
|
|
- case 'FILT':
|
|
|
- case 'DITH':
|
|
|
- case 'CONT':
|
|
|
- case 'BRIT':
|
|
|
- case 'SATR':
|
|
|
- case 'HUE ':
|
|
|
- case 'GAMM':
|
|
|
- case 'NEGA':
|
|
|
- case 'IFLT':
|
|
|
- case 'PFLT':
|
|
|
-
|
|
|
- // Image Map Layer skipped
|
|
|
- case 'PROJ':
|
|
|
- case 'AXIS':
|
|
|
- case 'AAST':
|
|
|
- case 'PIXB':
|
|
|
- case 'STCK':
|
|
|
-
|
|
|
- // Procedural Textures skipped
|
|
|
- case 'VALU':
|
|
|
-
|
|
|
- // Gradient Textures skipped
|
|
|
- case 'PNAM':
|
|
|
- case 'INAM':
|
|
|
- case 'GRST':
|
|
|
- case 'GREN':
|
|
|
- case 'GRPT':
|
|
|
- case 'FKEY':
|
|
|
- case 'IKEY':
|
|
|
-
|
|
|
- // Texture Mapping Form skipped
|
|
|
- case 'CSYS':
|
|
|
-
|
|
|
- // Surface CHUNKs skipped
|
|
|
- case 'OPAQ': // top level 'opacity' checkbox
|
|
|
- case 'CMAP': // clip map
|
|
|
-
|
|
|
- // Surface node CHUNKS skipped
|
|
|
- // These mainly specify the node editor setup in LW
|
|
|
- case 'NLOC':
|
|
|
- case 'NZOM':
|
|
|
- case 'NVER':
|
|
|
- case 'NSRV':
|
|
|
- case 'NCRD':
|
|
|
- case 'NMOD':
|
|
|
- case 'NPRW':
|
|
|
- case 'NPLA':
|
|
|
- case 'VERS':
|
|
|
- case 'ENUM':
|
|
|
- case 'TAG ':
|
|
|
-
|
|
|
- // Car Material CHUNKS
|
|
|
- case 'CGMD':
|
|
|
- case 'CGTY':
|
|
|
- case 'CGST':
|
|
|
- case 'CGEN':
|
|
|
- case 'CGTS':
|
|
|
- case 'CGTE':
|
|
|
- case 'OSMP':
|
|
|
- case 'OMDE':
|
|
|
- case 'OUTR':
|
|
|
- this.reader.skip( length );
|
|
|
- break;
|
|
|
+}
|
|
|
|
|
|
- case 'FLAG':
|
|
|
- if ( this.tree.format === 'LWO2' ) {
|
|
|
+Debugger.prototype = {
|
|
|
|
|
|
- this.reader.skip( 4 ); // not suported
|
|
|
+ constructor: Debugger,
|
|
|
|
|
|
- } else {
|
|
|
+ enable: function () {
|
|
|
|
|
|
- this.reader.skip( length );
|
|
|
+ this.active = true;
|
|
|
|
|
|
- }
|
|
|
- break;
|
|
|
- // Skipped LWO2 chunks
|
|
|
- case 'DIFF': // diffuse level, may be necessary to modulate COLR with this
|
|
|
- this.currentSurface.diffusePower = this.reader.getFloat32();
|
|
|
- this.reader.skip( 2 );
|
|
|
- break;
|
|
|
- case 'TRNL':
|
|
|
- case 'REFL':
|
|
|
- case 'GLOS':
|
|
|
- case 'SHRP':
|
|
|
- case 'RFOP':
|
|
|
- case 'RSAN':
|
|
|
- case 'TROP':
|
|
|
- case 'RBLR':
|
|
|
- case 'TBLR':
|
|
|
- case 'CLRH':
|
|
|
- case 'CLRF':
|
|
|
- case 'ADTR':
|
|
|
- case 'GLOW':
|
|
|
- case 'LINE':
|
|
|
- case 'ALPH':
|
|
|
- case 'VCOL':
|
|
|
- case 'ENAB':
|
|
|
- this.reader.skip( length );
|
|
|
- break;
|
|
|
- case 'SURF':
|
|
|
- if ( this.tree.format === 'LWO2' ) {
|
|
|
+ },
|
|
|
|
|
|
- this.parseSurfaceLwo2( length );
|
|
|
+ log: function () {
|
|
|
|
|
|
- }
|
|
|
- break;
|
|
|
- case 'CLIP':
|
|
|
- if ( this.tree.format === 'LWO2' ) {
|
|
|
+ if ( ! this.active ) return;
|
|
|
|
|
|
- this.parseClipLwo2( length );
|
|
|
+ var nodeType;
|
|
|
|
|
|
- }
|
|
|
- break;
|
|
|
- // Texture node chunks (not in spec)
|
|
|
- case 'IPIX': // usePixelBlending
|
|
|
- case 'IMIP': // useMipMaps
|
|
|
- case 'IMOD': // imageBlendingMode
|
|
|
- case 'AMOD': // unknown
|
|
|
- case 'IINV': // imageInvertAlpha
|
|
|
- case 'INCR': // imageInvertColor
|
|
|
- case 'IAXS': // imageAxis ( for non-UV maps)
|
|
|
- case 'IFOT': // imageFallofType
|
|
|
- case 'ITIM': // timing for animated textures
|
|
|
- case 'IWRL':
|
|
|
- case 'IUTI':
|
|
|
- case 'IINX':
|
|
|
- case 'IINY':
|
|
|
- case 'IINZ':
|
|
|
- case 'IREF': // possibly a VX for reused texture nodes
|
|
|
- if ( length === 4 ) this.currentNode[ blockID ] = this.reader.getInt32();
|
|
|
- else this.reader.skip( length );
|
|
|
- break;
|
|
|
+ switch ( this.node ) {
|
|
|
|
|
|
- case 'OTAG':
|
|
|
- this.parseObjectTag();
|
|
|
- break;
|
|
|
+ case 0:
|
|
|
+ nodeType = "FORM";
|
|
|
+ break;
|
|
|
|
|
|
- case 'LAYR':
|
|
|
- this.parseLayer( length );
|
|
|
- break;
|
|
|
+ case 1:
|
|
|
+ nodeType = "CHK";
|
|
|
+ break;
|
|
|
|
|
|
- case 'PNTS':
|
|
|
- this.parsePoints( length );
|
|
|
- break;
|
|
|
+ case 2:
|
|
|
+ nodeType = "S-CHK";
|
|
|
+ break;
|
|
|
|
|
|
- case 'VMAP':
|
|
|
- this.parseVertexMapping( length );
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- case 'POLS':
|
|
|
- this.parsePolygonList( length );
|
|
|
- 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 ) ? "}" : "" )
|
|
|
+ );
|
|
|
|
|
|
- case 'TAGS':
|
|
|
- this.parseTagStrings( length );
|
|
|
- break;
|
|
|
+ if ( this.node == 0 && ! this.skipped ) {
|
|
|
|
|
|
- case 'PTAG':
|
|
|
- this.parsePolygonTagMapping( length );
|
|
|
- break;
|
|
|
+ this.depth += 1;
|
|
|
+ this.formList.push( this.dataOffset + this.length );
|
|
|
|
|
|
- case 'VMAD':
|
|
|
- this.parseVertexMapping( length, true );
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- // Misc CHUNKS
|
|
|
- case 'DESC': // Description Line
|
|
|
- this.currentForm.description = this.reader.getString();
|
|
|
- break;
|
|
|
+ this.skipped = false;
|
|
|
|
|
|
- case 'TEXT':
|
|
|
- case 'CMNT':
|
|
|
- case 'NCOM':
|
|
|
- this.currentForm.comment = this.reader.getString();
|
|
|
- break;
|
|
|
+ },
|
|
|
|
|
|
- // Envelope Form
|
|
|
- case 'NAME':
|
|
|
- this.currentForm.channelName = this.reader.getString();
|
|
|
- break;
|
|
|
+ closeForms: function () {
|
|
|
|
|
|
- // Image Map Layer
|
|
|
+ if ( ! this.active ) return;
|
|
|
|
|
|
- case 'WRAP':
|
|
|
- this.currentForm.wrap = { w: this.reader.getUint16(), h: this.reader.getUint16() };
|
|
|
- break;
|
|
|
+ for ( var i = this.formList.length - 1; i >= 0; i -- ) {
|
|
|
|
|
|
- case 'IMAG':
|
|
|
- var index = this.reader.getVariableLengthIndex();
|
|
|
- this.currentForm.imageIndex = index;
|
|
|
- break;
|
|
|
+ if ( this.offset >= this.formList[ i ] ) {
|
|
|
|
|
|
- // Texture Mapping Form
|
|
|
+ this.depth -= 1;
|
|
|
+ console.log( "| ".repeat( this.depth ) + "}" );
|
|
|
+ this.formList.splice( - 1, 1 );
|
|
|
|
|
|
- case 'OREF':
|
|
|
- this.currentForm.referenceObject = this.reader.getString();
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- case 'ROID':
|
|
|
- this.currentForm.referenceObjectID = this.reader.getUint32();
|
|
|
- break;
|
|
|
+ }
|
|
|
|
|
|
- // Surface Blocks
|
|
|
+ }
|
|
|
|
|
|
- case 'SSHN':
|
|
|
- this.currentSurface.surfaceShaderName = this.reader.getString();
|
|
|
- break;
|
|
|
+};
|
|
|
|
|
|
- case 'AOVN':
|
|
|
- this.currentSurface.surfaceCustomAOVName = this.reader.getString();
|
|
|
- break;
|
|
|
+// ************** UTILITY FUNCTIONS **************
|
|
|
|
|
|
- // Nodal Blocks
|
|
|
+function isEven( num ) {
|
|
|
|
|
|
- case 'NSTA':
|
|
|
- this.currentForm.disabled = this.reader.getUint16();
|
|
|
- break;
|
|
|
+ return num % 2;
|
|
|
|
|
|
- case 'NRNM':
|
|
|
- this.currentForm.realName = this.reader.getString();
|
|
|
- break;
|
|
|
+}
|
|
|
|
|
|
- case 'NNME':
|
|
|
- this.currentForm.refName = this.reader.getString();
|
|
|
- this.currentSurface.nodes[ this.currentForm.refName ] = this.currentForm;
|
|
|
- break;
|
|
|
+// calculate the length of the string in the buffer
|
|
|
+// this will be string.length + nullbyte + optional padbyte to make the length even
|
|
|
+function stringOffset( string ) {
|
|
|
|
|
|
- // Nodal Blocks : connections
|
|
|
- case 'INME':
|
|
|
- if ( ! this.currentForm.nodeName ) this.currentForm.nodeName = [];
|
|
|
- this.currentForm.nodeName.push( this.reader.getString() );
|
|
|
- break;
|
|
|
+ return string.length + 1 + ( isEven( string.length + 1 ) ? 1 : 0 );
|
|
|
|
|
|
- case 'IINN':
|
|
|
- if ( ! this.currentForm.inputNodeName ) this.currentForm.inputNodeName = [];
|
|
|
- this.currentForm.inputNodeName.push( this.reader.getString() );
|
|
|
- break;
|
|
|
+}
|
|
|
|
|
|
- case 'IINM':
|
|
|
- if ( ! this.currentForm.inputName ) this.currentForm.inputName = [];
|
|
|
- this.currentForm.inputName.push( this.reader.getString() );
|
|
|
- break;
|
|
|
+// for testing purposes, dump buffer to console
|
|
|
+// printBuffer( this.reader.dv.buffer, this.reader.offset, length );
|
|
|
+function printBuffer( buffer, from, to ) {
|
|
|
|
|
|
- case 'IONM':
|
|
|
- if ( ! this.currentForm.inputOutputName ) this.currentForm.inputOutputName = [];
|
|
|
- this.currentForm.inputOutputName.push( this.reader.getString() );
|
|
|
- break;
|
|
|
+ console.log( LoaderUtils.decodeText( new Uint8Array( buffer, from, to ) ) );
|
|
|
|
|
|
- case 'FNAM':
|
|
|
- this.currentForm.fileName = this.reader.getString();
|
|
|
- break;
|
|
|
+}
|
|
|
|
|
|
- case 'CHAN': // NOTE: ENVL Forms may also have CHAN chunk, however ENVL is currently ignored
|
|
|
- if ( length === 4 ) this.currentForm.textureChannel = this.reader.getIDTag();
|
|
|
- else this.reader.skip( length );
|
|
|
- break;
|
|
|
+var lwoTree;
|
|
|
|
|
|
- // LWO2 Spec chunks: these are needed since the SURF FORMs are often in LWO2 format
|
|
|
+var LWOLoader = function ( manager, parameters ) {
|
|
|
|
|
|
- case 'SMAN':
|
|
|
- var maxSmoothingAngle = this.reader.getFloat32();
|
|
|
- this.currentSurface.attributes.smooth = ( maxSmoothingAngle < 0 ) ? false : true;
|
|
|
- break;
|
|
|
+ this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
|
|
|
|
|
|
- // LWO2: Basic Surface Parameters
|
|
|
- case 'COLR':
|
|
|
- this.currentSurface.attributes.Color = {};
|
|
|
- this.currentSurface.attributes.Color.value = this.reader.getFloat32Array( 3 );
|
|
|
- this.reader.skip( 2 ); // VX: envelope
|
|
|
- break;
|
|
|
+ parameters = parameters || {};
|
|
|
|
|
|
- case 'LUMI':
|
|
|
- this.currentSurface.attributes.luminosityLevel = this.reader.getFloat32();
|
|
|
- this.reader.skip( 2 );
|
|
|
- break;
|
|
|
+ this.resourcePath = ( parameters.resourcePath !== undefined ) ? parameters.resourcePath : undefined;
|
|
|
|
|
|
- case 'SPEC':
|
|
|
- this.currentSurface.attributes.specularLevel = this.reader.getFloat32();
|
|
|
- this.reader.skip( 2 );
|
|
|
- break;
|
|
|
+};
|
|
|
|
|
|
- case 'TRAN':
|
|
|
- this.currentSurface.attributes.opacity = this.reader.getFloat32();
|
|
|
- this.reader.skip( 2 );
|
|
|
- break;
|
|
|
+LWOLoader.prototype = {
|
|
|
|
|
|
- case 'BUMP':
|
|
|
- this.currentSurface.attributes.bumpStrength = this.reader.getFloat32();
|
|
|
- this.reader.skip( 2 );
|
|
|
- break;
|
|
|
+ constructor: LWOLoader,
|
|
|
|
|
|
- case 'SIDE':
|
|
|
- this.currentSurface.attributes.side = this.reader.getUint16();
|
|
|
- break;
|
|
|
+ crossOrigin: 'anonymous',
|
|
|
|
|
|
- case 'RIMG':
|
|
|
- this.currentSurface.attributes.reflectionMap = this.reader.getVariableLengthIndex();
|
|
|
- break;
|
|
|
+ load: function ( url, onLoad, onProgress, onError ) {
|
|
|
|
|
|
- case 'RIND':
|
|
|
- this.currentSurface.attributes.refractiveIndex = this.reader.getFloat32();
|
|
|
- this.reader.skip( 2 );
|
|
|
- break;
|
|
|
+ var self = this;
|
|
|
|
|
|
- case 'TIMG':
|
|
|
- this.currentSurface.attributes.refractionMap = this.reader.getVariableLengthIndex();
|
|
|
- break;
|
|
|
+ var path = ( self.path === undefined ) ? extractParentUrl( url, 'Objects' ) : self.path;
|
|
|
|
|
|
- case 'IMAP':
|
|
|
- if ( this.tree.format === 'LWO2' ) {
|
|
|
+ // give the mesh a default name based on the filename
|
|
|
+ var modelName = url.split( path ).pop().split( '.' )[ 0 ];
|
|
|
|
|
|
- this.reader.skip( 2 );
|
|
|
+ var loader = new FileLoader( this.manager );
|
|
|
+ loader.setPath( self.path );
|
|
|
+ loader.setResponseType( 'arraybuffer' );
|
|
|
|
|
|
- } else {
|
|
|
+ loader.load( url, function ( buffer ) {
|
|
|
|
|
|
- this.currentSurface.attributes.imageMapIndex = this.reader.getUint32();
|
|
|
+ // console.time( 'Total parsing: ' );
|
|
|
+ onLoad( self.parse( buffer, path, modelName ) );
|
|
|
+ // console.timeEnd( 'Total parsing: ' );
|
|
|
|
|
|
- }
|
|
|
- break;
|
|
|
+ }, onProgress, onError );
|
|
|
|
|
|
- case 'IUVI': // uv channel name
|
|
|
- this.currentNode.UVChannel = this.reader.getString( length );
|
|
|
- break;
|
|
|
+ },
|
|
|
|
|
|
- case 'IUTL': // widthWrappingMode: 0 = Reset, 1 = Repeat, 2 = Mirror, 3 = Edge
|
|
|
- this.currentNode.widthWrappingMode = this.reader.getUint32();
|
|
|
- break;
|
|
|
- case 'IVTL': // heightWrappingMode
|
|
|
- this.currentNode.heightWrappingMode = this.reader.getUint32();
|
|
|
- break;
|
|
|
+ setCrossOrigin: function ( value ) {
|
|
|
|
|
|
- // LWO2 USE
|
|
|
- case 'BLOK':
|
|
|
- // skip
|
|
|
- break;
|
|
|
+ this.crossOrigin = value;
|
|
|
+ return this;
|
|
|
|
|
|
- default:
|
|
|
- this.parseUnknownCHUNK( blockID, length );
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ setPath: function ( value ) {
|
|
|
|
|
|
- if ( this.reader.offset >= this.currentFormEnd ) {
|
|
|
+ this.path = value;
|
|
|
+ return this;
|
|
|
|
|
|
- this.currentForm = this.parentForm;
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ setResourcePath: function ( value ) {
|
|
|
|
|
|
- },
|
|
|
-
|
|
|
-
|
|
|
- ///
|
|
|
- // FORM PARSING METHODS
|
|
|
- ///
|
|
|
-
|
|
|
- // Forms are organisational and can contain any number of sub chunks and sub forms
|
|
|
- // FORM ::= 'FORM'[ID4], length[U4], type[ID4], ( chunk[CHUNK] | form[FORM] ) * }
|
|
|
- parseForm( length ) {
|
|
|
-
|
|
|
- var type = this.reader.getIDTag();
|
|
|
-
|
|
|
- switch ( type ) {
|
|
|
-
|
|
|
- // SKIPPED FORMS
|
|
|
- // if skipForm( length ) is called, the entire form and any sub forms and chunks are skipped
|
|
|
-
|
|
|
- case 'ISEQ': // Image sequence
|
|
|
- case 'ANIM': // plug in animation
|
|
|
- case 'STCC': // Color-cycling Still
|
|
|
- case 'VPVL':
|
|
|
- case 'VPRM':
|
|
|
- case 'NROT':
|
|
|
- case 'WRPW': // image wrap w ( for cylindrical and spherical projections)
|
|
|
- case 'WRPH': // image wrap h
|
|
|
- case 'FUNC':
|
|
|
- case 'FALL':
|
|
|
- case 'OPAC':
|
|
|
- case 'GRAD': // gradient texture
|
|
|
- case 'ENVS':
|
|
|
- case 'VMOP':
|
|
|
- case 'VMBG':
|
|
|
-
|
|
|
- // Car Material FORMS
|
|
|
- case 'OMAX':
|
|
|
- case 'STEX':
|
|
|
- case 'CKBG':
|
|
|
- case 'CKEY':
|
|
|
- case 'VMLA':
|
|
|
- case 'VMLB':
|
|
|
- this.skipForm( length ); // not currently supported
|
|
|
- break;
|
|
|
+ this.resourcePath = value;
|
|
|
+ return this;
|
|
|
|
|
|
- // if break; is called directly, the position in the lwoTree is not created
|
|
|
- // any sub chunks and forms are added to the parent form instead
|
|
|
- case 'META':
|
|
|
- case 'NNDS':
|
|
|
- case 'NODS':
|
|
|
- case 'NDTA':
|
|
|
- case 'ADAT':
|
|
|
- case 'AOVS':
|
|
|
- case 'BLOK':
|
|
|
-
|
|
|
- // used by texture nodes
|
|
|
- case 'IBGC': // imageBackgroundColor
|
|
|
- case 'IOPC': // imageOpacity
|
|
|
- case 'IIMG': // hold reference to image path
|
|
|
- case 'TXTR':
|
|
|
- // this.setupForm( type, length );
|
|
|
- break;
|
|
|
+ },
|
|
|
|
|
|
- case 'IFAL': // imageFallof
|
|
|
- case 'ISCL': // imageScale
|
|
|
- case 'IPOS': // imagePosition
|
|
|
- case 'IROT': // imageRotation
|
|
|
- case 'IBMP':
|
|
|
- case 'IUTD':
|
|
|
- case 'IVTD':
|
|
|
- this.parseTextureNodeAttribute( type );
|
|
|
- break;
|
|
|
+ parse: function ( iffBuffer, path, modelName ) {
|
|
|
|
|
|
- case 'LWO2':
|
|
|
- this.tree.format = type;
|
|
|
- break;
|
|
|
+ lwoTree = new IFFParser().parse( iffBuffer );
|
|
|
|
|
|
- case 'LWO3':
|
|
|
- this.tree.format = type;
|
|
|
- break;
|
|
|
+ // console.log( 'lwoTree', lwoTree );
|
|
|
|
|
|
- case 'ENVL':
|
|
|
- this.parseEnvelope( length );
|
|
|
- break;
|
|
|
+ var textureLoader = new TextureLoader( this.manager ).setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
|
|
|
|
|
|
- // CLIP FORM AND SUB FORMS
|
|
|
+ return new LWOTreeParser( textureLoader ).parse( modelName );
|
|
|
|
|
|
- case 'CLIP':
|
|
|
- if ( this.tree.format === 'LWO2' ) {
|
|
|
+ }
|
|
|
|
|
|
- this.parseForm( length );
|
|
|
+};
|
|
|
|
|
|
- } else {
|
|
|
+// Parse the lwoTree object
|
|
|
+function LWOTreeParser( textureLoader ) {
|
|
|
|
|
|
- this.parseClip( length );
|
|
|
+ this.textureLoader = textureLoader;
|
|
|
|
|
|
- }
|
|
|
- break;
|
|
|
+}
|
|
|
|
|
|
- case 'STIL':
|
|
|
- this.parseImage();
|
|
|
- break;
|
|
|
+LWOTreeParser.prototype = {
|
|
|
|
|
|
- case 'XREF': // clone of another STIL
|
|
|
- this.reader.skip( 8 ); // unknown
|
|
|
- this.currentForm.referenceTexture = {
|
|
|
- index: this.reader.getUint32(),
|
|
|
- refName: this.reader.getString() // internal unique ref
|
|
|
- };
|
|
|
- break;
|
|
|
+ constructor: LWOTreeParser,
|
|
|
|
|
|
- // Not in spec, used by texture nodes
|
|
|
+ parse: function ( modelName ) {
|
|
|
|
|
|
- case 'IMST':
|
|
|
- this.parseImageStateForm( length );
|
|
|
- break;
|
|
|
+ this.materials = new MaterialParser( this.textureLoader ).parse();
|
|
|
+ this.defaultLayerName = modelName;
|
|
|
|
|
|
- // SURF FORM AND SUB FORMS
|
|
|
+ this.meshes = this.parseLayers();
|
|
|
|
|
|
- case 'SURF':
|
|
|
- this.parseSurfaceForm( length );
|
|
|
- break;
|
|
|
+ return {
|
|
|
+ materials: this.materials,
|
|
|
+ meshes: this.meshes,
|
|
|
+ };
|
|
|
|
|
|
- case 'VALU': // Not in spec
|
|
|
- this.parseValueForm( length );
|
|
|
- break;
|
|
|
+ },
|
|
|
|
|
|
- case 'NTAG':
|
|
|
- this.parseSubNode( length );
|
|
|
- break;
|
|
|
+ parseLayers() {
|
|
|
|
|
|
- case 'ATTR': // BSDF Node Attributes
|
|
|
- case 'SATR': // Standard Node Attributes
|
|
|
- this.setupForm( 'attributes', length );
|
|
|
- break;
|
|
|
+ // array of all meshes for building hierarchy
|
|
|
+ var meshes = [];
|
|
|
|
|
|
- case 'NCON':
|
|
|
- this.parseConnections( length );
|
|
|
- break;
|
|
|
+ // final array containing meshes with scene graph hierarchy set up
|
|
|
+ var finalMeshes = [];
|
|
|
|
|
|
- case 'SSHA':
|
|
|
- this.parentForm = this.currentForm;
|
|
|
- this.currentForm = this.currentSurface;
|
|
|
- this.setupForm( 'surfaceShader', length );
|
|
|
- break;
|
|
|
+ var geometryParser = new GeometryParser();
|
|
|
|
|
|
- case 'SSHD':
|
|
|
- this.setupForm( 'surfaceShaderData', length );
|
|
|
- break;
|
|
|
+ var self = this;
|
|
|
+ lwoTree.layers.forEach( function ( layer ) {
|
|
|
|
|
|
- case 'ENTR': // Not in spec
|
|
|
- this.parseEntryForm( length );
|
|
|
- break;
|
|
|
+ var geometry = geometryParser.parse( layer.geometry, layer );
|
|
|
|
|
|
- // Image Map Layer
|
|
|
+ var mesh = self.parseMesh( geometry, layer );
|
|
|
|
|
|
- case 'IMAP':
|
|
|
- this.parseImageMap( length );
|
|
|
- break;
|
|
|
+ meshes[ layer.number ] = mesh;
|
|
|
|
|
|
- case 'TAMP':
|
|
|
- this.parseXVAL( 'amplitude', length );
|
|
|
- break;
|
|
|
+ if ( layer.parent === - 1 ) finalMeshes.push( mesh );
|
|
|
+ else meshes[ layer.parent ].add( mesh );
|
|
|
|
|
|
- //Texture Mapping Form
|
|
|
|
|
|
- case 'TMAP':
|
|
|
- this.setupForm( 'textureMap', length );
|
|
|
- break;
|
|
|
+ } );
|
|
|
|
|
|
- case 'CNTR':
|
|
|
- this.parseXVAL3( 'center', length );
|
|
|
- break;
|
|
|
+ this.applyPivots( finalMeshes );
|
|
|
|
|
|
- case 'SIZE':
|
|
|
- this.parseXVAL3( 'scale', length );
|
|
|
- break;
|
|
|
+ return finalMeshes;
|
|
|
|
|
|
- case 'ROTA':
|
|
|
- this.parseXVAL3( 'rotation', length );
|
|
|
- break;
|
|
|
+ },
|
|
|
|
|
|
- default:
|
|
|
- this.parseUnknownForm( type, length );
|
|
|
+ parseMesh( geometry, layer ) {
|
|
|
|
|
|
- }
|
|
|
+ var mesh;
|
|
|
|
|
|
- },
|
|
|
+ var materials = this.getMaterials( geometry.userData.matNames, layer.geometry.type );
|
|
|
|
|
|
- setupForm( type, length ) {
|
|
|
+ this.duplicateUVs( geometry, materials );
|
|
|
|
|
|
- if ( ! this.currentForm ) this.currentForm = this.currentNode;
|
|
|
+ if ( layer.geometry.type === 'points' ) mesh = new Points( geometry, materials );
|
|
|
+ else if ( layer.geometry.type === 'lines' ) mesh = new LineSegments( geometry, materials );
|
|
|
+ else mesh = new Mesh( geometry, materials );
|
|
|
|
|
|
- this.currentFormEnd = this.reader.offset + length;
|
|
|
- this.parentForm = this.currentForm;
|
|
|
+ if ( layer.name ) mesh.name = layer.name;
|
|
|
+ else mesh.name = this.defaultLayerName + '_layer_' + layer.number;
|
|
|
|
|
|
- if ( ! this.currentForm[ type ] ) {
|
|
|
+ mesh.userData.pivot = layer.pivot;
|
|
|
|
|
|
- this.currentForm[ type ] = {};
|
|
|
- this.currentForm = this.currentForm[ type ];
|
|
|
+ return mesh;
|
|
|
|
|
|
+ },
|
|
|
|
|
|
- } else {
|
|
|
+ // TODO: may need to be reversed in z to convert LWO to three.js coordinates
|
|
|
+ applyPivots( meshes ) {
|
|
|
|
|
|
- // should never see this unless there's a bug in the reader
|
|
|
- console.warn( 'LWOLoader: form already exists on parent: ', type, this.currentForm );
|
|
|
+ meshes.forEach( function ( mesh ) {
|
|
|
|
|
|
- this.currentForm = this.currentForm[ type ];
|
|
|
+ mesh.traverse( function ( child ) {
|
|
|
|
|
|
- }
|
|
|
+ var pivot = child.userData.pivot;
|
|
|
|
|
|
+ child.position.x += pivot[ 0 ];
|
|
|
+ child.position.y += pivot[ 1 ];
|
|
|
+ child.position.z += pivot[ 2 ];
|
|
|
|
|
|
- },
|
|
|
+ if ( child.parent ) {
|
|
|
|
|
|
- skipForm( length ) {
|
|
|
+ var parentPivot = child.parent.userData.pivot;
|
|
|
|
|
|
- this.reader.skip( length - 4 );
|
|
|
+ child.position.x -= parentPivot[ 0 ];
|
|
|
+ child.position.y -= parentPivot[ 1 ];
|
|
|
+ child.position.z -= parentPivot[ 2 ];
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- 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 );
|
|
|
+ },
|
|
|
|
|
|
- },
|
|
|
+ getMaterials( namesArray, type ) {
|
|
|
|
|
|
- parseSurfaceForm( length ) {
|
|
|
+ var materials = [];
|
|
|
|
|
|
- this.reader.skip( 8 ); // unknown Uint32 x2
|
|
|
+ var self = this;
|
|
|
|
|
|
- var name = this.reader.getString();
|
|
|
+ namesArray.forEach( function ( name, i ) {
|
|
|
|
|
|
- var surface = {
|
|
|
- attributes: {}, // LWO2 style non-node attributes will go here
|
|
|
- connections: {},
|
|
|
- name: name,
|
|
|
- inputName: name,
|
|
|
- nodes: {},
|
|
|
- source: this.reader.getString(),
|
|
|
- };
|
|
|
+ materials[ i ] = self.getMaterialByName( name );
|
|
|
|
|
|
- this.tree.materials[ name ] = surface;
|
|
|
- this.currentSurface = surface;
|
|
|
+ } );
|
|
|
|
|
|
- this.parentForm = this.tree.materials;
|
|
|
- this.currentForm = surface;
|
|
|
- this.currentFormEnd = this.reader.offset + length;
|
|
|
+ // convert materials to line or point mats if required
|
|
|
+ if ( type === 'points' || type === 'lines' ) {
|
|
|
|
|
|
- },
|
|
|
+ materials.forEach( function ( mat, i ) {
|
|
|
|
|
|
- parseSurfaceLwo2( length ) {
|
|
|
+ var spec = {
|
|
|
+ color: mat.color,
|
|
|
+ };
|
|
|
|
|
|
- var name = this.reader.getString();
|
|
|
+ if ( type === 'points' ) {
|
|
|
|
|
|
- var surface = {
|
|
|
- attributes: {}, // LWO2 style non-node attributes will go here
|
|
|
- connections: {},
|
|
|
- name: name,
|
|
|
- nodes: {},
|
|
|
- source: this.reader.getString(),
|
|
|
- };
|
|
|
+ spec.size = 0.1;
|
|
|
+ spec.map = mat.map;
|
|
|
+ spec.morphTargets = mat.morphTargets;
|
|
|
+ materials[ i ] = new PointsMaterial( spec );
|
|
|
|
|
|
- this.tree.materials[ name ] = surface;
|
|
|
- this.currentSurface = surface;
|
|
|
+ } else if ( type === 'lines' ) {
|
|
|
|
|
|
- this.parentForm = this.tree.materials;
|
|
|
- this.currentForm = surface;
|
|
|
- this.currentFormEnd = this.reader.offset + length;
|
|
|
+ materials[ i ] = new LineBasicMaterial( spec );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- parseSubNode( length ) {
|
|
|
+ } );
|
|
|
|
|
|
- // parse the NRNM CHUNK of the subnode FORM to get
|
|
|
- // a meaningful name for the subNode
|
|
|
- // some subnodes can be renamed, but Input and Surface cannot
|
|
|
+ }
|
|
|
|
|
|
- this.reader.skip( 8 ); // NRNM + length
|
|
|
- var name = this.reader.getString();
|
|
|
+ // if there is only one material, return that directly instead of array
|
|
|
+ var filtered = materials.filter( Boolean );
|
|
|
+ if ( filtered.length === 1 ) return filtered[ 0 ];
|
|
|
|
|
|
- var node = {
|
|
|
- name: name
|
|
|
- };
|
|
|
- this.currentForm = node;
|
|
|
- this.currentNode = node;
|
|
|
+ return materials;
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ getMaterialByName( name ) {
|
|
|
|
|
|
- this.currentFormEnd = this.reader.offset + length;
|
|
|
+ return this.materials.filter( function ( m ) {
|
|
|
|
|
|
+ return m.name === name;
|
|
|
|
|
|
- },
|
|
|
+ } )[ 0 ];
|
|
|
|
|
|
- // collect attributes from all nodes at the top level of a surface
|
|
|
- parseConnections( length ) {
|
|
|
+ },
|
|
|
|
|
|
- this.currentFormEnd = this.reader.offset + length;
|
|
|
- this.parentForm = this.currentForm;
|
|
|
+ // If the material has an aoMap, duplicate UVs
|
|
|
+ duplicateUVs( geometry, materials ) {
|
|
|
|
|
|
- this.currentForm = this.currentSurface.connections;
|
|
|
+ var duplicateUVs = false;
|
|
|
|
|
|
- },
|
|
|
+ if ( ! Array.isArray( materials ) ) {
|
|
|
|
|
|
- // surface node attribute data, e.g. specular, roughness etc
|
|
|
- parseEntryForm( length ) {
|
|
|
+ if ( materials.aoMap ) duplicateUVs = true;
|
|
|
|
|
|
- this.reader.skip( 8 ); // NAME + length
|
|
|
- var name = this.reader.getString();
|
|
|
- this.currentForm = this.currentNode.attributes;
|
|
|
+ } else {
|
|
|
|
|
|
- this.setupForm( name, length );
|
|
|
+ materials.forEach( function ( material ) {
|
|
|
|
|
|
- },
|
|
|
+ if ( material.aoMap ) duplicateUVs = true;
|
|
|
|
|
|
- // parse values from material - doesn't match up to other LWO3 data types
|
|
|
- // sub form of entry form
|
|
|
- parseValueForm() {
|
|
|
+ } );
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- this.reader.skip( 8 ); // unknown + length
|
|
|
+ if ( ! duplicateUVs ) return;
|
|
|
|
|
|
- var valueType = this.reader.getString();
|
|
|
+ geometry.addAttribute( 'uv2', new BufferAttribute( geometry.attributes.uv.array, 2 ) );
|
|
|
|
|
|
- if ( valueType === 'double' ) {
|
|
|
+ },
|
|
|
|
|
|
- this.currentForm.value = this.reader.getUint64();
|
|
|
+};
|
|
|
|
|
|
- } else if ( valueType === 'int' ) {
|
|
|
+function MaterialParser( textureLoader ) {
|
|
|
|
|
|
- this.currentForm.value = this.reader.getUint32();
|
|
|
+ this.textureLoader = textureLoader;
|
|
|
|
|
|
- } else if ( valueType === 'vparam' ) {
|
|
|
+}
|
|
|
|
|
|
- this.reader.skip( 24 );
|
|
|
- this.currentForm.value = this.reader.getFloat64();
|
|
|
+MaterialParser.prototype = {
|
|
|
|
|
|
- } else if ( valueType === 'vparam3' ) {
|
|
|
+ constructor: MaterialParser,
|
|
|
|
|
|
- this.reader.skip( 24 );
|
|
|
- this.currentForm.value = this.reader.getFloat64Array( 3 );
|
|
|
+ parse: function () {
|
|
|
|
|
|
+ var materials = [];
|
|
|
+ this.textures = {};
|
|
|
+
|
|
|
+ for ( var name in lwoTree.materials ) {
|
|
|
+
|
|
|
+ if ( lwoTree.format === 'LWO3' ) {
|
|
|
+
|
|
|
+ materials.push( this.parseMaterial( lwoTree.materials[ name ], name, lwoTree.textures ) );
|
|
|
+
|
|
|
+ } else if ( lwoTree.format === 'LWO2' ) {
|
|
|
+
|
|
|
+ materials.push( this.parseMaterialLwo2( lwoTree.materials[ name ], name, lwoTree.textures ) );
|
|
|
|
|
|
}
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- // holds various data about texture node image state
|
|
|
- // Data other thanmipMapLevel unknown
|
|
|
- parseImageStateForm() {
|
|
|
+ return materials;
|
|
|
|
|
|
- this.reader.skip( 8 ); // unknown
|
|
|
+ },
|
|
|
|
|
|
- this.currentForm.mipMapLevel = this.reader.getFloat32();
|
|
|
+ parseMaterial( materialData, name, textures ) {
|
|
|
|
|
|
- },
|
|
|
+ var params = {
|
|
|
+ name: name,
|
|
|
+ side: this.getSide( materialData.attributes ),
|
|
|
+ flatShading: this.getSmooth( materialData.attributes ),
|
|
|
+ };
|
|
|
|
|
|
- // LWO2 style image data node OR LWO3 textures defined at top level in editor (not as SURF node)
|
|
|
- parseImageMap( length ) {
|
|
|
+ var connections = this.parseConnections( materialData.connections, materialData.nodes );
|
|
|
|
|
|
- this.currentFormEnd = this.reader.offset + length;
|
|
|
- this.parentForm = this.currentForm;
|
|
|
+ var maps = this.parseTextureNodes( connections.maps );
|
|
|
|
|
|
- if ( ! this.currentForm.maps ) this.currentForm.maps = [];
|
|
|
+ this.parseAttributeImageMaps( connections.attributes, textures, maps, materialData.maps );
|
|
|
|
|
|
- var map = {};
|
|
|
- this.currentForm.maps.push( map );
|
|
|
- this.currentForm = map;
|
|
|
+ var attributes = this.parseAttributes( connections.attributes, maps );
|
|
|
|
|
|
- this.reader.skip( 10 ); // unknown, could be an issue if it contains a VX
|
|
|
+ this.parseEnvMap( connections, maps, attributes );
|
|
|
|
|
|
- },
|
|
|
+ params = Object.assign( maps, params );
|
|
|
+ params = Object.assign( params, attributes );
|
|
|
|
|
|
- parseTextureNodeAttribute( type ) {
|
|
|
+ var materialType = this.getMaterialType( connections.attributes );
|
|
|
|
|
|
- this.reader.skip( 28 ); // FORM + length + VPRM + unknown + Uint32 x2 + float32
|
|
|
+ return new materialType( params );
|
|
|
|
|
|
- this.reader.skip( 20 ); // FORM + length + VPVL + float32 + Uint32
|
|
|
+ },
|
|
|
|
|
|
- switch ( type ) {
|
|
|
+ parseMaterialLwo2( materialData, name/*, textures*/ ) {
|
|
|
|
|
|
- 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;
|
|
|
+ var params = {
|
|
|
+ name: name,
|
|
|
+ side: this.getSide( materialData.attributes ),
|
|
|
+ flatShading: this.getSmooth( materialData.attributes ),
|
|
|
+ };
|
|
|
|
|
|
- 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;
|
|
|
+ var attributes = this.parseAttributes( materialData.attributes, {} );
|
|
|
+ params = Object.assign( params, attributes );
|
|
|
+ return new MeshPhongMaterial( params );
|
|
|
|
|
|
- }
|
|
|
+ },
|
|
|
|
|
|
- this.reader.skip( 2 ); // unknown
|
|
|
+ // Note: converting from left to right handed coords by switching x -> -x in vertices, and
|
|
|
+ // then switching mat FrontSide -> BackSide
|
|
|
+ // NB: this means that FrontSide and BackSide have been switched!
|
|
|
+ getSide( attributes ) {
|
|
|
|
|
|
+ if ( ! attributes.side ) return BackSide;
|
|
|
|
|
|
- },
|
|
|
+ switch ( attributes.side ) {
|
|
|
|
|
|
- // ENVL forms are currently ignored
|
|
|
- parseEnvelope( length ) {
|
|
|
+ case 0:
|
|
|
+ case 1:
|
|
|
+ return BackSide;
|
|
|
+ case 2: return FrontSide;
|
|
|
+ case 3: return DoubleSide;
|
|
|
|
|
|
- this.reader.skip( length - 4 ); // skipping entirely for now
|
|
|
+ }
|
|
|
|
|
|
- },
|
|
|
+ },
|
|
|
|
|
|
- ///
|
|
|
- // CHUNK PARSING METHODS
|
|
|
- ///
|
|
|
+ getSmooth( attributes ) {
|
|
|
|
|
|
- // clips can either be defined inside a surface node, or at the top
|
|
|
- // level and they have a different format in each case
|
|
|
- parseClip( length ) {
|
|
|
+ if ( ! attributes.smooth ) return true;
|
|
|
+ return ! attributes.smooth;
|
|
|
|
|
|
- var tag = this.reader.getIDTag();
|
|
|
+ },
|
|
|
|
|
|
- // inside surface node
|
|
|
- if ( tag === 'FORM' ) {
|
|
|
+ parseConnections( connections, nodes ) {
|
|
|
|
|
|
- this.reader.skip( 16 );
|
|
|
+ var materialConnections = {
|
|
|
+ maps: {}
|
|
|
+ };
|
|
|
|
|
|
- this.currentNode.fileName = this.reader.getString();
|
|
|
+ var inputName = connections.inputName;
|
|
|
+ var inputNodeName = connections.inputNodeName;
|
|
|
+ var nodeName = connections.nodeName;
|
|
|
|
|
|
- return;
|
|
|
+ var self = this;
|
|
|
+ inputName.forEach( function ( name, index ) {
|
|
|
+
|
|
|
+ if ( name === 'Material' ) {
|
|
|
+
|
|
|
+ var matNode = self.getNodeByRefName( inputNodeName[ index ], nodes );
|
|
|
+ materialConnections.attributes = matNode.attributes;
|
|
|
+ materialConnections.envMap = matNode.fileName;
|
|
|
+ materialConnections.name = inputNodeName[ index ];
|
|
|
|
|
|
}
|
|
|
|
|
|
- // otherwise top level
|
|
|
- this.reader.setOffset( this.reader.offset - 4 );
|
|
|
+ } );
|
|
|
|
|
|
- this.currentFormEnd = this.reader.offset + length;
|
|
|
- this.parentForm = this.currentForm;
|
|
|
+ nodeName.forEach( function ( name, index ) {
|
|
|
|
|
|
- this.reader.skip( 8 ); // unknown
|
|
|
+ if ( name === materialConnections.name ) {
|
|
|
|
|
|
- var texture = {
|
|
|
- index: this.reader.getUint32()
|
|
|
- };
|
|
|
- this.tree.textures.push( texture );
|
|
|
- this.currentForm = texture;
|
|
|
+ materialConnections.maps[ inputName[ index ] ] = self.getNodeByRefName( inputNodeName[ index ], nodes );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- parseClipLwo2( length ) {
|
|
|
+ } );
|
|
|
|
|
|
- var texture = {
|
|
|
- index: this.reader.getUint32(),
|
|
|
- fileName: ""
|
|
|
- };
|
|
|
+ return materialConnections;
|
|
|
|
|
|
- // seach STIL block
|
|
|
- while ( true ) {
|
|
|
+ },
|
|
|
|
|
|
- var tag = this.reader.getIDTag();
|
|
|
- var n_length = this.reader.getUint16();
|
|
|
- if ( tag === 'STIL' ) {
|
|
|
+ getNodeByRefName( refName, nodes ) {
|
|
|
|
|
|
- texture.fileName = this.reader.getString();
|
|
|
- break;
|
|
|
+ for ( var name in nodes ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( nodes[ name ].refName === refName ) return nodes[ name ];
|
|
|
|
|
|
- if ( n_length >= length ) {
|
|
|
+ }
|
|
|
|
|
|
- break;
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ parseTextureNodes( textureNodes ) {
|
|
|
+
|
|
|
+ var maps = {};
|
|
|
+
|
|
|
+ for ( var name in textureNodes ) {
|
|
|
+
|
|
|
+ var node = textureNodes[ name ];
|
|
|
+ var path = node.fileName;
|
|
|
+
|
|
|
+ if ( ! path ) return;
|
|
|
+
|
|
|
+ var texture = this.loadTexture( path );
|
|
|
+
|
|
|
+ if ( node.widthWrappingMode !== undefined ) texture.wrapS = this.getWrappingType( node.widthWrappingMode );
|
|
|
+ if ( node.heightWrappingMode !== undefined ) texture.wrapT = this.getWrappingType( node.heightWrappingMode );
|
|
|
+
|
|
|
+ switch ( name ) {
|
|
|
+
|
|
|
+ case 'Color':
|
|
|
+ maps.map = texture;
|
|
|
+ break;
|
|
|
+ case 'Roughness':
|
|
|
+ maps.roughnessMap = texture;
|
|
|
+ maps.roughness = 0.5;
|
|
|
+ break;
|
|
|
+ case 'Specular':
|
|
|
+ maps.specularMap = texture;
|
|
|
+ maps.specular = 0xffffff;
|
|
|
+ break;
|
|
|
+ case 'Luminous':
|
|
|
+ maps.emissiveMap = texture;
|
|
|
+ maps.emissive = 0x808080;
|
|
|
+ break;
|
|
|
+ case 'Luminous Color':
|
|
|
+ maps.emissive = 0x808080;
|
|
|
+ break;
|
|
|
+ case 'Metallic':
|
|
|
+ maps.metalnessMap = texture;
|
|
|
+ maps.metalness = 0.5;
|
|
|
+ break;
|
|
|
+ case 'Transparency':
|
|
|
+ case 'Alpha':
|
|
|
+ maps.alphaMap = texture;
|
|
|
+ maps.transparent = true;
|
|
|
+ break;
|
|
|
+ case 'Normal':
|
|
|
+ maps.normalMap = texture;
|
|
|
+ if ( node.amplitude !== undefined ) maps.normalScale = new Vector2( node.amplitude, node.amplitude );
|
|
|
+ break;
|
|
|
+ case 'Bump':
|
|
|
+ maps.bumpMap = texture;
|
|
|
+ break;
|
|
|
|
|
|
}
|
|
|
|
|
|
- this.tree.textures.push( texture );
|
|
|
- this.currentForm = texture;
|
|
|
+ }
|
|
|
|
|
|
- },
|
|
|
+ // LWO BSDF materials can have both spec and rough, but this is not valid in three
|
|
|
+ if ( maps.roughnessMap && maps.specularMap ) delete maps.specularMap;
|
|
|
|
|
|
- parseImage() {
|
|
|
+ return maps;
|
|
|
|
|
|
- this.reader.skip( 8 ); // unknown
|
|
|
- this.currentForm.fileName = this.reader.getString();
|
|
|
+ },
|
|
|
|
|
|
- },
|
|
|
+ // maps can also be defined on individual material attributes, parse those here
|
|
|
+ // This occurs on Standard (Phong) surfaces
|
|
|
+ parseAttributeImageMaps( attributes, textures, maps ) {
|
|
|
|
|
|
- parseXVAL( type, length ) {
|
|
|
+ for ( var name in attributes ) {
|
|
|
|
|
|
- var endOffset = this.reader.offset + length - 4;
|
|
|
- this.reader.skip( 8 );
|
|
|
+ var attribute = attributes[ name ];
|
|
|
|
|
|
- this.currentForm[ type ] = this.reader.getFloat32();
|
|
|
+ if ( attribute.maps ) {
|
|
|
|
|
|
- this.reader.setOffset( endOffset ); // set end offset directly to skip optional envelope
|
|
|
+ var mapData = attribute.maps[ 0 ];
|
|
|
|
|
|
- },
|
|
|
+ var path = this.getTexturePathByIndex( mapData.imageIndex, textures );
|
|
|
+ if ( ! path ) return;
|
|
|
|
|
|
- parseXVAL3( type, length ) {
|
|
|
+ var texture = this.loadTexture( path );
|
|
|
|
|
|
- var endOffset = this.reader.offset + length - 4;
|
|
|
- this.reader.skip( 8 );
|
|
|
+ if ( mapData.wrap !== undefined ) texture.wrapS = this.getWrappingType( mapData.wrap.w );
|
|
|
+ if ( mapData.wrap !== undefined ) texture.wrapT = this.getWrappingType( mapData.wrap.h );
|
|
|
|
|
|
- this.currentForm[ type ] = {
|
|
|
- x: this.reader.getFloat32(),
|
|
|
- y: this.reader.getFloat32(),
|
|
|
- z: this.reader.getFloat32(),
|
|
|
- };
|
|
|
+ switch ( name ) {
|
|
|
|
|
|
- this.reader.setOffset( endOffset );
|
|
|
+ case 'Color':
|
|
|
+ maps.map = texture;
|
|
|
+ break;
|
|
|
+ case 'Diffuse':
|
|
|
+ maps.aoMap = texture;
|
|
|
+ break;
|
|
|
+ case 'Roughness':
|
|
|
+ maps.roughnessMap = texture;
|
|
|
+ maps.roughness = 1;
|
|
|
+ break;
|
|
|
+ case 'Specular':
|
|
|
+ maps.specularMap = texture;
|
|
|
+ maps.specular = 0xffffff;
|
|
|
+ break;
|
|
|
+ case 'Luminosity':
|
|
|
+ maps.emissiveMap = texture;
|
|
|
+ maps.emissive = 0x808080;
|
|
|
+ break;
|
|
|
+ case 'Metallic':
|
|
|
+ maps.metalnessMap = texture;
|
|
|
+ maps.metalness = 1;
|
|
|
+ break;
|
|
|
+ case 'Transparency':
|
|
|
+ case 'Alpha':
|
|
|
+ maps.alphaMap = texture;
|
|
|
+ maps.transparent = true;
|
|
|
+ break;
|
|
|
+ case 'Normal':
|
|
|
+ maps.normalMap = texture;
|
|
|
+ break;
|
|
|
+ case 'Bump':
|
|
|
+ maps.bumpMap = texture;
|
|
|
+ break;
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- // Tags associated with an object
|
|
|
- // OTAG { type[ID4], tag-string[S0] }
|
|
|
- parseObjectTag() {
|
|
|
+ }
|
|
|
|
|
|
- if ( ! this.tree.objectTags ) this.tree.objectTags = {};
|
|
|
+ }
|
|
|
|
|
|
- this.tree.objectTags[ this.reader.getIDTag() ] = {
|
|
|
- tagString: this.reader.getString()
|
|
|
- };
|
|
|
+ },
|
|
|
|
|
|
- },
|
|
|
+ parseAttributes( attributes, maps ) {
|
|
|
|
|
|
- // Signals the start of a new layer. All the data chunks which follow will be included in this layer until another layer chunk is encountered.
|
|
|
- // LAYR: number[U2], flags[U2], pivot[VEC12], name[S0], parent[U2]
|
|
|
- parseLayer( length ) {
|
|
|
+ var params = {};
|
|
|
|
|
|
- var layer = {
|
|
|
- number: this.reader.getUint16(),
|
|
|
- flags: this.reader.getUint16(), // If the least significant bit of flags is set, the layer is hidden.
|
|
|
- pivot: this.reader.getFloat32Array( 3 ), // Note: this seems to be superflous, as the geometry is translated when pivot is present
|
|
|
- name: this.reader.getString(),
|
|
|
- };
|
|
|
+ // don't use color data if color map is present
|
|
|
+ if ( attributes.Color && ! maps.map ) {
|
|
|
|
|
|
- this.tree.layers.push( layer );
|
|
|
- this.currentLayer = layer;
|
|
|
+ params.color = new Color().fromArray( attributes.Color.value );
|
|
|
|
|
|
- var parsedLength = 16 + stringOffset( this.currentLayer.name ); // index ( 2 ) + flags( 2 ) + pivot( 12 ) + stringlength
|
|
|
+ } else params.color = new Color();
|
|
|
|
|
|
- // if we have not reached then end of the layer block, there must be a parent defined
|
|
|
- this.currentLayer.parent = ( parsedLength < length ) ? this.reader.getUint16() : - 1; // omitted or -1 for no parent
|
|
|
|
|
|
- },
|
|
|
+ if ( attributes.Transparency && attributes.Transparency.value !== 0 ) {
|
|
|
|
|
|
- // VEC12 * ( F4 + F4 + F4 ) array of x,y,z vectors
|
|
|
- // Converting from left to right handed coordinate system:
|
|
|
- // x -> -x and switch material FrontSide -> BackSide
|
|
|
- parsePoints( length ) {
|
|
|
+ params.opacity = 1 - attributes.Transparency.value;
|
|
|
+ params.transparent = true;
|
|
|
|
|
|
- this.currentPoints = [];
|
|
|
- for ( var i = 0; i < length / 4; i += 3 ) {
|
|
|
+ }
|
|
|
|
|
|
- // z -> -z to match three.js right handed coords
|
|
|
- this.currentPoints.push( this.reader.getFloat32(), this.reader.getFloat32(), - this.reader.getFloat32() );
|
|
|
+ if ( attributes[ 'Bump Height' ] ) params.bumpScale = attributes[ 'Bump Height' ].value * 0.1;
|
|
|
|
|
|
- }
|
|
|
+ if ( attributes[ 'Refraction Index' ] ) params.refractionRatio = 1 / attributes[ 'Refraction Index' ].value;
|
|
|
+
|
|
|
+ this.parsePhysicalAttributes( params, attributes, maps );
|
|
|
+ this.parseStandardAttributes( params, attributes, maps );
|
|
|
+ this.parsePhongAttributes( params, attributes, maps );
|
|
|
|
|
|
- },
|
|
|
+ return params;
|
|
|
|
|
|
- // parse VMAP or VMAD
|
|
|
- // Associates a set of floating-point vectors with a set of points.
|
|
|
- // VMAP: { type[ID4], dimension[U2], name[S0], ( vert[VX], value[F4] # dimension ) * }
|
|
|
+ },
|
|
|
|
|
|
- // VMAD Associates a set of floating-point vectors with the vertices of specific polygons.
|
|
|
- // Similar to VMAP UVs, but associates with polygon vertices rather than points
|
|
|
- // to solve to problem of UV seams: VMAD chunks are paired with VMAPs of the same name,
|
|
|
- // if they exist. The vector values in the VMAD will then replace those in the
|
|
|
- // corresponding VMAP, but only for calculations involving the specified polygons.
|
|
|
- // VMAD { type[ID4], dimension[U2], name[S0], ( vert[VX], poly[VX], value[F4] # dimension ) * }
|
|
|
- parseVertexMapping( length, discontinuous ) {
|
|
|
+ parsePhysicalAttributes( params, attributes/*, maps*/ ) {
|
|
|
|
|
|
- var finalOffset = this.reader.offset + length;
|
|
|
+ if ( attributes.Clearcoat && attributes.Clearcoat.value > 0 ) {
|
|
|
|
|
|
- var channelName = this.reader.getString();
|
|
|
+ params.clearCoat = attributes.Clearcoat.value;
|
|
|
|
|
|
- if ( this.reader.offset === finalOffset ) {
|
|
|
+ if ( attributes[ 'Clearcoat Gloss' ] ) {
|
|
|
|
|
|
- // then we are in a texture node and the VMAP chunk is just a reference to a UV channel name
|
|
|
- this.currentForm.UVChannel = channelName;
|
|
|
- return;
|
|
|
+ params.clearCoatRoughness = 0.5 * ( 1 - attributes[ 'Clearcoat Gloss' ].value );
|
|
|
|
|
|
}
|
|
|
|
|
|
- // otherwise reset to initial length and parse normal VMAP CHUNK
|
|
|
- this.reader.setOffset( this.reader.offset - stringOffset( channelName ) );
|
|
|
+ }
|
|
|
|
|
|
- var type = this.reader.getIDTag();
|
|
|
+ },
|
|
|
|
|
|
- this.reader.getUint16(); // dimension
|
|
|
- var name = this.reader.getString();
|
|
|
+ parseStandardAttributes( params, attributes, maps ) {
|
|
|
|
|
|
- var remainingLength = length - 6 - stringOffset( name );
|
|
|
|
|
|
- switch ( type ) {
|
|
|
+ if ( attributes.Luminous ) {
|
|
|
|
|
|
- case 'TXUV':
|
|
|
- this.parseUVMapping( name, finalOffset, discontinuous );
|
|
|
- break;
|
|
|
- case 'MORF':
|
|
|
- case 'SPOT':
|
|
|
- this.parseMorphTargets( name, finalOffset, type ); // can't be discontinuous
|
|
|
- break;
|
|
|
- // unsupported VMAPs
|
|
|
- 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 );
|
|
|
+ params.emissiveIntensity = attributes.Luminous.value;
|
|
|
+
|
|
|
+ if ( attributes[ 'Luminous Color' ] && ! maps.emissive ) {
|
|
|
+
|
|
|
+ params.emissive = new Color().fromArray( attributes[ 'Luminous Color' ].value );
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ params.emissive = new Color( 0x808080 );
|
|
|
|
|
|
}
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- parseUVMapping( name, finalOffset, discontinuous ) {
|
|
|
+ if ( attributes.Roughness && ! maps.roughnessMap ) params.roughness = attributes.Roughness.value;
|
|
|
+ if ( attributes.Metallic && ! maps.metalnessMap ) params.metalness = attributes.Metallic.value;
|
|
|
|
|
|
- var uvIndices = [];
|
|
|
- var polyIndices = [];
|
|
|
- var uvs = [];
|
|
|
+ },
|
|
|
|
|
|
- while ( this.reader.offset < finalOffset ) {
|
|
|
+ parsePhongAttributes( params, attributes, maps ) {
|
|
|
|
|
|
- uvIndices.push( this.reader.getVariableLengthIndex() );
|
|
|
+ if ( attributes.Diffuse ) params.color.multiplyScalar( attributes.Diffuse.value );
|
|
|
|
|
|
- if ( discontinuous ) polyIndices.push( this.reader.getVariableLengthIndex() );
|
|
|
+ if ( attributes.Reflection ) {
|
|
|
|
|
|
- uvs.push( this.reader.getFloat32(), this.reader.getFloat32() );
|
|
|
+ params.reflectivity = attributes.Reflection.value;
|
|
|
+ params.combine = AddOperation;
|
|
|
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- if ( discontinuous ) {
|
|
|
+ if ( attributes.Luminosity ) {
|
|
|
|
|
|
- if ( ! this.currentLayer.discontinuousUVs ) this.currentLayer.discontinuousUVs = {};
|
|
|
+ params.emissiveIntensity = attributes.Luminosity.value;
|
|
|
|
|
|
- this.currentLayer.discontinuousUVs[ name ] = {
|
|
|
- uvIndices: uvIndices,
|
|
|
- polyIndices: polyIndices,
|
|
|
- uvs: uvs,
|
|
|
- };
|
|
|
+ if ( ! maps.emissiveMap && ! maps.map ) {
|
|
|
|
|
|
- } else {
|
|
|
+ params.emissive = params.color;
|
|
|
|
|
|
- if ( ! this.currentLayer.uvs ) this.currentLayer.uvs = {};
|
|
|
+ } else {
|
|
|
|
|
|
- this.currentLayer.uvs[ name ] = {
|
|
|
- uvIndices: uvIndices,
|
|
|
- uvs: uvs,
|
|
|
- };
|
|
|
+ params.emissive = new Color( 0x808080 );
|
|
|
|
|
|
}
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- parseMorphTargets( name, finalOffset, type ) {
|
|
|
+ // parse specular if there is no roughness - we will interpret the material as 'Phong' in this case
|
|
|
+ if ( ! attributes.Roughness && attributes.Specular && ! maps.specularMap ) {
|
|
|
|
|
|
- var indices = [];
|
|
|
- var points = [];
|
|
|
+ if ( attributes[ 'Color Highlight' ] ) {
|
|
|
|
|
|
- type = ( type === 'MORF' ) ? 'relative' : 'absolute';
|
|
|
+ params.specular = new Color().setScalar( attributes.Specular.value ).lerp( params.color.clone().multiplyScalar( attributes.Specular.value ), attributes[ 'Color Highlight' ].value );
|
|
|
|
|
|
- while ( this.reader.offset < finalOffset ) {
|
|
|
+ } else {
|
|
|
|
|
|
- indices.push( this.reader.getVariableLengthIndex() );
|
|
|
- // z -> -z to match three.js right handed coords
|
|
|
- points.push( this.reader.getFloat32(), this.reader.getFloat32(), - this.reader.getFloat32() );
|
|
|
+ params.specular = new Color().setScalar( attributes.Specular.value );
|
|
|
|
|
|
}
|
|
|
|
|
|
- if ( ! this.currentLayer.morphTargets ) this.currentLayer.morphTargets = {};
|
|
|
+ }
|
|
|
|
|
|
- this.currentLayer.morphTargets[ name ] = {
|
|
|
- indices: indices,
|
|
|
- points: points,
|
|
|
- type: type,
|
|
|
- };
|
|
|
+ if ( params.specular && attributes.Glossiness ) params.shininess = 7 + Math.pow( 2, attributes.Glossiness.value * 12 + 2 );
|
|
|
|
|
|
- },
|
|
|
+ },
|
|
|
|
|
|
- // A list of polygons for the current layer.
|
|
|
- // POLS { type[ID4], ( numvert+flags[U2], vert[VX] # numvert ) * }
|
|
|
- parsePolygonList( length ) {
|
|
|
+ parseEnvMap( connections, maps, attributes ) {
|
|
|
|
|
|
- var finalOffset = this.reader.offset + length;
|
|
|
- var type = this.reader.getIDTag();
|
|
|
+ if ( connections.envMap ) {
|
|
|
|
|
|
- var indices = [];
|
|
|
+ var envMap = this.loadTexture( connections.envMap );
|
|
|
|
|
|
- // hold a list of polygon sizes, to be split up later
|
|
|
- var polygonDimensions = [];
|
|
|
+ if ( attributes.transparent && attributes.opacity < 0.999 ) {
|
|
|
|
|
|
- while ( this.reader.offset < finalOffset ) {
|
|
|
+ envMap.mapping = EquirectangularRefractionMapping;
|
|
|
|
|
|
- var numverts = this.reader.getUint16();
|
|
|
+ // Reflectivity and refraction mapping don't work well together in Phong materials
|
|
|
+ if ( attributes.reflectivity !== undefined ) {
|
|
|
|
|
|
- //var flags = numverts & 64512; // 6 high order bits are flags - ignoring for now
|
|
|
- numverts = numverts & 1023; // remaining ten low order bits are vertex num
|
|
|
- polygonDimensions.push( numverts );
|
|
|
+ delete attributes.reflectivity;
|
|
|
+ delete attributes.combine;
|
|
|
|
|
|
- for ( var j = 0; j < numverts; j ++ ) indices.push( this.reader.getVariableLengthIndex() );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( attributes.metalness !== undefined ) {
|
|
|
|
|
|
- var geometryData = {
|
|
|
- type: type,
|
|
|
- vertexIndices: indices,
|
|
|
- polygonDimensions: polygonDimensions,
|
|
|
- points: this.currentPoints
|
|
|
- };
|
|
|
+ delete attributes.metalness;
|
|
|
|
|
|
- // Note: assuming that all polys will be lines or points if the first is
|
|
|
- if ( polygonDimensions[ 0 ] === 1 ) geometryData.type = 'points';
|
|
|
- else if ( polygonDimensions[ 0 ] === 2 ) geometryData.type = 'lines';
|
|
|
+ }
|
|
|
|
|
|
- this.currentLayer.geometry = geometryData;
|
|
|
+ } else envMap.mapping = EquirectangularReflectionMapping;
|
|
|
|
|
|
- },
|
|
|
+ maps.envMap = envMap;
|
|
|
|
|
|
- // Lists the tag strings that can be associated with polygons by the PTAG chunk.
|
|
|
- // TAGS { tag-string[S0] * }
|
|
|
- parseTagStrings( length ) {
|
|
|
+ }
|
|
|
|
|
|
- this.tree.tags = this.reader.getStringArray( length );
|
|
|
+ },
|
|
|
|
|
|
- },
|
|
|
+ // get texture defined at top level by its index
|
|
|
+ getTexturePathByIndex( index ) {
|
|
|
|
|
|
- // Associates tags of a given type with polygons in the most recent POLS chunk.
|
|
|
- // PTAG { type[ID4], ( poly[VX], tag[U2] ) * }
|
|
|
- parsePolygonTagMapping( length ) {
|
|
|
+ var fileName = '';
|
|
|
|
|
|
- var finalOffset = this.reader.offset + length;
|
|
|
- var type = this.reader.getIDTag();
|
|
|
- if ( type === 'SURF' ) this.parseMaterialIndices( finalOffset );
|
|
|
- else { //PART, SMGP, COLR not supported
|
|
|
+ if ( ! lwoTree.textures ) return fileName;
|
|
|
|
|
|
- this.reader.skip( length - 4 );
|
|
|
+ lwoTree.textures.forEach( function ( texture ) {
|
|
|
|
|
|
- }
|
|
|
+ if ( texture.index === index ) fileName = texture.fileName;
|
|
|
|
|
|
- },
|
|
|
+ } );
|
|
|
|
|
|
- parseMaterialIndices( finalOffset ) {
|
|
|
+ return fileName;
|
|
|
|
|
|
- // array holds polygon index followed by material index
|
|
|
- this.currentLayer.geometry.materialIndices = [];
|
|
|
+ },
|
|
|
|
|
|
- var initialMatIndex;
|
|
|
+ loadTexture( path ) {
|
|
|
|
|
|
- while ( this.reader.offset < finalOffset ) {
|
|
|
+ if ( ! path ) return null;
|
|
|
|
|
|
- var polygonIndex = this.reader.getVariableLengthIndex();
|
|
|
- var materialIndex = this.reader.getUint16();
|
|
|
+ var texture;
|
|
|
|
|
|
- if ( ! initialMatIndex ) initialMatIndex = materialIndex; // set up first mat index
|
|
|
+ texture = this.textureLoader.load(
|
|
|
+ path,
|
|
|
+ undefined,
|
|
|
+ undefined,
|
|
|
+ function () {
|
|
|
|
|
|
- this.currentLayer.geometry.materialIndices.push( polygonIndex, materialIndex );
|
|
|
+ console.warn( 'LWOLoader: non-standard resource hierarchy. Use \`resourcePath\` parameter to specify root content directory.' );
|
|
|
|
|
|
}
|
|
|
+ );
|
|
|
|
|
|
- },
|
|
|
-
|
|
|
- parseUnknownCHUNK( blockID, length ) {
|
|
|
+ return texture;
|
|
|
|
|
|
- console.warn( 'LWOLoader: unknown chunk type: ' + blockID + ' length: ' + length );
|
|
|
+ },
|
|
|
|
|
|
- // print the chunk plus some bytes padding either side
|
|
|
- // printBuffer( this.reader.dv.buffer, this.reader.offset - 20, length + 40 );
|
|
|
+ // 0 = Reset, 1 = Repeat, 2 = Mirror, 3 = Edge
|
|
|
+ getWrappingType( num ) {
|
|
|
|
|
|
- var data = this.reader.getString( length );
|
|
|
+ switch ( num ) {
|
|
|
|
|
|
- this.currentForm[ blockID ] = data;
|
|
|
+ case 0:
|
|
|
+ console.warn( 'LWOLoader: "Reset" texture wrapping type is not supported in three.js' );
|
|
|
+ return ClampToEdgeWrapping;
|
|
|
+ case 1: return RepeatWrapping;
|
|
|
+ case 2: return MirroredRepeatWrapping;
|
|
|
+ case 3: return ClampToEdgeWrapping;
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
-
|
|
|
- function DataViewReader( buffer ) {
|
|
|
+ },
|
|
|
|
|
|
- // For testing: dump whole buffer to console as a string
|
|
|
- // printBuffer( buffer, 0, buffer.byteLength );
|
|
|
+ getMaterialType( nodeData ) {
|
|
|
|
|
|
- this.dv = new DataView( buffer );
|
|
|
- this.offset = 0;
|
|
|
+ if ( nodeData.Clearcoat && nodeData.Clearcoat.value > 0 ) return MeshPhysicalMaterial;
|
|
|
+ if ( nodeData.Roughness ) return MeshStandardMaterial;
|
|
|
+ return MeshPhongMaterial;
|
|
|
|
|
|
}
|
|
|
|
|
|
- DataViewReader.prototype = {
|
|
|
+};
|
|
|
|
|
|
- constructor: DataViewReader,
|
|
|
+function GeometryParser() {}
|
|
|
|
|
|
- size: function () {
|
|
|
+GeometryParser.prototype = {
|
|
|
|
|
|
- return this.dv.buffer.byteLength;
|
|
|
+ constructor: GeometryParser,
|
|
|
|
|
|
- },
|
|
|
+ parse( geoData, layer ) {
|
|
|
|
|
|
- setOffset( offset ) {
|
|
|
+ var geometry = new BufferGeometry();
|
|
|
|
|
|
- if ( offset > 0 && offset < this.dv.buffer.byteLength ) {
|
|
|
+ geometry.addAttribute( 'position', new Float32BufferAttribute( geoData.points, 3 ) );
|
|
|
|
|
|
- this.offset = offset;
|
|
|
+ var indices = this.splitIndices( geoData.vertexIndices, geoData.polygonDimensions );
|
|
|
+ geometry.setIndex( indices );
|
|
|
|
|
|
- } else {
|
|
|
+ this.parseGroups( geometry, geoData );
|
|
|
|
|
|
- console.error( 'LWOLoader: invalid buffer offset' );
|
|
|
+ geometry.computeVertexNormals();
|
|
|
|
|
|
- }
|
|
|
+ this.parseUVs( geometry, layer, indices );
|
|
|
+ this.parseMorphTargets( geometry, layer, indices );
|
|
|
|
|
|
- },
|
|
|
+ // TODO: z may need to be reversed to account for coordinate system change
|
|
|
+ geometry.translate( - layer.pivot[ 0 ], - layer.pivot[ 1 ], - layer.pivot[ 2 ] );
|
|
|
|
|
|
- endOfFile: function () {
|
|
|
+ // var userData = geometry.userData;
|
|
|
+ // geometry = geometry.toNonIndexed()
|
|
|
+ // geometry.userData = userData;
|
|
|
|
|
|
- if ( this.offset >= this.size() ) return true;
|
|
|
- return false;
|
|
|
+ return geometry;
|
|
|
|
|
|
- },
|
|
|
+ },
|
|
|
|
|
|
- skip: function ( length ) {
|
|
|
+ // split quads into tris
|
|
|
+ splitIndices( indices, polygonDimensions ) {
|
|
|
|
|
|
- this.offset += length;
|
|
|
+ var remappedIndices = [];
|
|
|
|
|
|
- },
|
|
|
+ var i = 0;
|
|
|
+ polygonDimensions.forEach( function ( dim ) {
|
|
|
|
|
|
- getUint8: function () {
|
|
|
+ if ( dim < 4 ) {
|
|
|
|
|
|
- var value = this.dv.getUint8( this.offset );
|
|
|
- this.offset += 1;
|
|
|
- return value;
|
|
|
+ for ( var k = 0; k < dim; k ++ ) remappedIndices.push( indices[ i + k ] );
|
|
|
|
|
|
- },
|
|
|
+ } else if ( dim === 4 ) {
|
|
|
|
|
|
- getUint16: function () {
|
|
|
+ remappedIndices.push(
|
|
|
+ indices[ i ],
|
|
|
+ indices[ i + 1 ],
|
|
|
+ indices[ i + 2 ],
|
|
|
|
|
|
- var value = this.dv.getUint16( this.offset );
|
|
|
- this.offset += 2;
|
|
|
- return value;
|
|
|
+ indices[ i ],
|
|
|
+ indices[ i + 2 ],
|
|
|
+ indices[ i + 3 ]
|
|
|
|
|
|
- },
|
|
|
+ );
|
|
|
|
|
|
- getInt32: function () {
|
|
|
+ } else if ( dim > 4 ) {
|
|
|
|
|
|
- var value = this.dv.getInt32( this.offset, false );
|
|
|
- this.offset += 4;
|
|
|
- return value;
|
|
|
+ for ( var k = 1; k < dim - 1; k ++ ) {
|
|
|
|
|
|
- },
|
|
|
+ remappedIndices.push( indices[ i ], indices[ i + k ], indices[ i + k + 1 ] );
|
|
|
|
|
|
- getUint32: function () {
|
|
|
+ }
|
|
|
|
|
|
- var value = this.dv.getUint32( this.offset, false );
|
|
|
- this.offset += 4;
|
|
|
- return value;
|
|
|
+ console.warn( 'LWOLoader: polygons with greater than 4 sides are not supported' );
|
|
|
|
|
|
- },
|
|
|
+ }
|
|
|
|
|
|
- getUint64: function () {
|
|
|
+ i += dim;
|
|
|
|
|
|
- var low, high;
|
|
|
+ } );
|
|
|
|
|
|
- high = this.getUint32();
|
|
|
- low = this.getUint32();
|
|
|
- return high * 0x100000000 + low;
|
|
|
+ return remappedIndices;
|
|
|
|
|
|
- },
|
|
|
+ },
|
|
|
|
|
|
- getFloat32: function () {
|
|
|
+ // NOTE: currently ignoring poly indices and assuming that they are intelligently ordered
|
|
|
+ parseGroups( geometry, geoData ) {
|
|
|
|
|
|
- var value = this.dv.getFloat32( this.offset, false );
|
|
|
- this.offset += 4;
|
|
|
- return value;
|
|
|
+ var tags = lwoTree.tags;
|
|
|
+ var matNames = [];
|
|
|
|
|
|
- },
|
|
|
+ var elemSize = 3;
|
|
|
+ if ( geoData.type === 'lines' ) elemSize = 2;
|
|
|
+ if ( geoData.type === 'points' ) elemSize = 1;
|
|
|
|
|
|
- getFloat32Array: function ( size ) {
|
|
|
+ var remappedIndices = this.splitMaterialIndices( geoData.polygonDimensions, geoData.materialIndices );
|
|
|
|
|
|
- var a = [];
|
|
|
+ var indexNum = 0; // create new indices in numerical order
|
|
|
+ var indexPairs = {}; // original indices mapped to numerical indices
|
|
|
|
|
|
- for ( var i = 0; i < size; i ++ ) {
|
|
|
+ var prevMaterialIndex;
|
|
|
|
|
|
- a.push( this.getFloat32() );
|
|
|
+ var prevStart = 0;
|
|
|
+ var currentCount = 0;
|
|
|
|
|
|
- }
|
|
|
+ for ( var i = 0; i < remappedIndices.length; i += 2 ) {
|
|
|
|
|
|
- return a;
|
|
|
+ var materialIndex = remappedIndices[ i + 1 ];
|
|
|
|
|
|
- },
|
|
|
+ if ( i === 0 ) matNames[ indexNum ] = tags[ materialIndex ];
|
|
|
|
|
|
- getFloat64: function () {
|
|
|
+ if ( prevMaterialIndex === undefined ) prevMaterialIndex = materialIndex;
|
|
|
|
|
|
- var value = this.dv.getFloat64( this.offset, this.littleEndian );
|
|
|
- this.offset += 8;
|
|
|
- return value;
|
|
|
+ if ( materialIndex !== prevMaterialIndex ) {
|
|
|
|
|
|
- },
|
|
|
+ var currentIndex;
|
|
|
+ if ( indexPairs[ tags[ prevMaterialIndex ] ] ) {
|
|
|
|
|
|
- getFloat64Array: function ( size ) {
|
|
|
+ currentIndex = indexPairs[ tags[ prevMaterialIndex ] ];
|
|
|
|
|
|
- var a = [];
|
|
|
+ } else {
|
|
|
|
|
|
- for ( var i = 0; i < size; i ++ ) {
|
|
|
+ currentIndex = indexNum;
|
|
|
+ indexPairs[ tags[ prevMaterialIndex ] ] = indexNum;
|
|
|
+ matNames[ indexNum ] = tags[ prevMaterialIndex ];
|
|
|
+ indexNum ++;
|
|
|
|
|
|
- a.push( this.getFloat64() );
|
|
|
+ }
|
|
|
+
|
|
|
+ geometry.addGroup( prevStart, currentCount, currentIndex );
|
|
|
+
|
|
|
+ prevStart += currentCount;
|
|
|
+
|
|
|
+ prevMaterialIndex = materialIndex;
|
|
|
+ currentCount = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
- return a;
|
|
|
+ currentCount += elemSize;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
- },
|
|
|
+ // the loop above doesn't add the last group, do that here.
|
|
|
+ if ( geometry.groups.length > 0 ) {
|
|
|
|
|
|
- // get variable-length index data type
|
|
|
- // VX ::= index[U2] | (index + 0xFF000000)[U4]
|
|
|
- // If the index value is less than 65,280 (0xFF00),then VX === U2
|
|
|
- // otherwise VX === U4 with bits 24-31 set
|
|
|
- // When reading an index, if the first byte encountered is 255 (0xFF), then
|
|
|
- // the four-byte form is being used and the first byte should be discarded or masked out.
|
|
|
- getVariableLengthIndex() {
|
|
|
+ var currentIndex;
|
|
|
+ if ( indexPairs[ tags[ materialIndex ] ] ) {
|
|
|
|
|
|
- var firstByte = this.getUint8();
|
|
|
+ currentIndex = indexPairs[ tags[ materialIndex ] ];
|
|
|
|
|
|
- if ( firstByte === 255 ) {
|
|
|
+ } else {
|
|
|
|
|
|
- return this.getUint8() * 65536 + this.getUint8() * 256 + this.getUint8();
|
|
|
+ currentIndex = indexNum;
|
|
|
+ indexPairs[ tags[ materialIndex ] ] = indexNum;
|
|
|
+ matNames[ indexNum ] = tags[ materialIndex ];
|
|
|
|
|
|
}
|
|
|
|
|
|
- return firstByte * 256 + this.getUint8();
|
|
|
+ geometry.addGroup( prevStart, currentCount, currentIndex );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // Mat names from TAGS chunk, used to build up an array of materials for this geometry
|
|
|
+ geometry.userData.matNames = matNames;
|
|
|
+
|
|
|
+ },
|
|
|
|
|
|
- },
|
|
|
+ splitMaterialIndices( polygonDimensions, indices ) {
|
|
|
|
|
|
- // An ID tag is a sequence of 4 bytes containing 7-bit ASCII values
|
|
|
- getIDTag() {
|
|
|
+ var remappedIndices = [];
|
|
|
|
|
|
- return this.getString( 4 );
|
|
|
+ polygonDimensions.forEach( function ( dim, i ) {
|
|
|
|
|
|
- },
|
|
|
+ if ( dim <= 3 ) {
|
|
|
|
|
|
- getString: function ( size ) {
|
|
|
+ remappedIndices.push( indices[ i * 2 ], indices[ i * 2 + 1 ] );
|
|
|
|
|
|
- if ( size === 0 ) return;
|
|
|
+ } else if ( dim === 4 ) {
|
|
|
|
|
|
- // note: safari 9 doesn't support Uint8Array.indexOf; create intermediate array instead
|
|
|
- var a = [];
|
|
|
+ remappedIndices.push( indices[ i * 2 ], indices[ i * 2 + 1 ], indices[ i * 2 ], indices[ i * 2 + 1 ] );
|
|
|
|
|
|
- if ( size ) {
|
|
|
+ } else {
|
|
|
|
|
|
- for ( var i = 0; i < size; i ++ ) {
|
|
|
+ // ignore > 4 for now
|
|
|
+ for ( var k = 0; k < dim - 2; k ++ ) {
|
|
|
|
|
|
- a[ i ] = this.getUint8();
|
|
|
+ remappedIndices.push( indices[ i * 2 ], indices[ i * 2 + 1 ] );
|
|
|
|
|
|
}
|
|
|
|
|
|
- } else {
|
|
|
+ }
|
|
|
|
|
|
- var currentChar;
|
|
|
- var len = 0;
|
|
|
+ } );
|
|
|
|
|
|
- while ( currentChar !== 0 ) {
|
|
|
+ return remappedIndices;
|
|
|
|
|
|
- currentChar = this.getUint8();
|
|
|
- if ( currentChar !== 0 ) a.push( currentChar );
|
|
|
- len ++;
|
|
|
+ },
|
|
|
|
|
|
- }
|
|
|
+ // UV maps:
|
|
|
+ // 1: are defined via index into an array of points, not into a geometry
|
|
|
+ // - the geometry is also defined by an index into this array, but the indexes may not match
|
|
|
+ // 2: there can be any number of UV maps for a single geometry. Here these are combined,
|
|
|
+ // with preference given to the first map encountered
|
|
|
+ // 3: UV maps can be partial - that is, defined for only a part of the geometry
|
|
|
+ // 4: UV maps can be VMAP or VMAD (discontinuous, to allow for seams). In practice, most
|
|
|
+ // UV maps are defined as partially VMAP and partially VMAD
|
|
|
+ // VMADs are currently not supported
|
|
|
+ parseUVs( geometry, layer ) {
|
|
|
|
|
|
- if ( ! isEven( len + 1 ) ) this.getUint8(); // if string with terminating nullbyte is uneven, extra nullbyte is added
|
|
|
+ // start by creating a UV map set to zero for the whole geometry
|
|
|
+ var remappedUVs = Array.from( Array( geometry.attributes.position.count * 2 ), function () {
|
|
|
|
|
|
- }
|
|
|
+ return 0;
|
|
|
|
|
|
- return LoaderUtils.decodeText( new Uint8Array( a ) );
|
|
|
+ } );
|
|
|
|
|
|
- },
|
|
|
+ for ( var name in layer.uvs ) {
|
|
|
|
|
|
- getStringArray: function ( size ) {
|
|
|
+ var uvs = layer.uvs[ name ].uvs;
|
|
|
+ var uvIndices = layer.uvs[ name ].uvIndices;
|
|
|
|
|
|
- var a = this.getString( size );
|
|
|
- a = a.split( '\0' );
|
|
|
+ uvIndices.forEach( function ( i, j ) {
|
|
|
|
|
|
- return a.filter( Boolean ); // return array with any empty strings removed
|
|
|
+ remappedUVs[ i * 2 ] = uvs[ j * 2 ];
|
|
|
+ remappedUVs[ i * 2 + 1 ] = uvs[ j * 2 + 1 ];
|
|
|
+
|
|
|
+ } );
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ geometry.addAttribute( 'uv', new Float32BufferAttribute( remappedUVs, 2 ) );
|
|
|
|
|
|
- // ************** UTILITY FUNCTIONS **************
|
|
|
+ },
|
|
|
|
|
|
- function isEven( num ) {
|
|
|
+ parseMorphTargets( geometry, layer ) {
|
|
|
|
|
|
- return num % 2;
|
|
|
+ var num = 0;
|
|
|
+ for ( var name in layer.morphTargets ) {
|
|
|
|
|
|
- }
|
|
|
+ var remappedPoints = geometry.attributes.position.array.slice();
|
|
|
|
|
|
- // calculate the length of the string in the buffer
|
|
|
- // this will be string.length + nullbyte + optional padbyte to make the length even
|
|
|
- function stringOffset( string ) {
|
|
|
+ if ( ! geometry.morphAttributes.position ) geometry.morphAttributes.position = [];
|
|
|
|
|
|
- return string.length + 1 + ( isEven( string.length + 1 ) ? 1 : 0 );
|
|
|
+ var morphPoints = layer.morphTargets[ name ].points;
|
|
|
+ var morphIndices = layer.morphTargets[ name ].indices;
|
|
|
+ var type = layer.morphTargets[ name ].type;
|
|
|
|
|
|
- }
|
|
|
+ morphIndices.forEach( function ( i, j ) {
|
|
|
|
|
|
- // for testing purposes, dump buffer to console
|
|
|
- // printBuffer( this.reader.dv.buffer, this.reader.offset, length );
|
|
|
- function printBuffer( buffer, from, to ) {
|
|
|
+ if ( type === 'relative' ) {
|
|
|
|
|
|
- console.log( LoaderUtils.decodeText( new Uint8Array( buffer, from, to ) ) );
|
|
|
+ remappedPoints[ i * 3 ] += morphPoints[ j * 3 ];
|
|
|
+ remappedPoints[ i * 3 + 1 ] += morphPoints[ j * 3 + 1 ];
|
|
|
+ remappedPoints[ i * 3 + 2 ] += morphPoints[ j * 3 + 2 ];
|
|
|
|
|
|
- }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ remappedPoints[ i * 3 ] = morphPoints[ j * 3 ];
|
|
|
+ remappedPoints[ i * 3 + 1 ] = morphPoints[ j * 3 + 1 ];
|
|
|
+ remappedPoints[ i * 3 + 2 ] = morphPoints[ j * 3 + 2 ];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+ geometry.morphAttributes.position[ num ] = new Float32BufferAttribute( remappedPoints, 3 );
|
|
|
+ geometry.morphAttributes.position[ num ].name = name;
|
|
|
+
|
|
|
+ num ++;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+// ************** UTILITY FUNCTIONS **************
|
|
|
+
|
|
|
+function extractParentUrl( url, dir ) {
|
|
|
+
|
|
|
+ var index = url.indexOf( dir );
|
|
|
+
|
|
|
+ if ( index === - 1 ) return './';
|
|
|
|
|
|
- return LWOLoader;
|
|
|
+ return url.substr( 0, index );
|
|
|
|
|
|
-} )();
|
|
|
+}
|
|
|
|
|
|
export { LWOLoader };
|