ColladaLoader.js 87 KB

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