ColladaLoader.js 86 KB

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