ColladaLoader.js 81 KB

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