ColladaLoader.js 82 KB

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