ColladaLoader.js 100 KB

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