1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735 |
- THREE.XLoader = ( function () {
- var classCallCheck = function ( instance, Constructor ) {
- if ( ! ( instance instanceof Constructor ) ) {
- throw new TypeError( 'Cannot call a class as a function' );
- }
- };
- var createClass = function () {
- function defineProperties( target, props ) {
- for ( var i = 0; i < props.length; i ++ ) {
- var descriptor = props[ i ];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ( 'value' in descriptor ) descriptor.writable = true;
- Object.defineProperty( target, descriptor.key, descriptor );
- }
- }
- return function ( Constructor, protoProps, staticProps ) {
- if ( protoProps ) defineProperties( Constructor.prototype, protoProps );
- if ( staticProps ) defineProperties( Constructor, staticProps );
- return Constructor;
- };
- }();
- var XboneInf = function XboneInf() {
- classCallCheck( this, XboneInf );
- this.boneName = '';
- this.BoneIndex = 0;
- this.Indeces = [];
- this.Weights = [];
- this.initMatrix = null;
- this.OffsetMatrix = null;
- };
- var XAnimationInfo = function XAnimationInfo() {
- classCallCheck( this, XAnimationInfo );
- this.animeName = '';
- this.boneName = '';
- this.targetBone = null;
- this.keyType = 4;
- this.frameStartLv = 0;
- this.keyFrames = [];
- this.InverseMx = null;
- };
- var XAnimationObj = function () {
- function XAnimationObj( _flags ) {
- classCallCheck( this, XAnimationObj );
- this.fps = 30;
- this.name = 'xanimation';
- this.length = 0;
- this.hierarchy = [];
- this.putFlags = _flags;
- if ( this.putFlags.putPos === undefined ) {
- this.putFlags.putPos = true;
- }
- if ( this.putFlags.putRot === undefined ) {
- this.putFlags.putRot = true;
- }
- if ( this.putFlags.putScl === undefined ) {
- this.putFlags.putScl = true;
- }
- }
- createClass( XAnimationObj, [ {
- key: 'make',
- value: function make( XAnimationInfoArray ) {
- for ( var i = 0; i < XAnimationInfoArray.length; i ++ ) {
- this.hierarchy.push( this.makeBonekeys( XAnimationInfoArray[ i ] ) );
- }
- this.length = this.hierarchy[ 0 ].keys[ this.hierarchy[ 0 ].keys.length - 1 ].time;
- }
- }, {
- key: 'clone',
- value: function clone() {
- return Object.assign( {}, this );
- }
- }, {
- key: 'makeBonekeys',
- value: function makeBonekeys( XAnimationInfo ) {
- var refObj = {};
- refObj.name = XAnimationInfo.boneName;
- refObj.parent = '';
- refObj.keys = this.keyFrameRefactor( XAnimationInfo );
- refObj.copy = function () {
- return Object.assign( {}, this );
- };
- return refObj;
- }
- }, {
- key: 'keyFrameRefactor',
- value: function keyFrameRefactor( XAnimationInfo ) {
- var keys = [];
- for ( var i = 0; i < XAnimationInfo.keyFrames.length; i ++ ) {
- var keyframe = {};
- keyframe.time = XAnimationInfo.keyFrames[ i ].time * this.fps;
- if ( XAnimationInfo.keyFrames[ i ].pos && this.putFlags.putPos ) {
- keyframe.pos = XAnimationInfo.keyFrames[ i ].pos;
- }
- if ( XAnimationInfo.keyFrames[ i ].rot && this.putFlags.putRot ) {
- keyframe.rot = XAnimationInfo.keyFrames[ i ].rot;
- }
- if ( XAnimationInfo.keyFrames[ i ].scl && this.putFlags.putScl ) {
- keyframe.scl = XAnimationInfo.keyFrames[ i ].scl;
- }
- if ( XAnimationInfo.keyFrames[ i ].matrix ) {
- keyframe.matrix = XAnimationInfo.keyFrames[ i ].matrix;
- if ( this.putFlags.putPos ) {
- keyframe.pos = new THREE.Vector3().setFromMatrixPosition( keyframe.matrix );
- }
- if ( this.putFlags.putRot ) {
- keyframe.rot = new THREE.Quaternion().setFromRotationMatrix( keyframe.matrix );
- }
- if ( this.putFlags.putScl ) {
- keyframe.scl = new THREE.Vector3().setFromMatrixScale( keyframe.matrix );
- }
- }
- keys.push( keyframe );
- }
- return keys;
- }
- } ] );
- return XAnimationObj;
- }();
- var XKeyFrameInfo = function XKeyFrameInfo() {
- classCallCheck( this, XKeyFrameInfo );
- this.index = 0;
- this.Frame = 0;
- this.time = 0.0;
- this.matrix = null;
- };
- var XLoader = function () {
- function XLoader( manager ) {
- THREE.Loader.call( this, manager );
- classCallCheck( this, XLoader );
- this.debug = false;
- this.texloader = new THREE.TextureLoader( this.manager );
- this.url = '';
- this._putMatLength = 0;
- this._nowMat = null;
- this._nowFrameName = '';
- this.frameHierarchie = [];
- this.Hierarchies = {};
- this.HieStack = [];
- this._currentObject = {};
- this._currentFrame = {};
- this._data = null;
- this.onLoad = null;
- this.IsUvYReverse = true;
- this.Meshes = [];
- this.animations = [];
- this.animTicksPerSecond = 30;
- this._currentGeo = null;
- this._currentAnime = null;
- this._currentAnimeFrames = null;
- }
- createClass( XLoader, [ {
- key: '_setArgOption',
- value: function _setArgOption( _arg ) {
- var _start = arguments.length > 1 && arguments[ 1 ] !== undefined ? arguments[ 1 ] : 0;
- if ( ! _arg ) {
- return;
- }
- for ( var i = _start; i < _arg.length; i ++ ) {
- switch ( i ) {
- case 0:
- this.url = _arg[ i ];
- break;
- case 1:
- this.options = _arg[ i ];
- break;
- }
- }
- if ( this.options === undefined ) {
- this.options = {};
- }
- }
- }, {
- key: 'load',
- value: function load( _arg, onLoad, onProgress, onError ) {
- var _this = this;
- this._setArgOption( _arg );
- var loader = new THREE.FileLoader( this.manager );
- loader.setPath( this.path );
- loader.setResponseType( 'arraybuffer' );
- loader.setRequestHeader( this.requestHeader );
- loader.setWithCredentials( this.withCredentials );
- loader.load( this.url, function ( response ) {
- try {
- _this.parse( response, onLoad );
- } catch ( e ) {
- if ( onError ) {
- onError( e );
- } else {
- console.error( e );
- }
- _this.manager.itemError( _this.url );
- }
- }, onProgress, onError );
- }
- }, {
- key: '_readLine',
- value: function _readLine( line ) {
- var readed = 0;
- while ( true ) {
- var find = - 1;
- find = line.indexOf( '//', readed );
- if ( find === - 1 ) {
- find = line.indexOf( '#', readed );
- }
- if ( find > - 1 && find < 2 ) {
- var foundNewLine = - 1;
- foundNewLine = line.indexOf( '\r\n', readed );
- if ( foundNewLine > 0 ) {
- readed = foundNewLine + 2;
- } else {
- foundNewLine = line.indexOf( '\r', readed );
- if ( foundNewLine > 0 ) {
- readed = foundNewLine + 1;
- } else {
- readed = line.indexOf( '\n', readed ) + 1;
- }
- }
- } else {
- break;
- }
- }
- return line.substr( readed );
- }
- }, {
- key: '_readLine',
- value: function _readLine( line ) {
- var readed = 0;
- while ( true ) {
- var find = - 1;
- find = line.indexOf( '//', readed );
- if ( find === - 1 ) {
- find = line.indexOf( '#', readed );
- }
- if ( find > - 1 && find < 2 ) {
- var foundNewLine = - 1;
- foundNewLine = line.indexOf( '\r\n', readed );
- if ( foundNewLine > 0 ) {
- readed = foundNewLine + 2;
- } else {
- foundNewLine = line.indexOf( '\r', readed );
- if ( foundNewLine > 0 ) {
- readed = foundNewLine + 1;
- } else {
- readed = line.indexOf( '\n', readed ) + 1;
- }
- }
- } else {
- break;
- }
- }
- return line.substr( readed );
- }
- }, {
- key: '_isBinary',
- value: function _isBinary( binData ) {
- var reader = new DataView( binData );
- var face_size = 32 / 8 * 3 + 32 / 8 * 3 * 3 + 16 / 8;
- var n_faces = reader.getUint32( 80, true );
- var expect = 80 + 32 / 8 + n_faces * face_size;
- if ( expect === reader.byteLength ) {
- return true;
- }
- var fileLength = reader.byteLength;
- for ( var index = 0; index < fileLength; index ++ ) {
- if ( reader.getUint8( index, false ) > 127 ) {
- return true;
- }
- }
- return false;
- }
- }, {
- key: '_ensureBinary',
- value: function _ensureBinary( buf ) {
- if ( typeof buf === 'string' ) {
- var array_buffer = new Uint8Array( buf.length );
- for ( var i = 0; i < buf.length; i ++ ) {
- array_buffer[ i ] = buf.charCodeAt( i ) & 0xff;
- }
- return array_buffer.buffer || array_buffer;
- } else {
- return buf;
- }
- }
- }, {
- key: '_ensureString',
- value: function _ensureString( buf ) {
- if ( typeof buf !== 'string' ) {
- return THREE.LoaderUtils.decodeText( new Uint8Array( buf ) );
- } else {
- return buf;
- }
- }
- }, {
- key: 'parse',
- value: function _parse( data, onLoad ) {
- var binData = this._ensureBinary( data );
- this._data = this._ensureString( data );
- this.onLoad = onLoad;
- return this._isBinary( binData ) ? this._parseBinary( binData ) : this._parseASCII();
- }
- }, {
- key: '_parseBinary',
- value: function _parseBinary( data ) {
- return this._parseASCII( THREE.LoaderUtils.decodeText( new Uint8Array( data ) ) );
- }
- }, {
- key: '_parseASCII',
- value: function _parseASCII() {
- var path;
- if ( this.resourcePath !== '' ) {
- path = this.resourcePath;
- } else if ( this.path !== '' ) {
- path = this.path;
- } else {
- path = THREE.LoaderUtils.extractUrlBase( this.url );
- }
- this.texloader.setPath( path ).setCrossOrigin( this.crossOrigin );
- var endRead = 16;
- this.Hierarchies.children = [];
- this._hierarchieParse( this.Hierarchies, endRead );
- this._changeRoot();
- this._currentObject = this.Hierarchies.children.shift();
- this._mainloop();
- }
- }, {
- key: '_hierarchieParse',
- value: function _hierarchieParse( _parent, _end ) {
- var endRead = _end;
- while ( true ) {
- var find1 = this._data.indexOf( '{', endRead ) + 1;
- var findEnd = this._data.indexOf( '}', endRead );
- var findNext = this._data.indexOf( '{', find1 ) + 1;
- if ( find1 > 0 && findEnd > find1 ) {
- var _currentObject = {};
- _currentObject.children = [];
- var nameData = this._readLine( this._data.substr( endRead, find1 - endRead - 1 ) ).trim();
- var word = nameData.split( / /g );
- if ( word.length > 0 ) {
- _currentObject.type = word[ 0 ];
- if ( word.length >= 2 ) {
- _currentObject.name = word[ 1 ];
- } else {
- _currentObject.name = word[ 0 ] + this.Hierarchies.children.length;
- }
- } else {
- _currentObject.name = nameData;
- _currentObject.type = '';
- }
- if ( _currentObject.type === 'Animation' ) {
- _currentObject.data = this._data.substr( findNext, findEnd - findNext ).trim();
- var refs = this._hierarchieParse( _currentObject, findEnd + 1 );
- endRead = refs.end;
- _currentObject.children = refs.parent.children;
- } else {
- var DataEnder = this._data.lastIndexOf( ';', findNext > 0 ? Math.min( findNext, findEnd ) : findEnd );
- _currentObject.data = this._data.substr( find1, DataEnder - find1 ).trim();
- if ( findNext <= 0 || findEnd < findNext ) {
- endRead = findEnd + 1;
- } else {
- var nextStart = Math.max( DataEnder + 1, find1 );
- var _refs = this._hierarchieParse( _currentObject, nextStart );
- endRead = _refs.end;
- _currentObject.children = _refs.parent.children;
- }
- }
- _currentObject.parent = _parent;
- if ( _currentObject.type != 'template' ) {
- _parent.children.push( _currentObject );
- }
- } else {
- endRead = find1 === - 1 ? this._data.length : findEnd + 1;
- break;
- }
- }
- return {
- parent: _parent,
- end: endRead
- };
- }
- }, {
- key: '_mainloop',
- value: function _mainloop() {
- var _this2 = this;
- this._mainProc();
- if ( this._currentObject.parent || this._currentObject.children.length > 0 || ! this._currentObject.worked ) {
- setTimeout( function () {
- _this2._mainloop();
- }, 1 );
- } else {
- setTimeout( function () {
- _this2.onLoad( {
- models: _this2.Meshes,
- animations: _this2.animations
- } );
- }, 1 );
- }
- }
- }, {
- key: '_mainProc',
- value: function _mainProc() {
- var breakFlag = false;
- while ( true ) {
- if ( ! this._currentObject.worked ) {
- switch ( this._currentObject.type ) {
- case 'template':
- break;
- case 'AnimTicksPerSecond':
- this.animTicksPerSecond = parseInt( this._currentObject.data );
- break;
- case 'Frame':
- this._setFrame();
- break;
- case 'FrameTransformMatrix':
- this._setFrameTransformMatrix();
- break;
- case 'Mesh':
- this._changeRoot();
- this._currentGeo = {};
- this._currentGeo.name = this._currentObject.name.trim();
- this._currentGeo.parentName = this._getParentName( this._currentObject ).trim();
- this._currentGeo.VertexSetedBoneCount = [];
- this._currentGeo.GeometryData = {
- vertices: [],
- normals: [],
- uvs: [],
- skinIndices: [],
- skinWeights: [],
- indices: [],
- materialIndices: []
- };
- this._currentGeo.Materials = [];
- this._currentGeo.normalVectors = [];
- this._currentGeo.BoneInfs = [];
- this._currentGeo.baseFrame = this._currentFrame;
- this._makeBoneFrom_CurrentFrame();
- this._readVertexDatas();
- breakFlag = true;
- break;
- case 'MeshNormals':
- this._readVertexDatas();
- break;
- case 'MeshTextureCoords':
- this._setMeshTextureCoords();
- break;
- case 'VertexDuplicationIndices':
- break;
- case 'MeshMaterialList':
- this._setMeshMaterialList();
- break;
- case 'Material':
- this._setMaterial();
- break;
- case 'SkinWeights':
- this._setSkinWeights();
- break;
- case 'AnimationSet':
- this._changeRoot();
- this._currentAnime = {};
- this._currentAnime.name = this._currentObject.name.trim();
- this._currentAnime.AnimeFrames = [];
- break;
- case 'Animation':
- if ( this._currentAnimeFrames ) {
- this._currentAnime.AnimeFrames.push( this._currentAnimeFrames );
- }
- this._currentAnimeFrames = new XAnimationInfo();
- this._currentAnimeFrames.boneName = this._currentObject.data.trim();
- break;
- case 'AnimationKey':
- this._readAnimationKey();
- breakFlag = true;
- break;
- }
- this._currentObject.worked = true;
- }
- if ( this._currentObject.children.length > 0 ) {
- this._currentObject = this._currentObject.children.shift();
- if ( this.debug ) {
- console.log( 'processing ' + this._currentObject.name );
- }
- if ( breakFlag ) break;
- } else {
- if ( this._currentObject.worked ) {
- if ( this._currentObject.parent && ! this._currentObject.parent.parent ) {
- this._changeRoot();
- }
- }
- if ( this._currentObject.parent ) {
- this._currentObject = this._currentObject.parent;
- } else {
- breakFlag = true;
- }
- if ( breakFlag ) break;
- }
- }
- return;
- }
- }, {
- key: '_changeRoot',
- value: function _changeRoot() {
- if ( this._currentGeo != null && this._currentGeo.name ) {
- this._makeOutputGeometry();
- }
- this._currentGeo = {};
- if ( this._currentAnime != null && this._currentAnime.name ) {
- if ( this._currentAnimeFrames ) {
- this._currentAnime.AnimeFrames.push( this._currentAnimeFrames );
- this._currentAnimeFrames = null;
- }
- this._makeOutputAnimation();
- }
- this._currentAnime = {};
- }
- }, {
- key: '_getParentName',
- value: function _getParentName( _obj ) {
- if ( _obj.parent ) {
- if ( _obj.parent.name ) {
- return _obj.parent.name;
- } else {
- return this._getParentName( _obj.parent );
- }
- } else {
- return '';
- }
- }
- }, {
- key: '_setFrame',
- value: function _setFrame() {
- this._nowFrameName = this._currentObject.name.trim();
- this._currentFrame = {};
- this._currentFrame.name = this._nowFrameName;
- this._currentFrame.children = [];
- if ( this._currentObject.parent && this._currentObject.parent.name ) {
- this._currentFrame.parentName = this._currentObject.parent.name;
- }
- this.frameHierarchie.push( this._nowFrameName );
- this.HieStack[ this._nowFrameName ] = this._currentFrame;
- }
- }, {
- key: '_setFrameTransformMatrix',
- value: function _setFrameTransformMatrix() {
- this._currentFrame.FrameTransformMatrix = new THREE.Matrix4();
- var data = this._currentObject.data.split( ',' );
- this._ParseMatrixData( this._currentFrame.FrameTransformMatrix, data );
- this._makeBoneFrom_CurrentFrame();
- }
- }, {
- key: '_makeBoneFrom_CurrentFrame',
- value: function _makeBoneFrom_CurrentFrame() {
- if ( ! this._currentFrame.FrameTransformMatrix ) {
- return;
- }
- var b = new THREE.Bone();
- b.name = this._currentFrame.name;
- b.applyMatrix4( this._currentFrame.FrameTransformMatrix );
- b.matrixWorld = b.matrix;
- b.FrameTransformMatrix = this._currentFrame.FrameTransformMatrix;
- this._currentFrame.putBone = b;
- if ( this._currentFrame.parentName ) {
- for ( var frame in this.HieStack ) {
- if ( this.HieStack[ frame ].name === this._currentFrame.parentName ) {
- this.HieStack[ frame ].putBone.add( this._currentFrame.putBone );
- }
- }
- }
- }
- }, {
- key: '_readVertexDatas',
- value: function _readVertexDatas() {
- var endRead = 0;
- var mode = 0;
- var mode_local = 0;
- var maxLength = 0;
- while ( true ) {
- var changeMode = false;
- if ( mode_local === 0 ) {
- var refO = this._readInt1( endRead );
- endRead = refO.endRead;
- mode_local = 1;
- maxLength = this._currentObject.data.indexOf( ';;', endRead ) + 1;
- if ( maxLength <= 0 ) {
- maxLength = this._currentObject.data.length;
- }
- } else {
- var find = 0;
- switch ( mode ) {
- case 0:
- find = this._currentObject.data.indexOf( ',', endRead ) + 1;
- break;
- case 1:
- find = this._currentObject.data.indexOf( ';,', endRead ) + 1;
- break;
- }
- if ( find === 0 || find > maxLength ) {
- find = maxLength;
- mode_local = 0;
- changeMode = true;
- }
- switch ( this._currentObject.type ) {
- case 'Mesh':
- switch ( mode ) {
- case 0:
- this._readVertex1( this._currentObject.data.substr( endRead, find - endRead ) );
- break;
- case 1:
- this._readFace1( this._currentObject.data.substr( endRead, find - endRead ) );
- break;
- }
- break;
- case 'MeshNormals':
- switch ( mode ) {
- case 0:
- this._readNormalVector1( this._currentObject.data.substr( endRead, find - endRead ) );
- break;
- }
- break;
- }
- endRead = find + 1;
- if ( changeMode ) {
- mode ++;
- }
- }
- if ( endRead >= this._currentObject.data.length ) {
- break;
- }
- }
- }
- }, {
- key: '_readInt1',
- value: function _readInt1( start ) {
- var find = this._currentObject.data.indexOf( ';', start );
- return {
- refI: parseInt( this._currentObject.data.substr( start, find - start ) ),
- endRead: find + 1
- };
- }
- }, {
- key: '_readVertex1',
- value: function _readVertex1( line ) {
- var data = this._readLine( line.trim() ).substr( 0, line.length - 2 ).split( ';' );
- this._currentGeo.GeometryData.vertices.push( parseFloat( data[ 0 ] ), parseFloat( data[ 1 ] ), parseFloat( data[ 2 ] ) );
- this._currentGeo.GeometryData.skinIndices.push( 0, 0, 0, 0 );
- this._currentGeo.GeometryData.skinWeights.push( 1, 0, 0, 0 );
- this._currentGeo.VertexSetedBoneCount.push( 0 );
- }
- }, {
- key: '_readFace1',
- value: function _readFace1( line ) {
- var data = this._readLine( line.trim() ).substr( 2, line.length - 4 ).split( ',' );
- this._currentGeo.GeometryData.indices.push( parseInt( data[ 0 ], 10 ), parseInt( data[ 1 ], 10 ), parseInt( data[ 2 ], 10 ) );
- }
- }, {
- key: '_readNormalVector1',
- value: function _readNormalVector1( line ) {
- var data = this._readLine( line.trim() ).substr( 0, line.length - 2 ).split( ';' );
- this._currentGeo.GeometryData.normals.push( parseFloat( data[ 0 ] ), parseFloat( data[ 1 ] ), parseFloat( data[ 2 ] ) );
- }
- }, {
- key: '_buildGeometry',
- value: function _buildGeometry() {
- var bufferGeometry = new THREE.BufferGeometry();
- var position = [];
- var normals = [];
- var uvs = [];
- var skinIndices = [];
- var skinWeights = [];
- //
- var data = this._currentGeo.GeometryData;
- for ( var i = 0, l = data.indices.length; i < l; i ++ ) {
- var stride2 = data.indices[ i ] * 2;
- var stride3 = data.indices[ i ] * 3;
- var stride4 = data.indices[ i ] * 4;
- position.push( data.vertices[ stride3 ], data.vertices[ stride3 + 1 ], data.vertices[ stride3 + 2 ] );
- normals.push( data.normals[ stride3 ], data.normals[ stride3 + 1 ], data.normals[ stride3 + 2 ] );
- skinIndices.push( data.skinIndices[ stride4 ], data.skinIndices[ stride4 + 1 ], data.skinIndices[ stride4 + 2 ], data.skinIndices[ stride4 + 3 ] );
- skinWeights.push( data.skinWeights[ stride4 ], data.skinWeights[ stride4 + 1 ], data.skinWeights[ stride4 + 2 ], data.skinWeights[ stride4 + 3 ] );
- uvs.push( data.uvs[ stride2 ], data.uvs[ stride2 + 1 ] );
- }
- //
- bufferGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
- bufferGeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
- bufferGeometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
- bufferGeometry.setAttribute( 'skinIndex', new THREE.Uint16BufferAttribute( skinIndices, 4 ) );
- bufferGeometry.setAttribute( 'skinWeight', new THREE.Float32BufferAttribute( skinWeights, 4 ) );
- this._computeGroups( bufferGeometry, data.materialIndices );
- return bufferGeometry;
- }
- }, {
- key: '_computeGroups',
- value: function _computeGroups( bufferGeometry, materialIndices ) {
- var group;
- var groups = [];
- var materialIndex = undefined;
- for ( var i = 0; i < materialIndices.length; i ++ ) {
- var currentMaterialIndex = materialIndices[ i ];
- if ( currentMaterialIndex !== materialIndex ) {
- materialIndex = currentMaterialIndex;
- if ( group !== undefined ) {
- group.count = ( i * 3 ) - group.start;
- groups.push( group );
- }
- group = {
- start: i * 3,
- materialIndex: materialIndex
- };
- }
- }
- if ( group !== undefined ) {
- group.count = ( i * 3 ) - group.start;
- groups.push( group );
- }
- bufferGeometry.groups = groups;
- }
- }, {
- key: '_setMeshTextureCoords',
- value: function _setMeshTextureCoords() {
- var endRead = 0;
- var mode = 0;
- var mode_local = 0;
- while ( true ) {
- switch ( mode ) {
- case 0:
- if ( mode_local === 0 ) {
- var refO = this._readInt1( 0 );
- endRead = refO.endRead;
- mode_local = 1;
- } else {
- var find = this._currentObject.data.indexOf( ',', endRead ) + 1;
- if ( find === 0 ) {
- find = this._currentObject.data.length;
- mode = 2;
- mode_local = 0;
- }
- var line = this._currentObject.data.substr( endRead, find - endRead );
- var data = this._readLine( line.trim() ).split( ';' );
- if ( this.IsUvYReverse ) {
- this._currentGeo.GeometryData.uvs.push( parseFloat( data[ 0 ] ), 1 - parseFloat( data[ 1 ] ) );
- } else {
- this._currentGeo.GeometryData.uvs.push( parseFloat( data[ 0 ] ), parseFloat( data[ 1 ] ) );
- }
- endRead = find + 1;
- }
- break;
- }
- if ( endRead >= this._currentObject.data.length ) {
- break;
- }
- }
- }
- }, {
- key: '_setMeshMaterialList',
- value: function _setMeshMaterialList() {
- var endRead = 0;
- var mode = 0;
- var mode_local = 0;
- while ( true ) {
- if ( mode_local < 2 ) {
- var refO = this._readInt1( endRead );
- endRead = refO.endRead;
- mode_local ++;
- } else {
- var find = this._currentObject.data.indexOf( ';', endRead );
- if ( find === - 1 ) {
- find = this._currentObject.data.length;
- mode = 3;
- mode_local = 0;
- }
- var line = this._currentObject.data.substr( endRead, find - endRead );
- var data = this._readLine( line.trim() ).split( ',' );
- for ( var i = 0; i < data.length; i ++ ) {
- this._currentGeo.GeometryData.materialIndices[ i ] = parseInt( data[ i ] );
- }
- endRead = this._currentObject.data.length;
- }
- if ( endRead >= this._currentObject.data.length || mode >= 3 ) {
- break;
- }
- }
- }
- }, {
- key: '_setMaterial',
- value: function _setMaterial() {
- var _nowMat = new THREE.MeshPhongMaterial( {
- color: Math.random() * 0xffffff
- } );
- _nowMat.side = THREE.FrontSide;
- _nowMat.name = this._currentObject.name;
- var endRead = 0;
- var find = this._currentObject.data.indexOf( ';;', endRead );
- var line = this._currentObject.data.substr( endRead, find - endRead );
- var data = this._readLine( line.trim() ).split( ';' );
- _nowMat.color.r = parseFloat( data[ 0 ] );
- _nowMat.color.g = parseFloat( data[ 1 ] );
- _nowMat.color.b = parseFloat( data[ 2 ] );
- endRead = find + 2;
- find = this._currentObject.data.indexOf( ';', endRead );
- line = this._currentObject.data.substr( endRead, find - endRead );
- _nowMat.shininess = parseFloat( this._readLine( line ) );
- endRead = find + 1;
- find = this._currentObject.data.indexOf( ';;', endRead );
- line = this._currentObject.data.substr( endRead, find - endRead );
- var data2 = this._readLine( line.trim() ).split( ';' );
- _nowMat.specular.r = parseFloat( data2[ 0 ] );
- _nowMat.specular.g = parseFloat( data2[ 1 ] );
- _nowMat.specular.b = parseFloat( data2[ 2 ] );
- endRead = find + 2;
- find = this._currentObject.data.indexOf( ';;', endRead );
- if ( find === - 1 ) {
- find = this._currentObject.data.length;
- }
- line = this._currentObject.data.substr( endRead, find - endRead );
- var data3 = this._readLine( line.trim() ).split( ';' );
- _nowMat.emissive.r = parseFloat( data3[ 0 ] );
- _nowMat.emissive.g = parseFloat( data3[ 1 ] );
- _nowMat.emissive.b = parseFloat( data3[ 2 ] );
- var localObject = null;
- while ( true ) {
- if ( this._currentObject.children.length > 0 ) {
- localObject = this._currentObject.children.shift();
- if ( this.debug ) {
- console.log( 'processing ' + localObject.name );
- }
- var fileName = localObject.data.substr( 1, localObject.data.length - 2 );
- switch ( localObject.type ) {
- case 'TextureFilename':
- _nowMat.map = this.texloader.load( fileName );
- break;
- case 'BumpMapFilename':
- _nowMat.bumpMap = this.texloader.load( fileName );
- _nowMat.bumpScale = 0.05;
- break;
- case 'NormalMapFilename':
- _nowMat.normalMap = this.texloader.load( fileName );
- _nowMat.normalScale = new THREE.Vector2( 2, 2 );
- break;
- case 'EmissiveMapFilename':
- _nowMat.emissiveMap = this.texloader.load( fileName );
- break;
- case 'LightMapFilename':
- _nowMat.lightMap = this.texloader.load( fileName );
- break;
- }
- } else {
- break;
- }
- }
- this._currentGeo.Materials.push( _nowMat );
- }
- }, {
- key: '_setSkinWeights',
- value: function _setSkinWeights() {
- var boneInf = new XboneInf();
- var endRead = 0;
- var find = this._currentObject.data.indexOf( ';', endRead );
- var line = this._currentObject.data.substr( endRead, find - endRead );
- endRead = find + 1;
- boneInf.boneName = line.substr( 1, line.length - 2 );
- boneInf.BoneIndex = this._currentGeo.BoneInfs.length;
- find = this._currentObject.data.indexOf( ';', endRead );
- endRead = find + 1;
- find = this._currentObject.data.indexOf( ';', endRead );
- line = this._currentObject.data.substr( endRead, find - endRead );
- var data = this._readLine( line.trim() ).split( ',' );
- for ( var i = 0; i < data.length; i ++ ) {
- boneInf.Indeces.push( parseInt( data[ i ] ) );
- }
- endRead = find + 1;
- find = this._currentObject.data.indexOf( ';', endRead );
- line = this._currentObject.data.substr( endRead, find - endRead );
- var data2 = this._readLine( line.trim() ).split( ',' );
- for ( var _i = 0; _i < data2.length; _i ++ ) {
- boneInf.Weights.push( parseFloat( data2[ _i ] ) );
- }
- endRead = find + 1;
- find = this._currentObject.data.indexOf( ';', endRead );
- if ( find <= 0 ) {
- find = this._currentObject.data.length;
- }
- line = this._currentObject.data.substr( endRead, find - endRead );
- var data3 = this._readLine( line.trim() ).split( ',' );
- boneInf.OffsetMatrix = new THREE.Matrix4();
- this._ParseMatrixData( boneInf.OffsetMatrix, data3 );
- this._currentGeo.BoneInfs.push( boneInf );
- }
- }, {
- key: '_makePutBoneList',
- value: function _makePutBoneList( _RootName, _bones ) {
- var putting = false;
- for ( var frame in this.HieStack ) {
- if ( this.HieStack[ frame ].name === _RootName || putting ) {
- putting = true;
- var b = new THREE.Bone();
- b.name = this.HieStack[ frame ].name;
- b.applyMatrix4( this.HieStack[ frame ].FrameTransformMatrix );
- b.matrixWorld = b.matrix;
- b.FrameTransformMatrix = this.HieStack[ frame ].FrameTransformMatrix;
- b.pos = new THREE.Vector3().setFromMatrixPosition( b.FrameTransformMatrix ).toArray();
- b.rotq = new THREE.Quaternion().setFromRotationMatrix( b.FrameTransformMatrix ).toArray();
- b.scl = new THREE.Vector3().setFromMatrixScale( b.FrameTransformMatrix ).toArray();
- if ( this.HieStack[ frame ].parentName && this.HieStack[ frame ].parentName.length > 0 ) {
- for ( var i = 0; i < _bones.length; i ++ ) {
- if ( this.HieStack[ frame ].parentName === _bones[ i ].name ) {
- _bones[ i ].add( b );
- b.parent = i;
- break;
- }
- }
- }
- _bones.push( b );
- }
- }
- }
- }, {
- key: '_makeOutputGeometry',
- value: function _makeOutputGeometry() {
- var mesh = null;
- if ( this._currentGeo.BoneInfs.length > 0 ) {
- var putBones = [];
- this._makePutBoneList( this._currentGeo.baseFrame.parentName, putBones );
- for ( var bi = 0; bi < this._currentGeo.BoneInfs.length; bi ++ ) {
- var boneIndex = 0;
- for ( var bb = 0; bb < putBones.length; bb ++ ) {
- if ( putBones[ bb ].name === this._currentGeo.BoneInfs[ bi ].boneName ) {
- boneIndex = bb;
- putBones[ bb ].OffsetMatrix = new THREE.Matrix4();
- putBones[ bb ].OffsetMatrix.copy( this._currentGeo.BoneInfs[ bi ].OffsetMatrix );
- break;
- }
- }
- for ( var vi = 0; vi < this._currentGeo.BoneInfs[ bi ].Indeces.length; vi ++ ) {
- var nowVertexID = this._currentGeo.BoneInfs[ bi ].Indeces[ vi ];
- var nowVal = this._currentGeo.BoneInfs[ bi ].Weights[ vi ];
- var stride = nowVertexID * 4;
- switch ( this._currentGeo.VertexSetedBoneCount[ nowVertexID ] ) {
- case 0:
- this._currentGeo.GeometryData.skinIndices[ stride ] = boneIndex;
- this._currentGeo.GeometryData.skinWeights[ stride ] = nowVal;
- break;
- case 1:
- this._currentGeo.GeometryData.skinIndices[ stride + 1 ] = boneIndex;
- this._currentGeo.GeometryData.skinWeights[ stride + 1 ] = nowVal;
- break;
- case 2:
- this._currentGeo.GeometryData.skinIndices[ stride + 2 ] = boneIndex;
- this._currentGeo.GeometryData.skinWeights[ stride + 2 ] = nowVal;
- break;
- case 3:
- this._currentGeo.GeometryData.skinIndices[ stride + 3 ] = boneIndex;
- this._currentGeo.GeometryData.skinWeights[ stride + 3 ] = nowVal;
- break;
- }
- this._currentGeo.VertexSetedBoneCount[ nowVertexID ] ++;
- if ( this._currentGeo.VertexSetedBoneCount[ nowVertexID ] > 4 ) {
- console.log( 'warn! over 4 bone weight! :' + nowVertexID );
- }
- }
- }
- for ( var sk = 0; sk < this._currentGeo.Materials.length; sk ++ ) {
- this._currentGeo.Materials[ sk ].skinning = true;
- }
- var offsetList = [];
- for ( var _bi = 0; _bi < putBones.length; _bi ++ ) {
- if ( putBones[ _bi ].OffsetMatrix ) {
- offsetList.push( putBones[ _bi ].OffsetMatrix );
- } else {
- offsetList.push( new THREE.Matrix4() );
- }
- }
- var bufferGeometry = this._buildGeometry();
- mesh = new THREE.SkinnedMesh( bufferGeometry, this._currentGeo.Materials.length === 1 ? this._currentGeo.Materials[ 0 ] : this._currentGeo.Materials );
- this._initSkeleton( mesh, putBones, offsetList );
- } else {
- var _bufferGeometry = this._buildGeometry();
- mesh = new THREE.Mesh( _bufferGeometry, this._currentGeo.Materials.length === 1 ? this._currentGeo.Materials[ 0 ] : this._currentGeo.Materials );
- }
- mesh.name = this._currentGeo.name;
- var worldBaseMx = new THREE.Matrix4();
- var currentMxFrame = this._currentGeo.baseFrame.putBone;
- if ( currentMxFrame && currentMxFrame.parent ) {
- while ( true ) {
- currentMxFrame = currentMxFrame.parent;
- if ( currentMxFrame ) {
- worldBaseMx.multiply( currentMxFrame.FrameTransformMatrix );
- } else {
- break;
- }
- }
- mesh.applyMatrix4( worldBaseMx );
- }
- this.Meshes.push( mesh );
- }
- }, {
- key: '_initSkeleton',
- value: function _initSkeleton( mesh, boneList, boneInverses ) {
- var bones = [], bone, gbone;
- var i, il;
- for ( i = 0, il = boneList.length; i < il; i ++ ) {
- gbone = boneList[ i ];
- bone = new THREE.Bone();
- bones.push( bone );
- bone.name = gbone.name;
- bone.position.fromArray( gbone.pos );
- bone.quaternion.fromArray( gbone.rotq );
- if ( gbone.scl !== undefined ) bone.scale.fromArray( gbone.scl );
- }
- for ( i = 0, il = boneList.length; i < il; i ++ ) {
- gbone = boneList[ i ];
- if ( ( gbone.parent !== - 1 ) && ( gbone.parent !== null ) && ( bones[ gbone.parent ] !== undefined ) ) {
- bones[ gbone.parent ].add( bones[ i ] );
- } else {
- mesh.add( bones[ i ] );
- }
- }
- mesh.updateMatrixWorld( true );
- var skeleton = new THREE.Skeleton( bones, boneInverses );
- mesh.bind( skeleton, mesh.matrixWorld );
- }
- }, {
- key: '_readAnimationKey',
- value: function _readAnimationKey() {
- var endRead = 0;
- var find = this._currentObject.data.indexOf( ';', endRead );
- var line = this._currentObject.data.substr( endRead, find - endRead );
- endRead = find + 1;
- var nowKeyType = parseInt( this._readLine( line ) );
- find = this._currentObject.data.indexOf( ';', endRead );
- endRead = find + 1;
- line = this._currentObject.data.substr( endRead );
- var data = this._readLine( line.trim() ).split( ';;,' );
- for ( var i = 0; i < data.length; i ++ ) {
- var data2 = data[ i ].split( ';' );
- var keyInfo = new XKeyFrameInfo();
- keyInfo.type = nowKeyType;
- keyInfo.Frame = parseInt( data2[ 0 ] );
- keyInfo.index = this._currentAnimeFrames.keyFrames.length;
- keyInfo.time = keyInfo.Frame;
- if ( nowKeyType != 4 ) {
- var frameFound = false;
- for ( var mm = 0; mm < this._currentAnimeFrames.keyFrames.length; mm ++ ) {
- if ( this._currentAnimeFrames.keyFrames[ mm ].Frame === keyInfo.Frame ) {
- keyInfo = this._currentAnimeFrames.keyFrames[ mm ];
- frameFound = true;
- break;
- }
- }
- var frameValue = data2[ 2 ].split( ',' );
- switch ( nowKeyType ) {
- case 0:
- keyInfo.rot = new THREE.Quaternion( parseFloat( frameValue[ 1 ] ), parseFloat( frameValue[ 2 ] ), parseFloat( frameValue[ 3 ] ), parseFloat( frameValue[ 0 ] ) * - 1 );
- break;
- case 1:
- keyInfo.scl = new THREE.Vector3( parseFloat( frameValue[ 0 ] ), parseFloat( frameValue[ 1 ] ), parseFloat( frameValue[ 2 ] ) );
- break;
- case 2:
- keyInfo.pos = new THREE.Vector3( parseFloat( frameValue[ 0 ] ), parseFloat( frameValue[ 1 ] ), parseFloat( frameValue[ 2 ] ) );
- break;
- }
- if ( ! frameFound ) {
- this._currentAnimeFrames.keyFrames.push( keyInfo );
- }
- } else {
- keyInfo.matrix = new THREE.Matrix4();
- this._ParseMatrixData( keyInfo.matrix, data2[ 2 ].split( ',' ) );
- this._currentAnimeFrames.keyFrames.push( keyInfo );
- }
- }
- }
- }, {
- key: '_makeOutputAnimation',
- value: function _makeOutputAnimation() {
- var animationObj = new XAnimationObj( this.options );
- animationObj.fps = this.animTicksPerSecond;
- animationObj.name = this._currentAnime.name;
- animationObj.make( this._currentAnime.AnimeFrames );
- this.animations.push( animationObj );
- }
- }, {
- key: 'assignAnimation',
- value: function assignAnimation( _model, _animation ) {
- var model = _model;
- var animation = _animation;
- if ( ! model ) {
- model = this.Meshes[ 0 ];
- }
- if ( ! animation ) {
- animation = this.animations[ 0 ];
- }
- if ( ! model || ! animation ) {
- return null;
- }
- var put = {};
- put.fps = animation.fps;
- put.name = animation.name;
- put.length = animation.length;
- put.hierarchy = [];
- for ( var b = 0; b < model.skeleton.bones.length; b ++ ) {
- var findAnimation = false;
- for ( var i = 0; i < animation.hierarchy.length; i ++ ) {
- if ( model.skeleton.bones[ b ].name === animation.hierarchy[ i ].name ) {
- findAnimation = true;
- var c_key = animation.hierarchy[ i ].copy();
- c_key.parent = - 1;
- if ( model.skeleton.bones[ b ].parent && model.skeleton.bones[ b ].parent.type === 'Bone' ) {
- for ( var bb = 0; bb < put.hierarchy.length; bb ++ ) {
- if ( put.hierarchy[ bb ].name === model.skeleton.bones[ b ].parent.name ) {
- c_key.parent = bb;
- c_key.parentName = model.skeleton.bones[ b ].parent.name;
- }
- }
- }
- put.hierarchy.push( c_key );
- break;
- }
- }
- if ( ! findAnimation ) {
- var _c_key = animation.hierarchy[ 0 ].copy();
- _c_key.name = model.skeleton.bones[ b ].name;
- _c_key.parent = - 1;
- for ( var k = 0; k < _c_key.keys.length; k ++ ) {
- if ( _c_key.keys[ k ].pos ) {
- _c_key.keys[ k ].pos.set( 0, 0, 0 );
- }
- if ( _c_key.keys[ k ].scl ) {
- _c_key.keys[ k ].scl.set( 1, 1, 1 );
- }
- if ( _c_key.keys[ k ].rot ) {
- _c_key.keys[ k ].rot.set( 0, 0, 0, 1 );
- }
- }
- put.hierarchy.push( _c_key );
- }
- }
- if ( ! model.geometry.animations ) {
- model.geometry.animations = [];
- }
- model.geometry.animations.push( THREE.AnimationClip.parseAnimation( put, model.skeleton.bones ) );
- if ( ! model.animationMixer ) {
- model.animationMixer = new THREE.AnimationMixer( model );
- }
- return put;
- }
- }, {
- key: '_ParseMatrixData',
- value: function _ParseMatrixData( targetMatrix, data ) {
- targetMatrix.set( parseFloat( data[ 0 ] ), parseFloat( data[ 4 ] ), parseFloat( data[ 8 ] ), parseFloat( data[ 12 ] ), parseFloat( data[ 1 ] ), parseFloat( data[ 5 ] ), parseFloat( data[ 9 ] ), parseFloat( data[ 13 ] ), parseFloat( data[ 2 ] ), parseFloat( data[ 6 ] ), parseFloat( data[ 10 ] ), parseFloat( data[ 14 ] ), parseFloat( data[ 3 ] ), parseFloat( data[ 7 ] ), parseFloat( data[ 11 ] ), parseFloat( data[ 15 ] ) );
- }
- } ] );
- return XLoader;
- }();
- return XLoader;
- } )();
|