ColladaLoader.js 79 KB

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