ColladaLoader.js 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001
  1. /**
  2. * @author Tim Knip / http://www.floorplanner.com/ / tim at floorplanner.com
  3. * @author Tony Parisi / http://www.tonyparisi.com/
  4. */
  5. THREE.ColladaLoader = function () {
  6. var COLLADA = null;
  7. var scene = null;
  8. var daeScene;
  9. var readyCallbackFunc = null;
  10. var sources = {};
  11. var images = {};
  12. var animations = {};
  13. var controllers = {};
  14. var geometries = {};
  15. var materials = {};
  16. var effects = {};
  17. var cameras = {};
  18. var lights = {};
  19. var animData;
  20. var visualScenes;
  21. var baseUrl;
  22. var morphs;
  23. var skins;
  24. var flip_uv = true;
  25. var preferredShading = THREE.SmoothShading;
  26. var options = {
  27. // Force Geometry to always be centered at the local origin of the
  28. // containing Mesh.
  29. centerGeometry: false,
  30. // Axis conversion is done for geometries, animations, and controllers.
  31. // If we ever pull cameras or lights out of the COLLADA file, they'll
  32. // need extra work.
  33. convertUpAxis: false,
  34. subdivideFaces: true,
  35. upAxis: 'Y',
  36. // For reflective or refractive materials we'll use this cubemap
  37. defaultEnvMap: null
  38. };
  39. var colladaUnit = 1.0;
  40. var colladaUp = 'Y';
  41. var upConversion = null;
  42. function load ( url, readyCallback, progressCallback ) {
  43. var length = 0;
  44. if ( document.implementation && document.implementation.createDocument ) {
  45. var request = new XMLHttpRequest();
  46. request.onreadystatechange = function() {
  47. if( request.readyState == 4 ) {
  48. if( request.status == 0 || request.status == 200 ) {
  49. if ( request.responseXML ) {
  50. readyCallbackFunc = readyCallback;
  51. parse( request.responseXML, undefined, url );
  52. } else if ( request.responseText ) {
  53. readyCallbackFunc = readyCallback;
  54. var xmlParser = new DOMParser();
  55. var responseXML = xmlParser.parseFromString( request.responseText, "application/xml" );
  56. parse( responseXML, undefined, url );
  57. } else {
  58. console.error( "ColladaLoader: Empty or non-existing file (" + url + ")" );
  59. }
  60. }
  61. } else if ( request.readyState == 3 ) {
  62. if ( progressCallback ) {
  63. if ( length == 0 ) {
  64. length = request.getResponseHeader( "Content-Length" );
  65. }
  66. progressCallback( { total: length, loaded: request.responseText.length } );
  67. }
  68. }
  69. }
  70. request.open( "GET", url, true );
  71. request.send( null );
  72. } else {
  73. alert( "Don't know how to parse XML!" );
  74. }
  75. }
  76. function parse( doc, callBack, url ) {
  77. COLLADA = doc;
  78. callBack = callBack || readyCallbackFunc;
  79. if ( url !== undefined ) {
  80. var parts = url.split( '/' );
  81. parts.pop();
  82. baseUrl = ( parts.length < 1 ? '.' : parts.join( '/' ) ) + '/';
  83. }
  84. parseAsset();
  85. setUpConversion();
  86. images = parseLib( "library_images image", _Image, "image" );
  87. materials = parseLib( "library_materials material", Material, "material" );
  88. effects = parseLib( "library_effects effect", Effect, "effect" );
  89. geometries = parseLib( "library_geometries geometry", Geometry, "geometry" );
  90. cameras = parseLib( "library_cameras camera", Camera, "camera" );
  91. lights = parseLib( "library_lights light", Light, "light" );
  92. controllers = parseLib( "library_controllers controller", Controller, "controller" );
  93. animations = parseLib( "library_animations animation", Animation, "animation" );
  94. visualScenes = parseLib( "library_visual_scenes visual_scene", VisualScene, "visual_scene" );
  95. morphs = [];
  96. skins = [];
  97. daeScene = parseScene();
  98. scene = new THREE.Object3D();
  99. for ( var i = 0; i < daeScene.nodes.length; i ++ ) {
  100. scene.add( createSceneGraph( daeScene.nodes[ i ] ) );
  101. }
  102. // unit conversion
  103. scene.scale.multiplyScalar( colladaUnit );
  104. createAnimations();
  105. var result = {
  106. scene: scene,
  107. morphs: morphs,
  108. skins: skins,
  109. animations: animData,
  110. dae: {
  111. images: images,
  112. materials: materials,
  113. cameras: cameras,
  114. lights: lights,
  115. effects: effects,
  116. geometries: geometries,
  117. controllers: controllers,
  118. animations: animations,
  119. visualScenes: visualScenes,
  120. scene: daeScene
  121. }
  122. };
  123. if ( callBack ) {
  124. callBack( result );
  125. }
  126. return result;
  127. }
  128. function setPreferredShading ( shading ) {
  129. preferredShading = shading;
  130. }
  131. function parseAsset () {
  132. var elements = COLLADA.querySelectorAll('asset');
  133. var element = elements[0];
  134. if ( element && element.childNodes ) {
  135. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  136. var child = element.childNodes[ i ];
  137. switch ( child.nodeName ) {
  138. case 'unit':
  139. var meter = child.getAttribute( 'meter' );
  140. if ( meter ) {
  141. colladaUnit = parseFloat( meter );
  142. }
  143. break;
  144. case 'up_axis':
  145. colladaUp = child.textContent.charAt(0);
  146. break;
  147. }
  148. }
  149. }
  150. }
  151. function parseLib ( q, classSpec, prefix ) {
  152. var elements = COLLADA.querySelectorAll(q);
  153. var lib = {};
  154. var i = 0;
  155. var elementsLength = elements.length;
  156. for ( var j = 0; j < elementsLength; j ++ ) {
  157. var element = elements[j];
  158. var daeElement = ( new classSpec() ).parse( element );
  159. if ( !daeElement.id || daeElement.id.length == 0 ) daeElement.id = prefix + ( i ++ );
  160. lib[ daeElement.id ] = daeElement;
  161. }
  162. return lib;
  163. }
  164. function parseScene() {
  165. var sceneElement = COLLADA.querySelectorAll('scene instance_visual_scene')[0];
  166. if ( sceneElement ) {
  167. var url = sceneElement.getAttribute( 'url' ).replace( /^#/, '' );
  168. return visualScenes[ url.length > 0 ? url : 'visual_scene0' ];
  169. } else {
  170. return null;
  171. }
  172. }
  173. function createAnimations() {
  174. animData = [];
  175. // fill in the keys
  176. recurseHierarchy( scene );
  177. }
  178. function recurseHierarchy( node ) {
  179. var n = daeScene.getChildById( node.name, true ),
  180. newData = null;
  181. if ( n && n.keys ) {
  182. newData = {
  183. fps: 60,
  184. hierarchy: [ {
  185. node: n,
  186. keys: n.keys,
  187. sids: n.sids
  188. } ],
  189. node: node,
  190. name: 'animation_' + node.name,
  191. length: 0
  192. };
  193. animData.push(newData);
  194. for ( var i = 0, il = n.keys.length; i < il; i++ ) {
  195. newData.length = Math.max( newData.length, n.keys[i].time );
  196. }
  197. } else {
  198. newData = {
  199. hierarchy: [ {
  200. keys: [],
  201. sids: []
  202. } ]
  203. }
  204. }
  205. for ( var i = 0, il = node.children.length; i < il; i++ ) {
  206. var d = recurseHierarchy( node.children[i] );
  207. for ( var j = 0, jl = d.hierarchy.length; j < jl; j ++ ) {
  208. newData.hierarchy.push( {
  209. keys: [],
  210. sids: []
  211. } );
  212. }
  213. }
  214. return newData;
  215. }
  216. function calcAnimationBounds () {
  217. var start = 1000000;
  218. var end = -start;
  219. var frames = 0;
  220. var ID;
  221. for ( var id in animations ) {
  222. var animation = animations[ id ];
  223. ID = ID || animation.id;
  224. for ( var i = 0; i < animation.sampler.length; i ++ ) {
  225. var sampler = animation.sampler[ i ];
  226. sampler.create();
  227. start = Math.min( start, sampler.startTime );
  228. end = Math.max( end, sampler.endTime );
  229. frames = Math.max( frames, sampler.input.length );
  230. }
  231. }
  232. return { start:start, end:end, frames:frames,ID:ID };
  233. }
  234. function createMorph ( geometry, ctrl ) {
  235. var morphCtrl = ctrl instanceof InstanceController ? controllers[ ctrl.url ] : ctrl;
  236. if ( !morphCtrl || !morphCtrl.morph ) {
  237. console.log("could not find morph controller!");
  238. return;
  239. }
  240. var morph = morphCtrl.morph;
  241. for ( var i = 0; i < morph.targets.length; i ++ ) {
  242. var target_id = morph.targets[ i ];
  243. var daeGeometry = geometries[ target_id ];
  244. if ( !daeGeometry.mesh ||
  245. !daeGeometry.mesh.primitives ||
  246. !daeGeometry.mesh.primitives.length ) {
  247. continue;
  248. }
  249. var target = daeGeometry.mesh.primitives[ 0 ].geometry;
  250. if ( target.vertices.length === geometry.vertices.length ) {
  251. geometry.morphTargets.push( { name: "target_1", vertices: target.vertices } );
  252. }
  253. }
  254. geometry.morphTargets.push( { name: "target_Z", vertices: geometry.vertices } );
  255. };
  256. function createSkin ( geometry, ctrl, applyBindShape ) {
  257. var skinCtrl = controllers[ ctrl.url ];
  258. if ( !skinCtrl || !skinCtrl.skin ) {
  259. console.log( "could not find skin controller!" );
  260. return;
  261. }
  262. if ( !ctrl.skeleton || !ctrl.skeleton.length ) {
  263. console.log( "could not find the skeleton for the skin!" );
  264. return;
  265. }
  266. var skin = skinCtrl.skin;
  267. var skeleton = daeScene.getChildById( ctrl.skeleton[ 0 ] );
  268. var hierarchy = [];
  269. applyBindShape = applyBindShape !== undefined ? applyBindShape : true;
  270. var bones = [];
  271. geometry.skinWeights = [];
  272. geometry.skinIndices = [];
  273. //createBones( geometry.bones, skin, hierarchy, skeleton, null, -1 );
  274. //createWeights( skin, geometry.bones, geometry.skinIndices, geometry.skinWeights );
  275. /*
  276. geometry.animation = {
  277. name: 'take_001',
  278. fps: 30,
  279. length: 2,
  280. JIT: true,
  281. hierarchy: hierarchy
  282. };
  283. */
  284. if ( applyBindShape ) {
  285. for ( var i = 0; i < geometry.vertices.length; i ++ ) {
  286. geometry.vertices[ i ].applyMatrix4( skin.bindShapeMatrix );
  287. }
  288. }
  289. }
  290. function setupSkeleton ( node, bones, frame, parent ) {
  291. node.world = node.world || new THREE.Matrix4();
  292. node.localworld = node.localworld || new THREE.Matrix4();
  293. node.world.copy( node.matrix );
  294. node.localworld.copy( node.matrix );
  295. if ( node.channels && node.channels.length ) {
  296. var channel = node.channels[ 0 ];
  297. var m = channel.sampler.output[ frame ];
  298. if ( m instanceof THREE.Matrix4 ) {
  299. node.world.copy( m );
  300. node.localworld.copy(m);
  301. if(frame == 0)
  302. node.matrix.copy(m);
  303. }
  304. }
  305. if ( parent ) {
  306. node.world.multiplyMatrices( parent, node.world );
  307. }
  308. bones.push( node );
  309. for ( var i = 0; i < node.nodes.length; i ++ ) {
  310. setupSkeleton( node.nodes[ i ], bones, frame, node.world );
  311. }
  312. }
  313. function setupSkinningMatrices ( bones, skin ) {
  314. // FIXME: this is dumb...
  315. for ( var i = 0; i < bones.length; i ++ ) {
  316. var bone = bones[ i ];
  317. var found = -1;
  318. if ( bone.type != 'JOINT' ) continue;
  319. for ( var j = 0; j < skin.joints.length; j ++ ) {
  320. if ( bone.sid == skin.joints[ j ] ) {
  321. found = j;
  322. break;
  323. }
  324. }
  325. if ( found >= 0 ) {
  326. var inv = skin.invBindMatrices[ found ];
  327. bone.invBindMatrix = inv;
  328. bone.skinningMatrix = new THREE.Matrix4();
  329. bone.skinningMatrix.multiplyMatrices(bone.world, inv); // (IBMi * JMi)
  330. bone.animatrix = new THREE.Matrix4();
  331. bone.animatrix.copy(bone.localworld);
  332. bone.weights = [];
  333. for ( var j = 0; j < skin.weights.length; j ++ ) {
  334. for (var k = 0; k < skin.weights[ j ].length; k ++ ) {
  335. var w = skin.weights[ j ][ k ];
  336. if ( w.joint == found ) {
  337. bone.weights.push( w );
  338. }
  339. }
  340. }
  341. } else {
  342. console.warn( "ColladaLoader: Could not find joint '" + bone.sid + "'." );
  343. bone.skinningMatrix = new THREE.Matrix4();
  344. bone.weights = [];
  345. }
  346. }
  347. }
  348. //Walk the Collada tree and flatten the bones into a list, extract the position, quat and scale from the matrix
  349. function flattenSkeleton(skeleton) {
  350. var list = [];
  351. var walk = function(parentid, node, list) {
  352. var bone = {};
  353. bone.name = node.sid;
  354. bone.parent = parentid;
  355. bone.matrix = node.matrix;
  356. var data = [new THREE.Vector3(),new THREE.Quaternion(),new THREE.Vector3()];
  357. bone.matrix.decompose(data[0],data[1],data[2]);
  358. bone.pos = [data[0].x,data[0].y,data[0].z];
  359. bone.scl = [data[2].x,data[2].y,data[2].z];
  360. bone.rotq = [data[1].x,data[1].y,data[1].z,data[1].w];
  361. list.push(bone);
  362. for(var i in node.nodes) {
  363. walk(node.sid,node.nodes[i],list);
  364. }
  365. };
  366. walk(-1,skeleton,list);
  367. return list;
  368. }
  369. //Move the vertices into the pose that is proper for the start of the animation
  370. function skinToBindPose(geometry,skeleton,skinController) {
  371. var bones = [];
  372. setupSkeleton( skeleton, bones, -1 );
  373. setupSkinningMatrices( bones, skinController.skin );
  374. v = new THREE.Vector3();
  375. var skinned = [];
  376. for(var i =0; i < geometry.vertices.length; i++) {
  377. skinned.push(new THREE.Vector3());
  378. }
  379. for ( i = 0; i < bones.length; i ++ ) {
  380. if ( bones[ i ].type != 'JOINT' ) continue;
  381. for ( j = 0; j < bones[ i ].weights.length; j ++ ) {
  382. w = bones[ i ].weights[ j ];
  383. vidx = w.index;
  384. weight = w.weight;
  385. o = geometry.vertices[vidx];
  386. s = skinned[vidx];
  387. v.x = o.x;
  388. v.y = o.y;
  389. v.z = o.z;
  390. v.applyMatrix4( bones[i].skinningMatrix );
  391. s.x += (v.x * weight);
  392. s.y += (v.y * weight);
  393. s.z += (v.z * weight);
  394. }
  395. }
  396. for(var i =0; i < geometry.vertices.length; i++) {
  397. geometry.vertices[i] = skinned[i];
  398. }
  399. }
  400. function applySkin ( geometry, instanceCtrl, frame ) {
  401. // TODO: get this from the renderer or options
  402. var maxbones = 30;
  403. var skinController = controllers[ instanceCtrl.url ];
  404. frame = frame !== undefined ? frame : 40;
  405. if ( !skinController || !skinController.skin ) {
  406. console.log( 'ColladaLoader: Could not find skin controller.' );
  407. return;
  408. }
  409. if ( !instanceCtrl.skeleton || !instanceCtrl.skeleton.length ) {
  410. console.log( 'ColladaLoader: Could not find the skeleton for the skin. ' );
  411. return;
  412. }
  413. var animationBounds = calcAnimationBounds();
  414. var skeleton = daeScene.getChildById( instanceCtrl.skeleton[0], true ) ||
  415. daeScene.getChildBySid( instanceCtrl.skeleton[0], true );
  416. //flatten the skeleton into a list of bones
  417. var bonelist = flattenSkeleton(skeleton);
  418. var joints = skinController.skin.joints;
  419. //sort that list so that the order reflects the order in the joint list
  420. var sortedbones = [];
  421. for(var i = 0; i < joints.length; i++) {
  422. for(var j =0; j < bonelist.length; j++) {
  423. if(bonelist[j].name == joints[i]) {
  424. sortedbones[i] = bonelist[j];
  425. }
  426. }
  427. }
  428. //hook up the parents by index instead of name
  429. for(var i = 0; i < sortedbones.length; i++) {
  430. for(var j =0; j < sortedbones.length; j++) {
  431. if(sortedbones[i].parent == sortedbones[j].name) {
  432. sortedbones[i].parent = j;
  433. }
  434. }
  435. }
  436. var i, j, w, vidx, weight;
  437. var v = new THREE.Vector3(), o, s;
  438. // move vertices to bind shape
  439. for ( i = 0; i < geometry.vertices.length; i ++ ) {
  440. geometry.vertices[i].applyMatrix4( skinController.skin.bindShapeMatrix );
  441. }
  442. var skinIndices = [];
  443. var skinWeights = [];
  444. var weights = skinController.skin.weights;
  445. //hook up the skin weights
  446. // TODO - this might be a good place to choose greatest 4 weights
  447. for(var i =0; i < weights.length; i++) {
  448. var indicies = new THREE.Vector4(weights[i][0]?weights[i][0].joint:0,weights[i][1]?weights[i][1].joint:0,weights[i][2]?weights[i][2].joint:0,weights[i][3]?weights[i][3].joint:0);
  449. var weight = new THREE.Vector4(weights[i][0]?weights[i][0].weight:0,weights[i][1]?weights[i][1].weight:0,weights[i][2]?weights[i][2].weight:0,weights[i][3]?weights[i][3].weight:0);
  450. skinIndices.push(indicies);
  451. skinWeights.push(weight);
  452. }
  453. geometry.skinIndices = skinIndices;
  454. geometry.skinWeights = skinWeights;
  455. geometry.bones = sortedbones;
  456. // process animation, or simply pose the rig if no animation
  457. //create an animation for the animated bones
  458. //NOTE: this has no effect when using morphtargets
  459. var animationdata = {"name":animationBounds.ID,"fps":30,"length":animationBounds.frames/30,"hierarchy":[]};
  460. for(var j =0; j < sortedbones.length; j++) {
  461. animationdata.hierarchy.push({parent:sortedbones[j].parent, name:sortedbones[j].name, keys:[]});
  462. }
  463. //if using hardware skinning, move the vertices into the binding pose
  464. if(sortedbones.length < maxbones) {
  465. skinToBindPose(geometry,skeleton,skinController);
  466. }
  467. for ( frame = 0; frame < animationBounds.frames; frame ++ ) {
  468. var bones = [];
  469. var skinned = [];
  470. // process the frame and setup the rig with a fresh
  471. // transform, possibly from the bone's animation channel(s)
  472. setupSkeleton( skeleton, bones, frame );
  473. setupSkinningMatrices( bones, skinController.skin );
  474. //if using hardware skinning, just hook up the animiation data
  475. if(sortedbones.length < maxbones) {
  476. for(var i = 0; i < bones.length; i ++) {
  477. for(var j = 0; j < animationdata.hierarchy.length; j ++) {
  478. if(animationdata.hierarchy[j].name == bones[i].sid) {
  479. var key = {};
  480. key.time = (frame/30);
  481. key.matrix = bones[i].animatrix;
  482. if(frame == 0)
  483. bones[i].matrix = key.matrix;
  484. var data = [new THREE.Vector3(),new THREE.Quaternion(),new THREE.Vector3()];
  485. key.matrix.decompose(data[0],data[1],data[2]);
  486. key.pos = [data[0].x,data[0].y,data[0].z];
  487. key.scl = [data[2].x,data[2].y,data[2].z];
  488. key.rot = data[1];
  489. animationdata.hierarchy[j].keys.push(key);
  490. }
  491. }
  492. }
  493. geometry.animation = animationdata;
  494. } else {
  495. // otherwise, process the animation into morphtargets
  496. for ( i = 0; i < geometry.vertices.length; i++ ) {
  497. skinned.push( new THREE.Vector3() );
  498. }
  499. for ( i = 0; i < bones.length; i ++ ) {
  500. if ( bones[ i ].type != 'JOINT' ) continue;
  501. for ( j = 0; j < bones[ i ].weights.length; j ++ ) {
  502. w = bones[ i ].weights[ j ];
  503. vidx = w.index;
  504. weight = w.weight;
  505. o = geometry.vertices[vidx];
  506. s = skinned[vidx];
  507. v.x = o.x;
  508. v.y = o.y;
  509. v.z = o.z;
  510. v.applyMatrix4( bones[i].skinningMatrix );
  511. s.x += (v.x * weight);
  512. s.y += (v.y * weight);
  513. s.z += (v.z * weight);
  514. }
  515. }
  516. geometry.morphTargets.push( { name: "target_" + frame, vertices: skinned } );
  517. }
  518. }
  519. };
  520. function createSceneGraph ( node, parent ) {
  521. var obj = new THREE.Object3D();
  522. var skinned = false;
  523. var skinController;
  524. var morphController;
  525. var i, j;
  526. // FIXME: controllers
  527. for ( i = 0; i < node.controllers.length; i ++ ) {
  528. var controller = controllers[ node.controllers[ i ].url ];
  529. switch ( controller.type ) {
  530. case 'skin':
  531. if ( geometries[ controller.skin.source ] ) {
  532. var inst_geom = new InstanceGeometry();
  533. inst_geom.url = controller.skin.source;
  534. inst_geom.instance_material = node.controllers[ i ].instance_material;
  535. node.geometries.push( inst_geom );
  536. skinned = true;
  537. skinController = node.controllers[ i ];
  538. } else if ( controllers[ controller.skin.source ] ) {
  539. // urgh: controller can be chained
  540. // handle the most basic case...
  541. var second = controllers[ controller.skin.source ];
  542. morphController = second;
  543. // skinController = node.controllers[i];
  544. if ( second.morph && geometries[ second.morph.source ] ) {
  545. var inst_geom = new InstanceGeometry();
  546. inst_geom.url = second.morph.source;
  547. inst_geom.instance_material = node.controllers[ i ].instance_material;
  548. node.geometries.push( inst_geom );
  549. }
  550. }
  551. break;
  552. case 'morph':
  553. if ( geometries[ controller.morph.source ] ) {
  554. var inst_geom = new InstanceGeometry();
  555. inst_geom.url = controller.morph.source;
  556. inst_geom.instance_material = node.controllers[ i ].instance_material;
  557. node.geometries.push( inst_geom );
  558. morphController = node.controllers[ i ];
  559. }
  560. console.log( 'ColladaLoader: Morph-controller partially supported.' );
  561. default:
  562. break;
  563. }
  564. }
  565. // geometries
  566. var double_sided_materials = {};
  567. for ( i = 0; i < node.geometries.length; i ++ ) {
  568. var instance_geometry = node.geometries[i];
  569. var instance_materials = instance_geometry.instance_material;
  570. var geometry = geometries[ instance_geometry.url ];
  571. var used_materials = {};
  572. var used_materials_array = [];
  573. var num_materials = 0;
  574. var first_material;
  575. if ( geometry ) {
  576. if ( !geometry.mesh || !geometry.mesh.primitives )
  577. continue;
  578. if ( obj.name.length == 0 ) {
  579. obj.name = geometry.id;
  580. }
  581. // collect used fx for this geometry-instance
  582. if ( instance_materials ) {
  583. for ( j = 0; j < instance_materials.length; j ++ ) {
  584. var instance_material = instance_materials[ j ];
  585. var mat = materials[ instance_material.target ];
  586. var effect_id = mat.instance_effect.url;
  587. var shader = effects[ effect_id ].shader;
  588. var material3js = shader.material;
  589. if ( geometry.doubleSided ) {
  590. if ( !( instance_material.symbol in double_sided_materials ) ) {
  591. var _copied_material = material3js.clone();
  592. _copied_material.side = THREE.DoubleSide;
  593. double_sided_materials[ instance_material.symbol ] = _copied_material;
  594. }
  595. material3js = double_sided_materials[ instance_material.symbol ];
  596. }
  597. material3js.opacity = !material3js.opacity ? 1 : material3js.opacity;
  598. used_materials[ instance_material.symbol ] = num_materials;
  599. used_materials_array.push( material3js );
  600. first_material = material3js;
  601. first_material.name = mat.name == null || mat.name === '' ? mat.id : mat.name;
  602. num_materials ++;
  603. }
  604. }
  605. var mesh;
  606. var material = first_material || new THREE.MeshLambertMaterial( { color: 0xdddddd, shading: THREE.FlatShading, side: geometry.doubleSided ? THREE.DoubleSide : THREE.FrontSide } );
  607. var geom = geometry.mesh.geometry3js;
  608. if ( num_materials > 1 ) {
  609. material = new THREE.MeshFaceMaterial( used_materials_array );
  610. for ( j = 0; j < geom.faces.length; j ++ ) {
  611. var face = geom.faces[ j ];
  612. face.materialIndex = used_materials[ face.daeMaterial ]
  613. }
  614. }
  615. if ( skinController !== undefined ) {
  616. applySkin( geom, skinController );
  617. if(geom.morphTargets.length > 0) {
  618. material.morphTargets = true;
  619. material.skinning = false;
  620. } else {
  621. material.morphTargets = false;
  622. material.skinning = true;
  623. }
  624. mesh = new THREE.SkinnedMesh( geom, material, false );
  625. //mesh.skeleton = skinController.skeleton;
  626. //mesh.skinController = controllers[ skinController.url ];
  627. //mesh.skinInstanceController = skinController;
  628. mesh.name = 'skin_' + skins.length;
  629. //mesh.animationHandle.setKey(0);
  630. skins.push( mesh );
  631. } else if ( morphController !== undefined ) {
  632. createMorph( geom, morphController );
  633. material.morphTargets = true;
  634. mesh = new THREE.Mesh( geom, material );
  635. mesh.name = 'morph_' + morphs.length;
  636. morphs.push( mesh );
  637. } else {
  638. mesh = new THREE.Mesh( geom, material );
  639. // mesh.geom.name = geometry.id;
  640. }
  641. // N.B.: TP says this is not a great default behavior. It's a nice
  642. // optimization to flatten the hierarchy but this should be done
  643. // only if requested by the user via a flag. For now I undid it
  644. // and fixed the character animation example that uses it
  645. // node.geometries.length > 1 ? obj.add( mesh ) : obj = mesh;
  646. obj.add(mesh);
  647. }
  648. }
  649. for ( i = 0; i < node.cameras.length; i ++ ) {
  650. var instance_camera = node.cameras[i];
  651. var cparams = cameras[instance_camera.url];
  652. var cam = new THREE.PerspectiveCamera(cparams.yfov, parseFloat(cparams.aspect_ratio),
  653. parseFloat(cparams.znear), parseFloat(cparams.zfar));
  654. obj.add(cam);
  655. }
  656. for ( i = 0; i < node.lights.length; i ++ ) {
  657. var light = null;
  658. var instance_light = node.lights[i];
  659. var lparams = lights[instance_light.url];
  660. if ( lparams && lparams.technique ) {
  661. var color = lparams.color.getHex();
  662. var intensity = lparams.intensity;
  663. var distance = 0;
  664. var angle = lparams.falloff_angle;
  665. var exponent; // Intentionally undefined, don't know what this is yet
  666. switch ( lparams.technique ) {
  667. case 'directional':
  668. light = new THREE.DirectionalLight( color, intensity, distance );
  669. light.position.set(0, 0, 1);
  670. break;
  671. case 'point':
  672. light = new THREE.PointLight( color, intensity, distance );
  673. break;
  674. case 'spot':
  675. light = new THREE.SpotLight( color, intensity, distance, angle, exponent );
  676. light.position.set(0, 0, 1);
  677. break;
  678. case 'ambient':
  679. light = new THREE.AmbientLight( color );
  680. break;
  681. }
  682. }
  683. if (light) {
  684. obj.add(light);
  685. }
  686. }
  687. obj.name = node.name || node.id || "";
  688. obj.layer = node.layer || "";
  689. obj.matrix = node.matrix;
  690. obj.matrix.decompose( obj.position, obj.quaternion, obj.scale );
  691. if ( options.centerGeometry && obj.geometry ) {
  692. var delta = THREE.GeometryUtils.center( obj.geometry );
  693. delta.multiply( obj.scale );
  694. delta.applyQuaternion( obj.quaternion );
  695. obj.position.sub( delta );
  696. }
  697. for ( i = 0; i < node.nodes.length; i ++ ) {
  698. obj.add( createSceneGraph( node.nodes[i], node ) );
  699. }
  700. return obj;
  701. };
  702. function getJointId( skin, id ) {
  703. for ( var i = 0; i < skin.joints.length; i ++ ) {
  704. if ( skin.joints[ i ] == id ) {
  705. return i;
  706. }
  707. }
  708. };
  709. function getLibraryNode( id ) {
  710. var nodes = COLLADA.querySelectorAll('library_nodes node');
  711. for ( var i = 0; i < nodes.length; i++ ) {
  712. var attObj = nodes[i].attributes.getNamedItem('id');
  713. if ( attObj && attObj.value === id ) {
  714. return nodes[i];
  715. }
  716. }
  717. return undefined;
  718. };
  719. function getChannelsForNode (node ) {
  720. var channels = [];
  721. var startTime = 1000000;
  722. var endTime = -1000000;
  723. for ( var id in animations ) {
  724. var animation = animations[id];
  725. for ( var i = 0; i < animation.channel.length; i ++ ) {
  726. var channel = animation.channel[i];
  727. var sampler = animation.sampler[i];
  728. var id = channel.target.split('/')[0];
  729. if ( id == node.id ) {
  730. sampler.create();
  731. channel.sampler = sampler;
  732. startTime = Math.min(startTime, sampler.startTime);
  733. endTime = Math.max(endTime, sampler.endTime);
  734. channels.push(channel);
  735. }
  736. }
  737. }
  738. if ( channels.length ) {
  739. node.startTime = startTime;
  740. node.endTime = endTime;
  741. }
  742. return channels;
  743. };
  744. function calcFrameDuration( node ) {
  745. var minT = 10000000;
  746. for ( var i = 0; i < node.channels.length; i ++ ) {
  747. var sampler = node.channels[i].sampler;
  748. for ( var j = 0; j < sampler.input.length - 1; j ++ ) {
  749. var t0 = sampler.input[ j ];
  750. var t1 = sampler.input[ j + 1 ];
  751. minT = Math.min( minT, t1 - t0 );
  752. }
  753. }
  754. return minT;
  755. };
  756. function calcMatrixAt( node, t ) {
  757. var animated = {};
  758. var i, j;
  759. for ( i = 0; i < node.channels.length; i ++ ) {
  760. var channel = node.channels[ i ];
  761. animated[ channel.sid ] = channel;
  762. }
  763. var matrix = new THREE.Matrix4();
  764. for ( i = 0; i < node.transforms.length; i ++ ) {
  765. var transform = node.transforms[ i ];
  766. var channel = animated[ transform.sid ];
  767. if ( channel !== undefined ) {
  768. var sampler = channel.sampler;
  769. var value;
  770. for ( j = 0; j < sampler.input.length - 1; j ++ ) {
  771. if ( sampler.input[ j + 1 ] > t ) {
  772. value = sampler.output[ j ];
  773. //console.log(value.flatten)
  774. break;
  775. }
  776. }
  777. if ( value !== undefined ) {
  778. if ( value instanceof THREE.Matrix4 ) {
  779. matrix.multiplyMatrices( matrix, value );
  780. } else {
  781. // FIXME: handle other types
  782. matrix.multiplyMatrices( matrix, transform.matrix );
  783. }
  784. } else {
  785. matrix.multiplyMatrices( matrix, transform.matrix );
  786. }
  787. } else {
  788. matrix.multiplyMatrices( matrix, transform.matrix );
  789. }
  790. }
  791. return matrix;
  792. };
  793. function bakeAnimations ( node ) {
  794. if ( node.channels && node.channels.length ) {
  795. var keys = [],
  796. sids = [];
  797. for ( var i = 0, il = node.channels.length; i < il; i++ ) {
  798. var channel = node.channels[i],
  799. fullSid = channel.fullSid,
  800. sampler = channel.sampler,
  801. input = sampler.input,
  802. transform = node.getTransformBySid( channel.sid ),
  803. member;
  804. if ( channel.arrIndices ) {
  805. member = [];
  806. for ( var j = 0, jl = channel.arrIndices.length; j < jl; j++ ) {
  807. member[ j ] = getConvertedIndex( channel.arrIndices[ j ] );
  808. }
  809. } else {
  810. member = getConvertedMember( channel.member );
  811. }
  812. if ( transform ) {
  813. if ( sids.indexOf( fullSid ) === -1 ) {
  814. sids.push( fullSid );
  815. }
  816. for ( var j = 0, jl = input.length; j < jl; j++ ) {
  817. var time = input[j],
  818. data = sampler.getData( transform.type, j, member ),
  819. key = findKey( keys, time );
  820. if ( !key ) {
  821. key = new Key( time );
  822. var timeNdx = findTimeNdx( keys, time );
  823. keys.splice( timeNdx == -1 ? keys.length : timeNdx, 0, key );
  824. }
  825. key.addTarget( fullSid, transform, member, data );
  826. }
  827. } else {
  828. console.log( 'Could not find transform "' + channel.sid + '" in node ' + node.id );
  829. }
  830. }
  831. // post process
  832. for ( var i = 0; i < sids.length; i++ ) {
  833. var sid = sids[ i ];
  834. for ( var j = 0; j < keys.length; j++ ) {
  835. var key = keys[ j ];
  836. if ( !key.hasTarget( sid ) ) {
  837. interpolateKeys( keys, key, j, sid );
  838. }
  839. }
  840. }
  841. node.keys = keys;
  842. node.sids = sids;
  843. }
  844. };
  845. function findKey ( keys, time) {
  846. var retVal = null;
  847. for ( var i = 0, il = keys.length; i < il && retVal == null; i++ ) {
  848. var key = keys[i];
  849. if ( key.time === time ) {
  850. retVal = key;
  851. } else if ( key.time > time ) {
  852. break;
  853. }
  854. }
  855. return retVal;
  856. };
  857. function findTimeNdx ( keys, time) {
  858. var ndx = -1;
  859. for ( var i = 0, il = keys.length; i < il && ndx == -1; i++ ) {
  860. var key = keys[i];
  861. if ( key.time >= time ) {
  862. ndx = i;
  863. }
  864. }
  865. return ndx;
  866. };
  867. function interpolateKeys ( keys, key, ndx, fullSid ) {
  868. var prevKey = getPrevKeyWith( keys, fullSid, ndx ? ndx-1 : 0 ),
  869. nextKey = getNextKeyWith( keys, fullSid, ndx+1 );
  870. if ( prevKey && nextKey ) {
  871. var scale = (key.time - prevKey.time) / (nextKey.time - prevKey.time),
  872. prevTarget = prevKey.getTarget( fullSid ),
  873. nextData = nextKey.getTarget( fullSid ).data,
  874. prevData = prevTarget.data,
  875. data;
  876. if ( prevTarget.type === 'matrix' ) {
  877. data = prevData;
  878. } else if ( prevData.length ) {
  879. data = [];
  880. for ( var i = 0; i < prevData.length; ++i ) {
  881. data[ i ] = prevData[ i ] + ( nextData[ i ] - prevData[ i ] ) * scale;
  882. }
  883. } else {
  884. data = prevData + ( nextData - prevData ) * scale;
  885. }
  886. key.addTarget( fullSid, prevTarget.transform, prevTarget.member, data );
  887. }
  888. };
  889. // Get next key with given sid
  890. function getNextKeyWith( keys, fullSid, ndx ) {
  891. for ( ; ndx < keys.length; ndx++ ) {
  892. var key = keys[ ndx ];
  893. if ( key.hasTarget( fullSid ) ) {
  894. return key;
  895. }
  896. }
  897. return null;
  898. };
  899. // Get previous key with given sid
  900. function getPrevKeyWith( keys, fullSid, ndx ) {
  901. ndx = ndx >= 0 ? ndx : ndx + keys.length;
  902. for ( ; ndx >= 0; ndx-- ) {
  903. var key = keys[ ndx ];
  904. if ( key.hasTarget( fullSid ) ) {
  905. return key;
  906. }
  907. }
  908. return null;
  909. };
  910. function _Image() {
  911. this.id = "";
  912. this.init_from = "";
  913. };
  914. _Image.prototype.parse = function(element) {
  915. this.id = element.getAttribute('id');
  916. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  917. var child = element.childNodes[ i ];
  918. if ( child.nodeName == 'init_from' ) {
  919. this.init_from = child.textContent;
  920. }
  921. }
  922. return this;
  923. };
  924. function Controller() {
  925. this.id = "";
  926. this.name = "";
  927. this.type = "";
  928. this.skin = null;
  929. this.morph = null;
  930. };
  931. Controller.prototype.parse = function( element ) {
  932. this.id = element.getAttribute('id');
  933. this.name = element.getAttribute('name');
  934. this.type = "none";
  935. for ( var i = 0; i < element.childNodes.length; i++ ) {
  936. var child = element.childNodes[ i ];
  937. switch ( child.nodeName ) {
  938. case 'skin':
  939. this.skin = (new Skin()).parse(child);
  940. this.type = child.nodeName;
  941. break;
  942. case 'morph':
  943. this.morph = (new Morph()).parse(child);
  944. this.type = child.nodeName;
  945. break;
  946. default:
  947. break;
  948. }
  949. }
  950. return this;
  951. };
  952. function Morph() {
  953. this.method = null;
  954. this.source = null;
  955. this.targets = null;
  956. this.weights = null;
  957. };
  958. Morph.prototype.parse = function( element ) {
  959. var sources = {};
  960. var inputs = [];
  961. var i;
  962. this.method = element.getAttribute( 'method' );
  963. this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
  964. for ( i = 0; i < element.childNodes.length; i ++ ) {
  965. var child = element.childNodes[ i ];
  966. if ( child.nodeType != 1 ) continue;
  967. switch ( child.nodeName ) {
  968. case 'source':
  969. var source = ( new Source() ).parse( child );
  970. sources[ source.id ] = source;
  971. break;
  972. case 'targets':
  973. inputs = this.parseInputs( child );
  974. break;
  975. default:
  976. console.log( child.nodeName );
  977. break;
  978. }
  979. }
  980. for ( i = 0; i < inputs.length; i ++ ) {
  981. var input = inputs[ i ];
  982. var source = sources[ input.source ];
  983. switch ( input.semantic ) {
  984. case 'MORPH_TARGET':
  985. this.targets = source.read();
  986. break;
  987. case 'MORPH_WEIGHT':
  988. this.weights = source.read();
  989. break;
  990. default:
  991. break;
  992. }
  993. }
  994. return this;
  995. };
  996. Morph.prototype.parseInputs = function(element) {
  997. var inputs = [];
  998. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  999. var child = element.childNodes[i];
  1000. if ( child.nodeType != 1) continue;
  1001. switch ( child.nodeName ) {
  1002. case 'input':
  1003. inputs.push( (new Input()).parse(child) );
  1004. break;
  1005. default:
  1006. break;
  1007. }
  1008. }
  1009. return inputs;
  1010. };
  1011. function Skin() {
  1012. this.source = "";
  1013. this.bindShapeMatrix = null;
  1014. this.invBindMatrices = [];
  1015. this.joints = [];
  1016. this.weights = [];
  1017. };
  1018. Skin.prototype.parse = function( element ) {
  1019. var sources = {};
  1020. var joints, weights;
  1021. this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
  1022. this.invBindMatrices = [];
  1023. this.joints = [];
  1024. this.weights = [];
  1025. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1026. var child = element.childNodes[i];
  1027. if ( child.nodeType != 1 ) continue;
  1028. switch ( child.nodeName ) {
  1029. case 'bind_shape_matrix':
  1030. var f = _floats(child.textContent);
  1031. this.bindShapeMatrix = getConvertedMat4( f );
  1032. break;
  1033. case 'source':
  1034. var src = new Source().parse(child);
  1035. sources[ src.id ] = src;
  1036. break;
  1037. case 'joints':
  1038. joints = child;
  1039. break;
  1040. case 'vertex_weights':
  1041. weights = child;
  1042. break;
  1043. default:
  1044. console.log( child.nodeName );
  1045. break;
  1046. }
  1047. }
  1048. this.parseJoints( joints, sources );
  1049. this.parseWeights( weights, sources );
  1050. return this;
  1051. };
  1052. Skin.prototype.parseJoints = function ( element, sources ) {
  1053. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1054. var child = element.childNodes[ i ];
  1055. if ( child.nodeType != 1 ) continue;
  1056. switch ( child.nodeName ) {
  1057. case 'input':
  1058. var input = ( new Input() ).parse( child );
  1059. var source = sources[ input.source ];
  1060. if ( input.semantic == 'JOINT' ) {
  1061. this.joints = source.read();
  1062. } else if ( input.semantic == 'INV_BIND_MATRIX' ) {
  1063. this.invBindMatrices = source.read();
  1064. }
  1065. break;
  1066. default:
  1067. break;
  1068. }
  1069. }
  1070. };
  1071. Skin.prototype.parseWeights = function ( element, sources ) {
  1072. var v, vcount, inputs = [];
  1073. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1074. var child = element.childNodes[ i ];
  1075. if ( child.nodeType != 1 ) continue;
  1076. switch ( child.nodeName ) {
  1077. case 'input':
  1078. inputs.push( ( new Input() ).parse( child ) );
  1079. break;
  1080. case 'v':
  1081. v = _ints( child.textContent );
  1082. break;
  1083. case 'vcount':
  1084. vcount = _ints( child.textContent );
  1085. break;
  1086. default:
  1087. break;
  1088. }
  1089. }
  1090. var index = 0;
  1091. for ( var i = 0; i < vcount.length; i ++ ) {
  1092. var numBones = vcount[i];
  1093. var vertex_weights = [];
  1094. for ( var j = 0; j < numBones; j++ ) {
  1095. var influence = {};
  1096. for ( var k = 0; k < inputs.length; k ++ ) {
  1097. var input = inputs[ k ];
  1098. var value = v[ index + input.offset ];
  1099. switch ( input.semantic ) {
  1100. case 'JOINT':
  1101. influence.joint = value;//this.joints[value];
  1102. break;
  1103. case 'WEIGHT':
  1104. influence.weight = sources[ input.source ].data[ value ];
  1105. break;
  1106. default:
  1107. break;
  1108. }
  1109. }
  1110. vertex_weights.push( influence );
  1111. index += inputs.length;
  1112. }
  1113. for ( var j = 0; j < vertex_weights.length; j ++ ) {
  1114. vertex_weights[ j ].index = i;
  1115. }
  1116. this.weights.push( vertex_weights );
  1117. }
  1118. };
  1119. function VisualScene () {
  1120. this.id = "";
  1121. this.name = "";
  1122. this.nodes = [];
  1123. this.scene = new THREE.Object3D();
  1124. };
  1125. VisualScene.prototype.getChildById = function( id, recursive ) {
  1126. for ( var i = 0; i < this.nodes.length; i ++ ) {
  1127. var node = this.nodes[ i ].getChildById( id, recursive );
  1128. if ( node ) {
  1129. return node;
  1130. }
  1131. }
  1132. return null;
  1133. };
  1134. VisualScene.prototype.getChildBySid = function( sid, recursive ) {
  1135. for ( var i = 0; i < this.nodes.length; i ++ ) {
  1136. var node = this.nodes[ i ].getChildBySid( sid, recursive );
  1137. if ( node ) {
  1138. return node;
  1139. }
  1140. }
  1141. return null;
  1142. };
  1143. VisualScene.prototype.parse = function( element ) {
  1144. this.id = element.getAttribute( 'id' );
  1145. this.name = element.getAttribute( 'name' );
  1146. this.nodes = [];
  1147. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1148. var child = element.childNodes[ i ];
  1149. if ( child.nodeType != 1 ) continue;
  1150. switch ( child.nodeName ) {
  1151. case 'node':
  1152. this.nodes.push( ( new Node() ).parse( child ) );
  1153. break;
  1154. default:
  1155. break;
  1156. }
  1157. }
  1158. return this;
  1159. };
  1160. function Node() {
  1161. this.id = "";
  1162. this.name = "";
  1163. this.sid = "";
  1164. this.nodes = [];
  1165. this.controllers = [];
  1166. this.transforms = [];
  1167. this.geometries = [];
  1168. this.channels = [];
  1169. this.matrix = new THREE.Matrix4();
  1170. };
  1171. Node.prototype.getChannelForTransform = function( transformSid ) {
  1172. for ( var i = 0; i < this.channels.length; i ++ ) {
  1173. var channel = this.channels[i];
  1174. var parts = channel.target.split('/');
  1175. var id = parts.shift();
  1176. var sid = parts.shift();
  1177. var dotSyntax = (sid.indexOf(".") >= 0);
  1178. var arrSyntax = (sid.indexOf("(") >= 0);
  1179. var arrIndices;
  1180. var member;
  1181. if ( dotSyntax ) {
  1182. parts = sid.split(".");
  1183. sid = parts.shift();
  1184. member = parts.shift();
  1185. } else if ( arrSyntax ) {
  1186. arrIndices = sid.split("(");
  1187. sid = arrIndices.shift();
  1188. for ( var j = 0; j < arrIndices.length; j ++ ) {
  1189. arrIndices[ j ] = parseInt( arrIndices[ j ].replace( /\)/, '' ) );
  1190. }
  1191. }
  1192. if ( sid == transformSid ) {
  1193. channel.info = { sid: sid, dotSyntax: dotSyntax, arrSyntax: arrSyntax, arrIndices: arrIndices };
  1194. return channel;
  1195. }
  1196. }
  1197. return null;
  1198. };
  1199. Node.prototype.getChildById = function ( id, recursive ) {
  1200. if ( this.id == id ) {
  1201. return this;
  1202. }
  1203. if ( recursive ) {
  1204. for ( var i = 0; i < this.nodes.length; i ++ ) {
  1205. var n = this.nodes[ i ].getChildById( id, recursive );
  1206. if ( n ) {
  1207. return n;
  1208. }
  1209. }
  1210. }
  1211. return null;
  1212. };
  1213. Node.prototype.getChildBySid = function ( sid, recursive ) {
  1214. if ( this.sid == sid ) {
  1215. return this;
  1216. }
  1217. if ( recursive ) {
  1218. for ( var i = 0; i < this.nodes.length; i ++ ) {
  1219. var n = this.nodes[ i ].getChildBySid( sid, recursive );
  1220. if ( n ) {
  1221. return n;
  1222. }
  1223. }
  1224. }
  1225. return null;
  1226. };
  1227. Node.prototype.getTransformBySid = function ( sid ) {
  1228. for ( var i = 0; i < this.transforms.length; i ++ ) {
  1229. if ( this.transforms[ i ].sid == sid ) return this.transforms[ i ];
  1230. }
  1231. return null;
  1232. };
  1233. Node.prototype.parse = function( element ) {
  1234. var url;
  1235. this.id = element.getAttribute('id');
  1236. this.sid = element.getAttribute('sid');
  1237. this.name = element.getAttribute('name');
  1238. this.type = element.getAttribute('type');
  1239. this.layer = element.getAttribute('layer');
  1240. this.type = this.type == 'JOINT' ? this.type : 'NODE';
  1241. this.nodes = [];
  1242. this.transforms = [];
  1243. this.geometries = [];
  1244. this.cameras = [];
  1245. this.lights = [];
  1246. this.controllers = [];
  1247. this.matrix = new THREE.Matrix4();
  1248. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1249. var child = element.childNodes[ i ];
  1250. if ( child.nodeType != 1 ) continue;
  1251. switch ( child.nodeName ) {
  1252. case 'node':
  1253. this.nodes.push( ( new Node() ).parse( child ) );
  1254. break;
  1255. case 'instance_camera':
  1256. this.cameras.push( ( new InstanceCamera() ).parse( child ) );
  1257. break;
  1258. case 'instance_controller':
  1259. this.controllers.push( ( new InstanceController() ).parse( child ) );
  1260. break;
  1261. case 'instance_geometry':
  1262. this.geometries.push( ( new InstanceGeometry() ).parse( child ) );
  1263. break;
  1264. case 'instance_light':
  1265. this.lights.push( ( new InstanceLight() ).parse( child ) );
  1266. break;
  1267. case 'instance_node':
  1268. url = child.getAttribute( 'url' ).replace( /^#/, '' );
  1269. var iNode = getLibraryNode( url );
  1270. if ( iNode ) {
  1271. this.nodes.push( ( new Node() ).parse( iNode )) ;
  1272. }
  1273. break;
  1274. case 'rotate':
  1275. case 'translate':
  1276. case 'scale':
  1277. case 'matrix':
  1278. case 'lookat':
  1279. case 'skew':
  1280. this.transforms.push( ( new Transform() ).parse( child ) );
  1281. break;
  1282. case 'extra':
  1283. break;
  1284. default:
  1285. console.log( child.nodeName );
  1286. break;
  1287. }
  1288. }
  1289. this.channels = getChannelsForNode( this );
  1290. bakeAnimations( this );
  1291. this.updateMatrix();
  1292. return this;
  1293. };
  1294. Node.prototype.updateMatrix = function () {
  1295. this.matrix.identity();
  1296. for ( var i = 0; i < this.transforms.length; i ++ ) {
  1297. this.transforms[ i ].apply( this.matrix );
  1298. }
  1299. };
  1300. function Transform () {
  1301. this.sid = "";
  1302. this.type = "";
  1303. this.data = [];
  1304. this.obj = null;
  1305. };
  1306. Transform.prototype.parse = function ( element ) {
  1307. this.sid = element.getAttribute( 'sid' );
  1308. this.type = element.nodeName;
  1309. this.data = _floats( element.textContent );
  1310. this.convert();
  1311. return this;
  1312. };
  1313. Transform.prototype.convert = function () {
  1314. switch ( this.type ) {
  1315. case 'matrix':
  1316. this.obj = getConvertedMat4( this.data );
  1317. break;
  1318. case 'rotate':
  1319. this.angle = THREE.Math.degToRad( this.data[3] );
  1320. case 'translate':
  1321. fixCoords( this.data, -1 );
  1322. this.obj = new THREE.Vector3( this.data[ 0 ], this.data[ 1 ], this.data[ 2 ] );
  1323. break;
  1324. case 'scale':
  1325. fixCoords( this.data, 1 );
  1326. this.obj = new THREE.Vector3( this.data[ 0 ], this.data[ 1 ], this.data[ 2 ] );
  1327. break;
  1328. default:
  1329. console.log( 'Can not convert Transform of type ' + this.type );
  1330. break;
  1331. }
  1332. };
  1333. Transform.prototype.apply = function () {
  1334. var m1 = new THREE.Matrix4();
  1335. return function ( matrix ) {
  1336. switch ( this.type ) {
  1337. case 'matrix':
  1338. matrix.multiply( this.obj );
  1339. break;
  1340. case 'translate':
  1341. matrix.multiply( m1.makeTranslation( this.obj.x, this.obj.y, this.obj.z ) );
  1342. break;
  1343. case 'rotate':
  1344. matrix.multiply( m1.makeRotationAxis( this.obj, this.angle ) );
  1345. break;
  1346. case 'scale':
  1347. matrix.scale( this.obj );
  1348. break;
  1349. }
  1350. };
  1351. }();
  1352. Transform.prototype.update = function ( data, member ) {
  1353. var members = [ 'X', 'Y', 'Z', 'ANGLE' ];
  1354. switch ( this.type ) {
  1355. case 'matrix':
  1356. if ( ! member ) {
  1357. this.obj.copy( data );
  1358. } else if ( member.length === 1 ) {
  1359. switch ( member[ 0 ] ) {
  1360. case 0:
  1361. this.obj.n11 = data[ 0 ];
  1362. this.obj.n21 = data[ 1 ];
  1363. this.obj.n31 = data[ 2 ];
  1364. this.obj.n41 = data[ 3 ];
  1365. break;
  1366. case 1:
  1367. this.obj.n12 = data[ 0 ];
  1368. this.obj.n22 = data[ 1 ];
  1369. this.obj.n32 = data[ 2 ];
  1370. this.obj.n42 = data[ 3 ];
  1371. break;
  1372. case 2:
  1373. this.obj.n13 = data[ 0 ];
  1374. this.obj.n23 = data[ 1 ];
  1375. this.obj.n33 = data[ 2 ];
  1376. this.obj.n43 = data[ 3 ];
  1377. break;
  1378. case 3:
  1379. this.obj.n14 = data[ 0 ];
  1380. this.obj.n24 = data[ 1 ];
  1381. this.obj.n34 = data[ 2 ];
  1382. this.obj.n44 = data[ 3 ];
  1383. break;
  1384. }
  1385. } else if ( member.length === 2 ) {
  1386. var propName = 'n' + ( member[ 0 ] + 1 ) + ( member[ 1 ] + 1 );
  1387. this.obj[ propName ] = data;
  1388. } else {
  1389. console.log('Incorrect addressing of matrix in transform.');
  1390. }
  1391. break;
  1392. case 'translate':
  1393. case 'scale':
  1394. if ( Object.prototype.toString.call( member ) === '[object Array]' ) {
  1395. member = members[ member[ 0 ] ];
  1396. }
  1397. switch ( member ) {
  1398. case 'X':
  1399. this.obj.x = data;
  1400. break;
  1401. case 'Y':
  1402. this.obj.y = data;
  1403. break;
  1404. case 'Z':
  1405. this.obj.z = data;
  1406. break;
  1407. default:
  1408. this.obj.x = data[ 0 ];
  1409. this.obj.y = data[ 1 ];
  1410. this.obj.z = data[ 2 ];
  1411. break;
  1412. }
  1413. break;
  1414. case 'rotate':
  1415. if ( Object.prototype.toString.call( member ) === '[object Array]' ) {
  1416. member = members[ member[ 0 ] ];
  1417. }
  1418. switch ( member ) {
  1419. case 'X':
  1420. this.obj.x = data;
  1421. break;
  1422. case 'Y':
  1423. this.obj.y = data;
  1424. break;
  1425. case 'Z':
  1426. this.obj.z = data;
  1427. break;
  1428. case 'ANGLE':
  1429. this.angle = THREE.Math.degToRad( data );
  1430. break;
  1431. default:
  1432. this.obj.x = data[ 0 ];
  1433. this.obj.y = data[ 1 ];
  1434. this.obj.z = data[ 2 ];
  1435. this.angle = THREE.Math.degToRad( data[ 3 ] );
  1436. break;
  1437. }
  1438. break;
  1439. }
  1440. };
  1441. function InstanceController() {
  1442. this.url = "";
  1443. this.skeleton = [];
  1444. this.instance_material = [];
  1445. };
  1446. InstanceController.prototype.parse = function ( element ) {
  1447. this.url = element.getAttribute('url').replace(/^#/, '');
  1448. this.skeleton = [];
  1449. this.instance_material = [];
  1450. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1451. var child = element.childNodes[ i ];
  1452. if ( child.nodeType !== 1 ) continue;
  1453. switch ( child.nodeName ) {
  1454. case 'skeleton':
  1455. this.skeleton.push( child.textContent.replace(/^#/, '') );
  1456. break;
  1457. case 'bind_material':
  1458. var instances = child.querySelectorAll('instance_material');
  1459. for ( var j = 0; j < instances.length; j ++ ){
  1460. var instance = instances[j];
  1461. this.instance_material.push( (new InstanceMaterial()).parse(instance) );
  1462. }
  1463. break;
  1464. case 'extra':
  1465. break;
  1466. default:
  1467. break;
  1468. }
  1469. }
  1470. return this;
  1471. };
  1472. function InstanceMaterial () {
  1473. this.symbol = "";
  1474. this.target = "";
  1475. };
  1476. InstanceMaterial.prototype.parse = function ( element ) {
  1477. this.symbol = element.getAttribute('symbol');
  1478. this.target = element.getAttribute('target').replace(/^#/, '');
  1479. return this;
  1480. };
  1481. function InstanceGeometry() {
  1482. this.url = "";
  1483. this.instance_material = [];
  1484. };
  1485. InstanceGeometry.prototype.parse = function ( element ) {
  1486. this.url = element.getAttribute('url').replace(/^#/, '');
  1487. this.instance_material = [];
  1488. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1489. var child = element.childNodes[i];
  1490. if ( child.nodeType != 1 ) continue;
  1491. if ( child.nodeName == 'bind_material' ) {
  1492. var instances = child.querySelectorAll('instance_material');
  1493. for ( var j = 0; j < instances.length; j ++ ) {
  1494. var instance = instances[j];
  1495. this.instance_material.push( (new InstanceMaterial()).parse(instance) );
  1496. }
  1497. break;
  1498. }
  1499. }
  1500. return this;
  1501. };
  1502. function Geometry() {
  1503. this.id = "";
  1504. this.mesh = null;
  1505. };
  1506. Geometry.prototype.parse = function ( element ) {
  1507. this.id = element.getAttribute('id');
  1508. extractDoubleSided( this, element );
  1509. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1510. var child = element.childNodes[i];
  1511. switch ( child.nodeName ) {
  1512. case 'mesh':
  1513. this.mesh = (new Mesh(this)).parse(child);
  1514. break;
  1515. case 'extra':
  1516. // console.log( child );
  1517. break;
  1518. default:
  1519. break;
  1520. }
  1521. }
  1522. return this;
  1523. };
  1524. function Mesh( geometry ) {
  1525. this.geometry = geometry.id;
  1526. this.primitives = [];
  1527. this.vertices = null;
  1528. this.geometry3js = null;
  1529. };
  1530. Mesh.prototype.parse = function( element ) {
  1531. this.primitives = [];
  1532. var i, j;
  1533. for ( i = 0; i < element.childNodes.length; i ++ ) {
  1534. var child = element.childNodes[ i ];
  1535. switch ( child.nodeName ) {
  1536. case 'source':
  1537. _source( child );
  1538. break;
  1539. case 'vertices':
  1540. this.vertices = ( new Vertices() ).parse( child );
  1541. break;
  1542. case 'triangles':
  1543. this.primitives.push( ( new Triangles().parse( child ) ) );
  1544. break;
  1545. case 'polygons':
  1546. this.primitives.push( ( new Polygons().parse( child ) ) );
  1547. break;
  1548. case 'polylist':
  1549. this.primitives.push( ( new Polylist().parse( child ) ) );
  1550. break;
  1551. default:
  1552. break;
  1553. }
  1554. }
  1555. this.geometry3js = new THREE.Geometry();
  1556. var vertexData = sources[ this.vertices.input['POSITION'].source ].data;
  1557. for ( i = 0; i < vertexData.length; i += 3 ) {
  1558. this.geometry3js.vertices.push( getConvertedVec3( vertexData, i ).clone() );
  1559. }
  1560. for ( i = 0; i < this.primitives.length; i ++ ) {
  1561. var primitive = this.primitives[ i ];
  1562. primitive.setVertices( this.vertices );
  1563. this.handlePrimitive( primitive, this.geometry3js );
  1564. }
  1565. this.geometry3js.computeFaceNormals();
  1566. if ( this.geometry3js.calcNormals ) {
  1567. this.geometry3js.computeVertexNormals();
  1568. delete this.geometry3js.calcNormals;
  1569. }
  1570. // this.geometry3js.computeBoundingBox();
  1571. return this;
  1572. };
  1573. Mesh.prototype.handlePrimitive = function( primitive, geom ) {
  1574. var j, k, pList = primitive.p, inputs = primitive.inputs;
  1575. var input, index, idx32;
  1576. var source, numParams;
  1577. var vcIndex = 0, vcount = 3, maxOffset = 0;
  1578. var texture_sets = [];
  1579. for ( j = 0; j < inputs.length; j ++ ) {
  1580. input = inputs[ j ];
  1581. var offset = input.offset + 1;
  1582. maxOffset = (maxOffset < offset)? offset : maxOffset;
  1583. switch ( input.semantic ) {
  1584. case 'TEXCOORD':
  1585. texture_sets.push( input.set );
  1586. break;
  1587. }
  1588. }
  1589. for ( var pCount = 0; pCount < pList.length; ++pCount ) {
  1590. var p = pList[ pCount ], i = 0;
  1591. while ( i < p.length ) {
  1592. var vs = [];
  1593. var ns = [];
  1594. var ts = null;
  1595. var cs = [];
  1596. if ( primitive.vcount ) {
  1597. vcount = primitive.vcount.length ? primitive.vcount[ vcIndex ++ ] : primitive.vcount;
  1598. } else {
  1599. vcount = p.length / maxOffset;
  1600. }
  1601. for ( j = 0; j < vcount; j ++ ) {
  1602. for ( k = 0; k < inputs.length; k ++ ) {
  1603. input = inputs[ k ];
  1604. source = sources[ input.source ];
  1605. index = p[ i + ( j * maxOffset ) + input.offset ];
  1606. numParams = source.accessor.params.length;
  1607. idx32 = index * numParams;
  1608. switch ( input.semantic ) {
  1609. case 'VERTEX':
  1610. vs.push( index );
  1611. break;
  1612. case 'NORMAL':
  1613. ns.push( getConvertedVec3( source.data, idx32 ) );
  1614. break;
  1615. case 'TEXCOORD':
  1616. ts = ts || { };
  1617. if ( ts[ input.set ] === undefined ) ts[ input.set ] = [];
  1618. // invert the V
  1619. ts[ input.set ].push( new THREE.Vector2( source.data[ idx32 ], source.data[ idx32 + 1 ] ) );
  1620. break;
  1621. case 'COLOR':
  1622. cs.push( new THREE.Color().setRGB( source.data[ idx32 ], source.data[ idx32 + 1 ], source.data[ idx32 + 2 ] ) );
  1623. break;
  1624. default:
  1625. break;
  1626. }
  1627. }
  1628. }
  1629. if ( ns.length == 0 ) {
  1630. // check the vertices inputs
  1631. input = this.vertices.input.NORMAL;
  1632. if ( input ) {
  1633. source = sources[ input.source ];
  1634. numParams = source.accessor.params.length;
  1635. for ( var ndx = 0, len = vs.length; ndx < len; ndx++ ) {
  1636. ns.push( getConvertedVec3( source.data, vs[ ndx ] * numParams ) );
  1637. }
  1638. } else {
  1639. geom.calcNormals = true;
  1640. }
  1641. }
  1642. if ( !ts ) {
  1643. ts = { };
  1644. // check the vertices inputs
  1645. input = this.vertices.input.TEXCOORD;
  1646. if ( input ) {
  1647. texture_sets.push( input.set );
  1648. source = sources[ input.source ];
  1649. numParams = source.accessor.params.length;
  1650. for ( var ndx = 0, len = vs.length; ndx < len; ndx++ ) {
  1651. idx32 = vs[ ndx ] * numParams;
  1652. if ( ts[ input.set ] === undefined ) ts[ input.set ] = [ ];
  1653. // invert the V
  1654. ts[ input.set ].push( new THREE.Vector2( source.data[ idx32 ], 1.0 - source.data[ idx32 + 1 ] ) );
  1655. }
  1656. }
  1657. }
  1658. if ( cs.length == 0 ) {
  1659. // check the vertices inputs
  1660. input = this.vertices.input.COLOR;
  1661. if ( input ) {
  1662. source = sources[ input.source ];
  1663. numParams = source.accessor.params.length;
  1664. for ( var ndx = 0, len = vs.length; ndx < len; ndx++ ) {
  1665. idx32 = vs[ ndx ] * numParams;
  1666. cs.push( new THREE.Color().setRGB( source.data[ idx32 ], source.data[ idx32 + 1 ], source.data[ idx32 + 2 ] ) );
  1667. }
  1668. }
  1669. }
  1670. var face = null, faces = [], uv, uvArr;
  1671. if ( vcount === 3 ) {
  1672. faces.push( new THREE.Face3( vs[0], vs[1], vs[2], ns, cs.length ? cs : new THREE.Color() ) );
  1673. } else if ( vcount === 4 ) {
  1674. faces.push( new THREE.Face3( vs[0], vs[1], vs[3], [ns[0], ns[1], ns[3]], cs.length ? [cs[0], cs[1], cs[3]] : new THREE.Color() ) );
  1675. faces.push( new THREE.Face3( vs[1], vs[2], vs[3], [ns[1], ns[2], ns[3]], cs.length ? [cs[1], cs[2], cs[3]] : new THREE.Color() ) );
  1676. } else if ( vcount > 4 && options.subdivideFaces ) {
  1677. var clr = cs.length ? cs : new THREE.Color(),
  1678. vec1, vec2, vec3, v1, v2, norm;
  1679. // subdivide into multiple Face3s
  1680. for ( k = 1; k < vcount - 1; ) {
  1681. // FIXME: normals don't seem to be quite right
  1682. faces.push( new THREE.Face3( vs[0], vs[k], vs[k+1], [ ns[0], ns[k++], ns[k] ], clr ) );
  1683. }
  1684. }
  1685. if ( faces.length ) {
  1686. for ( var ndx = 0, len = faces.length; ndx < len; ndx ++ ) {
  1687. face = faces[ndx];
  1688. face.daeMaterial = primitive.material;
  1689. geom.faces.push( face );
  1690. for ( k = 0; k < texture_sets.length; k++ ) {
  1691. uv = ts[ texture_sets[k] ];
  1692. if ( vcount > 4 ) {
  1693. // Grab the right UVs for the vertices in this face
  1694. uvArr = [ uv[0], uv[ndx+1], uv[ndx+2] ];
  1695. } else if ( vcount === 4 ) {
  1696. if ( ndx === 0 ) {
  1697. uvArr = [ uv[0], uv[1], uv[3] ];
  1698. } else {
  1699. uvArr = [ uv[1].clone(), uv[2], uv[3].clone() ];
  1700. }
  1701. } else {
  1702. uvArr = [ uv[0], uv[1], uv[2] ];
  1703. }
  1704. if ( geom.faceVertexUvs[k] === undefined ) {
  1705. geom.faceVertexUvs[k] = [];
  1706. }
  1707. geom.faceVertexUvs[k].push( uvArr );
  1708. }
  1709. }
  1710. } else {
  1711. console.log( 'dropped face with vcount ' + vcount + ' for geometry with id: ' + geom.id );
  1712. }
  1713. i += maxOffset * vcount;
  1714. }
  1715. }
  1716. };
  1717. function Polygons () {
  1718. this.material = "";
  1719. this.count = 0;
  1720. this.inputs = [];
  1721. this.vcount = null;
  1722. this.p = [];
  1723. this.geometry = new THREE.Geometry();
  1724. };
  1725. Polygons.prototype.setVertices = function ( vertices ) {
  1726. for ( var i = 0; i < this.inputs.length; i ++ ) {
  1727. if ( this.inputs[ i ].source == vertices.id ) {
  1728. this.inputs[ i ].source = vertices.input[ 'POSITION' ].source;
  1729. }
  1730. }
  1731. };
  1732. Polygons.prototype.parse = function ( element ) {
  1733. this.material = element.getAttribute( 'material' );
  1734. this.count = _attr_as_int( element, 'count', 0 );
  1735. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1736. var child = element.childNodes[ i ];
  1737. switch ( child.nodeName ) {
  1738. case 'input':
  1739. this.inputs.push( ( new Input() ).parse( element.childNodes[ i ] ) );
  1740. break;
  1741. case 'vcount':
  1742. this.vcount = _ints( child.textContent );
  1743. break;
  1744. case 'p':
  1745. this.p.push( _ints( child.textContent ) );
  1746. break;
  1747. case 'ph':
  1748. console.warn( 'polygon holes not yet supported!' );
  1749. break;
  1750. default:
  1751. break;
  1752. }
  1753. }
  1754. return this;
  1755. };
  1756. function Polylist () {
  1757. Polygons.call( this );
  1758. this.vcount = [];
  1759. };
  1760. Polylist.prototype = Object.create( Polygons.prototype );
  1761. function Triangles () {
  1762. Polygons.call( this );
  1763. this.vcount = 3;
  1764. };
  1765. Triangles.prototype = Object.create( Polygons.prototype );
  1766. function Accessor() {
  1767. this.source = "";
  1768. this.count = 0;
  1769. this.stride = 0;
  1770. this.params = [];
  1771. };
  1772. Accessor.prototype.parse = function ( element ) {
  1773. this.params = [];
  1774. this.source = element.getAttribute( 'source' );
  1775. this.count = _attr_as_int( element, 'count', 0 );
  1776. this.stride = _attr_as_int( element, 'stride', 0 );
  1777. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1778. var child = element.childNodes[ i ];
  1779. if ( child.nodeName == 'param' ) {
  1780. var param = {};
  1781. param[ 'name' ] = child.getAttribute( 'name' );
  1782. param[ 'type' ] = child.getAttribute( 'type' );
  1783. this.params.push( param );
  1784. }
  1785. }
  1786. return this;
  1787. };
  1788. function Vertices() {
  1789. this.input = {};
  1790. };
  1791. Vertices.prototype.parse = function ( element ) {
  1792. this.id = element.getAttribute('id');
  1793. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1794. if ( element.childNodes[i].nodeName == 'input' ) {
  1795. var input = ( new Input() ).parse( element.childNodes[ i ] );
  1796. this.input[ input.semantic ] = input;
  1797. }
  1798. }
  1799. return this;
  1800. };
  1801. function Input () {
  1802. this.semantic = "";
  1803. this.offset = 0;
  1804. this.source = "";
  1805. this.set = 0;
  1806. };
  1807. Input.prototype.parse = function ( element ) {
  1808. this.semantic = element.getAttribute('semantic');
  1809. this.source = element.getAttribute('source').replace(/^#/, '');
  1810. this.set = _attr_as_int(element, 'set', -1);
  1811. this.offset = _attr_as_int(element, 'offset', 0);
  1812. if ( this.semantic == 'TEXCOORD' && this.set < 0 ) {
  1813. this.set = 0;
  1814. }
  1815. return this;
  1816. };
  1817. function Source ( id ) {
  1818. this.id = id;
  1819. this.type = null;
  1820. };
  1821. Source.prototype.parse = function ( element ) {
  1822. this.id = element.getAttribute( 'id' );
  1823. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1824. var child = element.childNodes[i];
  1825. switch ( child.nodeName ) {
  1826. case 'bool_array':
  1827. this.data = _bools( child.textContent );
  1828. this.type = child.nodeName;
  1829. break;
  1830. case 'float_array':
  1831. this.data = _floats( child.textContent );
  1832. this.type = child.nodeName;
  1833. break;
  1834. case 'int_array':
  1835. this.data = _ints( child.textContent );
  1836. this.type = child.nodeName;
  1837. break;
  1838. case 'IDREF_array':
  1839. case 'Name_array':
  1840. this.data = _strings( child.textContent );
  1841. this.type = child.nodeName;
  1842. break;
  1843. case 'technique_common':
  1844. for ( var j = 0; j < child.childNodes.length; j ++ ) {
  1845. if ( child.childNodes[ j ].nodeName == 'accessor' ) {
  1846. this.accessor = ( new Accessor() ).parse( child.childNodes[ j ] );
  1847. break;
  1848. }
  1849. }
  1850. break;
  1851. default:
  1852. // console.log(child.nodeName);
  1853. break;
  1854. }
  1855. }
  1856. return this;
  1857. };
  1858. Source.prototype.read = function () {
  1859. var result = [];
  1860. //for (var i = 0; i < this.accessor.params.length; i++) {
  1861. var param = this.accessor.params[ 0 ];
  1862. //console.log(param.name + " " + param.type);
  1863. switch ( param.type ) {
  1864. case 'IDREF':
  1865. case 'Name': case 'name':
  1866. case 'float':
  1867. return this.data;
  1868. case 'float4x4':
  1869. for ( var j = 0; j < this.data.length; j += 16 ) {
  1870. var s = this.data.slice( j, j + 16 );
  1871. var m = getConvertedMat4( s );
  1872. result.push( m );
  1873. }
  1874. break;
  1875. default:
  1876. console.log( 'ColladaLoader: Source: Read dont know how to read ' + param.type + '.' );
  1877. break;
  1878. }
  1879. //}
  1880. return result;
  1881. };
  1882. function Material () {
  1883. this.id = "";
  1884. this.name = "";
  1885. this.instance_effect = null;
  1886. };
  1887. Material.prototype.parse = function ( element ) {
  1888. this.id = element.getAttribute( 'id' );
  1889. this.name = element.getAttribute( 'name' );
  1890. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1891. if ( element.childNodes[ i ].nodeName == 'instance_effect' ) {
  1892. this.instance_effect = ( new InstanceEffect() ).parse( element.childNodes[ i ] );
  1893. break;
  1894. }
  1895. }
  1896. return this;
  1897. };
  1898. function ColorOrTexture () {
  1899. this.color = new THREE.Color();
  1900. this.color.setRGB( Math.random(), Math.random(), Math.random() );
  1901. this.color.a = 1.0;
  1902. this.texture = null;
  1903. this.texcoord = null;
  1904. this.texOpts = null;
  1905. };
  1906. ColorOrTexture.prototype.isColor = function () {
  1907. return ( this.texture == null );
  1908. };
  1909. ColorOrTexture.prototype.isTexture = function () {
  1910. return ( this.texture != null );
  1911. };
  1912. ColorOrTexture.prototype.parse = function ( element ) {
  1913. if (element.nodeName == 'transparent') {
  1914. this.opaque = element.getAttribute('opaque');
  1915. }
  1916. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1917. var child = element.childNodes[ i ];
  1918. if ( child.nodeType != 1 ) continue;
  1919. switch ( child.nodeName ) {
  1920. case 'color':
  1921. var rgba = _floats( child.textContent );
  1922. this.color = new THREE.Color();
  1923. this.color.setRGB( rgba[0], rgba[1], rgba[2] );
  1924. this.color.a = rgba[3];
  1925. break;
  1926. case 'texture':
  1927. this.texture = child.getAttribute('texture');
  1928. this.texcoord = child.getAttribute('texcoord');
  1929. // Defaults from:
  1930. // https://collada.org/mediawiki/index.php/Maya_texture_placement_MAYA_extension
  1931. this.texOpts = {
  1932. offsetU: 0,
  1933. offsetV: 0,
  1934. repeatU: 1,
  1935. repeatV: 1,
  1936. wrapU: 1,
  1937. wrapV: 1
  1938. };
  1939. this.parseTexture( child );
  1940. break;
  1941. default:
  1942. break;
  1943. }
  1944. }
  1945. return this;
  1946. };
  1947. ColorOrTexture.prototype.parseTexture = function ( element ) {
  1948. if ( ! element.childNodes ) return this;
  1949. // This should be supported by Maya, 3dsMax, and MotionBuilder
  1950. if ( element.childNodes[1] && element.childNodes[1].nodeName === 'extra' ) {
  1951. element = element.childNodes[1];
  1952. if ( element.childNodes[1] && element.childNodes[1].nodeName === 'technique' ) {
  1953. element = element.childNodes[1];
  1954. }
  1955. }
  1956. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1957. var child = element.childNodes[ i ];
  1958. switch ( child.nodeName ) {
  1959. case 'offsetU':
  1960. case 'offsetV':
  1961. case 'repeatU':
  1962. case 'repeatV':
  1963. this.texOpts[ child.nodeName ] = parseFloat( child.textContent );
  1964. break;
  1965. case 'wrapU':
  1966. case 'wrapV':
  1967. // some dae have a value of true which becomes NaN via parseInt
  1968. if ( child.textContent.toUpperCase() === 'TRUE' ) {
  1969. this.texOpts[ child.nodeName ] = 1;
  1970. } else {
  1971. this.texOpts[ child.nodeName ] = parseInt( child.textContent );
  1972. }
  1973. break;
  1974. default:
  1975. this.texOpts[ child.nodeName ] = child.textContent;
  1976. break;
  1977. }
  1978. }
  1979. return this;
  1980. };
  1981. function Shader ( type, effect ) {
  1982. this.type = type;
  1983. this.effect = effect;
  1984. this.material = null;
  1985. };
  1986. Shader.prototype.parse = function ( element ) {
  1987. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1988. var child = element.childNodes[ i ];
  1989. if ( child.nodeType != 1 ) continue;
  1990. switch ( child.nodeName ) {
  1991. case 'ambient':
  1992. case 'emission':
  1993. case 'diffuse':
  1994. case 'specular':
  1995. case 'transparent':
  1996. case 'bump':
  1997. this[ child.nodeName ] = ( new ColorOrTexture() ).parse( child );
  1998. break;
  1999. case 'shininess':
  2000. case 'reflectivity':
  2001. case 'index_of_refraction':
  2002. case 'transparency':
  2003. var f = child.querySelectorAll('float');
  2004. if ( f.length > 0 )
  2005. this[ child.nodeName ] = parseFloat( f[ 0 ].textContent );
  2006. break;
  2007. default:
  2008. break;
  2009. }
  2010. }
  2011. this.create();
  2012. return this;
  2013. };
  2014. Shader.prototype.create = function() {
  2015. var props = {};
  2016. var transparent = false;
  2017. if (this['transparency'] !== undefined && this['transparent'] !== undefined) {
  2018. // convert transparent color RBG to average value
  2019. var transparentColor = this['transparent'];
  2020. var transparencyLevel = (this.transparent.color.r +
  2021. this.transparent.color.g +
  2022. this.transparent.color.b)
  2023. / 3 * this.transparency;
  2024. if (transparencyLevel > 0) {
  2025. transparent = true;
  2026. props[ 'transparent' ] = true;
  2027. props[ 'opacity' ] = 1 - transparencyLevel;
  2028. }
  2029. }
  2030. var keys = {
  2031. 'diffuse':'map',
  2032. 'ambient':"lightMap" ,
  2033. 'specular':'specularMap',
  2034. 'emission':'emissionMap',
  2035. 'bump':'normalMap'
  2036. };
  2037. for ( var prop in this ) {
  2038. switch ( prop ) {
  2039. case 'ambient':
  2040. case 'emission':
  2041. case 'diffuse':
  2042. case 'specular':
  2043. case 'bump':
  2044. var cot = this[ prop ];
  2045. if ( cot instanceof ColorOrTexture ) {
  2046. if ( cot.isTexture() ) {
  2047. var samplerId = cot.texture;
  2048. var surfaceId = this.effect.sampler[samplerId];
  2049. if ( surfaceId !== undefined && surfaceId.source !== undefined ) {
  2050. var surface = this.effect.surface[surfaceId.source];
  2051. var image = images[surface.init_from];
  2052. if (image) {
  2053. var texture = THREE.ImageUtils.loadTexture(baseUrl + image.init_from);
  2054. texture.wrapS = cot.texOpts.wrapU ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  2055. texture.wrapT = cot.texOpts.wrapV ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  2056. texture.offset.x = cot.texOpts.offsetU;
  2057. texture.offset.y = cot.texOpts.offsetV;
  2058. texture.repeat.x = cot.texOpts.repeatU;
  2059. texture.repeat.y = cot.texOpts.repeatV;
  2060. props[keys[prop]] = texture;
  2061. // Texture with baked lighting?
  2062. if (prop === 'emission') props['emissive'] = 0xffffff;
  2063. }
  2064. }
  2065. } else if ( prop === 'diffuse' || !transparent ) {
  2066. if ( prop === 'emission' ) {
  2067. props[ 'emissive' ] = cot.color.getHex();
  2068. } else {
  2069. props[ prop ] = cot.color.getHex();
  2070. }
  2071. }
  2072. }
  2073. break;
  2074. case 'shininess':
  2075. props[ prop ] = this[ prop ];
  2076. break;
  2077. case 'reflectivity':
  2078. props[ prop ] = this[ prop ];
  2079. if( props[ prop ] > 0.0 ) props['envMap'] = options.defaultEnvMap;
  2080. props['combine'] = THREE.MixOperation; //mix regular shading with reflective component
  2081. break;
  2082. case 'index_of_refraction':
  2083. props[ 'refractionRatio' ] = this[ prop ]; //TODO: "index_of_refraction" becomes "refractionRatio" in shader, but I'm not sure if the two are actually comparable
  2084. if ( this[ prop ] !== 1.0 ) props['envMap'] = options.defaultEnvMap;
  2085. break;
  2086. case 'transparency':
  2087. // gets figured out up top
  2088. break;
  2089. default:
  2090. break;
  2091. }
  2092. }
  2093. props[ 'shading' ] = preferredShading;
  2094. props[ 'side' ] = this.effect.doubleSided ? THREE.DoubleSide : THREE.FrontSide;
  2095. switch ( this.type ) {
  2096. case 'constant':
  2097. if (props.emissive != undefined) props.color = props.emissive;
  2098. this.material = new THREE.MeshBasicMaterial( props );
  2099. break;
  2100. case 'phong':
  2101. case 'blinn':
  2102. if (props.diffuse != undefined) props.color = props.diffuse;
  2103. this.material = new THREE.MeshPhongMaterial( props );
  2104. break;
  2105. case 'lambert':
  2106. default:
  2107. if (props.diffuse != undefined) props.color = props.diffuse;
  2108. this.material = new THREE.MeshLambertMaterial( props );
  2109. break;
  2110. }
  2111. return this.material;
  2112. };
  2113. function Surface ( effect ) {
  2114. this.effect = effect;
  2115. this.init_from = null;
  2116. this.format = null;
  2117. };
  2118. Surface.prototype.parse = function ( element ) {
  2119. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2120. var child = element.childNodes[ i ];
  2121. if ( child.nodeType != 1 ) continue;
  2122. switch ( child.nodeName ) {
  2123. case 'init_from':
  2124. this.init_from = child.textContent;
  2125. break;
  2126. case 'format':
  2127. this.format = child.textContent;
  2128. break;
  2129. default:
  2130. console.log( "unhandled Surface prop: " + child.nodeName );
  2131. break;
  2132. }
  2133. }
  2134. return this;
  2135. };
  2136. function Sampler2D ( effect ) {
  2137. this.effect = effect;
  2138. this.source = null;
  2139. this.wrap_s = null;
  2140. this.wrap_t = null;
  2141. this.minfilter = null;
  2142. this.magfilter = null;
  2143. this.mipfilter = null;
  2144. };
  2145. Sampler2D.prototype.parse = function ( element ) {
  2146. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2147. var child = element.childNodes[ i ];
  2148. if ( child.nodeType != 1 ) continue;
  2149. switch ( child.nodeName ) {
  2150. case 'source':
  2151. this.source = child.textContent;
  2152. break;
  2153. case 'minfilter':
  2154. this.minfilter = child.textContent;
  2155. break;
  2156. case 'magfilter':
  2157. this.magfilter = child.textContent;
  2158. break;
  2159. case 'mipfilter':
  2160. this.mipfilter = child.textContent;
  2161. break;
  2162. case 'wrap_s':
  2163. this.wrap_s = child.textContent;
  2164. break;
  2165. case 'wrap_t':
  2166. this.wrap_t = child.textContent;
  2167. break;
  2168. default:
  2169. console.log( "unhandled Sampler2D prop: " + child.nodeName );
  2170. break;
  2171. }
  2172. }
  2173. return this;
  2174. };
  2175. function Effect () {
  2176. this.id = "";
  2177. this.name = "";
  2178. this.shader = null;
  2179. this.surface = {};
  2180. this.sampler = {};
  2181. };
  2182. Effect.prototype.create = function () {
  2183. if ( this.shader == null ) {
  2184. return null;
  2185. }
  2186. };
  2187. Effect.prototype.parse = function ( element ) {
  2188. this.id = element.getAttribute( 'id' );
  2189. this.name = element.getAttribute( 'name' );
  2190. extractDoubleSided( this, element );
  2191. this.shader = null;
  2192. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2193. var child = element.childNodes[ i ];
  2194. if ( child.nodeType != 1 ) continue;
  2195. switch ( child.nodeName ) {
  2196. case 'profile_COMMON':
  2197. this.parseTechnique( this.parseProfileCOMMON( child ) );
  2198. break;
  2199. default:
  2200. break;
  2201. }
  2202. }
  2203. return this;
  2204. };
  2205. Effect.prototype.parseNewparam = function ( element ) {
  2206. var sid = element.getAttribute( 'sid' );
  2207. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2208. var child = element.childNodes[ i ];
  2209. if ( child.nodeType != 1 ) continue;
  2210. switch ( child.nodeName ) {
  2211. case 'surface':
  2212. this.surface[sid] = ( new Surface( this ) ).parse( child );
  2213. break;
  2214. case 'sampler2D':
  2215. this.sampler[sid] = ( new Sampler2D( this ) ).parse( child );
  2216. break;
  2217. case 'extra':
  2218. break;
  2219. default:
  2220. console.log( child.nodeName );
  2221. break;
  2222. }
  2223. }
  2224. };
  2225. Effect.prototype.parseProfileCOMMON = function ( element ) {
  2226. var technique;
  2227. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2228. var child = element.childNodes[ i ];
  2229. if ( child.nodeType != 1 ) continue;
  2230. switch ( child.nodeName ) {
  2231. case 'profile_COMMON':
  2232. this.parseProfileCOMMON( child );
  2233. break;
  2234. case 'technique':
  2235. technique = child;
  2236. break;
  2237. case 'newparam':
  2238. this.parseNewparam( child );
  2239. break;
  2240. case 'image':
  2241. var _image = ( new _Image() ).parse( child );
  2242. images[ _image.id ] = _image;
  2243. break;
  2244. case 'extra':
  2245. break;
  2246. default:
  2247. console.log( child.nodeName );
  2248. break;
  2249. }
  2250. }
  2251. return technique;
  2252. };
  2253. Effect.prototype.parseTechnique= function ( element ) {
  2254. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2255. var child = element.childNodes[i];
  2256. if ( child.nodeType != 1 ) continue;
  2257. switch ( child.nodeName ) {
  2258. case 'constant':
  2259. case 'lambert':
  2260. case 'blinn':
  2261. case 'phong':
  2262. this.shader = ( new Shader( child.nodeName, this ) ).parse( child );
  2263. break;
  2264. case 'extra':
  2265. this.parseExtra(child);
  2266. break;
  2267. default:
  2268. break;
  2269. }
  2270. }
  2271. };
  2272. Effect.prototype.parseExtra = function ( element ) {
  2273. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2274. var child = element.childNodes[i];
  2275. if ( child.nodeType != 1 ) continue;
  2276. switch ( child.nodeName ) {
  2277. case 'technique':
  2278. this.parseExtraTechnique( child );
  2279. break;
  2280. default:
  2281. break;
  2282. }
  2283. }
  2284. };
  2285. Effect.prototype.parseExtraTechnique= function ( element ) {
  2286. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2287. var child = element.childNodes[i];
  2288. if ( child.nodeType != 1 ) continue;
  2289. switch ( child.nodeName ) {
  2290. case 'bump':
  2291. this.shader.parse( element );
  2292. break;
  2293. default:
  2294. break;
  2295. }
  2296. }
  2297. };
  2298. function InstanceEffect () {
  2299. this.url = "";
  2300. };
  2301. InstanceEffect.prototype.parse = function ( element ) {
  2302. this.url = element.getAttribute( 'url' ).replace( /^#/, '' );
  2303. return this;
  2304. };
  2305. function Animation() {
  2306. this.id = "";
  2307. this.name = "";
  2308. this.source = {};
  2309. this.sampler = [];
  2310. this.channel = [];
  2311. };
  2312. Animation.prototype.parse = function ( element ) {
  2313. this.id = element.getAttribute( 'id' );
  2314. this.name = element.getAttribute( 'name' );
  2315. this.source = {};
  2316. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2317. var child = element.childNodes[ i ];
  2318. if ( child.nodeType != 1 ) continue;
  2319. switch ( child.nodeName ) {
  2320. case 'animation':
  2321. var anim = ( new Animation() ).parse( child );
  2322. for ( var src in anim.source ) {
  2323. this.source[ src ] = anim.source[ src ];
  2324. }
  2325. for ( var j = 0; j < anim.channel.length; j ++ ) {
  2326. this.channel.push( anim.channel[ j ] );
  2327. this.sampler.push( anim.sampler[ j ] );
  2328. }
  2329. break;
  2330. case 'source':
  2331. var src = ( new Source() ).parse( child );
  2332. this.source[ src.id ] = src;
  2333. break;
  2334. case 'sampler':
  2335. this.sampler.push( ( new Sampler( this ) ).parse( child ) );
  2336. break;
  2337. case 'channel':
  2338. this.channel.push( ( new Channel( this ) ).parse( child ) );
  2339. break;
  2340. default:
  2341. break;
  2342. }
  2343. }
  2344. return this;
  2345. };
  2346. function Channel( animation ) {
  2347. this.animation = animation;
  2348. this.source = "";
  2349. this.target = "";
  2350. this.fullSid = null;
  2351. this.sid = null;
  2352. this.dotSyntax = null;
  2353. this.arrSyntax = null;
  2354. this.arrIndices = null;
  2355. this.member = null;
  2356. };
  2357. Channel.prototype.parse = function ( element ) {
  2358. this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
  2359. this.target = element.getAttribute( 'target' );
  2360. var parts = this.target.split( '/' );
  2361. var id = parts.shift();
  2362. var sid = parts.shift();
  2363. var dotSyntax = ( sid.indexOf(".") >= 0 );
  2364. var arrSyntax = ( sid.indexOf("(") >= 0 );
  2365. if ( dotSyntax ) {
  2366. parts = sid.split(".");
  2367. this.sid = parts.shift();
  2368. this.member = parts.shift();
  2369. } else if ( arrSyntax ) {
  2370. var arrIndices = sid.split("(");
  2371. this.sid = arrIndices.shift();
  2372. for (var j = 0; j < arrIndices.length; j ++ ) {
  2373. arrIndices[j] = parseInt( arrIndices[j].replace(/\)/, '') );
  2374. }
  2375. this.arrIndices = arrIndices;
  2376. } else {
  2377. this.sid = sid;
  2378. }
  2379. this.fullSid = sid;
  2380. this.dotSyntax = dotSyntax;
  2381. this.arrSyntax = arrSyntax;
  2382. return this;
  2383. };
  2384. function Sampler ( animation ) {
  2385. this.id = "";
  2386. this.animation = animation;
  2387. this.inputs = [];
  2388. this.input = null;
  2389. this.output = null;
  2390. this.strideOut = null;
  2391. this.interpolation = null;
  2392. this.startTime = null;
  2393. this.endTime = null;
  2394. this.duration = 0;
  2395. };
  2396. Sampler.prototype.parse = function ( element ) {
  2397. this.id = element.getAttribute( 'id' );
  2398. this.inputs = [];
  2399. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2400. var child = element.childNodes[ i ];
  2401. if ( child.nodeType != 1 ) continue;
  2402. switch ( child.nodeName ) {
  2403. case 'input':
  2404. this.inputs.push( (new Input()).parse( child ) );
  2405. break;
  2406. default:
  2407. break;
  2408. }
  2409. }
  2410. return this;
  2411. };
  2412. Sampler.prototype.create = function () {
  2413. for ( var i = 0; i < this.inputs.length; i ++ ) {
  2414. var input = this.inputs[ i ];
  2415. var source = this.animation.source[ input.source ];
  2416. switch ( input.semantic ) {
  2417. case 'INPUT':
  2418. this.input = source.read();
  2419. break;
  2420. case 'OUTPUT':
  2421. this.output = source.read();
  2422. this.strideOut = source.accessor.stride;
  2423. break;
  2424. case 'INTERPOLATION':
  2425. this.interpolation = source.read();
  2426. break;
  2427. case 'IN_TANGENT':
  2428. break;
  2429. case 'OUT_TANGENT':
  2430. break;
  2431. default:
  2432. console.log(input.semantic);
  2433. break;
  2434. }
  2435. }
  2436. this.startTime = 0;
  2437. this.endTime = 0;
  2438. this.duration = 0;
  2439. if ( this.input.length ) {
  2440. this.startTime = 100000000;
  2441. this.endTime = -100000000;
  2442. for ( var i = 0; i < this.input.length; i ++ ) {
  2443. this.startTime = Math.min( this.startTime, this.input[ i ] );
  2444. this.endTime = Math.max( this.endTime, this.input[ i ] );
  2445. }
  2446. this.duration = this.endTime - this.startTime;
  2447. }
  2448. };
  2449. Sampler.prototype.getData = function ( type, ndx, member ) {
  2450. var data;
  2451. if ( type === 'matrix' && this.strideOut === 16 ) {
  2452. data = this.output[ ndx ];
  2453. } else if ( this.strideOut > 1 ) {
  2454. data = [];
  2455. ndx *= this.strideOut;
  2456. for ( var i = 0; i < this.strideOut; ++i ) {
  2457. data[ i ] = this.output[ ndx + i ];
  2458. }
  2459. if ( this.strideOut === 3 ) {
  2460. switch ( type ) {
  2461. case 'rotate':
  2462. case 'translate':
  2463. fixCoords( data, -1 );
  2464. break;
  2465. case 'scale':
  2466. fixCoords( data, 1 );
  2467. break;
  2468. }
  2469. } else if ( this.strideOut === 4 && type === 'matrix' ) {
  2470. fixCoords( data, -1 );
  2471. }
  2472. } else {
  2473. data = this.output[ ndx ];
  2474. if ( member && type == 'translate' ) {
  2475. data = getConvertedTranslation( member, data );
  2476. }
  2477. }
  2478. return data;
  2479. };
  2480. function Key ( time ) {
  2481. this.targets = [];
  2482. this.time = time;
  2483. };
  2484. Key.prototype.addTarget = function ( fullSid, transform, member, data ) {
  2485. this.targets.push( {
  2486. sid: fullSid,
  2487. member: member,
  2488. transform: transform,
  2489. data: data
  2490. } );
  2491. };
  2492. Key.prototype.apply = function ( opt_sid ) {
  2493. for ( var i = 0; i < this.targets.length; ++i ) {
  2494. var target = this.targets[ i ];
  2495. if ( !opt_sid || target.sid === opt_sid ) {
  2496. target.transform.update( target.data, target.member );
  2497. }
  2498. }
  2499. };
  2500. Key.prototype.getTarget = function ( fullSid ) {
  2501. for ( var i = 0; i < this.targets.length; ++i ) {
  2502. if ( this.targets[ i ].sid === fullSid ) {
  2503. return this.targets[ i ];
  2504. }
  2505. }
  2506. return null;
  2507. };
  2508. Key.prototype.hasTarget = function ( fullSid ) {
  2509. for ( var i = 0; i < this.targets.length; ++i ) {
  2510. if ( this.targets[ i ].sid === fullSid ) {
  2511. return true;
  2512. }
  2513. }
  2514. return false;
  2515. };
  2516. // TODO: Currently only doing linear interpolation. Should support full COLLADA spec.
  2517. Key.prototype.interpolate = function ( nextKey, time ) {
  2518. for ( var i = 0, l = this.targets.length; i < l; i ++ ) {
  2519. var target = this.targets[ i ],
  2520. nextTarget = nextKey.getTarget( target.sid ),
  2521. data;
  2522. if ( target.transform.type !== 'matrix' && nextTarget ) {
  2523. var scale = ( time - this.time ) / ( nextKey.time - this.time ),
  2524. nextData = nextTarget.data,
  2525. prevData = target.data;
  2526. if ( scale < 0 ) scale = 0;
  2527. if ( scale > 1 ) scale = 1;
  2528. if ( prevData.length ) {
  2529. data = [];
  2530. for ( var j = 0; j < prevData.length; ++j ) {
  2531. data[ j ] = prevData[ j ] + ( nextData[ j ] - prevData[ j ] ) * scale;
  2532. }
  2533. } else {
  2534. data = prevData + ( nextData - prevData ) * scale;
  2535. }
  2536. } else {
  2537. data = target.data;
  2538. }
  2539. target.transform.update( data, target.member );
  2540. }
  2541. };
  2542. // Camera
  2543. function Camera() {
  2544. this.id = "";
  2545. this.name = "";
  2546. this.technique = "";
  2547. };
  2548. Camera.prototype.parse = function ( element ) {
  2549. this.id = element.getAttribute( 'id' );
  2550. this.name = element.getAttribute( 'name' );
  2551. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2552. var child = element.childNodes[ i ];
  2553. if ( child.nodeType != 1 ) continue;
  2554. switch ( child.nodeName ) {
  2555. case 'optics':
  2556. this.parseOptics( child );
  2557. break;
  2558. default:
  2559. break;
  2560. }
  2561. }
  2562. return this;
  2563. };
  2564. Camera.prototype.parseOptics = function ( element ) {
  2565. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2566. if ( element.childNodes[ i ].nodeName == 'technique_common' ) {
  2567. var technique = element.childNodes[ i ];
  2568. for ( var j = 0; j < technique.childNodes.length; j ++ ) {
  2569. this.technique = technique.childNodes[ j ].nodeName;
  2570. if ( this.technique == 'perspective' ) {
  2571. var perspective = technique.childNodes[ j ];
  2572. for ( var k = 0; k < perspective.childNodes.length; k ++ ) {
  2573. var param = perspective.childNodes[ k ];
  2574. switch ( param.nodeName ) {
  2575. case 'yfov':
  2576. this.yfov = param.textContent;
  2577. break;
  2578. case 'xfov':
  2579. this.xfov = param.textContent;
  2580. break;
  2581. case 'znear':
  2582. this.znear = param.textContent;
  2583. break;
  2584. case 'zfar':
  2585. this.zfar = param.textContent;
  2586. break;
  2587. case 'aspect_ratio':
  2588. this.aspect_ratio = param.textContent;
  2589. break;
  2590. }
  2591. }
  2592. } else if ( this.technique == 'orthographic' ) {
  2593. var orthographic = technique.childNodes[ j ];
  2594. for ( var k = 0; k < orthographic.childNodes.length; k ++ ) {
  2595. var param = orthographic.childNodes[ k ];
  2596. switch ( param.nodeName ) {
  2597. case 'xmag':
  2598. this.xmag = param.textContent;
  2599. break;
  2600. case 'ymag':
  2601. this.ymag = param.textContent;
  2602. break;
  2603. case 'znear':
  2604. this.znear = param.textContent;
  2605. break;
  2606. case 'zfar':
  2607. this.zfar = param.textContent;
  2608. break;
  2609. case 'aspect_ratio':
  2610. this.aspect_ratio = param.textContent;
  2611. break;
  2612. }
  2613. }
  2614. }
  2615. }
  2616. }
  2617. }
  2618. return this;
  2619. };
  2620. function InstanceCamera() {
  2621. this.url = "";
  2622. };
  2623. InstanceCamera.prototype.parse = function ( element ) {
  2624. this.url = element.getAttribute('url').replace(/^#/, '');
  2625. return this;
  2626. };
  2627. // Light
  2628. function Light() {
  2629. this.id = "";
  2630. this.name = "";
  2631. this.technique = "";
  2632. };
  2633. Light.prototype.parse = function ( element ) {
  2634. this.id = element.getAttribute( 'id' );
  2635. this.name = element.getAttribute( 'name' );
  2636. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2637. var child = element.childNodes[ i ];
  2638. if ( child.nodeType != 1 ) continue;
  2639. switch ( child.nodeName ) {
  2640. case 'technique_common':
  2641. this.parseCommon( child );
  2642. break;
  2643. case 'technique':
  2644. this.parseTechnique( child );
  2645. break;
  2646. default:
  2647. break;
  2648. }
  2649. }
  2650. return this;
  2651. };
  2652. Light.prototype.parseCommon = function ( element ) {
  2653. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2654. switch ( element.childNodes[ i ].nodeName ) {
  2655. case 'directional':
  2656. case 'point':
  2657. case 'spot':
  2658. case 'ambient':
  2659. this.technique = element.childNodes[ i ].nodeName;
  2660. var light = element.childNodes[ i ];
  2661. for ( var j = 0; j < light.childNodes.length; j ++ ) {
  2662. var child = light.childNodes[j];
  2663. switch ( child.nodeName ) {
  2664. case 'color':
  2665. var rgba = _floats( child.textContent );
  2666. this.color = new THREE.Color(0);
  2667. this.color.setRGB( rgba[0], rgba[1], rgba[2] );
  2668. this.color.a = rgba[3];
  2669. break;
  2670. case 'falloff_angle':
  2671. this.falloff_angle = parseFloat( child.textContent );
  2672. break;
  2673. }
  2674. }
  2675. }
  2676. }
  2677. return this;
  2678. };
  2679. Light.prototype.parseTechnique = function ( element ) {
  2680. this.profile = element.getAttribute( 'profile' );
  2681. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2682. var child = element.childNodes[ i ];
  2683. switch ( child.nodeName ) {
  2684. case 'intensity':
  2685. this.intensity = parseFloat(child.textContent);
  2686. break;
  2687. }
  2688. }
  2689. return this;
  2690. };
  2691. function InstanceLight() {
  2692. this.url = "";
  2693. };
  2694. InstanceLight.prototype.parse = function ( element ) {
  2695. this.url = element.getAttribute('url').replace(/^#/, '');
  2696. return this;
  2697. };
  2698. function _source( element ) {
  2699. var id = element.getAttribute( 'id' );
  2700. if ( sources[ id ] != undefined ) {
  2701. return sources[ id ];
  2702. }
  2703. sources[ id ] = ( new Source(id )).parse( element );
  2704. return sources[ id ];
  2705. };
  2706. function _nsResolver( nsPrefix ) {
  2707. if ( nsPrefix == "dae" ) {
  2708. return "http://www.collada.org/2005/11/COLLADASchema";
  2709. }
  2710. return null;
  2711. };
  2712. function _bools( str ) {
  2713. var raw = _strings( str );
  2714. var data = [];
  2715. for ( var i = 0, l = raw.length; i < l; i ++ ) {
  2716. data.push( (raw[i] == 'true' || raw[i] == '1') ? true : false );
  2717. }
  2718. return data;
  2719. };
  2720. function _floats( str ) {
  2721. var raw = _strings(str);
  2722. var data = [];
  2723. for ( var i = 0, l = raw.length; i < l; i ++ ) {
  2724. data.push( parseFloat( raw[ i ] ) );
  2725. }
  2726. return data;
  2727. };
  2728. function _ints( str ) {
  2729. var raw = _strings( str );
  2730. var data = [];
  2731. for ( var i = 0, l = raw.length; i < l; i ++ ) {
  2732. data.push( parseInt( raw[ i ], 10 ) );
  2733. }
  2734. return data;
  2735. };
  2736. function _strings( str ) {
  2737. return ( str.length > 0 ) ? _trimString( str ).split( /\s+/ ) : [];
  2738. };
  2739. function _trimString( str ) {
  2740. return str.replace( /^\s+/, "" ).replace( /\s+$/, "" );
  2741. };
  2742. function _attr_as_float( element, name, defaultValue ) {
  2743. if ( element.hasAttribute( name ) ) {
  2744. return parseFloat( element.getAttribute( name ) );
  2745. } else {
  2746. return defaultValue;
  2747. }
  2748. };
  2749. function _attr_as_int( element, name, defaultValue ) {
  2750. if ( element.hasAttribute( name ) ) {
  2751. return parseInt( element.getAttribute( name ), 10) ;
  2752. } else {
  2753. return defaultValue;
  2754. }
  2755. };
  2756. function _attr_as_string( element, name, defaultValue ) {
  2757. if ( element.hasAttribute( name ) ) {
  2758. return element.getAttribute( name );
  2759. } else {
  2760. return defaultValue;
  2761. }
  2762. };
  2763. function _format_float( f, num ) {
  2764. if ( f === undefined ) {
  2765. var s = '0.';
  2766. while ( s.length < num + 2 ) {
  2767. s += '0';
  2768. }
  2769. return s;
  2770. }
  2771. num = num || 2;
  2772. var parts = f.toString().split( '.' );
  2773. parts[ 1 ] = parts.length > 1 ? parts[ 1 ].substr( 0, num ) : "0";
  2774. while( parts[ 1 ].length < num ) {
  2775. parts[ 1 ] += '0';
  2776. }
  2777. return parts.join( '.' );
  2778. };
  2779. function extractDoubleSided( obj, element ) {
  2780. obj.doubleSided = false;
  2781. var node = element.querySelectorAll('extra double_sided')[0];
  2782. if ( node ) {
  2783. if ( node && parseInt( node.textContent, 10 ) === 1 ) {
  2784. obj.doubleSided = true;
  2785. }
  2786. }
  2787. };
  2788. // Up axis conversion
  2789. function setUpConversion() {
  2790. if ( options.convertUpAxis !== true || colladaUp === options.upAxis ) {
  2791. upConversion = null;
  2792. } else {
  2793. switch ( colladaUp ) {
  2794. case 'X':
  2795. upConversion = options.upAxis === 'Y' ? 'XtoY' : 'XtoZ';
  2796. break;
  2797. case 'Y':
  2798. upConversion = options.upAxis === 'X' ? 'YtoX' : 'YtoZ';
  2799. break;
  2800. case 'Z':
  2801. upConversion = options.upAxis === 'X' ? 'ZtoX' : 'ZtoY';
  2802. break;
  2803. }
  2804. }
  2805. };
  2806. function fixCoords( data, sign ) {
  2807. if ( options.convertUpAxis !== true || colladaUp === options.upAxis ) {
  2808. return;
  2809. }
  2810. switch ( upConversion ) {
  2811. case 'XtoY':
  2812. var tmp = data[ 0 ];
  2813. data[ 0 ] = sign * data[ 1 ];
  2814. data[ 1 ] = tmp;
  2815. break;
  2816. case 'XtoZ':
  2817. var tmp = data[ 2 ];
  2818. data[ 2 ] = data[ 1 ];
  2819. data[ 1 ] = data[ 0 ];
  2820. data[ 0 ] = tmp;
  2821. break;
  2822. case 'YtoX':
  2823. var tmp = data[ 0 ];
  2824. data[ 0 ] = data[ 1 ];
  2825. data[ 1 ] = sign * tmp;
  2826. break;
  2827. case 'YtoZ':
  2828. var tmp = data[ 1 ];
  2829. data[ 1 ] = sign * data[ 2 ];
  2830. data[ 2 ] = tmp;
  2831. break;
  2832. case 'ZtoX':
  2833. var tmp = data[ 0 ];
  2834. data[ 0 ] = data[ 1 ];
  2835. data[ 1 ] = data[ 2 ];
  2836. data[ 2 ] = tmp;
  2837. break;
  2838. case 'ZtoY':
  2839. var tmp = data[ 1 ];
  2840. data[ 1 ] = data[ 2 ];
  2841. data[ 2 ] = sign * tmp;
  2842. break;
  2843. }
  2844. };
  2845. function getConvertedTranslation( axis, data ) {
  2846. if ( options.convertUpAxis !== true || colladaUp === options.upAxis ) {
  2847. return data;
  2848. }
  2849. switch ( axis ) {
  2850. case 'X':
  2851. data = upConversion == 'XtoY' ? data * -1 : data;
  2852. break;
  2853. case 'Y':
  2854. data = upConversion == 'YtoZ' || upConversion == 'YtoX' ? data * -1 : data;
  2855. break;
  2856. case 'Z':
  2857. data = upConversion == 'ZtoY' ? data * -1 : data ;
  2858. break;
  2859. default:
  2860. break;
  2861. }
  2862. return data;
  2863. };
  2864. function getConvertedVec3( data, offset ) {
  2865. var arr = [ data[ offset ], data[ offset + 1 ], data[ offset + 2 ] ];
  2866. fixCoords( arr, -1 );
  2867. return new THREE.Vector3( arr[ 0 ], arr[ 1 ], arr[ 2 ] );
  2868. };
  2869. function getConvertedMat4( data ) {
  2870. if ( options.convertUpAxis ) {
  2871. // First fix rotation and scale
  2872. // Columns first
  2873. var arr = [ data[ 0 ], data[ 4 ], data[ 8 ] ];
  2874. fixCoords( arr, -1 );
  2875. data[ 0 ] = arr[ 0 ];
  2876. data[ 4 ] = arr[ 1 ];
  2877. data[ 8 ] = arr[ 2 ];
  2878. arr = [ data[ 1 ], data[ 5 ], data[ 9 ] ];
  2879. fixCoords( arr, -1 );
  2880. data[ 1 ] = arr[ 0 ];
  2881. data[ 5 ] = arr[ 1 ];
  2882. data[ 9 ] = arr[ 2 ];
  2883. arr = [ data[ 2 ], data[ 6 ], data[ 10 ] ];
  2884. fixCoords( arr, -1 );
  2885. data[ 2 ] = arr[ 0 ];
  2886. data[ 6 ] = arr[ 1 ];
  2887. data[ 10 ] = arr[ 2 ];
  2888. // Rows second
  2889. arr = [ data[ 0 ], data[ 1 ], data[ 2 ] ];
  2890. fixCoords( arr, -1 );
  2891. data[ 0 ] = arr[ 0 ];
  2892. data[ 1 ] = arr[ 1 ];
  2893. data[ 2 ] = arr[ 2 ];
  2894. arr = [ data[ 4 ], data[ 5 ], data[ 6 ] ];
  2895. fixCoords( arr, -1 );
  2896. data[ 4 ] = arr[ 0 ];
  2897. data[ 5 ] = arr[ 1 ];
  2898. data[ 6 ] = arr[ 2 ];
  2899. arr = [ data[ 8 ], data[ 9 ], data[ 10 ] ];
  2900. fixCoords( arr, -1 );
  2901. data[ 8 ] = arr[ 0 ];
  2902. data[ 9 ] = arr[ 1 ];
  2903. data[ 10 ] = arr[ 2 ];
  2904. // Now fix translation
  2905. arr = [ data[ 3 ], data[ 7 ], data[ 11 ] ];
  2906. fixCoords( arr, -1 );
  2907. data[ 3 ] = arr[ 0 ];
  2908. data[ 7 ] = arr[ 1 ];
  2909. data[ 11 ] = arr[ 2 ];
  2910. }
  2911. return new THREE.Matrix4(
  2912. data[0], data[1], data[2], data[3],
  2913. data[4], data[5], data[6], data[7],
  2914. data[8], data[9], data[10], data[11],
  2915. data[12], data[13], data[14], data[15]
  2916. );
  2917. };
  2918. function getConvertedIndex( index ) {
  2919. if ( index > -1 && index < 3 ) {
  2920. var members = ['X', 'Y', 'Z'],
  2921. indices = { X: 0, Y: 1, Z: 2 };
  2922. index = getConvertedMember( members[ index ] );
  2923. index = indices[ index ];
  2924. }
  2925. return index;
  2926. };
  2927. function getConvertedMember( member ) {
  2928. if ( options.convertUpAxis ) {
  2929. switch ( member ) {
  2930. case 'X':
  2931. switch ( upConversion ) {
  2932. case 'XtoY':
  2933. case 'XtoZ':
  2934. case 'YtoX':
  2935. member = 'Y';
  2936. break;
  2937. case 'ZtoX':
  2938. member = 'Z';
  2939. break;
  2940. }
  2941. break;
  2942. case 'Y':
  2943. switch ( upConversion ) {
  2944. case 'XtoY':
  2945. case 'YtoX':
  2946. case 'ZtoX':
  2947. member = 'X';
  2948. break;
  2949. case 'XtoZ':
  2950. case 'YtoZ':
  2951. case 'ZtoY':
  2952. member = 'Z';
  2953. break;
  2954. }
  2955. break;
  2956. case 'Z':
  2957. switch ( upConversion ) {
  2958. case 'XtoZ':
  2959. member = 'X';
  2960. break;
  2961. case 'YtoZ':
  2962. case 'ZtoX':
  2963. case 'ZtoY':
  2964. member = 'Y';
  2965. break;
  2966. }
  2967. break;
  2968. }
  2969. }
  2970. return member;
  2971. };
  2972. return {
  2973. load: load,
  2974. parse: parse,
  2975. setPreferredShading: setPreferredShading,
  2976. applySkin: applySkin,
  2977. geometries : geometries,
  2978. options: options
  2979. };
  2980. };