FBXLoader.js 69 KB

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