ColladaLoader.js 76 KB

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