FBXLoader.js 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161
  1. /**
  2. * @author yamahigashi https://github.com/yamahigashi
  3. * @author Kyle-Larson https://github.com/Kyle-Larson
  4. *
  5. * This loader loads FBX file in *ASCII and version 7 format*.
  6. *
  7. * Support
  8. * - mesh
  9. * - skinning
  10. * - normal / uv
  11. * - material (Multi-Material too)
  12. * - textures (Must be in same directory)
  13. *
  14. * No Support
  15. * - morph
  16. */
  17. ( function () {
  18. THREE.FBXLoader = function ( manager ) {
  19. THREE.Loader.call( this );
  20. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  21. this.textureLoader = null;
  22. this.textureBasePath = null;
  23. };
  24. THREE.FBXLoader.prototype = Object.create( THREE.Loader.prototype );
  25. THREE.FBXLoader.prototype.constructor = THREE.FBXLoader;
  26. Object.assign( THREE.FBXLoader.prototype, {
  27. load: function ( url, onLoad, onProgress, onError ) {
  28. var scope = this;
  29. var loader = new THREE.FileLoader( scope.manager );
  30. // loader.setCrossOrigin( this.crossOrigin );
  31. loader.load( url, function ( text ) {
  32. if ( ! scope.isFbxFormatASCII( text ) ) {
  33. console.warn( 'FBXLoader: !!! FBX Binary format not supported !!!' );
  34. } else if ( ! scope.isFbxVersionSupported( text ) ) {
  35. console.warn( 'FBXLoader: !!! FBX Version below 7 not supported !!!' );
  36. } else {
  37. scope.textureBasePath = scope.extractUrlBase( url );
  38. onLoad( scope.parse( text ) );
  39. }
  40. }, onProgress, onError );
  41. },
  42. setCrossOrigin: function ( value ) {
  43. this.crossOrigin = value;
  44. },
  45. isFbxFormatASCII: function ( body ) {
  46. var CORRECT = [ 'K', 'a', 'y', 'd', 'a', 'r', 'a', '\\', 'F', 'B', 'X', '\\', 'B', 'i', 'n', 'a', 'r', 'y', '\\', '\\' ];
  47. var cursor = 0;
  48. var read = function ( offset ) {
  49. var result = body[ offset - 1 ];
  50. body = body.slice( cursor + offset );
  51. cursor ++;
  52. return result;
  53. };
  54. for ( var i = 0; i < CORRECT.length; ++ i ) {
  55. var num = read( 1 );
  56. if ( num == CORRECT[ i ] ) {
  57. return false;
  58. }
  59. }
  60. return true;
  61. },
  62. isFbxVersionSupported: function ( body ) {
  63. var versionExp = /FBXVersion: (\d+)/;
  64. var match = body.match( versionExp );
  65. if ( match ) {
  66. var version = parseInt( match[ 1 ] );
  67. console.log( 'FBXLoader: FBX version ' + version );
  68. return version >= 7000;
  69. }
  70. return false;
  71. },
  72. parse: function ( text ) {
  73. var scope = this;
  74. console.time( 'FBXLoader' );
  75. console.time( 'FBXLoader: TextParser' );
  76. var nodes = new FBXParser().parse( text );
  77. console.timeEnd( 'FBXLoader: TextParser' );
  78. console.time( 'FBXLoader: ObjectParser' );
  79. scope.hierarchy = ( new Bones() ).parseHierarchy( nodes );
  80. scope.weights = ( new Weights() ).parse( nodes, scope.hierarchy );
  81. scope.animations = ( new Animation() ).parse( nodes, scope.hierarchy );
  82. scope.textures = ( new Textures() ).parse( nodes, scope.hierarchy );
  83. scope.materials = ( new Materials() ).parse( nodes, scope.hierarchy );
  84. scope.geometries = ( new Geometries() ).parse( nodes, scope.hierarchy );
  85. console.timeEnd( 'FBXLoader: ObjectParser' );
  86. this.texture_cache = {};
  87. this.material_cache = {};
  88. this.geometry_cache = {};
  89. console.time( 'FBXLoader: MeshParser' );
  90. var meshes = this.parseMeshes( nodes );
  91. console.timeEnd( 'FBXLoader: MeshParser' );
  92. var container = new THREE.Group();
  93. for ( var i = 0; i < meshes.length; ++ i ) {
  94. if ( meshes[ i ] === undefined ) {
  95. continue;
  96. }
  97. container.add( meshes[ i ] );
  98. //wireframe = new THREE.WireframeHelper( geometries[i], 0x00ff00 );
  99. //container.add( wireframe );
  100. //vnh = new THREE.VertexNormalsHelper( geometries[i], 0.6 );
  101. //container.add( vnh );
  102. //skh = new THREE.SkeletonHelper( geometries[i] );
  103. //container.add( skh );
  104. // container.add( new THREE.BoxHelper( geometries[i] ) );
  105. }
  106. console.timeEnd( 'FBXLoader' );
  107. return container;
  108. },
  109. getTexture: function ( texNode ) {
  110. if ( ! ( texNode.id in this.texture_cache ) ) {
  111. if ( this.textureLoader === null ) {
  112. this.textureLoader = new THREE.TextureLoader();
  113. }
  114. this.texture_cache[ texNode.id ] = this.textureLoader.load( this.textureBasePath + '/' + texNode.fileName );
  115. }
  116. return this.texture_cache[ texNode.id ];
  117. },
  118. getMaterial: function ( matNode, nodes ) {
  119. if ( ! ( matNode.id in this.material_cache ) ) {
  120. // TODO:
  121. // Cannot find a list of possible ShadingModel values.
  122. // If someone finds a list, please add additional cases
  123. // and map to appropriate materials.
  124. var tmpMat;
  125. switch ( matNode.type ) {
  126. case "phong":
  127. tmpMat = new THREE.MeshPhongMaterial();
  128. break;
  129. case "lambert":
  130. tmpMat = new THREE.MeshLambertMaterial();
  131. break;
  132. default:
  133. console.warn( "No implementation given for material type " + matNode.type + " in FBXLoader.js. Defaulting to basic material" );
  134. tmpMat = new THREE.MeshBasicMaterial( { color: 0x3300ff } );
  135. break;
  136. }
  137. var children = nodes.searchConnectionChildren( matNode.id );
  138. for ( var i = 0; i < children.length; ++ i ) {
  139. var type = nodes.searchConnectionType( children[ i ], matNode.id );
  140. switch ( type ) {
  141. case " \"AmbientColor":
  142. //TODO: Support AmbientColor textures
  143. break;
  144. case " \"DiffuseColor":
  145. matNode.parameters.map = this.getTexture( this.textures.textures[ children[ i ] ] );
  146. break;
  147. default:
  148. console.warn( 'Unknown texture application of type ' + type + ', skipping texture' );
  149. break;
  150. }
  151. }
  152. tmpMat.setValues( matNode.parameters );
  153. this.material_cache[ matNode.id ] = tmpMat;
  154. }
  155. return this.material_cache[ matNode.id ];
  156. },
  157. getGeometry: function ( geoNode ) {
  158. if ( ! ( geoNode.id in this.geometry_cache ) ) {
  159. var tmpGeo = new THREE.BufferGeometry();
  160. tmpGeo.name = geoNode.name;
  161. tmpGeo.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( geoNode.vertices ), 3 ) );
  162. if ( geoNode.normals !== undefined && geoNode.normals.length > 0 ) {
  163. tmpGeo.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( geoNode.normals ), 3 ) );
  164. }
  165. if ( geoNode.uvs !== undefined && geoNode.uvs.length > 0 ) {
  166. tmpGeo.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( geoNode.uvs ), 2 ) );
  167. }
  168. if ( geoNode.indices !== undefined && geoNode.indices.length > 0 ) {
  169. if ( geoNode.indices.length > 65535 ) {
  170. tmpGeo.setIndex( new THREE.BufferAttribute( new Uint32Array( geoNode.indices ), 1 ) );
  171. } else {
  172. tmpGeo.setIndex( new THREE.BufferAttribute( new Uint16Array( geoNode.indices ), 1 ) );
  173. }
  174. }
  175. tmpGeo.verticesNeedUpdate = true;
  176. tmpGeo.computeBoundingSphere();
  177. tmpGeo.computeBoundingBox();
  178. //Material groupings
  179. if ( geoNode.materialIndices.length > 1 ) {
  180. tmpGeo.groups = [];
  181. for ( var i = 0, prevIndex = - 1; i < geoNode.materialIndices.length; ++ i ) {
  182. if ( geoNode.materialIndices[ i ] !== prevIndex ) {
  183. tmpGeo.groups.push( { start: i * 3, count: 0, materialIndex: geoNode.materialIndices[ i ] } );
  184. prevIndex = geoNode.materialIndices[ i ];
  185. }
  186. tmpGeo.groups[ tmpGeo.groups.length - 1 ].count += 3;
  187. }
  188. }
  189. this.geometry_cache[ geoNode.id ] = new THREE.Geometry().fromBufferGeometry( tmpGeo );
  190. this.geometry_cache[ geoNode.id ].bones = geoNode.bones;
  191. this.geometry_cache[ geoNode.id ].skinIndices = this.weights.skinIndices;
  192. this.geometry_cache[ geoNode.id ].skinWeights = this.weights.skinWeights;
  193. }
  194. return this.geometry_cache[ geoNode.id ];
  195. },
  196. parseMeshes: function ( node ) {
  197. var modelNode = node.Objects.subNodes.Model;
  198. var meshes = [];
  199. for ( var ID in modelNode ) {
  200. if ( modelNode[ ID ].attrType === 'Mesh' ) {
  201. //Parse Mesh
  202. meshes.push( this.parseMesh( modelNode[ ID ], node ) );
  203. }
  204. }
  205. return meshes;
  206. },
  207. parseMesh: function ( meshNode, FBXNodes ) {
  208. var geoNodes = FBXNodes.Objects.subNodes.Geometry;
  209. var matNodes = FBXNodes.Objects.subNodes.Material;
  210. var children = FBXNodes.searchConnectionChildren( meshNode.id );
  211. var geometry;
  212. var materials = [];
  213. var material;
  214. var mesh;
  215. for ( var i = 0; i < children.length; ++ i ) {
  216. if ( children[ i ] in geoNodes ) {
  217. geometry = this.getGeometry( this.geometries.geometries[ children[ i ] ] );
  218. continue;
  219. }
  220. if ( children[ i ] in matNodes ) {
  221. materials.push( this.getMaterial( this.materials.materials[ children[ i ] ], FBXNodes ) );
  222. continue;
  223. }
  224. }
  225. if ( materials.length > 1 ) {
  226. material = new THREE.MultiMaterial( materials );
  227. //material = materials[ 0 ];
  228. } else {
  229. material = materials[ 0 ];
  230. }
  231. if ( geometry.bones !== undefined && geometry.skinWeights !== undefined ) {
  232. if ( material instanceof THREE.MultiMaterial ) {
  233. for ( var i = 0; i < material.materials.length; ++ i ) {
  234. material.materials[ i ].skinning = true;
  235. }
  236. } else {
  237. material.skinning = true;
  238. }
  239. mesh = new THREE.SkinnedMesh( geometry, material );
  240. } else {
  241. mesh = new THREE.Mesh( geometry, material );
  242. }
  243. if ( this.animations !== undefined ) {
  244. this.addAnimation( mesh, this.weights.matrices, this.animations );
  245. }
  246. return mesh;
  247. },
  248. addAnimation: function ( mesh, matrices, animations ) {
  249. for ( var key in animations.stacks ) {
  250. var animationData = {
  251. name: animations.stacks[ key ].name,
  252. fps: 30,
  253. length: animations.stacks[ key ].length,
  254. hierarchy: []
  255. };
  256. for ( var i = 0; i < mesh.geometry.bones.length; ++ i ) {
  257. var name = mesh.geometry.bones[ i ].name;
  258. name = name.replace( /.*:/, '' );
  259. animationData.hierarchy.push( { parent: mesh.geometry.bones[ i ].parent, name: name, keys: [] } );
  260. }
  261. function hasCurve( animNode, attr ) {
  262. if ( animNode === undefined ) {
  263. return false;
  264. }
  265. var attrNode;
  266. switch ( attr ) {
  267. case 'S':
  268. if ( ! ( animNode.S ) ) {
  269. return false;
  270. }
  271. attrNode = animNode.S;
  272. break;
  273. case 'R':
  274. if ( ! ( animNode.R ) ) {
  275. return false;
  276. }
  277. attrNode = animNode.R;
  278. break;
  279. case 'T':
  280. if ( ! ( animNode.T ) ) {
  281. return false;
  282. }
  283. attrNode = animNode.T;
  284. break;
  285. }
  286. if ( attrNode.curves.x === undefined ) {
  287. return false;
  288. }
  289. if ( attrNode.curves.y === undefined ) {
  290. return false;
  291. }
  292. if ( attrNode.curves.z === undefined ) {
  293. return false;
  294. }
  295. return true;
  296. }
  297. function hasKeyOnFrame( attrNode, frame ) {
  298. var x = isKeyExistOnFrame( attrNode.curves.x, frame );
  299. var y = isKeyExistOnFrame( attrNode.curves.y, frame );
  300. var z = isKeyExistOnFrame( attrNode.curves.z, frame );
  301. return x && y && z;
  302. }
  303. function isKeyExistOnFrame( curve, frame ) {
  304. var value = curve.values[ frame ];
  305. return value !== undefined;
  306. }
  307. function genKey( animNode, bone ) {
  308. // key initialize with its bone's bind pose at first
  309. var key = {};
  310. key.time = frame / animations.fps; // TODO:
  311. key.pos = bone.pos;
  312. key.rot = bone.rotq;
  313. key.scl = bone.scl;
  314. if ( animNode === undefined ) {
  315. return key;
  316. }
  317. try {
  318. if ( hasCurve( animNode, 'T' ) && hasKeyOnFrame( animNode.T, frame ) ) {
  319. var pos = new THREE.Vector3(
  320. animNode.T.curves.x.values[ frame ],
  321. animNode.T.curves.y.values[ frame ],
  322. animNode.T.curves.z.values[ frame ] );
  323. key.pos = [ pos.x, pos.y, pos.z ];
  324. }
  325. if ( hasCurve( animNode, 'R' ) && hasKeyOnFrame( animNode.R, frame ) ) {
  326. var rx = degToRad( animNode.R.curves.x.values[ frame ] );
  327. var ry = degToRad( animNode.R.curves.y.values[ frame ] );
  328. var rz = degToRad( animNode.R.curves.z.values[ frame ] );
  329. var eul = new THREE.Vector3( rx, ry, rz );
  330. var rot = quatFromVec( eul.x, eul.y, eul.z );
  331. key.rot = [ rot.x, rot.y, rot.z, rot.w ];
  332. }
  333. if ( hasCurve( animNode, 'S' ) && hasKeyOnFrame( animNode.S, frame ) ) {
  334. var scl = new THREE.Vector3(
  335. animNode.S.curves.x.values[ frame ],
  336. animNode.S.curves.y.values[ frame ],
  337. animNode.S.curves.z.values[ frame ] );
  338. key.scl = [ scl.x, scl.y, scl.z ];
  339. }
  340. } catch ( e ) {
  341. // curve is not full plotted
  342. console.log( bone );
  343. console.log( e );
  344. }
  345. return key;
  346. }
  347. var bones = mesh.geometry.bones;
  348. for ( var frame = 0; frame < animations.stacks[ key ].frames; frame ++ ) {
  349. for ( i = 0; i < bones.length; i ++ ) {
  350. var bone = bones[ i ];
  351. var animNode = animations.stacks[ key ].layers[ 0 ][ i ];
  352. for ( var j = 0; j < animationData.hierarchy.length; j ++ ) {
  353. if ( animationData.hierarchy[ j ].name === bone.name ) {
  354. animationData.hierarchy[ j ].keys.push( genKey( animNode, bone ) );
  355. }
  356. }
  357. }
  358. }
  359. if ( mesh.geometry.animations === undefined ) {
  360. mesh.geometry.animations = [];
  361. }
  362. mesh.geometry.animations.push( THREE.AnimationClip.parseAnimation( animationData, mesh.geometry.bones ) );
  363. }
  364. },
  365. loadFile: function ( url, onLoad, onProgress, onError, responseType ) {
  366. var loader = new THREE.FileLoader( this.manager );
  367. loader.setResponseType( responseType );
  368. var request = loader.load( url, onLoad, onProgress, onError );
  369. return request;
  370. },
  371. loadFileAsBuffer: function ( url, onLoad, onProgress, onError ) {
  372. this.loadFile( url, onLoad, onProgress, onError, 'arraybuffer' );
  373. },
  374. loadFileAsText: function ( url, onLoad, onProgress, onError ) {
  375. this.loadFile( url, onLoad, onProgress, onError, 'text' );
  376. }
  377. } );
  378. /* ----------------------------------------------------------------- */
  379. function FBXNodes() {}
  380. Object.assign( FBXNodes.prototype, {
  381. add: function ( key, val ) {
  382. this[ key ] = val;
  383. },
  384. searchConnectionParent: function ( id ) {
  385. if ( this.__cache_search_connection_parent === undefined ) {
  386. this.__cache_search_connection_parent = [];
  387. }
  388. if ( this.__cache_search_connection_parent[ id ] !== undefined ) {
  389. return this.__cache_search_connection_parent[ id ];
  390. } else {
  391. this.__cache_search_connection_parent[ id ] = [];
  392. }
  393. var conns = this.Connections.properties.connections;
  394. var results = [];
  395. for ( var i = 0; i < conns.length; ++ i ) {
  396. if ( conns[ i ][ 0 ] == id ) {
  397. // 0 means scene root
  398. var res = conns[ i ][ 1 ] === 0 ? - 1 : conns[ i ][ 1 ];
  399. results.push( res );
  400. }
  401. }
  402. if ( results.length > 0 ) {
  403. this.__cache_search_connection_parent[ id ] = this.__cache_search_connection_parent[ id ].concat( results );
  404. return results;
  405. } else {
  406. this.__cache_search_connection_parent[ id ] = [ - 1 ];
  407. return [ - 1 ];
  408. }
  409. },
  410. searchConnectionChildren: function ( id ) {
  411. if ( this.__cache_search_connection_children === undefined ) {
  412. this.__cache_search_connection_children = [];
  413. }
  414. if ( this.__cache_search_connection_children[ id ] !== undefined ) {
  415. return this.__cache_search_connection_children[ id ];
  416. } else {
  417. this.__cache_search_connection_children[ id ] = [];
  418. }
  419. var conns = this.Connections.properties.connections;
  420. var res = [];
  421. for ( var i = 0; i < conns.length; ++ i ) {
  422. if ( conns[ i ][ 1 ] == id ) {
  423. // 0 means scene root
  424. res.push( conns[ i ][ 0 ] === 0 ? - 1 : conns[ i ][ 0 ] );
  425. // there may more than one kid, then search to the end
  426. }
  427. }
  428. if ( res.length > 0 ) {
  429. this.__cache_search_connection_children[ id ] = this.__cache_search_connection_children[ id ].concat( res );
  430. return res;
  431. } else {
  432. this.__cache_search_connection_children[ id ] = [ ];
  433. return [ ];
  434. }
  435. },
  436. searchConnectionType: function ( id, to ) {
  437. var key = id + ',' + to; // TODO: to hash
  438. if ( this.__cache_search_connection_type === undefined ) {
  439. this.__cache_search_connection_type = {};
  440. }
  441. if ( this.__cache_search_connection_type[ key ] !== undefined ) {
  442. return this.__cache_search_connection_type[ key ];
  443. } else {
  444. this.__cache_search_connection_type[ key ] = '';
  445. }
  446. var conns = this.Connections.properties.connections;
  447. for ( var i = 0; i < conns.length; ++ i ) {
  448. if ( conns[ i ][ 0 ] == id && conns[ i ][ 1 ] == to ) {
  449. // 0 means scene root
  450. this.__cache_search_connection_type[ key ] = conns[ i ][ 2 ];
  451. return conns[ i ][ 2 ];
  452. }
  453. }
  454. this.__cache_search_connection_type[ id ] = null;
  455. return null;
  456. }
  457. } );
  458. function FBXParser() {}
  459. Object.assign( FBXParser.prototype, {
  460. getPrevNode: function () {
  461. return this.nodeStack[ this.currentIndent - 2 ];
  462. },
  463. getCurrentNode: function () {
  464. return this.nodeStack[ this.currentIndent - 1 ];
  465. },
  466. getCurrentProp: function () {
  467. return this.currentProp;
  468. },
  469. pushStack: function ( node ) {
  470. this.nodeStack.push( node );
  471. this.currentIndent += 1;
  472. },
  473. popStack: function () {
  474. this.nodeStack.pop();
  475. this.currentIndent -= 1;
  476. },
  477. setCurrentProp: function ( val, name ) {
  478. this.currentProp = val;
  479. this.currentPropName = name;
  480. },
  481. // ----------parse ---------------------------------------------------
  482. parse: function ( text ) {
  483. this.currentIndent = 0;
  484. this.allNodes = new FBXNodes();
  485. this.nodeStack = [];
  486. this.currentProp = [];
  487. this.currentPropName = '';
  488. var split = text.split( "\n" );
  489. for ( var line in split ) {
  490. var l = split[ line ];
  491. // short cut
  492. if ( l.match( /^[\s\t]*;/ ) ) {
  493. continue;
  494. } // skip comment line
  495. if ( l.match( /^[\s\t]*$/ ) ) {
  496. continue;
  497. } // skip empty line
  498. // beginning of node
  499. var beginningOfNodeExp = new RegExp( "^\\t{" + this.currentIndent + "}(\\w+):(.*){", '' );
  500. var match = l.match( beginningOfNodeExp );
  501. if ( match ) {
  502. var nodeName = match[ 1 ].trim().replace( /^"/, '' ).replace( /"$/, "" );
  503. var nodeAttrs = match[ 2 ].split( ',' ).map( function ( element ) {
  504. return element.trim().replace( /^"/, '' ).replace( /"$/, '' );
  505. } );
  506. this.parseNodeBegin( l, nodeName, nodeAttrs || null );
  507. continue;
  508. }
  509. // node's property
  510. var propExp = new RegExp( "^\\t{" + ( this.currentIndent ) + "}(\\w+):[\\s\\t\\r\\n](.*)" );
  511. var match = l.match( propExp );
  512. if ( match ) {
  513. var propName = match[ 1 ].replace( /^"/, '' ).replace( /"$/, "" ).trim();
  514. var propValue = match[ 2 ].replace( /^"/, '' ).replace( /"$/, "" ).trim();
  515. this.parseNodeProperty( l, propName, propValue );
  516. continue;
  517. }
  518. // end of node
  519. var endOfNodeExp = new RegExp( "^\\t{" + ( this.currentIndent - 1 ) + "}}" );
  520. if ( l.match( endOfNodeExp ) ) {
  521. this.nodeEnd();
  522. continue;
  523. }
  524. // for special case,
  525. //
  526. // Vertices: *8670 {
  527. // a: 0.0356229953467846,13.9599733352661,-0.399196773.....(snip)
  528. // -0.0612030513584614,13.960485458374,-0.409748703241348,-0.10.....
  529. // 0.12490539252758,13.7450733184814,-0.454119384288788,0.09272.....
  530. // 0.0836158767342567,13.5432004928589,-0.435397416353226,0.028.....
  531. //
  532. // these case the lines must contiue with previous line
  533. if ( l.match( /^[^\s\t}]/ ) ) {
  534. this.parseNodePropertyContinued( l );
  535. }
  536. }
  537. return this.allNodes;
  538. },
  539. parseNodeBegin: function ( line, nodeName, nodeAttrs ) {
  540. // var nodeName = match[1];
  541. var node = { 'name': nodeName, properties: {}, 'subNodes': {} };
  542. var attrs = this.parseNodeAttr( nodeAttrs );
  543. var currentNode = this.getCurrentNode();
  544. // a top node
  545. if ( this.currentIndent === 0 ) {
  546. this.allNodes.add( nodeName, node );
  547. } else {
  548. // a subnode
  549. // already exists subnode, then append it
  550. if ( nodeName in currentNode.subNodes ) {
  551. var tmp = currentNode.subNodes[ nodeName ];
  552. // console.log( "duped entry found\nkey: " + nodeName + "\nvalue: " + propValue );
  553. if ( this.isFlattenNode( currentNode.subNodes[ nodeName ] ) ) {
  554. if ( attrs.id === '' ) {
  555. currentNode.subNodes[ nodeName ] = [];
  556. currentNode.subNodes[ nodeName ].push( tmp );
  557. } else {
  558. currentNode.subNodes[ nodeName ] = {};
  559. currentNode.subNodes[ nodeName ][ tmp.id ] = tmp;
  560. }
  561. }
  562. if ( attrs.id === '' ) {
  563. currentNode.subNodes[ nodeName ].push( node );
  564. } else {
  565. currentNode.subNodes[ nodeName ][ attrs.id ] = node;
  566. }
  567. } else if ( typeof attrs.id === 'number' || attrs.id.match( /^\d+$/ ) ) {
  568. currentNode.subNodes[ nodeName ] = {};
  569. currentNode.subNodes[ nodeName ][ attrs.id ] = node;
  570. } else {
  571. currentNode.subNodes[ nodeName ] = node;
  572. }
  573. }
  574. // for this ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
  575. // NodeAttribute: 1001463072, "NodeAttribute::", "LimbNode" {
  576. if ( nodeAttrs ) {
  577. node.id = attrs.id;
  578. node.attrName = attrs.name;
  579. node.attrType = attrs.type;
  580. }
  581. this.pushStack( node );
  582. },
  583. parseNodeAttr: function ( attrs ) {
  584. var id = attrs[ 0 ];
  585. if ( attrs[ 0 ] !== "" ) {
  586. id = parseInt( attrs[ 0 ] );
  587. if ( isNaN( id ) ) {
  588. // PolygonVertexIndex: *16380 {
  589. id = attrs[ 0 ];
  590. }
  591. }
  592. var name;
  593. var type;
  594. if ( attrs.length > 1 ) {
  595. name = attrs[ 1 ].replace( /^(\w+)::/, '' );
  596. type = attrs[ 2 ];
  597. }
  598. return { id: id, name: name || '', type: type || '' };
  599. },
  600. parseNodeProperty: function ( line, propName, propValue ) {
  601. var currentNode = this.getCurrentNode();
  602. var parentName = currentNode.name;
  603. // special case parent node's is like "Properties70"
  604. // these chilren nodes must treat with careful
  605. if ( parentName !== undefined ) {
  606. var propMatch = parentName.match( /Properties(\d)+/ );
  607. if ( propMatch ) {
  608. this.parseNodeSpecialProperty( line, propName, propValue );
  609. return;
  610. }
  611. }
  612. // special case Connections
  613. if ( propName == 'C' ) {
  614. var connProps = propValue.split( ',' ).slice( 1 );
  615. var from = parseInt( connProps[ 0 ] );
  616. var to = parseInt( connProps[ 1 ] );
  617. var rest = propValue.split( ',' ).slice( 3 );
  618. propName = 'connections';
  619. propValue = [ from, to ];
  620. propValue = propValue.concat( rest );
  621. if ( currentNode.properties[ propName ] === undefined ) {
  622. currentNode.properties[ propName ] = [];
  623. }
  624. }
  625. // special case Connections
  626. if ( propName == 'Node' ) {
  627. var id = parseInt( propValue );
  628. currentNode.properties.id = id;
  629. currentNode.id = id;
  630. }
  631. // already exists in properties, then append this
  632. if ( propName in currentNode.properties ) {
  633. // console.log( "duped entry found\nkey: " + propName + "\nvalue: " + propValue );
  634. if ( Array.isArray( currentNode.properties[ propName ] ) ) {
  635. currentNode.properties[ propName ].push( propValue );
  636. } else {
  637. currentNode.properties[ propName ] += propValue;
  638. }
  639. } else {
  640. // console.log( propName + ": " + propValue );
  641. if ( Array.isArray( currentNode.properties[ propName ] ) ) {
  642. currentNode.properties[ propName ].push( propValue );
  643. } else {
  644. currentNode.properties[ propName ] = propValue;
  645. }
  646. }
  647. this.setCurrentProp( currentNode.properties, propName );
  648. },
  649. // TODO:
  650. parseNodePropertyContinued: function ( line ) {
  651. this.currentProp[ this.currentPropName ] += line;
  652. },
  653. parseNodeSpecialProperty: function ( line, propName, propValue ) {
  654. // split this
  655. // P: "Lcl Scaling", "Lcl Scaling", "", "A",1,1,1
  656. // into array like below
  657. // ["Lcl Scaling", "Lcl Scaling", "", "A", "1,1,1" ]
  658. var props = propValue.split( '",' ).map( function ( element ) {
  659. return element.trim().replace( /^\"/, '' ).replace( /\s/, '_' );
  660. } );
  661. var innerPropName = props[ 0 ];
  662. var innerPropType1 = props[ 1 ];
  663. var innerPropType2 = props[ 2 ];
  664. var innerPropFlag = props[ 3 ];
  665. var innerPropValue = props[ 4 ];
  666. /*
  667. if ( innerPropValue === undefined ) {
  668. innerPropValue = props[3];
  669. }
  670. */
  671. // cast value in its type
  672. switch ( innerPropType1 ) {
  673. case "int":
  674. innerPropValue = parseInt( innerPropValue );
  675. break;
  676. case "double":
  677. innerPropValue = parseFloat( innerPropValue );
  678. break;
  679. case "ColorRGB":
  680. case "Vector3D":
  681. var tmp = innerPropValue.split( ',' );
  682. innerPropValue = new THREE.Vector3( tmp[ 0 ], tmp[ 1 ], tmp[ 2 ] );
  683. break;
  684. }
  685. // CAUTION: these props must append to parent's parent
  686. this.getPrevNode().properties[ innerPropName ] = {
  687. 'type': innerPropType1,
  688. 'type2': innerPropType2,
  689. 'flag': innerPropFlag,
  690. 'value': innerPropValue
  691. };
  692. this.setCurrentProp( this.getPrevNode().properties, innerPropName );
  693. },
  694. nodeEnd: function () {
  695. this.popStack();
  696. },
  697. /* ---------------------------------------------------------------- */
  698. /* util */
  699. isFlattenNode: function ( node ) {
  700. return ( 'subNodes' in node && 'properties' in node ) ? true : false;
  701. }
  702. } );
  703. // generate skinIndices, skinWeights
  704. // @skinIndices: per vertex data, this represents the bone indexes affects that vertex
  705. // @skinWeights: per vertex data, this represents the Weight Values affects that vertex
  706. // @matrices: per `bones` data
  707. function Weights() {
  708. this.skinIndices = [];
  709. this.skinWeights = [];
  710. this.matrices = [];
  711. }
  712. Weights.prototype.parseCluster = function ( node, id, entry ) {
  713. var _p = node.searchConnectionParent( id );
  714. var _indices = parseArrayToInt( entry.subNodes.Indexes.properties.a );
  715. var _weights = parseArrayToFloat( entry.subNodes.Weights.properties.a );
  716. var _transform = parseArrayToMatrix( entry.subNodes.Transform.properties.a );
  717. var _link = parseArrayToMatrix( entry.subNodes.TransformLink.properties.a );
  718. return {
  719. 'parent': _p,
  720. 'id': parseInt( id ),
  721. 'indices': _indices,
  722. 'weights': _weights,
  723. 'transform': _transform,
  724. 'transformlink': _link,
  725. 'linkMode': entry.properties.Mode
  726. };
  727. };
  728. Weights.prototype.parse = function ( node, bones ) {
  729. this.skinIndices = [];
  730. this.skinWeights = [];
  731. this.matrices = [];
  732. var deformers = node.Objects.subNodes.Deformer;
  733. var clusters = {};
  734. for ( var id in deformers ) {
  735. if ( deformers[ id ].attrType === 'Cluster' ) {
  736. if ( ! ( 'Indexes' in deformers[ id ].subNodes ) ) {
  737. continue;
  738. }
  739. //clusters.push( this.parseCluster( node, id, deformers[id] ) );
  740. var cluster = this.parseCluster( node, id, deformers[ id ] );
  741. var boneId = node.searchConnectionChildren( cluster.id )[ 0 ];
  742. clusters[ boneId ] = cluster;
  743. }
  744. }
  745. // this clusters is per Bone data, thus we make this into per vertex data
  746. var weights = [];
  747. var hi = bones.hierarchy;
  748. for ( var b = 0; b < hi.length; ++ b ) {
  749. var bid = hi[ b ].internalId;
  750. if ( clusters[ bid ] === undefined ) {
  751. //console.log( bid );
  752. this.matrices.push( new THREE.Matrix4() );
  753. continue;
  754. }
  755. var clst = clusters[ bid ];
  756. // store transform matrix per bones
  757. this.matrices.push( clst.transform );
  758. //this.matrices.push( clst.transformlink );
  759. for ( var v = 0; v < clst.indices.length; ++ v ) {
  760. if ( weights[ clst.indices[ v ] ] === undefined ) {
  761. weights[ clst.indices[ v ] ] = {};
  762. weights[ clst.indices[ v ] ].joint = [];
  763. weights[ clst.indices[ v ] ].weight = [];
  764. }
  765. // indices
  766. var affect = node.searchConnectionChildren( clst.id );
  767. if ( affect.length > 1 ) {
  768. console.warn( "FBXLoader: node " + clst.id + " have many weight kids: " + affect );
  769. }
  770. weights[ clst.indices[ v ] ].joint.push( bones.getBoneIdfromInternalId( node, affect[ 0 ] ) );
  771. // weight value
  772. weights[ clst.indices[ v ] ].weight.push( clst.weights[ v ] );
  773. }
  774. }
  775. // normalize the skin weights
  776. // TODO - this might be a good place to choose greatest 4 weights
  777. for ( var i = 0; i < weights.length; i ++ ) {
  778. if ( weights[ i ] === undefined ) {
  779. this.skinIndices.push( new THREE.Vector4( 0, 0, 0, 0 ) );
  780. this.skinWeights.push( new THREE.Vector4( 0, 0, 0, 0 ) );
  781. continue;
  782. }
  783. var indicies = new THREE.Vector4(
  784. weights[ i ].joint[ 0 ] ? weights[ i ].joint[ 0 ] : 0,
  785. weights[ i ].joint[ 1 ] ? weights[ i ].joint[ 1 ] : 0,
  786. weights[ i ].joint[ 2 ] ? weights[ i ].joint[ 2 ] : 0,
  787. weights[ i ].joint[ 3 ] ? weights[ i ].joint[ 3 ] : 0 );
  788. var weight = new THREE.Vector4(
  789. weights[ i ].weight[ 0 ] ? weights[ i ].weight[ 0 ] : 0,
  790. weights[ i ].weight[ 1 ] ? weights[ i ].weight[ 1 ] : 0,
  791. weights[ i ].weight[ 2 ] ? weights[ i ].weight[ 2 ] : 0,
  792. weights[ i ].weight[ 3 ] ? weights[ i ].weight[ 3 ] : 0 );
  793. this.skinIndices.push( indicies );
  794. this.skinWeights.push( weight );
  795. }
  796. //console.log( this );
  797. return this;
  798. };
  799. function Bones() {
  800. // returns bones hierarchy tree.
  801. // [
  802. // {
  803. // "parent": id,
  804. // "name": name,
  805. // "pos": pos,
  806. // "rotq": quat
  807. // },
  808. // ...
  809. // {},
  810. // ...
  811. // ]
  812. //
  813. /* sample response
  814. "bones" : [
  815. {"parent":-1, "name":"Fbx01", "pos":[-0.002, 98.739, 1.6e-05], "rotq":[0, 0, 0, 1]},
  816. {"parent":0, "name":"Fbx01_Pelvis", "pos":[0.00015963, 0, 7.33107e-08], "rotq":[0, 0, 0, 1]},
  817. {"parent":1, "name":"Fbx01_Spine", "pos":[6.577e-06, 10.216, 0.0106811], "rotq":[0, 0, 0, 1]},
  818. {"parent":2, "name":"Fbx01_R_Thigh", "pos":[14.6537, -10.216, -0.00918758], "rotq":[0, 0, 0, 1]},
  819. {"parent":3, "name":"Fbx01_R_Calf", "pos":[-3.70047, -42.9681, -7.78158], "rotq":[0, 0, 0, 1]},
  820. {"parent":4, "name":"Fbx01_R_Foot", "pos":[-2.0696, -46.0488, 9.42052], "rotq":[0, 0, 0, 1]},
  821. {"parent":5, "name":"Fbx01_R_Toe0", "pos":[-0.0234785, -9.46233, -15.3187], "rotq":[0, 0, 0, 1]},
  822. {"parent":2, "name":"Fbx01_L_Thigh", "pos":[-14.6537, -10.216, -0.00918314], "rotq":[0, 0, 0, 1]},
  823. {"parent":7, "name":"Fbx01_L_Calf", "pos":[3.70037, -42.968, -7.78155], "rotq":[0, 0, 0, 1]},
  824. {"parent":8, "name":"Fbx01_L_Foot", "pos":[2.06954, -46.0488, 9.42052], "rotq":[0, 0, 0, 1]},
  825. {"parent":9, "name":"Fbx01_L_Toe0", "pos":[0.0234566, -9.46235, -15.3187], "rotq":[0, 0, 0, 1]},
  826. {"parent":2, "name":"Fbx01_Spine1", "pos":[-2.97523e-05, 11.5892, -9.81027e-05], "rotq":[0, 0, 0, 1]},
  827. {"parent":11, "name":"Fbx01_Spine2", "pos":[-2.91292e-05, 11.4685, 8.27126e-05], "rotq":[0, 0, 0, 1]},
  828. {"parent":12, "name":"Fbx01_Spine3", "pos":[-4.48857e-05, 11.5783, 8.35108e-05], "rotq":[0, 0, 0, 1]},
  829. {"parent":13, "name":"Fbx01_Neck", "pos":[1.22987e-05, 11.5582, -0.0044775], "rotq":[0, 0, 0, 1]},
  830. {"parent":14, "name":"Fbx01_Head", "pos":[-3.50709e-05, 6.62915, -0.00523254], "rotq":[0, 0, 0, 1]},
  831. {"parent":15, "name":"Fbx01_R_Eye", "pos":[3.31681, 12.739, -10.5267], "rotq":[0, 0, 0, 1]},
  832. {"parent":15, "name":"Fbx01_L_Eye", "pos":[-3.32038, 12.7391, -10.5267], "rotq":[0, 0, 0, 1]},
  833. {"parent":15, "name":"Jaw", "pos":[-0.0017738, 7.43481, -4.08114], "rotq":[0, 0, 0, 1]},
  834. {"parent":14, "name":"Fbx01_R_Clavicle", "pos":[3.10919, 2.46577, -0.0115284], "rotq":[0, 0, 0, 1]},
  835. {"parent":19, "name":"Fbx01_R_UpperArm", "pos":[16.014, 4.57764e-05, 3.10405], "rotq":[0, 0, 0, 1]},
  836. {"parent":20, "name":"Fbx01_R_Forearm", "pos":[22.7068, -1.66322, -2.13803], "rotq":[0, 0, 0, 1]},
  837. {"parent":21, "name":"Fbx01_R_Hand", "pos":[25.5881, -0.80249, -6.37307], "rotq":[0, 0, 0, 1]},
  838. ...
  839. {"parent":27, "name":"Fbx01_R_Finger32", "pos":[2.15572, -0.548737, -0.539604], "rotq":[0, 0, 0, 1]},
  840. {"parent":22, "name":"Fbx01_R_Finger2", "pos":[9.79318, 0.132553, -2.97845], "rotq":[0, 0, 0, 1]},
  841. {"parent":29, "name":"Fbx01_R_Finger21", "pos":[2.74037, 0.0483093, -0.650531], "rotq":[0, 0, 0, 1]},
  842. {"parent":55, "name":"Fbx01_L_Finger02", "pos":[-1.65308, -1.43208, -1.82885], "rotq":[0, 0, 0, 1]}
  843. ]
  844. */
  845. this.hierarchy = [];
  846. }
  847. Bones.prototype.parseHierarchy = function ( node ) {
  848. var objects = node.Objects;
  849. var models = objects.subNodes.Model;
  850. var bones = [];
  851. for ( var id in models ) {
  852. if ( models[ id ].attrType === undefined ) {
  853. continue;
  854. }
  855. bones.push( models[ id ] );
  856. }
  857. this.hierarchy = [];
  858. for ( var i = 0; i < bones.length; ++ i ) {
  859. var bone = bones[ i ];
  860. var p = node.searchConnectionParent( bone.id )[ 0 ];
  861. var t = [ 0.0, 0.0, 0.0 ];
  862. var r = [ 0.0, 0.0, 0.0, 1.0 ];
  863. var s = [ 1.0, 1.0, 1.0 ];
  864. if ( 'Lcl_Translation' in bone.properties ) {
  865. t = parseArrayToFloat( bone.properties.Lcl_Translation.value );
  866. }
  867. if ( 'Lcl_Rotation' in bone.properties ) {
  868. r = parseArrayToRadians( bone.properties.Lcl_Rotation.value );
  869. var q = new THREE.Quaternion();
  870. q.setFromEuler( new THREE.Euler( r[ 0 ], r[ 1 ], r[ 2 ], 'ZYX' ) );
  871. r = [ q.x, q.y, q.z, q.w ];
  872. }
  873. if ( 'Lcl_Scaling' in bone.properties ) {
  874. s = parseArrayToFloat( bone.properties.Lcl_Scaling.value );
  875. }
  876. // replace unsafe character
  877. var name = bone.attrName;
  878. name = name.replace( /:/, '' );
  879. name = name.replace( /_/, '' );
  880. name = name.replace( /-/, '' );
  881. this.hierarchy.push( { "parent": p, "name": name, "pos": t, "rotq": r, "scl": s, "internalId": bone.id } );
  882. }
  883. this.reindexParentId();
  884. this.restoreBindPose( node );
  885. return this;
  886. };
  887. Bones.prototype.reindexParentId = function () {
  888. for ( var h = 0; h < this.hierarchy.length; h ++ ) {
  889. for ( var ii = 0; ii < this.hierarchy.length; ++ ii ) {
  890. if ( this.hierarchy[ h ].parent == this.hierarchy[ ii ].internalId ) {
  891. this.hierarchy[ h ].parent = ii;
  892. break;
  893. }
  894. }
  895. }
  896. };
  897. Bones.prototype.restoreBindPose = function ( node ) {
  898. var bindPoseNode = node.Objects.subNodes.Pose;
  899. if ( bindPoseNode === undefined ) {
  900. return;
  901. }
  902. for ( var key in bindPoseNode ) {
  903. if ( bindPoseNode[ key ].attrType === 'BindPose' ) {
  904. bindPoseNode = bindPoseNode[ key ];
  905. break;
  906. }
  907. }
  908. var poseNode = bindPoseNode.subNodes.PoseNode;
  909. var localMatrices = {}; // store local matrices, modified later( initialy world space )
  910. var worldMatrices = {}; // store world matrices
  911. for ( var i = 0; i < poseNode.length; ++ i ) {
  912. var rawMatLcl = parseArrayToMatrix( poseNode[ i ].subNodes.Matrix.properties.a );
  913. var rawMatWrd = parseArrayToMatrix( poseNode[ i ].subNodes.Matrix.properties.a );
  914. localMatrices[ poseNode[ i ].id ] = rawMatLcl;
  915. worldMatrices[ poseNode[ i ].id ] = rawMatWrd;
  916. }
  917. for ( var h = 0; h < this.hierarchy.length; ++ h ) {
  918. var bone = this.hierarchy[ h ];
  919. var inId = bone.internalId;
  920. if ( worldMatrices[ inId ] === undefined ) {
  921. // has no bind pose node, possibly be mesh
  922. // console.log( bone );
  923. continue;
  924. }
  925. var t = new THREE.Vector3( 0, 0, 0 );
  926. var r = new THREE.Quaternion();
  927. var s = new THREE.Vector3( 1, 1, 1 );
  928. var parentId;
  929. var parentNodes = node.searchConnectionParent( inId );
  930. for ( var pn = 0; pn < parentNodes.length; ++ pn ) {
  931. if ( this.isBoneNode( parentNodes[ pn ] ) ) {
  932. parentId = parentNodes[ pn ];
  933. break;
  934. }
  935. }
  936. if ( parentId !== undefined && localMatrices[ parentId ] !== undefined ) {
  937. // convert world space matrix into local space
  938. var inv = new THREE.Matrix4();
  939. inv.getInverse( worldMatrices[ parentId ] );
  940. inv.multiply( localMatrices[ inId ] );
  941. localMatrices[ inId ] = inv;
  942. } else {
  943. //console.log( bone );
  944. }
  945. localMatrices[ inId ].decompose( t, r, s );
  946. bone.pos = [ t.x, t.y, t.z ];
  947. bone.rotq = [ r.x, r.y, r.z, r.w ];
  948. bone.scl = [ s.x, s.y, s.z ];
  949. }
  950. };
  951. Bones.prototype.searchRealId = function ( internalId ) {
  952. for ( var h = 0; h < this.hierarchy.length; h ++ ) {
  953. if ( internalId == this.hierarchy[ h ].internalId ) {
  954. return h;
  955. }
  956. }
  957. // console.warn( 'FBXLoader: notfound internalId in bones: ' + internalId);
  958. return - 1;
  959. };
  960. Bones.prototype.getByInternalId = function ( internalId ) {
  961. for ( var h = 0; h < this.hierarchy.length; h ++ ) {
  962. if ( internalId == this.hierarchy[ h ].internalId ) {
  963. return this.hierarchy[ h ];
  964. }
  965. }
  966. return null;
  967. };
  968. Bones.prototype.isBoneNode = function ( id ) {
  969. for ( var i = 0; i < this.hierarchy.length; ++ i ) {
  970. if ( id === this.hierarchy[ i ].internalId ) {
  971. return true;
  972. }
  973. }
  974. return false;
  975. };
  976. Bones.prototype.getBoneIdfromInternalId = function ( node, id ) {
  977. if ( node.__cache_get_boneid_from_internalid === undefined ) {
  978. node.__cache_get_boneid_from_internalid = [];
  979. }
  980. if ( node.__cache_get_boneid_from_internalid[ id ] !== undefined ) {
  981. return node.__cache_get_boneid_from_internalid[ id ];
  982. }
  983. for ( var i = 0; i < this.hierarchy.length; ++ i ) {
  984. if ( this.hierarchy[ i ].internalId == id ) {
  985. node.__cache_get_boneid_from_internalid[ id ] = i;
  986. return i;
  987. }
  988. }
  989. // console.warn( 'FBXLoader: bone internalId(' + id + ') not found in bone hierarchy' );
  990. return - 1;
  991. };
  992. function Geometries() {
  993. this.geometries = {};
  994. }
  995. Object.assign( Geometries.prototype, {
  996. parse: function ( FBXNodes, hierarchy ) {
  997. if ( ! ( 'Geometry' in FBXNodes.Objects.subNodes ) ) {
  998. return this;
  999. }
  1000. for ( var geo in FBXNodes.Objects.subNodes.Geometry ) {
  1001. if ( FBXNodes.Objects.subNodes.Geometry[ geo ].attrType === 'Mesh' ) {
  1002. this.geometries[ geo ] = ( new Geometry() ).parse( FBXNodes.Objects.subNodes.Geometry[ geo ] );
  1003. this.geometries[ geo ].addBones( hierarchy.hierarchy );
  1004. }
  1005. }
  1006. return this;
  1007. }
  1008. } );
  1009. function Geometry() {
  1010. this.node = null;
  1011. this.name = null;
  1012. this.id = null;
  1013. this.vertices = [];
  1014. this.indices = [];
  1015. this.normals = [];
  1016. this.uvs = [];
  1017. this.bones = [];
  1018. }
  1019. Geometry.prototype.parse = function ( geoNode ) {
  1020. this.node = geoNode;
  1021. this.name = geoNode.attrName;
  1022. this.id = geoNode.id;
  1023. this.vertices = this.getVertices();
  1024. if ( this.vertices === undefined ) {
  1025. console.log( 'FBXLoader: Geometry.parse(): pass' + this.node.id );
  1026. return;
  1027. }
  1028. this.indices = this.getPolygonVertexIndices();
  1029. this.uvs = ( new UV() ).parse( this.node, this );
  1030. this.normals = ( new Normal() ).parse( this.node, this );
  1031. this.materialIndices = ( new MaterialIndex() ).parse( this.node );
  1032. if ( this.getPolygonTopologyMax() > 3 ) {
  1033. var indexInfo = this.convertPolyIndicesToTri(
  1034. this.indices,
  1035. this.materialIndices,
  1036. this.getPolygonTopologyArray() );
  1037. this.indices = indexInfo.res;
  1038. this.materialIndices = indexInfo.materialIndices;
  1039. this.polyIndices = indexInfo.polyIndices;
  1040. }
  1041. return this;
  1042. };
  1043. Geometry.prototype.getVertices = function () {
  1044. if ( this.node.__cache_vertices ) {
  1045. return this.node.__cache_vertices;
  1046. }
  1047. if ( this.node.subNodes.Vertices === undefined ) {
  1048. console.warn( 'this.node: ' + this.node.attrName + "(" + this.node.id + ") does not have Vertices" );
  1049. this.node.__cache_vertices = undefined;
  1050. return null;
  1051. }
  1052. var rawTextVert = this.node.subNodes.Vertices.properties.a;
  1053. var vertices = rawTextVert.split( ',' ).map( function ( element ) {
  1054. return parseFloat( element );
  1055. } );
  1056. this.node.__cache_vertices = vertices;
  1057. return this.node.__cache_vertices;
  1058. };
  1059. Geometry.prototype.getPolygonVertexIndices = function () {
  1060. if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
  1061. return this.node.__cache_indices;
  1062. }
  1063. if ( this.node.subNodes === undefined ) {
  1064. console.error( 'this.node.subNodes undefined' );
  1065. console.log( this.node );
  1066. return;
  1067. }
  1068. if ( this.node.subNodes.PolygonVertexIndex === undefined ) {
  1069. console.warn( 'this.node: ' + this.node.attrName + "(" + this.node.id + ") does not have PolygonVertexIndex " );
  1070. this.node.__cache_indices = undefined;
  1071. return;
  1072. }
  1073. var rawTextIndices = this.node.subNodes.PolygonVertexIndex.properties.a;
  1074. var indices = rawTextIndices.split( ',' );
  1075. var currentTopo = 1;
  1076. var topologyN = null;
  1077. var topologyArr = [];
  1078. // The indices that make up the polygon are in order and a negative index
  1079. // means that it’s the last index of the polygon. That index needs
  1080. // to be made positive and then you have to subtract 1 from it!
  1081. for ( var i = 0; i < indices.length; ++ i ) {
  1082. var tmpI = parseInt( indices[ i ] );
  1083. // found n
  1084. if ( tmpI < 0 ) {
  1085. if ( currentTopo > topologyN ) {
  1086. topologyN = currentTopo;
  1087. }
  1088. indices[ i ] = tmpI ^ - 1;
  1089. topologyArr.push( currentTopo );
  1090. currentTopo = 1;
  1091. } else {
  1092. indices[ i ] = tmpI;
  1093. currentTopo ++;
  1094. }
  1095. }
  1096. if ( topologyN === null ) {
  1097. console.warn( "FBXLoader: topology N not found: " + this.node.attrName );
  1098. console.warn( this.node );
  1099. topologyN = 3;
  1100. }
  1101. this.node.__cache_poly_topology_max = topologyN;
  1102. this.node.__cache_poly_topology_arr = topologyArr;
  1103. this.node.__cache_indices = indices;
  1104. return this.node.__cache_indices;
  1105. };
  1106. Geometry.prototype.getPolygonTopologyMax = function () {
  1107. if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
  1108. return this.node.__cache_poly_topology_max;
  1109. }
  1110. this.getPolygonVertexIndices( this.node );
  1111. return this.node.__cache_poly_topology_max;
  1112. };
  1113. Geometry.prototype.getPolygonTopologyArray = function () {
  1114. if ( this.node.__cache_indices && this.node.__cache_poly_topology_max ) {
  1115. return this.node.__cache_poly_topology_arr;
  1116. }
  1117. this.getPolygonVertexIndices( this.node );
  1118. return this.node.__cache_poly_topology_arr;
  1119. };
  1120. // a - d
  1121. // | |
  1122. // b - c
  1123. //
  1124. // [( a, b, c, d ) ...........
  1125. // [( a, b, c ), (a, c, d )....
  1126. // Also keep track of original poly index.
  1127. Geometry.prototype.convertPolyIndicesToTri = function ( indices, materialIndices, strides ) {
  1128. var res = [];
  1129. var i = 0;
  1130. var currentPolyNum = 0;
  1131. var currentStride = 0;
  1132. var polyIndices = [];
  1133. while ( i < indices.length ) {
  1134. currentStride = strides[ currentPolyNum ];
  1135. // CAUTIN: NG over 6gon
  1136. for ( var j = 0; j <= ( currentStride - 3 ); j ++ ) {
  1137. res.push( indices[ i ] );
  1138. res.push( indices[ i + ( currentStride - 2 - j ) ] );
  1139. res.push( indices[ i + ( currentStride - 1 - j ) ] );
  1140. polyIndices.push( currentPolyNum );
  1141. }
  1142. currentPolyNum ++;
  1143. i += currentStride;
  1144. }
  1145. var newMaterialIndices = [ materialIndices[ 0 ] ];
  1146. if ( materialIndices.length > 1 ) {
  1147. for ( var i = 0; i < polyIndices.length; ++ i ) {
  1148. newMaterialIndices[ i ] = materialIndices[ polyIndices[ i ] ];
  1149. }
  1150. }
  1151. return {
  1152. res: res,
  1153. materialIndices: newMaterialIndices,
  1154. polyIndices: polyIndices };
  1155. };
  1156. Geometry.prototype.addBones = function ( bones ) {
  1157. this.bones = bones;
  1158. };
  1159. function UV() {
  1160. this.uv = null;
  1161. this.map = null;
  1162. this.ref = null;
  1163. this.node = null;
  1164. this.index = null;
  1165. }
  1166. UV.prototype.getUV = function ( node ) {
  1167. if ( this.node && this.uv && this.map && this.ref ) {
  1168. return this.uv;
  1169. } else {
  1170. return this._parseText( node );
  1171. }
  1172. };
  1173. UV.prototype.getMap = function ( node ) {
  1174. if ( this.node && this.uv && this.map && this.ref ) {
  1175. return this.map;
  1176. } else {
  1177. this._parseText( node );
  1178. return this.map;
  1179. }
  1180. };
  1181. UV.prototype.getRef = function ( node ) {
  1182. if ( this.node && this.uv && this.map && this.ref ) {
  1183. return this.ref;
  1184. } else {
  1185. this._parseText( node );
  1186. return this.ref;
  1187. }
  1188. };
  1189. UV.prototype.getIndex = function ( node ) {
  1190. if ( this.node && this.uv && this.map && this.ref ) {
  1191. return this.index;
  1192. } else {
  1193. this._parseText( node );
  1194. return this.index;
  1195. }
  1196. };
  1197. UV.prototype.getNode = function ( topnode ) {
  1198. if ( this.node !== null ) {
  1199. return this.node;
  1200. }
  1201. this.node = topnode.subNodes.LayerElementUV;
  1202. return this.node;
  1203. };
  1204. UV.prototype._parseText = function ( node ) {
  1205. var uvNode = this.getNode( node )[ 0 ];
  1206. if ( uvNode === undefined ) {
  1207. // console.log( node.attrName + "(" + node.id + ")" + " has no LayerElementUV." );
  1208. return [];
  1209. }
  1210. var count = 0;
  1211. for ( var n in uvNode ) {
  1212. if ( n.match( /^\d+$/ ) ) {
  1213. count ++;
  1214. }
  1215. }
  1216. if ( count > 0 ) {
  1217. console.warn( 'multi uv not supported' );
  1218. uvNode = uvNode[ n ];
  1219. }
  1220. var uvIndex = uvNode.subNodes.UVIndex.properties.a;
  1221. var uvs = uvNode.subNodes.UV.properties.a;
  1222. var uvMap = uvNode.properties.MappingInformationType;
  1223. var uvRef = uvNode.properties.ReferenceInformationType;
  1224. this.uv = parseArrayToFloat( uvs );
  1225. this.index = parseArrayToInt( uvIndex );
  1226. this.map = uvMap; // TODO: normalize notation shaking... FOR BLENDER
  1227. this.ref = uvRef;
  1228. return this.uv;
  1229. };
  1230. UV.prototype.parse = function ( node, geo ) {
  1231. if ( ! ( 'LayerElementUV' in node.subNodes ) ) {
  1232. return;
  1233. }
  1234. this.uvNode = this.getNode( node );
  1235. this.uv = this.getUV( node );
  1236. var mappingType = this.getMap( node );
  1237. var refType = this.getRef( node );
  1238. var indices = this.getIndex( node );
  1239. var strides = geo.getPolygonTopologyArray();
  1240. // it means that there is a normal for every vertex of every polygon of the model.
  1241. // For example, if the models has 8 vertices that make up four quads, then there
  1242. // will be 16 normals (one normal * 4 polygons * 4 vertices of the polygon). Note
  1243. // that generally a game engine needs the vertices to have only one normal defined.
  1244. // So, if you find a vertex has more tha one normal, you can either ignore the normals
  1245. // you find after the first, or calculate the mean from all of them (normal smoothing).
  1246. //if ( mappingType == "ByPolygonVertex" ){
  1247. switch ( mappingType ) {
  1248. case "ByPolygonVertex":
  1249. switch ( refType ) {
  1250. // Direct
  1251. // The this.uv are in order.
  1252. case "Direct":
  1253. this.uv = this.parseUV_ByPolygonVertex_Direct( this.uv, indices, strides, 2 );
  1254. break;
  1255. // IndexToDirect
  1256. // The order of the this.uv is given by the uvsIndex property.
  1257. case "IndexToDirect":
  1258. this.uv = this.parseUV_ByPolygonVertex_IndexToDirect( this.uv, indices );
  1259. break;
  1260. }
  1261. // convert from by polygon(vert) data into by verts data
  1262. this.uv = mapByPolygonVertexToByVertex( this.uv, geo.getPolygonVertexIndices( node ), 2 );
  1263. break;
  1264. case "ByPolygon":
  1265. switch ( refType ) {
  1266. // Direct
  1267. // The this.uv are in order.
  1268. case "Direct":
  1269. this.uv = this.parseUV_ByPolygon_Direct();
  1270. break;
  1271. // IndexToDirect
  1272. // The order of the this.uv is given by the uvsIndex property.
  1273. case "IndexToDirect":
  1274. this.uv = this.parseUV_ByPolygon_IndexToDirect();
  1275. break;
  1276. }
  1277. break;
  1278. }
  1279. return this.uv;
  1280. };
  1281. UV.prototype.parseUV_ByPolygonVertex_Direct = function ( node, indices, strides, itemSize ) {
  1282. return parse_Data_ByPolygonVertex_Direct( node, indices, strides, itemSize );
  1283. };
  1284. UV.prototype.parseUV_ByPolygonVertex_IndexToDirect = function ( node, indices ) {
  1285. return parse_Data_ByPolygonVertex_IndexToDirect( node, indices, 2 );
  1286. };
  1287. UV.prototype.parseUV_ByPolygon_Direct = function ( node ) {
  1288. console.warn( "not implemented" );
  1289. return node;
  1290. };
  1291. UV.prototype.parseUV_ByPolygon_IndexToDirect = function ( node ) {
  1292. console.warn( "not implemented" );
  1293. return node;
  1294. };
  1295. UV.prototype.parseUV_ByVertex_Direct = function ( node ) {
  1296. console.warn( "not implemented" );
  1297. return node;
  1298. };
  1299. function Normal() {
  1300. this.normal = null;
  1301. this.map = null;
  1302. this.ref = null;
  1303. this.node = null;
  1304. this.index = null;
  1305. }
  1306. Normal.prototype.getNormal = function ( node ) {
  1307. if ( this.node && this.normal && this.map && this.ref ) {
  1308. return this.normal;
  1309. } else {
  1310. this._parseText( node );
  1311. return this.normal;
  1312. }
  1313. };
  1314. // mappingType: possible variant
  1315. // ByPolygon
  1316. // ByPolygonVertex
  1317. // ByVertex (or also ByVertice, as the Blender exporter writes)
  1318. // ByEdge
  1319. // AllSame
  1320. // var mappingType = node.properties.MappingInformationType;
  1321. Normal.prototype.getMap = function ( node ) {
  1322. if ( this.node && this.normal && this.map && this.ref ) {
  1323. return this.map;
  1324. } else {
  1325. this._parseText( node );
  1326. return this.map;
  1327. }
  1328. };
  1329. // refType: possible variants
  1330. // Direct
  1331. // IndexToDirect (or Index for older versions)
  1332. // var refType = node.properties.ReferenceInformationType;
  1333. Normal.prototype.getRef = function ( node ) {
  1334. if ( this.node && this.normal && this.map && this.ref ) {
  1335. return this.ref;
  1336. } else {
  1337. this._parseText( node );
  1338. return this.ref;
  1339. }
  1340. };
  1341. Normal.prototype.getNode = function ( node ) {
  1342. if ( this.node ) {
  1343. return this.node;
  1344. }
  1345. this.node = node.subNodes.LayerElementNormal;
  1346. return this.node;
  1347. };
  1348. Normal.prototype._parseText = function ( node ) {
  1349. var normalNode = this.getNode( node )[ 0 ];
  1350. if ( normalNode === undefined ) {
  1351. console.warn( 'node: ' + node.attrName + "(" + node.id + ") does not have LayerElementNormal" );
  1352. return;
  1353. }
  1354. var mappingType = normalNode.properties.MappingInformationType;
  1355. var refType = normalNode.properties.ReferenceInformationType;
  1356. var rawTextNormals = normalNode.subNodes.Normals.properties.a;
  1357. this.normal = parseArrayToFloat( rawTextNormals );
  1358. // TODO: normalize notation shaking, vertex / vertice... blender...
  1359. this.map = mappingType;
  1360. this.ref = refType;
  1361. };
  1362. Normal.prototype.parse = function ( topnode, geo ) {
  1363. var normals = this.getNormal( topnode );
  1364. //var normalNode = this.getNode( topnode );
  1365. var mappingType = this.getMap( topnode );
  1366. var refType = this.getRef( topnode );
  1367. var indices = geo.getPolygonVertexIndices( topnode );
  1368. var strides = geo.getPolygonTopologyArray( topnode );
  1369. // it means that there is a normal for every vertex of every polygon of the model.
  1370. // For example, if the models has 8 vertices that make up four quads, then there
  1371. // will be 16 normals (one normal * 4 polygons * 4 vertices of the polygon). Note
  1372. // that generally a game engine needs the vertices to have only one normal defined.
  1373. // So, if you find a vertex has more tha one normal, you can either ignore the normals
  1374. // you find after the first, or calculate the mean from all of them (normal smoothing).
  1375. //if ( mappingType == "ByPolygonVertex" ){
  1376. switch ( mappingType ) {
  1377. case "ByPolygonVertex":
  1378. switch ( refType ) {
  1379. // Direct
  1380. // The normals are in order.
  1381. case "Direct":
  1382. normals = this.parseNormal_ByPolygonVertex_Direct( normals, indices, strides, 3 );
  1383. break;
  1384. // IndexToDirect
  1385. // The order of the normals is given by the NormalsIndex property.
  1386. case "IndexToDirect":
  1387. normals = this.parseNormal_ByPolygonVertex_IndexToDirect();
  1388. break;
  1389. }
  1390. break;
  1391. case "ByPolygon":
  1392. switch ( refType ) {
  1393. // Direct
  1394. // The normals are in order.
  1395. case "Direct":
  1396. normals = this.parseNormal_ByPolygon_Direct();
  1397. break;
  1398. // IndexToDirect
  1399. // The order of the normals is given by the NormalsIndex property.
  1400. case "IndexToDirect":
  1401. normals = this.parseNormal_ByPolygon_IndexToDirect();
  1402. break;
  1403. }
  1404. break;
  1405. }
  1406. return normals;
  1407. };
  1408. Normal.prototype.parseNormal_ByPolygonVertex_Direct = function ( node, indices, strides, itemSize ) {
  1409. return parse_Data_ByPolygonVertex_Direct( node, indices, strides, itemSize );
  1410. };
  1411. Normal.prototype.parseNormal_ByPolygonVertex_IndexToDirect = function ( node ) {
  1412. console.warn( "not implemented" );
  1413. return node;
  1414. };
  1415. Normal.prototype.parseNormal_ByPolygon_Direct = function ( node ) {
  1416. console.warn( "not implemented" );
  1417. return node;
  1418. };
  1419. Normal.prototype.parseNormal_ByPolygon_IndexToDirect = function ( node ) {
  1420. console.warn( "not implemented" );
  1421. return node;
  1422. };
  1423. Normal.prototype.parseNormal_ByVertex_Direct = function ( node ) {
  1424. console.warn( "not implemented" );
  1425. return node;
  1426. };
  1427. function MaterialIndex() {
  1428. this.indexBuffer = [];
  1429. }
  1430. Object.assign( MaterialIndex.prototype, {
  1431. parse: function ( node ) {
  1432. if ( ! ( 'LayerElementMaterial' in node.subNodes ) ) {
  1433. return;
  1434. }
  1435. var indexNode = node.subNodes.LayerElementMaterial[ 0 ];
  1436. var mappingType = indexNode.properties.MappingInformationType;
  1437. var refType = indexNode.properties.ReferenceInformationType;
  1438. var indices = parseArrayToInt( indexNode.subNodes.Materials.properties.a );
  1439. // it means that there is a normal for every vertex of every polygon of the model.
  1440. // For example, if the models has 8 vertices that make up four quads, then there
  1441. // will be 16 normals (one normal * 4 polygons * 4 vertices of the polygon). Note
  1442. // that generally a game engine needs the vertices to have only one normal defined.
  1443. // So, if you find a vertex has more tha one normal, you can either ignore the normals
  1444. // you find after the first, or calculate the mean from all of them (normal smoothing).
  1445. //if ( mappingType == "ByPolygonVertex" ){
  1446. switch ( mappingType ) {
  1447. case "ByPolygon":
  1448. switch ( refType ) {
  1449. // Direct
  1450. // The material indices are in order.
  1451. case "IndexToDirect":
  1452. this.indexBuffer = this.parse_ByPolygon_IndexToDirect( indices );
  1453. break;
  1454. default:
  1455. this.indexBuffer = [ 0 ];
  1456. break;
  1457. }
  1458. break;
  1459. default:
  1460. this.indexBuffer = [ 0 ];
  1461. break;
  1462. }
  1463. return this.indexBuffer;
  1464. },
  1465. parse_ByPolygon_IndexToDirect: function ( indices ) {
  1466. return indices;
  1467. },
  1468. } );
  1469. function AnimationCurve() {
  1470. this.version = null;
  1471. this.id = null;
  1472. this.internalId = null;
  1473. this.times = null;
  1474. this.values = null;
  1475. this.attrFlag = null; // tangeant
  1476. this.attrData = null; // slope, weight
  1477. }
  1478. AnimationCurve.prototype.fromNode = function ( curveNode ) {
  1479. this.id = curveNode.id;
  1480. this.internalId = curveNode.id;
  1481. this.times = curveNode.subNodes.KeyTime.properties.a;
  1482. this.values = curveNode.subNodes.KeyValueFloat.properties.a;
  1483. this.attrFlag = curveNode.subNodes.KeyAttrFlags.properties.a;
  1484. this.attrData = curveNode.subNodes.KeyAttrDataFloat.properties.a;
  1485. this.times = parseArrayToFloat( this.times );
  1486. this.values = parseArrayToFloat( this.values );
  1487. this.attrData = parseArrayToFloat( this.attrData );
  1488. this.attrFlag = parseArrayToInt( this.attrFlag );
  1489. this.times = this.times.map( function ( element ) {
  1490. return FBXTimeToSeconds( element );
  1491. } );
  1492. return this;
  1493. };
  1494. AnimationCurve.prototype.getLength = function () {
  1495. return this.times[ this.times.length - 1 ];
  1496. };
  1497. function AnimationNode() {
  1498. this.id = null;
  1499. this.attr = null; // S, R, T
  1500. this.attrX = false;
  1501. this.attrY = false;
  1502. this.attrZ = false;
  1503. this.internalId = null;
  1504. this.containerInternalId = null; // bone, null etc Id
  1505. this.containerBoneId = null; // bone, null etc Id
  1506. this.curveIdx = null; // AnimationCurve's indices
  1507. this.curves = {}; // AnimationCurve refs
  1508. }
  1509. AnimationNode.prototype.fromNode = function ( allNodes, node, bones ) {
  1510. this.id = node.id;
  1511. this.attr = node.attrName;
  1512. this.internalId = node.id;
  1513. if ( this.attr.match( /S|R|T/ ) ) {
  1514. for ( var attrKey in node.properties ) {
  1515. if ( attrKey.match( /X/ ) ) {
  1516. this.attrX = true;
  1517. }
  1518. if ( attrKey.match( /Y/ ) ) {
  1519. this.attrY = true;
  1520. }
  1521. if ( attrKey.match( /Z/ ) ) {
  1522. this.attrZ = true;
  1523. }
  1524. }
  1525. } else {
  1526. // may be deform percent nodes
  1527. return null;
  1528. }
  1529. this.containerIndices = allNodes.searchConnectionParent( this.id );
  1530. this.curveIdx = allNodes.searchConnectionChildren( this.id );
  1531. for ( var i = this.containerIndices.length - 1; i >= 0; -- i ) {
  1532. var boneId = bones.searchRealId( this.containerIndices[ i ] );
  1533. if ( boneId >= 0 ) {
  1534. this.containerBoneId = boneId;
  1535. this.containerId = this.containerIndices[ i ];
  1536. }
  1537. if ( boneId >= 0 ) {
  1538. break;
  1539. }
  1540. }
  1541. // this.containerBoneId = bones.searchRealId( this.containerIndices );
  1542. return this;
  1543. };
  1544. AnimationNode.prototype.setCurve = function ( curve ) {
  1545. this.curves.push( curve );
  1546. };
  1547. function Animation() {
  1548. this.curves = {};
  1549. this.length = 0.0;
  1550. this.fps = 30.0;
  1551. this.frames = 0.0;
  1552. }
  1553. Animation.prototype.parse = function ( node, bones ) {
  1554. var rawNodes = node.Objects.subNodes.AnimationCurveNode;
  1555. var rawCurves = node.Objects.subNodes.AnimationCurve;
  1556. var rawLayers = node.Objects.subNodes.AnimationLayer;
  1557. var rawStacks = node.Objects.subNodes.AnimationStack;
  1558. // first: expand AnimationCurveNode into curve nodes
  1559. var curveNodes = [];
  1560. for ( var key in rawNodes ) {
  1561. if ( key.match( /\d+/ ) ) {
  1562. var a = ( new AnimationNode() ).fromNode( node, rawNodes[ key ], bones );
  1563. curveNodes.push( a );
  1564. }
  1565. }
  1566. // second: gen dict, mapped by internalId
  1567. var tmp = {};
  1568. for ( var i = 0; i < curveNodes.length; ++ i ) {
  1569. if ( curveNodes[ i ] === null ) {
  1570. continue;
  1571. }
  1572. tmp[ curveNodes[ i ].id ] = curveNodes[ i ];
  1573. }
  1574. // third: insert curves into the dict
  1575. var ac = [];
  1576. for ( key in rawCurves ) {
  1577. if ( key.match( /\d+/ ) ) {
  1578. var c = ( new AnimationCurve() ).fromNode( rawCurves[ key ] );
  1579. ac.push( c );
  1580. var parentId = node.searchConnectionParent( c.id )[ 0 ];
  1581. var axis = node.searchConnectionType( c.id, parentId );
  1582. if ( axis.match( /X/ ) ) {
  1583. axis = 'x';
  1584. } else if ( axis.match( /Y/ ) ) {
  1585. axis = 'y';
  1586. } else if ( axis.match( /Z/ ) ) {
  1587. axis = 'z';
  1588. } else {
  1589. continue;
  1590. }
  1591. tmp[ parentId ].curves[ axis ] = c;
  1592. }
  1593. }
  1594. // forth:
  1595. for ( var t in tmp ) {
  1596. var id = tmp[ t ].containerBoneId;
  1597. if ( this.curves[ id ] === undefined ) {
  1598. this.curves[ id ] = {
  1599. T: null,
  1600. R: null,
  1601. S: null
  1602. };
  1603. }
  1604. this.curves[ id ][ tmp[ t ].attr ] = tmp[ t ];
  1605. }
  1606. //Layers
  1607. this.layers = {};
  1608. for ( var key in rawLayers ) {
  1609. var layer = [];
  1610. var children = node.searchConnectionChildren( key );
  1611. for ( var i = 0; i < children.length; ++ i ) {
  1612. //Skip lockInfluenceWeights
  1613. if ( tmp[ children[ i ] ] ) {
  1614. if ( layer[ tmp[ children[ i ] ].containerBoneId ] === undefined ) {
  1615. layer[ tmp[ children[ i ] ].containerBoneId ] = {
  1616. T: null,
  1617. R: null,
  1618. S: null
  1619. };
  1620. }
  1621. layer[ tmp[ children[ i ] ].containerBoneId ][ tmp[ children[ i ] ].attr ] = tmp[ children[ i ] ];
  1622. }
  1623. }
  1624. this.layers[ key ] = layer;
  1625. }
  1626. //Takes
  1627. this.stacks = {};
  1628. for ( var key in rawStacks ) {
  1629. var layers = [];
  1630. var children = node.searchConnectionChildren( key );
  1631. var max = 0.0;
  1632. var min = Number.MAX_VALUE;
  1633. for ( var i = 0; i < children.length; ++ i ) {
  1634. if ( children[ i ] in this.layers ) {
  1635. layers.push( this.layers[ children[ i ] ] );
  1636. for ( var j = 0; j < this.layers[ children[ i ] ].length; ++ j ) {
  1637. function getMaxMin( layer ) {
  1638. function _getMaxMin( curves ) {
  1639. if ( curves.x ) {
  1640. max = curves.x.getLength() > max ? curves.x.getLength() : max;
  1641. min = curves.x.times[ 0 ] < min ? curves.x.times[ 0 ] : min;
  1642. }
  1643. if ( curves.y ) {
  1644. max = curves.y.getLength() > max ? curves.y.getLength() : max;
  1645. min = curves.y.times[ 0 ] < min ? curves.y.times[ 0 ] : min;
  1646. }
  1647. if ( curves.z ) {
  1648. max = curves.z.getLength() > max ? curves.z.getLength() : max;
  1649. min = curves.z.times[ 0 ] < min ? curves.z.times[ 0 ] : min;
  1650. }
  1651. }
  1652. if ( layer.R ) {
  1653. _getMaxMin( layer.R.curves );
  1654. }
  1655. if ( layer.S ) {
  1656. _getMaxMin( layer.S.curves );
  1657. }
  1658. if ( layer.T ) {
  1659. _getMaxMin( layer.T.curves );
  1660. }
  1661. }
  1662. var layer = this.layers[ children[ i ] ][ j ];
  1663. if ( layer ) {
  1664. getMaxMin( layer );
  1665. }
  1666. }
  1667. }
  1668. }
  1669. //Do we have an animation clip with an actual length?
  1670. if ( max > min ) {
  1671. this.stacks[ key ] = {
  1672. name: rawStacks[ key ].attrName,
  1673. layers: layers,
  1674. length: max - min,
  1675. frames: ( max - min ) * 30,
  1676. };
  1677. }
  1678. }
  1679. return this;
  1680. };
  1681. function Textures() {
  1682. this.textures = {};
  1683. //this.perGeoMap = {};
  1684. }
  1685. Textures.prototype.add = function ( tex ) {
  1686. debugger;
  1687. if ( this.textures === undefined ) {
  1688. this.textures = [];
  1689. }
  1690. this.textures.push( tex );
  1691. for ( var i = 0; i < tex.parentIds.length; ++ i ) {
  1692. if ( this.perGeoMap[ tex.parentIds[ i ] ] === undefined ) {
  1693. this.perGeoMap[ tex.parentIds[ i ] ] = [];
  1694. }
  1695. this.perGeoMap[ tex.parentIds[ i ] ].push( this.textures[ this.textures.length - 1 ] );
  1696. }
  1697. };
  1698. Textures.prototype.parse = function ( node ) {
  1699. var rawNodes = node.Objects.subNodes.Texture;
  1700. for ( var n in rawNodes ) {
  1701. var tex = ( new Texture() ).parse( rawNodes[ n ], node );
  1702. this.textures[ n ] = tex;
  1703. }
  1704. return this;
  1705. };
  1706. Textures.prototype.getById = function ( id ) {
  1707. return this.perGeoMap[ id ];
  1708. };
  1709. function Texture() {
  1710. this.fileName = "";
  1711. this.name = "";
  1712. this.id = null;
  1713. this.parentIds = [];
  1714. }
  1715. Texture.prototype.parse = function ( node, nodes ) {
  1716. this.id = node.id;
  1717. this.name = node.attrName;
  1718. this.fileName = this.parseFileName( node.properties.FileName );
  1719. this.parentIds = this.searchParents( this.id, nodes );
  1720. return this;
  1721. };
  1722. // TODO: support directory
  1723. Texture.prototype.parseFileName = function ( fname ) {
  1724. if ( fname === undefined ) {
  1725. return "";
  1726. }
  1727. // ignore directory structure, flatten path
  1728. var splitted = fname.split( /[\\\/]/ );
  1729. if ( splitted.length > 0 ) {
  1730. return splitted[ splitted.length - 1 ];
  1731. } else {
  1732. return fname;
  1733. }
  1734. };
  1735. Texture.prototype.searchParents = function ( id, nodes ) {
  1736. var p = nodes.searchConnectionParent( id );
  1737. return p;
  1738. };
  1739. function Materials() {
  1740. this.materials = {};
  1741. this.perGeoMap = {};
  1742. }
  1743. Object.assign( Materials.prototype, {
  1744. parse: function ( node ) {
  1745. var rawNodes = node.Objects.subNodes.Material;
  1746. for ( var n in rawNodes ) {
  1747. var mat = ( new Material() ).parse( rawNodes[ n ], node );
  1748. this.materials[ n ] = mat;
  1749. }
  1750. return this;
  1751. }
  1752. } );
  1753. function Material() {
  1754. this.fileName = "";
  1755. this.name = "";
  1756. this.id = null;
  1757. this.parentIds = [];
  1758. }
  1759. Object.assign( Material.prototype, {
  1760. parse: function ( node, nodes ) {
  1761. this.id = node.id;
  1762. this.name = node.attrName;
  1763. this.type = node.properties.ShadingModel;
  1764. this.parameters = this.parseParameters( node.properties );
  1765. this.parentIds = this.searchParents( this.id, nodes );
  1766. return this;
  1767. },
  1768. parseParameters: function ( properties ) {
  1769. var parameters = {};
  1770. //TODO: Missing parameters:
  1771. // - Ambient
  1772. // - MultiLayer
  1773. // - ShininessExponent (Same vals as Shininess)
  1774. // - Specular (Same vals as SpecularColor)
  1775. // - TransparencyFactor (Maybe same as Opacity?).
  1776. if ( properties.Diffuse ) {
  1777. parameters.color = new THREE.Color().fromArray( [ parseFloat( properties.Diffuse.value.x ), parseFloat( properties.Diffuse.value.y ), parseFloat( properties.Diffuse.value.z ) ] );
  1778. }
  1779. if ( properties.Specular ) {
  1780. parameters.specular = new THREE.Color().fromArray( [ parseFloat( properties.Specular.value.x ), parseFloat( properties.Specular.value.y ), parseFloat( properties.Specular.value.z ) ] );
  1781. }
  1782. if ( properties.Shininess ) {
  1783. parameters.shininess = properties.Shininess.value;
  1784. }
  1785. if ( properties.Emissive ) {
  1786. parameters.emissive = new THREE.Color().fromArray( [ parseFloat( properties.Emissive.value.x ), parseFloat( properties.Emissive.value.y ), parseFloat( properties.Emissive.value.z ) ] );
  1787. }
  1788. if ( properties.EmissiveFactor ) {
  1789. parameters.emissiveIntensity = properties.EmissiveFactor.value;
  1790. }
  1791. if ( properties.Reflectivity ) {
  1792. parameters.reflectivity = properties.Reflectivity.value;
  1793. }
  1794. if ( properties.Opacity ) {
  1795. parameters.opacity = properties.Opacity.value;
  1796. }
  1797. if ( parameters.opacity < 1.0 ) {
  1798. parameters.transparent = true;
  1799. }
  1800. //Assigning textures
  1801. return parameters;
  1802. },
  1803. searchParents: function ( id, nodes ) {
  1804. return nodes.searchConnectionParent( id );
  1805. }
  1806. } );
  1807. /* --------------------------------------------------------------------- */
  1808. /* --------------------------------------------------------------------- */
  1809. /* --------------------------------------------------------------------- */
  1810. /* --------------------------------------------------------------------- */
  1811. // LayerElementUV: 0 {
  1812. // Version: 101
  1813. // Name: "Texture_Projection"
  1814. // MappingInformationType: "ByPolygonVertex"
  1815. // ReferenceInformationType: "IndexToDirect"
  1816. // UV: *1746 {
  1817. // UVIndex: *7068 {
  1818. //
  1819. // The order of the uvs is given by the UVIndex property.
  1820. function parse_Data_ByPolygonVertex_IndexToDirect( node, indices, itemSize ) {
  1821. var res = [];
  1822. for ( var i = 0; i < indices.length; ++ i ) {
  1823. for ( var j = 0; j < itemSize; ++ j ) {
  1824. res.push( node[ ( indices[ i ] * itemSize ) + j ] );
  1825. }
  1826. }
  1827. return res;
  1828. }
  1829. // what want: normal per vertex, order vertice
  1830. // i have: normal per polygon
  1831. // i have: indice per polygon
  1832. function parse_Data_ByPolygonVertex_Direct( node, indices, strides, itemSize ) {
  1833. // *21204 > 3573
  1834. // Geometry: 690680816, "Geometry::", "Mesh" {
  1835. // Vertices: *3573 {
  1836. // PolygonVertexIndex: *7068 {
  1837. var tmp = [];
  1838. var currentIndex = 0;
  1839. // first: sort to per vertex
  1840. for ( var i = 0; i < indices.length; ++ i ) {
  1841. tmp[ indices[ i ] ] = [];
  1842. // TODO: duped entry? blend or something?
  1843. for ( var s = 0; s < itemSize; ++ s ) {
  1844. tmp[ indices[ i ] ][ s ] = node[ currentIndex + s ];
  1845. }
  1846. currentIndex += itemSize;
  1847. }
  1848. // second: expand x,y,z into serial array
  1849. var res = [];
  1850. for ( var jj = 0; jj < tmp.length; ++ jj ) {
  1851. if ( tmp[ jj ] === undefined ) {
  1852. continue;
  1853. }
  1854. for ( var t = 0; t < itemSize; ++ t ) {
  1855. if ( tmp[ jj ][ t ] === undefined ) {
  1856. continue;
  1857. }
  1858. res.push( tmp[ jj ][ t ] );
  1859. }
  1860. }
  1861. return res;
  1862. }
  1863. // convert from by polygon(vert) data into by verts data
  1864. function mapByPolygonVertexToByVertex( data, indices, stride ) {
  1865. var tmp = {};
  1866. var res = [];
  1867. var max = 0;
  1868. for ( var i = 0; i < indices.length; ++ i ) {
  1869. if ( indices[ i ] in tmp ) {
  1870. continue;
  1871. }
  1872. tmp[ indices[ i ] ] = {};
  1873. for ( var j = 0; j < stride; ++ j ) {
  1874. tmp[ indices[ i ] ][ j ] = data[ i * stride + j ];
  1875. }
  1876. max = max < indices[ i ] ? indices[ i ] : max;
  1877. }
  1878. try {
  1879. for ( i = 0; i <= max; i ++ ) {
  1880. for ( var s = 0; s < stride; s ++ ) {
  1881. res.push( tmp[ i ][ s ] );
  1882. }
  1883. }
  1884. } catch ( e ) {
  1885. //console.log( max );
  1886. //console.log( tmp );
  1887. //console.log( i );
  1888. //console.log( e );
  1889. }
  1890. return res;
  1891. }
  1892. // AUTODESK uses broken clock. i guess
  1893. function FBXTimeToSeconds( adskTime ) {
  1894. return adskTime / 46186158000;
  1895. }
  1896. function degToRad( degrees ) {
  1897. return degrees * Math.PI / 180;
  1898. }
  1899. function quatFromVec( x, y, z ) {
  1900. var euler = new THREE.Euler( x, y, z, 'ZYX' );
  1901. var quat = new THREE.Quaternion();
  1902. quat.setFromEuler( euler );
  1903. return quat;
  1904. }
  1905. function parseArrayToInt( string ) {
  1906. return string.split( ',' ).map( function ( element ) {
  1907. return parseInt( element );
  1908. } );
  1909. }
  1910. function parseArrayToFloat( string ) {
  1911. return string.split( ',' ).map( function ( element ) {
  1912. return parseFloat( element );
  1913. } );
  1914. }
  1915. function parseArrayToRadians( string ) {
  1916. return string.split( ',' ).map( function ( element ) {
  1917. return degToRad( parseFloat( element ) );
  1918. } );
  1919. }
  1920. function parseArrayToMatrix( string ) {
  1921. var arr = parseArrayToFloat( string );
  1922. return new THREE.Matrix4().fromArray( arr );
  1923. }
  1924. } )();