ColladaLoader.js 81 KB

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