12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646 |
- /**
- * @author adrs2002 / https://github.com/adrs2002
- */
- 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.load( this.url, function ( response ) {
- _this.parse( response, onLoad );
- }, 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;
- } )();
|