ColladaLoader.js 81 KB

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