ColladaLoader.js 100 KB

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