ColladaLoader.js 81 KB

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