ColladaLoader.js 80 KB

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