ColladaLoader.js 79 KB

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