ColladaLoader.js 86 KB

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