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