ColladaLoader.js 86 KB

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