ColladaLoader.js 80 KB

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