FBXLoader.js 61 KB

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