ColladaLoader.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032
  1. /**
  2. * @author Tim Knip / http://www.floorplanner.com/ / tim at floorplanner.com
  3. */
  4. THREE.ColladaLoader = function () {
  5. var COLLADA = null;
  6. var scene = null;
  7. var daeScene;
  8. var readyCallbackFunc = null;
  9. var sources = {};
  10. var images = {};
  11. var animations = {};
  12. var controllers = {};
  13. var geometries = {};
  14. var materials = {};
  15. var effects = {};
  16. var animData;
  17. var visualScenes;
  18. var baseUrl;
  19. var morphs;
  20. var skins;
  21. var flip_uv = true;
  22. var preferredShading = THREE.SmoothShading;
  23. var options = {
  24. // Force Geometry to always be centered at the local origin of the
  25. // containing Mesh.
  26. centerGeometry: false,
  27. // Axis conversion is done for geometries, animations, and controllers.
  28. // If we ever pull cameras or lights out of the COLLADA file, they'll
  29. // need extra work.
  30. convertUpAxis: false,
  31. subdivideFaces: true,
  32. upAxis: 'Y'
  33. };
  34. // TODO: support unit conversion as well
  35. var colladaUnit = 1.0;
  36. var colladaUp = 'Y';
  37. var upConversion = null;
  38. var TO_RADIANS = Math.PI / 180;
  39. function load ( url, readyCallback, progressCallback ) {
  40. var length = 0;
  41. if ( document.implementation && document.implementation.createDocument ) {
  42. var req = new XMLHttpRequest();
  43. if ( req.overrideMimeType ) req.overrideMimeType( "text/xml" );
  44. req.onreadystatechange = function() {
  45. if( req.readyState == 4 ) {
  46. if( req.status == 0 || req.status == 200 ) {
  47. if ( req.responseXML ) {
  48. readyCallbackFunc = readyCallback;
  49. parse( req.responseXML, undefined, url );
  50. } else {
  51. console.error( "ColladaLoader: Empty or non-existing file (" + url + ")" );
  52. }
  53. }
  54. } else if ( req.readyState == 3 ) {
  55. if ( progressCallback ) {
  56. if ( length == 0 ) {
  57. length = req.getResponseHeader( "Content-Length" );
  58. }
  59. progressCallback( { total: length, loaded: req.responseText.length } );
  60. }
  61. }
  62. }
  63. req.open( "GET", url, true );
  64. req.send( null );
  65. } else {
  66. alert( "Don't know how to parse XML!" );
  67. }
  68. };
  69. function parse( doc, callBack, url ) {
  70. COLLADA = doc;
  71. callBack = callBack || readyCallbackFunc;
  72. if ( url !== undefined ) {
  73. var parts = url.split( '/' );
  74. parts.pop();
  75. baseUrl = parts.length < 1 ? '' : parts.join( '/' ) + '/';
  76. }
  77. parseAsset();
  78. setUpConversion();
  79. images = parseLib( "//dae:library_images/dae:image", _Image, "image" );
  80. materials = parseLib( "//dae:library_materials/dae:material", Material, "material") ;
  81. effects = parseLib( "//dae:library_effects/dae:effect", Effect, "effect" );
  82. geometries = parseLib( "//dae:library_geometries/dae:geometry", Geometry, "geometry" );
  83. controllers = parseLib( "//dae:library_controllers/dae:controller", Controller, "controller" );
  84. animations = parseLib( "//dae:library_animations/dae:animation", Animation, "animation" );
  85. visualScenes = parseLib( ".//dae:library_visual_scenes/dae:visual_scene", VisualScene, "visual_scene" );
  86. morphs = [];
  87. skins = [];
  88. daeScene = parseScene();
  89. scene = new THREE.Object3D();
  90. for ( var i = 0; i < daeScene.nodes.length; i ++ ) {
  91. scene.add( createSceneGraph( daeScene.nodes[ i ] ) );
  92. }
  93. createAnimations();
  94. var result = {
  95. scene: scene,
  96. morphs: morphs,
  97. skins: skins,
  98. animations: animData,
  99. dae: {
  100. images: images,
  101. materials: materials,
  102. effects: effects,
  103. geometries: geometries,
  104. controllers: controllers,
  105. animations: animations,
  106. visualScenes: visualScenes,
  107. scene: daeScene
  108. }
  109. };
  110. if ( callBack ) {
  111. callBack( result );
  112. }
  113. return result;
  114. };
  115. function setPreferredShading ( shading ) {
  116. preferredShading = shading;
  117. };
  118. function parseAsset () {
  119. var elements = COLLADA.evaluate( '//dae:asset', COLLADA, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
  120. var element = elements.iterateNext();
  121. if ( element && element.childNodes ) {
  122. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  123. var child = element.childNodes[ i ];
  124. switch ( child.nodeName ) {
  125. case 'unit':
  126. var meter = child.getAttribute( 'meter' );
  127. if ( meter ) {
  128. colladaUnit = parseFloat( meter );
  129. }
  130. break;
  131. case 'up_axis':
  132. colladaUp = child.textContent.charAt(0);
  133. break;
  134. }
  135. }
  136. }
  137. };
  138. function parseLib ( q, classSpec, prefix ) {
  139. var elements = COLLADA.evaluate(q, COLLADA, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null) ;
  140. var lib = {};
  141. var element = elements.iterateNext();
  142. var i = 0;
  143. while ( element ) {
  144. var daeElement = ( new classSpec() ).parse( element );
  145. if ( !daeElement.id || daeElement.id.length == 0 ) daeElement.id = prefix + ( i ++ );
  146. lib[ daeElement.id ] = daeElement;
  147. element = elements.iterateNext();
  148. }
  149. return lib;
  150. };
  151. function parseScene () {
  152. var sceneElement = COLLADA.evaluate( './/dae:scene/dae:instance_visual_scene', COLLADA, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ).iterateNext();
  153. if ( sceneElement ) {
  154. var url = sceneElement.getAttribute( 'url' ).replace( /^#/, '' );
  155. return visualScenes[ url ];
  156. } else {
  157. return null;
  158. }
  159. };
  160. function createAnimations() {
  161. animData = [];
  162. // fill in the keys
  163. recurseHierarchy( scene );
  164. };
  165. function recurseHierarchy( node ) {
  166. var n = daeScene.getChildById( node.name, true ),
  167. newData = null;
  168. if ( n && n.keys ) {
  169. newData = {
  170. fps: 60,
  171. hierarchy: [ {
  172. node: n,
  173. keys: n.keys,
  174. sids: n.sids
  175. } ],
  176. node: node,
  177. name: 'animation_' + node.name,
  178. length: 0
  179. };
  180. animData.push(newData);
  181. for ( var i = 0, il = n.keys.length; i < il; i++ ) {
  182. newData.length = Math.max( newData.length, n.keys[i].time );
  183. }
  184. } else {
  185. newData = {
  186. hierarchy: [ {
  187. keys: [],
  188. sids: []
  189. } ]
  190. }
  191. }
  192. for ( var i = 0, il = node.children.length; i < il; i++ ) {
  193. var d = recurseHierarchy( node.children[i] );
  194. for ( var j = 0, jl = d.hierarchy.length; j < jl; j ++ ) {
  195. newData.hierarchy.push( {
  196. keys: [],
  197. sids: []
  198. } );
  199. }
  200. }
  201. return newData;
  202. };
  203. function calcAnimationBounds () {
  204. var start = 1000000;
  205. var end = -start;
  206. var frames = 0;
  207. for ( var id in animations ) {
  208. var animation = animations[ id ];
  209. for ( var i = 0; i < animation.sampler.length; i ++ ) {
  210. var sampler = animation.sampler[ i ];
  211. sampler.create();
  212. start = Math.min( start, sampler.startTime );
  213. end = Math.max( end, sampler.endTime );
  214. frames = Math.max( frames, sampler.input.length );
  215. }
  216. }
  217. return { start:start, end:end, frames:frames };
  218. };
  219. function createMorph ( geometry, ctrl ) {
  220. var morphCtrl = ctrl instanceof InstanceController ? controllers[ ctrl.url ] : ctrl;
  221. if ( !morphCtrl || !morphCtrl.morph ) {
  222. console.log("could not find morph controller!");
  223. return;
  224. }
  225. var morph = morphCtrl.morph;
  226. for ( var i = 0; i < morph.targets.length; i ++ ) {
  227. var target_id = morph.targets[ i ];
  228. var daeGeometry = geometries[ target_id ];
  229. if ( !daeGeometry.mesh ||
  230. !daeGeometry.mesh.primitives ||
  231. !daeGeometry.mesh.primitives.length ) {
  232. continue;
  233. }
  234. var target = daeGeometry.mesh.primitives[ 0 ].geometry;
  235. if ( target.vertices.length === geometry.vertices.length ) {
  236. geometry.morphTargets.push( { name: "target_1", vertices: target.vertices } );
  237. }
  238. }
  239. geometry.morphTargets.push( { name: "target_Z", vertices: geometry.vertices } );
  240. };
  241. function createSkin ( geometry, ctrl, applyBindShape ) {
  242. var skinCtrl = controllers[ ctrl.url ];
  243. if ( !skinCtrl || !skinCtrl.skin ) {
  244. console.log( "could not find skin controller!" );
  245. return;
  246. }
  247. if ( !ctrl.skeleton || !ctrl.skeleton.length ) {
  248. console.log( "could not find the skeleton for the skin!" );
  249. return;
  250. }
  251. var skin = skinCtrl.skin;
  252. var skeleton = daeScene.getChildById( ctrl.skeleton[ 0 ] );
  253. var hierarchy = [];
  254. applyBindShape = applyBindShape !== undefined ? applyBindShape : true;
  255. var bones = [];
  256. geometry.skinWeights = [];
  257. geometry.skinIndices = [];
  258. //createBones( geometry.bones, skin, hierarchy, skeleton, null, -1 );
  259. //createWeights( skin, geometry.bones, geometry.skinIndices, geometry.skinWeights );
  260. /*
  261. geometry.animation = {
  262. name: 'take_001',
  263. fps: 30,
  264. length: 2,
  265. JIT: true,
  266. hierarchy: hierarchy
  267. };
  268. */
  269. if ( applyBindShape ) {
  270. for ( var i = 0; i < geometry.vertices.length; i ++ ) {
  271. skin.bindShapeMatrix.multiplyVector3( geometry.vertices[ i ].position );
  272. }
  273. }
  274. };
  275. function setupSkeleton ( node, bones, frame, parent ) {
  276. node.world = node.world || new THREE.Matrix4();
  277. node.world.copy( node.matrix );
  278. if ( node.channels && node.channels.length ) {
  279. var channel = node.channels[ 0 ];
  280. var m = channel.sampler.output[ frame ];
  281. if ( m instanceof THREE.Matrix4 ) {
  282. node.world.copy( m );
  283. }
  284. }
  285. if ( parent ) {
  286. node.world.multiply( parent, node.world );
  287. }
  288. bones.push( node );
  289. for ( var i = 0; i < node.nodes.length; i ++ ) {
  290. setupSkeleton( node.nodes[ i ], bones, frame, node.world );
  291. }
  292. };
  293. function setupSkinningMatrices ( bones, skin ) {
  294. // FIXME: this is dumb...
  295. for ( var i = 0; i < bones.length; i ++ ) {
  296. var bone = bones[ i ];
  297. var found = -1;
  298. if ( bone.type != 'JOINT' ) continue;
  299. for ( var j = 0; j < skin.joints.length; j ++ ) {
  300. if ( bone.sid == skin.joints[ j ] ) {
  301. found = j;
  302. break;
  303. }
  304. }
  305. if ( found >= 0 ) {
  306. var inv = skin.invBindMatrices[ found ];
  307. bone.invBindMatrix = inv;
  308. bone.skinningMatrix = new THREE.Matrix4();
  309. bone.skinningMatrix.multiply(bone.world, inv); // (IBMi * JMi)
  310. bone.weights = [];
  311. for ( var j = 0; j < skin.weights.length; j ++ ) {
  312. for (var k = 0; k < skin.weights[ j ].length; k ++) {
  313. var w = skin.weights[ j ][ k ];
  314. if ( w.joint == found ) {
  315. bone.weights.push( w );
  316. }
  317. }
  318. }
  319. } else {
  320. throw 'ColladaLoader: Could not find joint \'' + bone.sid + '\'.';
  321. }
  322. }
  323. };
  324. function applySkin ( geometry, instanceCtrl, frame ) {
  325. var skinController = controllers[ instanceCtrl.url ];
  326. frame = frame !== undefined ? frame : 40;
  327. if ( !skinController || !skinController.skin ) {
  328. console.log( 'ColladaLoader: Could not find skin controller.' );
  329. return;
  330. }
  331. if ( !instanceCtrl.skeleton || !instanceCtrl.skeleton.length ) {
  332. console.log( 'ColladaLoader: Could not find the skeleton for the skin. ' );
  333. return;
  334. }
  335. var animationBounds = calcAnimationBounds();
  336. var skeleton = daeScene.getChildById( instanceCtrl.skeleton[0], true ) ||
  337. daeScene.getChildBySid( instanceCtrl.skeleton[0], true );
  338. var i, j, w, vidx, weight;
  339. var v = new THREE.Vector3(), o, s;
  340. // move vertices to bind shape
  341. for ( i = 0; i < geometry.vertices.length; i ++ ) {
  342. skinController.skin.bindShapeMatrix.multiplyVector3( geometry.vertices[i].position );
  343. }
  344. // process animation, or simply pose the rig if no animation
  345. for ( frame = 0; frame < animationBounds.frames; frame ++ ) {
  346. var bones = [];
  347. var skinned = [];
  348. // zero skinned vertices
  349. for ( i = 0; i < geometry.vertices.length; i++ ) {
  350. skinned.push( new THREE.Vertex( new THREE.Vector3() ) );
  351. }
  352. // process the frame and setup the rig with a fresh
  353. // transform, possibly from the bone's animation channel(s)
  354. setupSkeleton( skeleton, bones, frame );
  355. setupSkinningMatrices( bones, skinController.skin );
  356. // skin 'm
  357. for ( i = 0; i < bones.length; i ++ ) {
  358. if ( bones[ i ].type != 'JOINT' ) continue;
  359. for ( j = 0; j < bones[ i ].weights.length; j ++ ) {
  360. w = bones[ i ].weights[ j ];
  361. vidx = w.index;
  362. weight = w.weight;
  363. o = geometry.vertices[vidx];
  364. s = skinned[vidx];
  365. v.x = o.position.x;
  366. v.y = o.position.y;
  367. v.z = o.position.z;
  368. bones[i].skinningMatrix.multiplyVector3(v);
  369. s.position.x += (v.x * weight);
  370. s.position.y += (v.y * weight);
  371. s.position.z += (v.z * weight);
  372. }
  373. }
  374. geometry.morphTargets.push( { name: "target_" + frame, vertices: skinned } );
  375. }
  376. };
  377. function createSceneGraph ( node, parent ) {
  378. var obj = new THREE.Object3D();
  379. var skinned = false;
  380. var skinController;
  381. var morphController;
  382. var i, j;
  383. // FIXME: controllers
  384. for ( i = 0; i < node.controllers.length; i ++ ) {
  385. var controller = controllers[ node.controllers[ i ].url ];
  386. switch ( controller.type ) {
  387. case 'skin':
  388. if ( geometries[ controller.skin.source ] ) {
  389. var inst_geom = new InstanceGeometry();
  390. inst_geom.url = controller.skin.source;
  391. inst_geom.instance_material = node.controllers[ i ].instance_material;
  392. node.geometries.push( inst_geom );
  393. skinned = true;
  394. skinController = node.controllers[ i ];
  395. } else if ( controllers[ controller.skin.source ] ) {
  396. // urgh: controller can be chained
  397. // handle the most basic case...
  398. var second = controllers[ controller.skin.source ];
  399. morphController = second;
  400. // skinController = node.controllers[i];
  401. if ( second.morph && geometries[ second.morph.source ] ) {
  402. var inst_geom = new InstanceGeometry();
  403. inst_geom.url = second.morph.source;
  404. inst_geom.instance_material = node.controllers[ i ].instance_material;
  405. node.geometries.push( inst_geom );
  406. }
  407. }
  408. break;
  409. case 'morph':
  410. if ( geometries[ controller.morph.source ] ) {
  411. var inst_geom = new InstanceGeometry();
  412. inst_geom.url = controller.morph.source;
  413. inst_geom.instance_material = node.controllers[ i ].instance_material;
  414. node.geometries.push( inst_geom );
  415. morphController = node.controllers[ i ];
  416. }
  417. console.log( 'ColladaLoader: Morph-controller partially supported.' );
  418. default:
  419. break;
  420. }
  421. }
  422. // FIXME: multi-material mesh?
  423. // geometries
  424. for ( i = 0; i < node.geometries.length; i ++ ) {
  425. var instance_geometry = node.geometries[i];
  426. var instance_materials = instance_geometry.instance_material;
  427. var geometry = geometries[instance_geometry.url];
  428. var used_materials = {};
  429. var used_materials_array = [];
  430. var num_materials = 0;
  431. var first_material;
  432. if ( geometry ) {
  433. if ( !geometry.mesh || !geometry.mesh.primitives )
  434. continue;
  435. if ( obj.name.length == 0 ) {
  436. obj.name = geometry.id;
  437. }
  438. // collect used fx for this geometry-instance
  439. if ( instance_materials ) {
  440. for ( j = 0; j < instance_materials.length; j ++ ) {
  441. var inst_material = instance_materials[j];
  442. var effect_id = materials[inst_material.target].instance_effect.url;
  443. var shader = effects[effect_id].shader;
  444. shader.material.opacity = !shader.material.opacity ? 1 : shader.material.opacity;
  445. used_materials[inst_material.symbol] = num_materials;
  446. used_materials_array.push(shader.material)
  447. first_material = shader.material;
  448. num_materials ++;
  449. }
  450. }
  451. var mesh;
  452. var material = first_material || new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading } );
  453. var geom = geometry.mesh.geometry3js;
  454. if ( num_materials > 1 ) {
  455. material = new THREE.MeshFaceMaterial();
  456. geom.materials = used_materials_array;
  457. for ( j = 0; j < geom.faces.length; j ++ ) {
  458. var face = geom.faces[ j ];
  459. face.materialIndex = used_materials[ face.daeMaterial ]
  460. }
  461. }
  462. if ( skinController !== undefined) {
  463. applySkin( geom, skinController );
  464. material.morphTargets = true;
  465. mesh = new THREE.SkinnedMesh( geom, material );
  466. mesh.skeleton = skinController.skeleton;
  467. mesh.skinController = controllers[ skinController.url ];
  468. mesh.skinInstanceController = skinController;
  469. mesh.name = 'skin_' + skins.length;
  470. skins.push( mesh );
  471. } else if ( morphController !== undefined ) {
  472. createMorph( geom, morphController );
  473. material.morphTargets = true;
  474. mesh = new THREE.Mesh( geom, material );
  475. mesh.name = 'morph_' + morphs.length;
  476. morphs.push( mesh );
  477. } else {
  478. mesh = new THREE.Mesh( geom, material );
  479. // mesh.geom.name = geometry.id;
  480. }
  481. node.geometries.length > 1 ? obj.add( mesh ) : obj = mesh;
  482. }
  483. }
  484. obj.name = node.id || "";
  485. obj.matrix = node.matrix;
  486. var props = node.matrix.decompose();
  487. obj.position = props[ 0 ];
  488. obj.quaternion = props[ 1 ];
  489. obj.useQuaternion = true;
  490. obj.scale = props[ 2 ];
  491. if ( options.centerGeometry && obj.geometry ) {
  492. var delta = THREE.GeometryUtils.center( obj.geometry );
  493. obj.quaternion.multiplyVector3( delta.multiplySelf( obj.scale ) );
  494. obj.position.subSelf( delta );
  495. }
  496. for ( i = 0; i < node.nodes.length; i ++ ) {
  497. obj.add( createSceneGraph( node.nodes[i], node ) );
  498. }
  499. return obj;
  500. };
  501. function getJointId( skin, id ) {
  502. for ( var i = 0; i < skin.joints.length; i ++ ) {
  503. if ( skin.joints[ i ] == id ) {
  504. return i;
  505. }
  506. }
  507. };
  508. function getLibraryNode( id ) {
  509. return COLLADA.evaluate( './/dae:library_nodes//dae:node[@id=\'' + id + '\']', COLLADA, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null ).iterateNext();
  510. };
  511. function getChannelsForNode (node ) {
  512. var channels = [];
  513. var startTime = 1000000;
  514. var endTime = -1000000;
  515. for ( var id in animations ) {
  516. var animation = animations[id];
  517. for ( var i = 0; i < animation.channel.length; i ++ ) {
  518. var channel = animation.channel[i];
  519. var sampler = animation.sampler[i];
  520. var id = channel.target.split('/')[0];
  521. if ( id == node.id ) {
  522. sampler.create();
  523. channel.sampler = sampler;
  524. startTime = Math.min(startTime, sampler.startTime);
  525. endTime = Math.max(endTime, sampler.endTime);
  526. channels.push(channel);
  527. }
  528. }
  529. }
  530. if ( channels.length ) {
  531. node.startTime = startTime;
  532. node.endTime = endTime;
  533. }
  534. return channels;
  535. };
  536. function calcFrameDuration( node ) {
  537. var minT = 10000000;
  538. for ( var i = 0; i < node.channels.length; i ++ ) {
  539. var sampler = node.channels[i].sampler;
  540. for ( var j = 0; j < sampler.input.length - 1; j ++ ) {
  541. var t0 = sampler.input[ j ];
  542. var t1 = sampler.input[ j + 1 ];
  543. minT = Math.min( minT, t1 - t0 );
  544. }
  545. }
  546. return minT;
  547. };
  548. function calcMatrixAt( node, t ) {
  549. var animated = {};
  550. var i, j;
  551. for ( i = 0; i < node.channels.length; i ++ ) {
  552. var channel = node.channels[ i ];
  553. animated[ channel.sid ] = channel;
  554. }
  555. var matrix = new THREE.Matrix4();
  556. for ( i = 0; i < node.transforms.length; i ++ ) {
  557. var transform = node.transforms[ i ];
  558. var channel = animated[ transform.sid ];
  559. if ( channel !== undefined ) {
  560. var sampler = channel.sampler;
  561. var value;
  562. for ( j = 0; j < sampler.input.length - 1; j ++ ) {
  563. if ( sampler.input[ j + 1 ] > t ) {
  564. value = sampler.output[ j ];
  565. //console.log(value.flatten)
  566. break;
  567. }
  568. }
  569. if ( value !== undefined ) {
  570. if ( value instanceof THREE.Matrix4 ) {
  571. matrix = matrix.multiply( matrix, value );
  572. } else {
  573. // FIXME: handle other types
  574. matrix = matrix.multiply( matrix, transform.matrix );
  575. }
  576. } else {
  577. matrix = matrix.multiply( matrix, transform.matrix );
  578. }
  579. } else {
  580. matrix = matrix.multiply( matrix, transform.matrix );
  581. }
  582. }
  583. return matrix;
  584. };
  585. function bakeAnimations ( node ) {
  586. if ( node.channels && node.channels.length ) {
  587. var keys = [],
  588. sids = [];
  589. for ( var i = 0, il = node.channels.length; i < il; i++ ) {
  590. var channel = node.channels[i],
  591. fullSid = channel.fullSid,
  592. member = getConvertedMember( channel.member ),
  593. sampler = channel.sampler,
  594. input = sampler.input,
  595. transform = node.getTransformBySid( channel.sid );
  596. if ( transform ) {
  597. if ( sids.indexOf( fullSid ) === -1 ) {
  598. sids.push( fullSid );
  599. }
  600. for ( var j = 0, jl = input.length; j < jl; j++ ) {
  601. var time = input[j],
  602. data = sampler.getData( transform.type, j ),
  603. key = findKey( keys, time );
  604. if ( !key ) {
  605. key = new Key( time );
  606. var timeNdx = findTimeNdx( keys, time );
  607. keys.splice( timeNdx == -1 ? keys.length : timeNdx, 0, key );
  608. }
  609. key.addTarget( fullSid, transform, member, data );
  610. }
  611. } else {
  612. console.log( 'Could not find transform "' + channel.sid + '" in node ' + node.id );
  613. }
  614. }
  615. // post process
  616. for ( var i = 0; i < sids.length; i++ ) {
  617. var sid = sids[ i ];
  618. for ( var j = 0; j < keys.length; j++ ) {
  619. var key = keys[ j ];
  620. if ( !key.hasTarget( sid ) ) {
  621. interpolateKeys( keys, key, j, sid );
  622. }
  623. }
  624. }
  625. node.keys = keys;
  626. node.sids = sids;
  627. }
  628. };
  629. function findKey ( keys, time) {
  630. var retVal = null;
  631. for ( var i = 0, il = keys.length; i < il && retVal == null; i++ ) {
  632. var key = keys[i];
  633. if ( key.time === time ) {
  634. retVal = key;
  635. } else if ( key.time > time ) {
  636. break;
  637. }
  638. }
  639. return retVal;
  640. };
  641. function findTimeNdx ( keys, time) {
  642. var ndx = -1;
  643. for ( var i = 0, il = keys.length; i < il && ndx == -1; i++ ) {
  644. var key = keys[i];
  645. if ( key.time >= time ) {
  646. ndx = i;
  647. }
  648. }
  649. return ndx;
  650. };
  651. function interpolateKeys ( keys, key, ndx, fullSid ) {
  652. var prevKey = getPrevKeyWith( keys, fullSid, ndx ? ndx-1 : 0 ),
  653. nextKey = getNextKeyWith( keys, fullSid, ndx+1 );
  654. if ( prevKey && nextKey ) {
  655. var scale = (key.time - prevKey.time) / (nextKey.time - prevKey.time),
  656. prevTarget = prevKey.getTarget( fullSid ),
  657. nextData = nextKey.getTarget( fullSid ).data,
  658. prevData = prevTarget.data,
  659. data;
  660. if ( prevData.length ) {
  661. data = [];
  662. for ( var i = 0; i < prevData.length; ++i ) {
  663. data[ i ] = prevData[ i ] + ( nextData[ i ] - prevData[ i ] ) * scale;
  664. }
  665. } else {
  666. data = prevData + ( nextData - prevData ) * scale;
  667. }
  668. key.addTarget( fullSid, prevTarget.transform, prevTarget.member, data );
  669. }
  670. };
  671. // Get next key with given sid
  672. function getNextKeyWith( keys, fullSid, ndx ) {
  673. for ( ; ndx < keys.length; ndx++ ) {
  674. var key = keys[ ndx ];
  675. if ( key.hasTarget( fullSid ) ) {
  676. return key;
  677. }
  678. }
  679. return null;
  680. };
  681. // Get previous key with given sid
  682. function getPrevKeyWith( keys, fullSid, ndx ) {
  683. ndx = ndx >= 0 ? ndx : ndx + keys.length;
  684. for ( ; ndx >= 0; ndx-- ) {
  685. var key = keys[ ndx ];
  686. if ( key.hasTarget( fullSid ) ) {
  687. return key;
  688. }
  689. }
  690. return null;
  691. };
  692. function _Image() {
  693. this.id = "";
  694. this.init_from = "";
  695. };
  696. _Image.prototype.parse = function(element) {
  697. this.id = element.getAttribute('id');
  698. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  699. var child = element.childNodes[ i ];
  700. if ( child.nodeName == 'init_from' ) {
  701. this.init_from = child.textContent;
  702. }
  703. }
  704. return this;
  705. };
  706. function Controller() {
  707. this.id = "";
  708. this.name = "";
  709. this.type = "";
  710. this.skin = null;
  711. this.morph = null;
  712. };
  713. Controller.prototype.parse = function( element ) {
  714. this.id = element.getAttribute('id');
  715. this.name = element.getAttribute('name');
  716. this.type = "none";
  717. for ( var i = 0; i < element.childNodes.length; i++ ) {
  718. var child = element.childNodes[ i ];
  719. switch ( child.nodeName ) {
  720. case 'skin':
  721. this.skin = (new Skin()).parse(child);
  722. this.type = child.nodeName;
  723. break;
  724. case 'morph':
  725. this.morph = (new Morph()).parse(child);
  726. this.type = child.nodeName;
  727. break;
  728. default:
  729. break;
  730. }
  731. }
  732. return this;
  733. };
  734. function Morph() {
  735. this.method = null;
  736. this.source = null;
  737. this.targets = null;
  738. this.weights = null;
  739. };
  740. Morph.prototype.parse = function( element ) {
  741. var sources = {};
  742. var inputs = [];
  743. var i;
  744. this.method = element.getAttribute( 'method' );
  745. this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
  746. for ( i = 0; i < element.childNodes.length; i ++ ) {
  747. var child = element.childNodes[ i ];
  748. if ( child.nodeType != 1 ) continue;
  749. switch ( child.nodeName ) {
  750. case 'source':
  751. var source = ( new Source() ).parse( child );
  752. sources[ source.id ] = source;
  753. break;
  754. case 'targets':
  755. inputs = this.parseInputs( child );
  756. break;
  757. default:
  758. console.log( child.nodeName );
  759. break;
  760. }
  761. }
  762. for ( i = 0; i < inputs.length; i ++ ) {
  763. var input = inputs[ i ];
  764. var source = sources[ input.source ];
  765. switch ( input.semantic ) {
  766. case 'MORPH_TARGET':
  767. this.targets = source.read();
  768. break;
  769. case 'MORPH_WEIGHT':
  770. this.weights = source.read();
  771. break;
  772. default:
  773. break;
  774. }
  775. }
  776. return this;
  777. };
  778. Morph.prototype.parseInputs = function(element) {
  779. var inputs = [];
  780. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  781. var child = element.childNodes[i];
  782. if ( child.nodeType != 1) continue;
  783. switch ( child.nodeName ) {
  784. case 'input':
  785. inputs.push( (new Input()).parse(child) );
  786. break;
  787. default:
  788. break;
  789. }
  790. }
  791. return inputs;
  792. };
  793. function Skin() {
  794. this.source = "";
  795. this.bindShapeMatrix = null;
  796. this.invBindMatrices = [];
  797. this.joints = [];
  798. this.weights = [];
  799. };
  800. Skin.prototype.parse = function( element ) {
  801. var sources = {};
  802. var joints, weights;
  803. this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
  804. this.invBindMatrices = [];
  805. this.joints = [];
  806. this.weights = [];
  807. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  808. var child = element.childNodes[i];
  809. if ( child.nodeType != 1 ) continue;
  810. switch ( child.nodeName ) {
  811. case 'bind_shape_matrix':
  812. var f = _floats(child.textContent);
  813. this.bindShapeMatrix = getConvertedMat4( f );
  814. break;
  815. case 'source':
  816. var src = new Source().parse(child);
  817. sources[ src.id ] = src;
  818. break;
  819. case 'joints':
  820. joints = child;
  821. break;
  822. case 'vertex_weights':
  823. weights = child;
  824. break;
  825. default:
  826. console.log( child.nodeName );
  827. break;
  828. }
  829. }
  830. this.parseJoints( joints, sources );
  831. this.parseWeights( weights, sources );
  832. return this;
  833. };
  834. Skin.prototype.parseJoints = function ( element, sources ) {
  835. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  836. var child = element.childNodes[ i ];
  837. if ( child.nodeType != 1 ) continue;
  838. switch ( child.nodeName ) {
  839. case 'input':
  840. var input = ( new Input() ).parse( child );
  841. var source = sources[ input.source ];
  842. if ( input.semantic == 'JOINT' ) {
  843. this.joints = source.read();
  844. } else if ( input.semantic == 'INV_BIND_MATRIX' ) {
  845. this.invBindMatrices = source.read();
  846. }
  847. break;
  848. default:
  849. break;
  850. }
  851. }
  852. };
  853. Skin.prototype.parseWeights = function ( element, sources ) {
  854. var v, vcount, inputs = [];
  855. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  856. var child = element.childNodes[ i ];
  857. if ( child.nodeType != 1 ) continue;
  858. switch ( child.nodeName ) {
  859. case 'input':
  860. inputs.push( ( new Input() ).parse( child ) );
  861. break;
  862. case 'v':
  863. v = _ints( child.textContent );
  864. break;
  865. case 'vcount':
  866. vcount = _ints( child.textContent );
  867. break;
  868. default:
  869. break;
  870. }
  871. }
  872. var index = 0;
  873. for ( var i = 0; i < vcount.length; i ++ ) {
  874. var numBones = vcount[i];
  875. var vertex_weights = [];
  876. for ( var j = 0; j < numBones; j++ ) {
  877. var influence = {};
  878. for ( var k = 0; k < inputs.length; k ++ ) {
  879. var input = inputs[ k ];
  880. var value = v[ index + input.offset ];
  881. switch ( input.semantic ) {
  882. case 'JOINT':
  883. influence.joint = value;//this.joints[value];
  884. break;
  885. case 'WEIGHT':
  886. influence.weight = sources[ input.source ].data[ value ];
  887. break;
  888. default:
  889. break;
  890. }
  891. }
  892. vertex_weights.push( influence );
  893. index += inputs.length;
  894. }
  895. for ( var j = 0; j < vertex_weights.length; j ++ ) {
  896. vertex_weights[ j ].index = i;
  897. }
  898. this.weights.push( vertex_weights );
  899. }
  900. };
  901. function VisualScene () {
  902. this.id = "";
  903. this.name = "";
  904. this.nodes = [];
  905. this.scene = new THREE.Object3D();
  906. };
  907. VisualScene.prototype.getChildById = function( id, recursive ) {
  908. for ( var i = 0; i < this.nodes.length; i ++ ) {
  909. var node = this.nodes[ i ].getChildById( id, recursive );
  910. if ( node ) {
  911. return node;
  912. }
  913. }
  914. return null;
  915. };
  916. VisualScene.prototype.getChildBySid = function( sid, recursive ) {
  917. for ( var i = 0; i < this.nodes.length; i ++ ) {
  918. var node = this.nodes[ i ].getChildBySid( sid, recursive );
  919. if ( node ) {
  920. return node;
  921. }
  922. }
  923. return null;
  924. };
  925. VisualScene.prototype.parse = function( element ) {
  926. this.id = element.getAttribute( 'id' );
  927. this.name = element.getAttribute( 'name' );
  928. this.nodes = [];
  929. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  930. var child = element.childNodes[ i ];
  931. if ( child.nodeType != 1 ) continue;
  932. switch ( child.nodeName ) {
  933. case 'node':
  934. this.nodes.push( ( new Node() ).parse( child ) );
  935. break;
  936. default:
  937. break;
  938. }
  939. }
  940. return this;
  941. };
  942. function Node() {
  943. this.id = "";
  944. this.name = "";
  945. this.sid = "";
  946. this.nodes = [];
  947. this.controllers = [];
  948. this.transforms = [];
  949. this.geometries = [];
  950. this.channels = [];
  951. this.matrix = new THREE.Matrix4();
  952. };
  953. Node.prototype.getChannelForTransform = function( transformSid ) {
  954. for ( var i = 0; i < this.channels.length; i ++ ) {
  955. var channel = this.channels[i];
  956. var parts = channel.target.split('/');
  957. var id = parts.shift();
  958. var sid = parts.shift();
  959. var dotSyntax = (sid.indexOf(".") >= 0);
  960. var arrSyntax = (sid.indexOf("(") >= 0);
  961. var arrIndices;
  962. var member;
  963. if ( dotSyntax ) {
  964. parts = sid.split(".");
  965. sid = parts.shift();
  966. member = parts.shift();
  967. } else if ( arrSyntax ) {
  968. arrIndices = sid.split("(");
  969. sid = arrIndices.shift();
  970. for ( var j = 0; j < arrIndices.length; j ++ ) {
  971. arrIndices[ j ] = parseInt( arrIndices[ j ].replace( /\)/, '' ) );
  972. }
  973. }
  974. if ( sid == transformSid ) {
  975. channel.info = { sid: sid, dotSyntax: dotSyntax, arrSyntax: arrSyntax, arrIndices: arrIndices };
  976. return channel;
  977. }
  978. }
  979. return null;
  980. };
  981. Node.prototype.getChildById = function ( id, recursive ) {
  982. if ( this.id == id ) {
  983. return this;
  984. }
  985. if ( recursive ) {
  986. for ( var i = 0; i < this.nodes.length; i ++ ) {
  987. var n = this.nodes[ i ].getChildById( id, recursive );
  988. if ( n ) {
  989. return n;
  990. }
  991. }
  992. }
  993. return null;
  994. };
  995. Node.prototype.getChildBySid = function ( sid, recursive ) {
  996. if ( this.sid == sid ) {
  997. return this;
  998. }
  999. if ( recursive ) {
  1000. for ( var i = 0; i < this.nodes.length; i ++ ) {
  1001. var n = this.nodes[ i ].getChildBySid( sid, recursive );
  1002. if ( n ) {
  1003. return n;
  1004. }
  1005. }
  1006. }
  1007. return null;
  1008. };
  1009. Node.prototype.getTransformBySid = function ( sid ) {
  1010. for ( var i = 0; i < this.transforms.length; i ++ ) {
  1011. if ( this.transforms[ i ].sid == sid ) return this.transforms[ i ];
  1012. }
  1013. return null;
  1014. };
  1015. Node.prototype.parse = function( element ) {
  1016. var url;
  1017. this.id = element.getAttribute('id');
  1018. this.sid = element.getAttribute('sid');
  1019. this.name = element.getAttribute('name');
  1020. this.type = element.getAttribute('type');
  1021. this.type = this.type == 'JOINT' ? this.type : 'NODE';
  1022. this.nodes = [];
  1023. this.transforms = [];
  1024. this.geometries = [];
  1025. this.controllers = [];
  1026. this.matrix = new THREE.Matrix4();
  1027. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1028. var child = element.childNodes[ i ];
  1029. if ( child.nodeType != 1 ) continue;
  1030. switch ( child.nodeName ) {
  1031. case 'node':
  1032. this.nodes.push( ( new Node() ).parse( child ) );
  1033. break;
  1034. case 'instance_camera':
  1035. break;
  1036. case 'instance_controller':
  1037. this.controllers.push( ( new InstanceController() ).parse( child ) );
  1038. break;
  1039. case 'instance_geometry':
  1040. this.geometries.push( ( new InstanceGeometry() ).parse( child ) );
  1041. break;
  1042. case 'instance_light':
  1043. break;
  1044. case 'instance_node':
  1045. url = child.getAttribute( 'url' ).replace( /^#/, '' );
  1046. var iNode = getLibraryNode( url );
  1047. if ( iNode ) {
  1048. this.nodes.push( ( new Node() ).parse( iNode )) ;
  1049. }
  1050. break;
  1051. case 'rotate':
  1052. case 'translate':
  1053. case 'scale':
  1054. case 'matrix':
  1055. case 'lookat':
  1056. case 'skew':
  1057. this.transforms.push( ( new Transform() ).parse( child ) );
  1058. break;
  1059. case 'extra':
  1060. break;
  1061. default:
  1062. console.log( child.nodeName );
  1063. break;
  1064. }
  1065. }
  1066. this.channels = getChannelsForNode( this );
  1067. bakeAnimations( this );
  1068. this.updateMatrix();
  1069. return this;
  1070. };
  1071. Node.prototype.updateMatrix = function () {
  1072. this.matrix.identity();
  1073. for ( var i = 0; i < this.transforms.length; i ++ ) {
  1074. this.transforms[ i ].apply( this.matrix );
  1075. }
  1076. };
  1077. function Transform () {
  1078. this.sid = "";
  1079. this.type = "";
  1080. this.data = [];
  1081. this.obj = null;
  1082. };
  1083. Transform.prototype.parse = function ( element ) {
  1084. this.sid = element.getAttribute( 'sid' );
  1085. this.type = element.nodeName;
  1086. this.data = _floats( element.textContent );
  1087. this.convert();
  1088. return this;
  1089. };
  1090. Transform.prototype.convert = function () {
  1091. switch ( this.type ) {
  1092. case 'matrix':
  1093. this.obj = getConvertedMat4( this.data );
  1094. break;
  1095. case 'rotate':
  1096. this.angle = this.data[3] * TO_RADIANS;
  1097. case 'translate':
  1098. fixCoords( this.data, -1 );
  1099. this.obj = new THREE.Vector3( this.data[ 0 ], this.data[ 1 ], this.data[ 2 ] );
  1100. break;
  1101. case 'scale':
  1102. fixCoords( this.data, 1 );
  1103. this.obj = new THREE.Vector3( this.data[ 0 ], this.data[ 1 ], this.data[ 2 ] );
  1104. break;
  1105. default:
  1106. console.log( 'Can not convert Transform of type ' + this.type );
  1107. break;
  1108. }
  1109. };
  1110. Transform.prototype.apply = function ( matrix ) {
  1111. switch ( this.type ) {
  1112. case 'matrix':
  1113. matrix.multiplySelf( this.obj );
  1114. break;
  1115. case 'translate':
  1116. matrix.translate( this.obj );
  1117. break;
  1118. case 'rotate':
  1119. matrix.rotateByAxis( this.obj, this.angle );
  1120. break;
  1121. case 'scale':
  1122. matrix.scale( this.obj );
  1123. break;
  1124. }
  1125. };
  1126. Transform.prototype.update = function ( data, member ) {
  1127. switch ( this.type ) {
  1128. case 'matrix':
  1129. console.log( 'Currently not handling matrix transform updates' );
  1130. break;
  1131. case 'translate':
  1132. case 'scale':
  1133. switch ( member ) {
  1134. case 'X':
  1135. this.obj.x = data;
  1136. break;
  1137. case 'Y':
  1138. this.obj.y = data;
  1139. break;
  1140. case 'Z':
  1141. this.obj.z = data;
  1142. break;
  1143. default:
  1144. this.obj.x = data[ 0 ];
  1145. this.obj.y = data[ 1 ];
  1146. this.obj.z = data[ 2 ];
  1147. break;
  1148. }
  1149. break;
  1150. case 'rotate':
  1151. switch ( member ) {
  1152. case 'X':
  1153. this.obj.x = data;
  1154. break;
  1155. case 'Y':
  1156. this.obj.y = data;
  1157. break;
  1158. case 'Z':
  1159. this.obj.z = data;
  1160. break;
  1161. case 'ANGLE':
  1162. this.angle = data * TO_RADIANS;
  1163. break;
  1164. default:
  1165. this.obj.x = data[ 0 ];
  1166. this.obj.y = data[ 1 ];
  1167. this.obj.z = data[ 2 ];
  1168. this.angle = data[ 3 ] * TO_RADIANS;
  1169. break;
  1170. }
  1171. break;
  1172. }
  1173. };
  1174. function InstanceController() {
  1175. this.url = "";
  1176. this.skeleton = [];
  1177. this.instance_material = [];
  1178. };
  1179. InstanceController.prototype.parse = function ( element ) {
  1180. this.url = element.getAttribute('url').replace(/^#/, '');
  1181. this.skeleton = [];
  1182. this.instance_material = [];
  1183. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1184. var child = element.childNodes[i];
  1185. if (child.nodeType != 1) continue;
  1186. switch ( child.nodeName ) {
  1187. case 'skeleton':
  1188. this.skeleton.push( child.textContent.replace(/^#/, '') );
  1189. break;
  1190. case 'bind_material':
  1191. var instances = COLLADA.evaluate( './/dae:instance_material', child, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
  1192. if ( instances ) {
  1193. var instance = instances.iterateNext();
  1194. while ( instance ) {
  1195. this.instance_material.push((new InstanceMaterial()).parse(instance));
  1196. instance = instances.iterateNext();
  1197. }
  1198. }
  1199. break;
  1200. case 'extra':
  1201. break;
  1202. default:
  1203. break;
  1204. }
  1205. }
  1206. return this;
  1207. };
  1208. function InstanceMaterial () {
  1209. this.symbol = "";
  1210. this.target = "";
  1211. };
  1212. InstanceMaterial.prototype.parse = function ( element ) {
  1213. this.symbol = element.getAttribute('symbol');
  1214. this.target = element.getAttribute('target').replace(/^#/, '');
  1215. return this;
  1216. };
  1217. function InstanceGeometry() {
  1218. this.url = "";
  1219. this.instance_material = [];
  1220. };
  1221. InstanceGeometry.prototype.parse = function ( element ) {
  1222. this.url = element.getAttribute('url').replace(/^#/, '');
  1223. this.instance_material = [];
  1224. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1225. var child = element.childNodes[i];
  1226. if ( child.nodeType != 1 ) continue;
  1227. if ( child.nodeName == 'bind_material' ) {
  1228. var instances = COLLADA.evaluate( './/dae:instance_material', child, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
  1229. if ( instances ) {
  1230. var instance = instances.iterateNext();
  1231. while ( instance ) {
  1232. this.instance_material.push( (new InstanceMaterial()).parse(instance) );
  1233. instance = instances.iterateNext();
  1234. }
  1235. }
  1236. break;
  1237. }
  1238. }
  1239. return this;
  1240. };
  1241. function Geometry() {
  1242. this.id = "";
  1243. this.mesh = null;
  1244. };
  1245. Geometry.prototype.parse = function ( element ) {
  1246. this.id = element.getAttribute('id');
  1247. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1248. var child = element.childNodes[i];
  1249. switch ( child.nodeName ) {
  1250. case 'mesh':
  1251. this.mesh = (new Mesh(this)).parse(child);
  1252. break;
  1253. case 'extra':
  1254. // console.log( child );
  1255. break;
  1256. default:
  1257. break;
  1258. }
  1259. }
  1260. return this;
  1261. };
  1262. function Mesh( geometry ) {
  1263. this.geometry = geometry.id;
  1264. this.primitives = [];
  1265. this.vertices = null;
  1266. this.geometry3js = null;
  1267. };
  1268. Mesh.prototype.parse = function( element ) {
  1269. this.primitives = [];
  1270. var i, j;
  1271. for ( i = 0; i < element.childNodes.length; i ++ ) {
  1272. var child = element.childNodes[ i ];
  1273. switch ( child.nodeName ) {
  1274. case 'source':
  1275. _source( child );
  1276. break;
  1277. case 'vertices':
  1278. this.vertices = ( new Vertices() ).parse( child );
  1279. break;
  1280. case 'triangles':
  1281. this.primitives.push( ( new Triangles().parse( child ) ) );
  1282. break;
  1283. case 'polygons':
  1284. console.warn( 'polygon holes not yet supported!' );
  1285. case 'polylist':
  1286. this.primitives.push( ( new Polylist().parse( child ) ) );
  1287. break;
  1288. default:
  1289. break;
  1290. }
  1291. }
  1292. this.geometry3js = new THREE.Geometry();
  1293. var vertexData = sources[ this.vertices.input['POSITION'].source ].data;
  1294. for ( i = 0; i < vertexData.length; i += 3 ) {
  1295. var v = new THREE.Vertex( getConvertedVec3( vertexData, i ) );
  1296. this.geometry3js.vertices.push( v );
  1297. }
  1298. for ( i = 0; i < this.primitives.length; i ++ ) {
  1299. var primitive = this.primitives[ i ];
  1300. primitive.setVertices( this.vertices );
  1301. this.handlePrimitive( primitive, this.geometry3js );
  1302. }
  1303. this.geometry3js.computeCentroids();
  1304. this.geometry3js.computeFaceNormals();
  1305. this.geometry3js.computeVertexNormals();
  1306. this.geometry3js.computeBoundingBox();
  1307. return this;
  1308. };
  1309. Mesh.prototype.handlePrimitive = function( primitive, geom ) {
  1310. var i = 0, j, k, p = primitive.p, inputs = primitive.inputs;
  1311. var input, index, idx32;
  1312. var source, numParams;
  1313. var vcIndex = 0, vcount = 3;
  1314. var texture_sets = [];
  1315. for ( j = 0; j < inputs.length; j ++ ) {
  1316. input = inputs[ j ];
  1317. switch ( input.semantic ) {
  1318. case 'TEXCOORD':
  1319. texture_sets.push( input.set );
  1320. break;
  1321. }
  1322. }
  1323. while ( i < p.length ) {
  1324. var vs = [];
  1325. var ns = [];
  1326. var ts = {};
  1327. var cs = [];
  1328. if ( primitive.vcount ) {
  1329. vcount = primitive.vcount[ vcIndex ++ ];
  1330. }
  1331. for ( j = 0; j < vcount; j ++ ) {
  1332. for ( k = 0; k < inputs.length; k ++ ) {
  1333. input = inputs[ k ];
  1334. source = sources[ input.source ];
  1335. index = p[ i + ( j * inputs.length ) + input.offset ];
  1336. numParams = source.accessor.params.length;
  1337. idx32 = index * numParams;
  1338. switch ( input.semantic ) {
  1339. case 'VERTEX':
  1340. vs.push( index );
  1341. break;
  1342. case 'NORMAL':
  1343. ns.push( getConvertedVec3( source.data, idx32 ) );
  1344. break;
  1345. case 'TEXCOORD':
  1346. if ( ts[ input.set ] === undefined ) ts[ input.set ] = [];
  1347. // invert the V
  1348. ts[ input.set ].push( new THREE.UV( source.data[ idx32 ], 1.0 - source.data[ idx32 + 1 ] ) );
  1349. break;
  1350. case 'COLOR':
  1351. cs.push( new THREE.Color().setRGB( source.data[ idx32 ], source.data[ idx32 + 1 ], source.data[ idx32 + 2 ] ) );
  1352. break;
  1353. default:
  1354. break;
  1355. }
  1356. }
  1357. }
  1358. var face = null, faces = [], uv, uvArr;
  1359. if ( vcount === 3 ) {
  1360. faces.push( new THREE.Face3( vs[0], vs[1], vs[2], [ ns[0], ns[1], ns[2] ], cs.length ? cs : new THREE.Color() ) );
  1361. } else if ( vcount === 4 ) {
  1362. faces.push( new THREE.Face4( vs[0], vs[1], vs[2], vs[3], [ ns[0], ns[1], ns[2], ns[3] ], cs.length ? cs : new THREE.Color() ) );
  1363. } else if ( vcount > 4 && options.subdivideFaces ) {
  1364. var clr = cs.length ? cs : new THREE.Color(),
  1365. vec1, vec2, vec3, v1, v2, norm;
  1366. // subdivide into multiple Face3s
  1367. for ( k = 1; k < vcount-1; ) {
  1368. // FIXME: normals don't seem to be quite right
  1369. faces.push( new THREE.Face3( vs[0], vs[k], vs[k+1], [ ns[0], ns[k++], ns[k] ], clr ) );
  1370. }
  1371. }
  1372. if ( faces.length ) {
  1373. for (var ndx = 0, len = faces.length; ndx < len; ndx++) {
  1374. face = faces[ndx];
  1375. face.daeMaterial = primitive.material;
  1376. geom.faces.push( face );
  1377. for ( k = 0; k < texture_sets.length; k++ ) {
  1378. uv = ts[ texture_sets[k] ];
  1379. if ( vcount > 4 ) {
  1380. // Grab the right UVs for the vertices in this face
  1381. uvArr = [ uv[0], uv[ndx+1], uv[ndx+2] ];
  1382. } else if ( vcount === 4 ) {
  1383. uvArr = [ uv[0], uv[1], uv[2], uv[3] ];
  1384. } else {
  1385. uvArr = [ uv[0], uv[1], uv[2] ];
  1386. }
  1387. if ( !geom.faceVertexUvs[k] ) {
  1388. geom.faceVertexUvs[k] = [];
  1389. }
  1390. geom.faceVertexUvs[k].push( uvArr );
  1391. }
  1392. }
  1393. } else {
  1394. console.log( 'dropped face with vcount ' + vcount + ' for geometry with id: ' + geom.id );
  1395. }
  1396. i += inputs.length * vcount;
  1397. }
  1398. };
  1399. function Polylist () {
  1400. };
  1401. Polylist.prototype = new Triangles();
  1402. Polylist.prototype.constructor = Polylist;
  1403. function Triangles( flip_uv ) {
  1404. this.material = "";
  1405. this.count = 0;
  1406. this.inputs = [];
  1407. this.vcount = null;
  1408. this.p = [];
  1409. this.geometry = new THREE.Geometry();
  1410. };
  1411. Triangles.prototype.setVertices = function ( vertices ) {
  1412. for ( var i = 0; i < this.inputs.length; i ++ ) {
  1413. if ( this.inputs[ i ].source == vertices.id ) {
  1414. this.inputs[ i ].source = vertices.input[ 'POSITION' ].source;
  1415. }
  1416. }
  1417. };
  1418. Triangles.prototype.parse = function ( element ) {
  1419. this.inputs = [];
  1420. this.material = element.getAttribute( 'material' );
  1421. this.count = _attr_as_int( element, 'count', 0 );
  1422. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1423. var child = element.childNodes[ i ];
  1424. switch ( child.nodeName ) {
  1425. case 'input':
  1426. this.inputs.push( ( new Input() ).parse( element.childNodes[ i ] ) );
  1427. break;
  1428. case 'vcount':
  1429. this.vcount = _ints( child.textContent );
  1430. break;
  1431. case 'p':
  1432. this.p = _ints( child.textContent );
  1433. break;
  1434. default:
  1435. break;
  1436. }
  1437. }
  1438. return this;
  1439. };
  1440. function Accessor() {
  1441. this.source = "";
  1442. this.count = 0;
  1443. this.stride = 0;
  1444. this.params = [];
  1445. };
  1446. Accessor.prototype.parse = function ( element ) {
  1447. this.params = [];
  1448. this.source = element.getAttribute( 'source' );
  1449. this.count = _attr_as_int( element, 'count', 0 );
  1450. this.stride = _attr_as_int( element, 'stride', 0 );
  1451. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1452. var child = element.childNodes[ i ];
  1453. if ( child.nodeName == 'param' ) {
  1454. var param = {};
  1455. param[ 'name' ] = child.getAttribute( 'name' );
  1456. param[ 'type' ] = child.getAttribute( 'type' );
  1457. this.params.push( param );
  1458. }
  1459. }
  1460. return this;
  1461. };
  1462. function Vertices() {
  1463. this.input = {};
  1464. };
  1465. Vertices.prototype.parse = function ( element ) {
  1466. this.id = element.getAttribute('id');
  1467. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1468. if ( element.childNodes[i].nodeName == 'input' ) {
  1469. var input = ( new Input() ).parse( element.childNodes[ i ] );
  1470. this.input[ input.semantic ] = input;
  1471. }
  1472. }
  1473. return this;
  1474. };
  1475. function Input () {
  1476. this.semantic = "";
  1477. this.offset = 0;
  1478. this.source = "";
  1479. this.set = 0;
  1480. };
  1481. Input.prototype.parse = function ( element ) {
  1482. this.semantic = element.getAttribute('semantic');
  1483. this.source = element.getAttribute('source').replace(/^#/, '');
  1484. this.set = _attr_as_int(element, 'set', -1);
  1485. this.offset = _attr_as_int(element, 'offset', 0);
  1486. if ( this.semantic == 'TEXCOORD' && this.set < 0 ) {
  1487. this.set = 0;
  1488. }
  1489. return this;
  1490. };
  1491. function Source ( id ) {
  1492. this.id = id;
  1493. this.type = null;
  1494. };
  1495. Source.prototype.parse = function ( element ) {
  1496. this.id = element.getAttribute( 'id' );
  1497. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1498. var child = element.childNodes[i];
  1499. switch ( child.nodeName ) {
  1500. case 'bool_array':
  1501. this.data = _bools( child.textContent );
  1502. this.type = child.nodeName;
  1503. break;
  1504. case 'float_array':
  1505. this.data = _floats( child.textContent );
  1506. this.type = child.nodeName;
  1507. break;
  1508. case 'int_array':
  1509. this.data = _ints( child.textContent );
  1510. this.type = child.nodeName;
  1511. break;
  1512. case 'IDREF_array':
  1513. case 'Name_array':
  1514. this.data = _strings( child.textContent );
  1515. this.type = child.nodeName;
  1516. break;
  1517. case 'technique_common':
  1518. for ( var j = 0; j < child.childNodes.length; j ++ ) {
  1519. if ( child.childNodes[ j ].nodeName == 'accessor' ) {
  1520. this.accessor = ( new Accessor() ).parse( child.childNodes[ j ] );
  1521. break;
  1522. }
  1523. }
  1524. break;
  1525. default:
  1526. // console.log(child.nodeName);
  1527. break;
  1528. }
  1529. }
  1530. return this;
  1531. };
  1532. Source.prototype.read = function () {
  1533. var result = [];
  1534. //for (var i = 0; i < this.accessor.params.length; i++) {
  1535. var param = this.accessor.params[ 0 ];
  1536. //console.log(param.name + " " + param.type);
  1537. switch ( param.type ) {
  1538. case 'IDREF':
  1539. case 'Name': case 'name':
  1540. case 'float':
  1541. return this.data;
  1542. case 'float4x4':
  1543. for ( var j = 0; j < this.data.length; j += 16 ) {
  1544. var s = this.data.slice( j, j + 16 );
  1545. var m = getConvertedMat4( s );
  1546. result.push( m );
  1547. }
  1548. break;
  1549. default:
  1550. console.log( 'ColladaLoader: Source: Read dont know how to read ' + param.type + '.' );
  1551. break;
  1552. }
  1553. //}
  1554. return result;
  1555. };
  1556. function Material () {
  1557. this.id = "";
  1558. this.name = "";
  1559. this.instance_effect = null;
  1560. };
  1561. Material.prototype.parse = function ( element ) {
  1562. this.id = element.getAttribute( 'id' );
  1563. this.name = element.getAttribute( 'name' );
  1564. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1565. if ( element.childNodes[ i ].nodeName == 'instance_effect' ) {
  1566. this.instance_effect = ( new InstanceEffect() ).parse( element.childNodes[ i ] );
  1567. break;
  1568. }
  1569. }
  1570. return this;
  1571. };
  1572. function ColorOrTexture () {
  1573. this.color = new THREE.Color( 0 );
  1574. this.color.setRGB( Math.random(), Math.random(), Math.random() );
  1575. this.color.a = 1.0;
  1576. this.texture = null;
  1577. this.texcoord = null;
  1578. this.texOpts = null;
  1579. };
  1580. ColorOrTexture.prototype.isColor = function () {
  1581. return ( this.texture == null );
  1582. };
  1583. ColorOrTexture.prototype.isTexture = function () {
  1584. return ( this.texture != null );
  1585. };
  1586. ColorOrTexture.prototype.parse = function ( element ) {
  1587. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1588. var child = element.childNodes[ i ];
  1589. if ( child.nodeType != 1 ) continue;
  1590. switch ( child.nodeName ) {
  1591. case 'color':
  1592. var rgba = _floats( child.textContent );
  1593. this.color = new THREE.Color(0);
  1594. this.color.setRGB( rgba[0], rgba[1], rgba[2] );
  1595. this.color.a = rgba[3];
  1596. break;
  1597. case 'texture':
  1598. this.texture = child.getAttribute('texture');
  1599. this.texcoord = child.getAttribute('texcoord');
  1600. // Defaults from:
  1601. // https://collada.org/mediawiki/index.php/Maya_texture_placement_MAYA_extension
  1602. this.texOpts = {
  1603. offsetU: 0,
  1604. offsetV: 0,
  1605. repeatU: 1,
  1606. repeatV: 1,
  1607. wrapU: 1,
  1608. wrapV: 1,
  1609. };
  1610. this.parseTexture( child );
  1611. break;
  1612. default:
  1613. break;
  1614. }
  1615. }
  1616. return this;
  1617. };
  1618. ColorOrTexture.prototype.parseTexture = function ( element ) {
  1619. if ( ! element.childNodes ) return this;
  1620. // This should be supported by Maya, 3dsMax, and MotionBuilder
  1621. if ( element.childNodes[1] && element.childNodes[1].nodeName === 'extra' ) {
  1622. element = element.childNodes[1];
  1623. if ( element.childNodes[1] && element.childNodes[1].nodeName === 'technique' ) {
  1624. element = element.childNodes[1];
  1625. }
  1626. }
  1627. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1628. var child = element.childNodes[ i ];
  1629. switch ( child.nodeName ) {
  1630. case 'offsetU':
  1631. case 'offsetV':
  1632. case 'repeatU':
  1633. case 'repeatV':
  1634. this.texOpts[ child.nodeName ] = parseFloat( child.textContent );
  1635. break;
  1636. case 'wrapU':
  1637. case 'wrapV':
  1638. this.texOpts[ child.nodeName ] = parseInt( child.textContent );
  1639. break;
  1640. default:
  1641. this.texOpts[ child.nodeName ] = child.textContent;
  1642. break;
  1643. }
  1644. }
  1645. return this;
  1646. };
  1647. function Shader ( type, effect ) {
  1648. this.type = type;
  1649. this.effect = effect;
  1650. this.material = null;
  1651. };
  1652. Shader.prototype.parse = function ( element ) {
  1653. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1654. var child = element.childNodes[ i ];
  1655. if ( child.nodeType != 1 ) continue;
  1656. switch ( child.nodeName ) {
  1657. case 'ambient':
  1658. case 'emission':
  1659. case 'diffuse':
  1660. case 'specular':
  1661. case 'transparent':
  1662. this[ child.nodeName ] = ( new ColorOrTexture() ).parse( child );
  1663. break;
  1664. case 'shininess':
  1665. case 'reflectivity':
  1666. case 'transparency':
  1667. var f = evaluateXPath( child, './/dae:float' );
  1668. if ( f.length > 0 )
  1669. this[ child.nodeName ] = parseFloat( f[ 0 ].textContent );
  1670. break;
  1671. default:
  1672. break;
  1673. }
  1674. }
  1675. this.create();
  1676. return this;
  1677. };
  1678. Shader.prototype.create = function() {
  1679. var props = {};
  1680. var transparent = ( this['transparency'] !== undefined && this['transparency'] < 1.0 );
  1681. for ( var prop in this ) {
  1682. switch ( prop ) {
  1683. case 'ambient':
  1684. case 'emission':
  1685. case 'diffuse':
  1686. case 'specular':
  1687. var cot = this[prop];
  1688. if ( cot instanceof ColorOrTexture ) {
  1689. if ( cot.isTexture() ) {
  1690. if ( this.effect.sampler && this.effect.surface ) {
  1691. if ( this.effect.sampler.source == this.effect.surface.sid ) {
  1692. var image = images[this.effect.surface.init_from];
  1693. if ( image ) {
  1694. var texture = THREE.ImageUtils.loadTexture(baseUrl + image.init_from);
  1695. texture.wrapS = cot.texOpts.wrapU ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  1696. texture.wrapT = cot.texOpts.wrapV ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  1697. texture.offset.x = cot.texOpts.offsetU;
  1698. texture.offset.y = cot.texOpts.offsetV;
  1699. texture.repeat.x = cot.texOpts.repeatU;
  1700. texture.repeat.y = cot.texOpts.repeatV;
  1701. props['map'] = texture;
  1702. }
  1703. }
  1704. }
  1705. } else {
  1706. if ( prop == 'diffuse' ) {
  1707. props[ 'color' ] = cot.color.getHex();
  1708. } else if ( !transparent ) {
  1709. props[ prop ] = cot.color.getHex();
  1710. }
  1711. }
  1712. }
  1713. break;
  1714. case 'shininess':
  1715. case 'reflectivity':
  1716. props[ prop ] = this[ prop ];
  1717. break;
  1718. case 'transparency':
  1719. if ( transparent ) {
  1720. props[ 'transparent' ] = true;
  1721. props[ 'opacity' ] = this[ prop ];
  1722. transparent = true;
  1723. }
  1724. break;
  1725. default:
  1726. break;
  1727. }
  1728. }
  1729. props[ 'shading' ] = preferredShading;
  1730. this.material = new THREE.MeshLambertMaterial( props );
  1731. switch ( this.type ) {
  1732. case 'constant':
  1733. case 'lambert':
  1734. break;
  1735. case 'phong':
  1736. case 'blinn':
  1737. default:
  1738. /*
  1739. if ( !transparent ) {
  1740. // this.material = new THREE.MeshPhongMaterial(props);
  1741. }
  1742. */
  1743. break;
  1744. }
  1745. return this.material;
  1746. };
  1747. function Surface ( effect ) {
  1748. this.effect = effect;
  1749. this.init_from = null;
  1750. this.format = null;
  1751. };
  1752. Surface.prototype.parse = function ( element ) {
  1753. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1754. var child = element.childNodes[ i ];
  1755. if ( child.nodeType != 1 ) continue;
  1756. switch ( child.nodeName ) {
  1757. case 'init_from':
  1758. this.init_from = child.textContent;
  1759. break;
  1760. case 'format':
  1761. this.format = child.textContent;
  1762. break;
  1763. default:
  1764. console.log( "unhandled Surface prop: " + child.nodeName );
  1765. break;
  1766. }
  1767. }
  1768. return this;
  1769. };
  1770. function Sampler2D ( effect ) {
  1771. this.effect = effect;
  1772. this.source = null;
  1773. this.wrap_s = null;
  1774. this.wrap_t = null;
  1775. this.minfilter = null;
  1776. this.magfilter = null;
  1777. this.mipfilter = null;
  1778. };
  1779. Sampler2D.prototype.parse = function ( element ) {
  1780. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1781. var child = element.childNodes[ i ];
  1782. if ( child.nodeType != 1 ) continue;
  1783. switch ( child.nodeName ) {
  1784. case 'source':
  1785. this.source = child.textContent;
  1786. break;
  1787. case 'minfilter':
  1788. this.minfilter = child.textContent;
  1789. break;
  1790. case 'magfilter':
  1791. this.magfilter = child.textContent;
  1792. break;
  1793. case 'mipfilter':
  1794. this.mipfilter = child.textContent;
  1795. break;
  1796. case 'wrap_s':
  1797. this.wrap_s = child.textContent;
  1798. break;
  1799. case 'wrap_t':
  1800. this.wrap_t = child.textContent;
  1801. break;
  1802. default:
  1803. console.log( "unhandled Sampler2D prop: " + child.nodeName );
  1804. break;
  1805. }
  1806. }
  1807. return this;
  1808. };
  1809. function Effect () {
  1810. this.id = "";
  1811. this.name = "";
  1812. this.shader = null;
  1813. this.surface = null;
  1814. this.sampler = null;
  1815. };
  1816. Effect.prototype.create = function () {
  1817. if ( this.shader == null ) {
  1818. return null;
  1819. }
  1820. };
  1821. Effect.prototype.parse = function ( element ) {
  1822. this.id = element.getAttribute( 'id' );
  1823. this.name = element.getAttribute( 'name' );
  1824. this.shader = null;
  1825. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1826. var child = element.childNodes[ i ];
  1827. if ( child.nodeType != 1 ) continue;
  1828. switch ( child.nodeName ) {
  1829. case 'profile_COMMON':
  1830. this.parseTechnique( this.parseProfileCOMMON( child ) );
  1831. break;
  1832. default:
  1833. break;
  1834. }
  1835. }
  1836. return this;
  1837. };
  1838. Effect.prototype.parseNewparam = function ( element ) {
  1839. var sid = element.getAttribute( 'sid' );
  1840. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1841. var child = element.childNodes[ i ];
  1842. if ( child.nodeType != 1 ) continue;
  1843. switch ( child.nodeName ) {
  1844. case 'surface':
  1845. this.surface = ( new Surface( this ) ).parse( child );
  1846. this.surface.sid = sid;
  1847. break;
  1848. case 'sampler2D':
  1849. this.sampler = ( new Sampler2D( this ) ).parse( child );
  1850. this.sampler.sid = sid;
  1851. break;
  1852. case 'extra':
  1853. break;
  1854. default:
  1855. console.log( child.nodeName );
  1856. break;
  1857. }
  1858. }
  1859. };
  1860. Effect.prototype.parseProfileCOMMON = function ( element ) {
  1861. var technique;
  1862. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1863. var child = element.childNodes[ i ];
  1864. if ( child.nodeType != 1 ) continue;
  1865. switch ( child.nodeName ) {
  1866. case 'profile_COMMON':
  1867. this.parseProfileCOMMON( child );
  1868. break;
  1869. case 'technique':
  1870. technique = child;
  1871. break;
  1872. case 'newparam':
  1873. this.parseNewparam( child );
  1874. break;
  1875. case 'extra':
  1876. break;
  1877. default:
  1878. console.log( child.nodeName );
  1879. break;
  1880. }
  1881. }
  1882. return technique;
  1883. };
  1884. Effect.prototype.parseTechnique= function ( element ) {
  1885. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1886. var child = element.childNodes[i];
  1887. if ( child.nodeType != 1 ) continue;
  1888. switch ( child.nodeName ) {
  1889. case 'constant':
  1890. case 'lambert':
  1891. case 'blinn':
  1892. case 'phong':
  1893. this.shader = ( new Shader( child.nodeName, this ) ).parse( child );
  1894. break;
  1895. default:
  1896. break;
  1897. }
  1898. }
  1899. };
  1900. function InstanceEffect () {
  1901. this.url = "";
  1902. };
  1903. InstanceEffect.prototype.parse = function ( element ) {
  1904. this.url = element.getAttribute( 'url' ).replace( /^#/, '' );
  1905. return this;
  1906. };
  1907. function Animation() {
  1908. this.id = "";
  1909. this.name = "";
  1910. this.source = {};
  1911. this.sampler = [];
  1912. this.channel = [];
  1913. };
  1914. Animation.prototype.parse = function ( element ) {
  1915. this.id = element.getAttribute( 'id' );
  1916. this.name = element.getAttribute( 'name' );
  1917. this.source = {};
  1918. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1919. var child = element.childNodes[ i ];
  1920. if ( child.nodeType != 1 ) continue;
  1921. switch ( child.nodeName ) {
  1922. case 'source':
  1923. var src = ( new Source() ).parse( child );
  1924. this.source[ src.id ] = src;
  1925. break;
  1926. case 'sampler':
  1927. this.sampler.push( ( new Sampler( this ) ).parse( child ) );
  1928. break;
  1929. case 'channel':
  1930. this.channel.push( ( new Channel( this ) ).parse( child ) );
  1931. break;
  1932. default:
  1933. break;
  1934. }
  1935. }
  1936. return this;
  1937. };
  1938. function Channel( animation ) {
  1939. this.animation = animation;
  1940. this.source = "";
  1941. this.target = "";
  1942. this.fullSid = null;
  1943. this.sid = null;
  1944. this.dotSyntax = null;
  1945. this.arrSyntax = null;
  1946. this.arrIndices = null;
  1947. this.member = null;
  1948. };
  1949. Channel.prototype.parse = function ( element ) {
  1950. this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
  1951. this.target = element.getAttribute( 'target' );
  1952. var parts = this.target.split( '/' );
  1953. var id = parts.shift();
  1954. var sid = parts.shift();
  1955. var dotSyntax = ( sid.indexOf(".") >= 0 );
  1956. var arrSyntax = ( sid.indexOf("(") >= 0 );
  1957. if ( dotSyntax ) {
  1958. parts = sid.split(".");
  1959. this.sid = parts.shift();
  1960. this.member = parts.shift();
  1961. } else if ( arrSyntax ) {
  1962. var arrIndices = sid.split("(");
  1963. this.sid = arrIndices.shift();
  1964. for (var j = 0; j < arrIndices.length; j ++ ) {
  1965. arrIndices[j] = parseInt( arrIndices[j].replace(/\)/, '') );
  1966. }
  1967. this.arrIndices = arrIndices;
  1968. } else {
  1969. this.sid = sid;
  1970. }
  1971. this.fullSid = sid;
  1972. this.dotSyntax = dotSyntax;
  1973. this.arrSyntax = arrSyntax;
  1974. return this;
  1975. };
  1976. function Sampler ( animation ) {
  1977. this.id = "";
  1978. this.animation = animation;
  1979. this.inputs = [];
  1980. this.input = null;
  1981. this.output = null;
  1982. this.strideOut = null;
  1983. this.interpolation = null;
  1984. this.startTime = null;
  1985. this.endTime = null;
  1986. this.duration = 0;
  1987. };
  1988. Sampler.prototype.parse = function ( element ) {
  1989. this.id = element.getAttribute( 'id' );
  1990. this.inputs = [];
  1991. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1992. var child = element.childNodes[ i ];
  1993. if ( child.nodeType != 1 ) continue;
  1994. switch ( child.nodeName ) {
  1995. case 'input':
  1996. this.inputs.push( (new Input()).parse( child ) );
  1997. break;
  1998. default:
  1999. break;
  2000. }
  2001. }
  2002. return this;
  2003. };
  2004. Sampler.prototype.create = function () {
  2005. for ( var i = 0; i < this.inputs.length; i ++ ) {
  2006. var input = this.inputs[ i ];
  2007. var source = this.animation.source[ input.source ];
  2008. switch ( input.semantic ) {
  2009. case 'INPUT':
  2010. this.input = source.read();
  2011. break;
  2012. case 'OUTPUT':
  2013. this.output = source.read();
  2014. this.strideOut = source.accessor.stride;
  2015. break;
  2016. case 'INTERPOLATION':
  2017. this.interpolation = source.read();
  2018. break;
  2019. case 'IN_TANGENT':
  2020. break;
  2021. case 'OUT_TANGENT':
  2022. break;
  2023. default:
  2024. console.log(input.semantic);
  2025. break;
  2026. }
  2027. }
  2028. this.startTime = 0;
  2029. this.endTime = 0;
  2030. this.duration = 0;
  2031. if ( this.input.length ) {
  2032. this.startTime = 100000000;
  2033. this.endTime = -100000000;
  2034. for ( var i = 0; i < this.input.length; i ++ ) {
  2035. this.startTime = Math.min( this.startTime, this.input[ i ] );
  2036. this.endTime = Math.max( this.endTime, this.input[ i ] );
  2037. }
  2038. this.duration = this.endTime - this.startTime;
  2039. }
  2040. };
  2041. Sampler.prototype.getData = function ( type, ndx ) {
  2042. var data;
  2043. if ( this.strideOut > 1 ) {
  2044. data = [];
  2045. ndx *= this.strideOut;
  2046. for ( var i = 0; i < this.strideOut; ++i ) {
  2047. data[ i ] = this.output[ ndx + i ];
  2048. }
  2049. if ( this.strideOut === 3 ) {
  2050. switch ( type ) {
  2051. case 'rotate':
  2052. case 'translate':
  2053. fixCoords( data, -1 );
  2054. break;
  2055. case 'scale':
  2056. fixCoords( data, 1 );
  2057. break;
  2058. }
  2059. }
  2060. } else {
  2061. data = this.output[ ndx ];
  2062. }
  2063. return data;
  2064. };
  2065. function Key ( time ) {
  2066. this.targets = [];
  2067. this.time = time;
  2068. };
  2069. Key.prototype.addTarget = function ( fullSid, transform, member, data ) {
  2070. this.targets.push( {
  2071. sid: fullSid,
  2072. member: member,
  2073. transform: transform,
  2074. data: data
  2075. } );
  2076. };
  2077. Key.prototype.apply = function ( opt_sid ) {
  2078. for ( var i = 0; i < this.targets.length; ++i ) {
  2079. var target = this.targets[ i ];
  2080. if ( !opt_sid || target.sid === opt_sid ) {
  2081. target.transform.update( target.data, target.member );
  2082. }
  2083. }
  2084. };
  2085. Key.prototype.getTarget = function ( fullSid ) {
  2086. for ( var i = 0; i < this.targets.length; ++i ) {
  2087. if ( this.targets[ i ].sid === fullSid ) {
  2088. return this.targets[ i ];
  2089. }
  2090. }
  2091. return null;
  2092. };
  2093. Key.prototype.hasTarget = function ( fullSid ) {
  2094. for ( var i = 0; i < this.targets.length; ++i ) {
  2095. if ( this.targets[ i ].sid === fullSid ) {
  2096. return true;
  2097. }
  2098. }
  2099. return false;
  2100. };
  2101. // TODO: Currently only doing linear interpolation. Should support full COLLADA spec.
  2102. Key.prototype.interpolate = function ( nextKey, time ) {
  2103. for ( var i = 0; i < this.targets.length; ++i ) {
  2104. var target = this.targets[ i ],
  2105. nextTarget = nextKey.getTarget( target.sid ),
  2106. data;
  2107. if ( nextTarget ) {
  2108. var scale = ( time - this.time ) / ( nextKey.time - this.time ),
  2109. nextData = nextTarget.data,
  2110. prevData = target.data;
  2111. // check scale error
  2112. if ( scale < 0 || scale > 1 ) {
  2113. console.log( "Key.interpolate: Warning! Scale out of bounds:" + scale );
  2114. scale = scale < 0 ? 0 : 1;
  2115. }
  2116. if ( prevData.length ) {
  2117. data = [];
  2118. for ( var j = 0; j < prevData.length; ++j ) {
  2119. data[ j ] = prevData[ j ] + ( nextData[ j ] - prevData[ j ] ) * scale;
  2120. }
  2121. } else {
  2122. data = prevData + ( nextData - prevData ) * scale;
  2123. }
  2124. } else {
  2125. data = target.data;
  2126. }
  2127. target.transform.update( data, target.member );
  2128. }
  2129. };
  2130. function _source ( element ) {
  2131. var id = element.getAttribute( 'id' );
  2132. if ( sources[ id ] != undefined ) {
  2133. return sources[ id ];
  2134. }
  2135. sources[ id ] = ( new Source(id )).parse( element );
  2136. return sources[ id ];
  2137. };
  2138. function _nsResolver ( nsPrefix ) {
  2139. if ( nsPrefix == "dae" ) {
  2140. return "http://www.collada.org/2005/11/COLLADASchema";
  2141. }
  2142. return null;
  2143. };
  2144. function _bools ( str ) {
  2145. var raw = _strings( str );
  2146. var data = [];
  2147. for ( var i = 0; i < raw.length; i ++ ) {
  2148. data.push( (raw[i] == 'true' || raw[i] == '1') ? true : false );
  2149. }
  2150. return data;
  2151. };
  2152. function _floats ( str ) {
  2153. var raw = _strings(str);
  2154. var data = [];
  2155. for ( var i = 0; i < raw.length; i ++ ) {
  2156. data.push( parseFloat( raw[ i ] ) );
  2157. }
  2158. return data;
  2159. };
  2160. function _ints ( str ) {
  2161. var raw = _strings( str );
  2162. var data = [];
  2163. for ( var i = 0; i < raw.length; i ++ ) {
  2164. data.push( parseInt( raw[ i ], 10 ) );
  2165. }
  2166. return data;
  2167. };
  2168. function _strings ( str ) {
  2169. return _trimString( str ).split( /\s+/ );
  2170. };
  2171. function _trimString ( str ) {
  2172. return str.replace( /^\s+/, "" ).replace( /\s+$/, "" );
  2173. };
  2174. function _attr_as_float ( element, name, defaultValue ) {
  2175. if ( element.hasAttribute( name ) ) {
  2176. return parseFloat( element.getAttribute( name ) );
  2177. } else {
  2178. return defaultValue;
  2179. }
  2180. };
  2181. function _attr_as_int ( element, name, defaultValue ) {
  2182. if ( element.hasAttribute( name ) ) {
  2183. return parseInt( element.getAttribute( name ), 10) ;
  2184. } else {
  2185. return defaultValue;
  2186. }
  2187. };
  2188. function _attr_as_string ( element, name, defaultValue ) {
  2189. if ( element.hasAttribute( name ) ) {
  2190. return element.getAttribute( name );
  2191. } else {
  2192. return defaultValue;
  2193. }
  2194. };
  2195. function _format_float ( f, num ) {
  2196. if ( f === undefined ) {
  2197. var s = '0.';
  2198. while ( s.length < num + 2 ) {
  2199. s += '0';
  2200. }
  2201. return s;
  2202. }
  2203. num = num || 2;
  2204. var parts = f.toString().split( '.' );
  2205. parts[ 1 ] = parts.length > 1 ? parts[ 1 ].substr( 0, num ) : "0";
  2206. while( parts[ 1 ].length < num ) {
  2207. parts[ 1 ] += '0';
  2208. }
  2209. return parts.join( '.' );
  2210. };
  2211. function evaluateXPath ( node, query ) {
  2212. var instances = COLLADA.evaluate( query, node, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
  2213. var inst = instances.iterateNext();
  2214. var result = [];
  2215. while ( inst ) {
  2216. result.push( inst );
  2217. inst = instances.iterateNext();
  2218. }
  2219. return result;
  2220. };
  2221. // Up axis conversion
  2222. function setUpConversion () {
  2223. if ( !options.convertUpAxis || colladaUp === options.upAxis ) {
  2224. upConversion = null;
  2225. } else {
  2226. switch ( colladaUp ) {
  2227. case 'X':
  2228. upConversion = options.upAxis === 'Y' ? 'XtoY' : 'XtoZ';
  2229. break;
  2230. case 'Y':
  2231. upConversion = options.upAxis === 'X' ? 'YtoX' : 'YtoZ';
  2232. break;
  2233. case 'Z':
  2234. upConversion = options.upAxis === 'X' ? 'ZtoX' : 'ZtoY';
  2235. break;
  2236. }
  2237. }
  2238. };
  2239. function fixCoords ( data, sign ) {
  2240. if ( !options.convertUpAxis || colladaUp === options.upAxis ) {
  2241. return;
  2242. }
  2243. switch ( upConversion ) {
  2244. case 'XtoY':
  2245. var tmp = data[ 0 ];
  2246. data[ 0 ] = sign * data[ 1 ];
  2247. data[ 1 ] = tmp;
  2248. break;
  2249. case 'XtoZ':
  2250. var tmp = data[ 2 ];
  2251. data[ 2 ] = data[ 1 ];
  2252. data[ 1 ] = data[ 0 ];
  2253. data[ 0 ] = tmp;
  2254. break;
  2255. case 'YtoX':
  2256. var tmp = data[ 0 ];
  2257. data[ 0 ] = data[ 1 ];
  2258. data[ 1 ] = sign * tmp;
  2259. break;
  2260. case 'YtoZ':
  2261. var tmp = data[ 1 ];
  2262. data[ 1 ] = sign * data[ 2 ];
  2263. data[ 2 ] = tmp;
  2264. break;
  2265. case 'ZtoX':
  2266. var tmp = data[ 0 ];
  2267. data[ 0 ] = data[ 1 ];
  2268. data[ 1 ] = data[ 2 ];
  2269. data[ 2 ] = tmp;
  2270. break;
  2271. case 'ZtoY':
  2272. var tmp = data[ 1 ];
  2273. data[ 1 ] = data[ 2 ];
  2274. data[ 2 ] = sign * tmp;
  2275. break;
  2276. }
  2277. };
  2278. function getConvertedVec3 ( data, offset ) {
  2279. var arr = [ data[ offset ], data[ offset + 1 ], data[ offset + 2 ] ];
  2280. fixCoords( arr, -1 );
  2281. return new THREE.Vector3( arr[ 0 ], arr[ 1 ], arr[ 2 ] );
  2282. };
  2283. function getConvertedMat4 ( data ) {
  2284. if ( options.convertUpAxis ) {
  2285. // First fix rotation and scale
  2286. // Columns first
  2287. var arr = [ data[ 0 ], data[ 4 ], data[ 8 ] ];
  2288. fixCoords( arr, -1 );
  2289. data[ 0 ] = arr[ 0 ];
  2290. data[ 4 ] = arr[ 1 ];
  2291. data[ 8 ] = arr[ 2 ];
  2292. arr = [ data[ 1 ], data[ 5 ], data[ 9 ] ];
  2293. fixCoords( arr, -1 );
  2294. data[ 1 ] = arr[ 0 ];
  2295. data[ 5 ] = arr[ 1 ];
  2296. data[ 9 ] = arr[ 2 ];
  2297. arr = [ data[ 2 ], data[ 6 ], data[ 10 ] ];
  2298. fixCoords( arr, -1 );
  2299. data[ 2 ] = arr[ 0 ];
  2300. data[ 6 ] = arr[ 1 ];
  2301. data[ 10 ] = arr[ 2 ];
  2302. // Rows second
  2303. arr = [ data[ 0 ], data[ 1 ], data[ 2 ] ];
  2304. fixCoords( arr, -1 );
  2305. data[ 0 ] = arr[ 0 ];
  2306. data[ 1 ] = arr[ 1 ];
  2307. data[ 2 ] = arr[ 2 ];
  2308. arr = [ data[ 4 ], data[ 5 ], data[ 6 ] ];
  2309. fixCoords( arr, -1 );
  2310. data[ 4 ] = arr[ 0 ];
  2311. data[ 5 ] = arr[ 1 ];
  2312. data[ 6 ] = arr[ 2 ];
  2313. arr = [ data[ 8 ], data[ 9 ], data[ 10 ] ];
  2314. fixCoords( arr, -1 );
  2315. data[ 8 ] = arr[ 0 ];
  2316. data[ 9 ] = arr[ 1 ];
  2317. data[ 10 ] = arr[ 2 ];
  2318. // Now fix translation
  2319. arr = [ data[ 3 ], data[ 7 ], data[ 11 ] ];
  2320. fixCoords( arr, -1 );
  2321. data[ 3 ] = arr[ 0 ];
  2322. data[ 7 ] = arr[ 1 ];
  2323. data[ 11 ] = arr[ 2 ];
  2324. }
  2325. return new THREE.Matrix4(
  2326. data[0], data[1], data[2], data[3],
  2327. data[4], data[5], data[6], data[7],
  2328. data[8], data[9], data[10], data[11],
  2329. data[12], data[13], data[14], data[15]
  2330. );
  2331. };
  2332. function getConvertedMember ( member ) {
  2333. if ( options.convertUpAxis ) {
  2334. switch ( member ) {
  2335. case 'X':
  2336. switch ( upConversion ) {
  2337. case 'XtoY':
  2338. case 'XtoZ':
  2339. case 'YtoX':
  2340. member = 'Y';
  2341. break;
  2342. case 'ZtoX':
  2343. member = 'Z';
  2344. break;
  2345. }
  2346. break;
  2347. case 'Y':
  2348. switch ( upConversion ) {
  2349. case 'XtoY':
  2350. case 'YtoX':
  2351. case 'ZtoX':
  2352. member = 'X';
  2353. break;
  2354. case 'XtoZ':
  2355. case 'YtoZ':
  2356. case 'ZtoY':
  2357. member = 'Z';
  2358. break;
  2359. }
  2360. break;
  2361. case 'Z':
  2362. switch ( upConversion ) {
  2363. case 'XtoZ':
  2364. member = 'X';
  2365. break;
  2366. case 'YtoZ':
  2367. case 'ZtoX':
  2368. case 'ZtoY':
  2369. member = 'Y';
  2370. break;
  2371. }
  2372. break;
  2373. }
  2374. }
  2375. return member;
  2376. };
  2377. return {
  2378. load: load,
  2379. parse: parse,
  2380. setPreferredShading: setPreferredShading,
  2381. applySkin: applySkin,
  2382. geometries : geometries,
  2383. options: options
  2384. };
  2385. };
粤ICP备19079148号