ColladaLoader.js 87 KB

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