ColladaLoader.js 75 KB

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