ColladaLoader.js 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432
  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 = new Polygons();
  1558. Polylist.prototype.constructor = Polylist;
  1559. function Triangles () {
  1560. Polygons.call( this );
  1561. this.vcount = 3;
  1562. };
  1563. Triangles.prototype = new Polygons();
  1564. Triangles.prototype.constructor = Triangles;
  1565. function Accessor() {
  1566. this.source = "";
  1567. this.count = 0;
  1568. this.stride = 0;
  1569. this.params = [];
  1570. };
  1571. Accessor.prototype.parse = function ( element ) {
  1572. this.params = [];
  1573. this.source = element.getAttribute( 'source' );
  1574. this.count = _attr_as_int( element, 'count', 0 );
  1575. this.stride = _attr_as_int( element, 'stride', 0 );
  1576. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1577. var child = element.childNodes[ i ];
  1578. if ( child.nodeName == 'param' ) {
  1579. var param = {};
  1580. param[ 'name' ] = child.getAttribute( 'name' );
  1581. param[ 'type' ] = child.getAttribute( 'type' );
  1582. this.params.push( param );
  1583. }
  1584. }
  1585. return this;
  1586. };
  1587. function Vertices() {
  1588. this.input = {};
  1589. };
  1590. Vertices.prototype.parse = function ( element ) {
  1591. this.id = element.getAttribute('id');
  1592. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1593. if ( element.childNodes[i].nodeName == 'input' ) {
  1594. var input = ( new Input() ).parse( element.childNodes[ i ] );
  1595. this.input[ input.semantic ] = input;
  1596. }
  1597. }
  1598. return this;
  1599. };
  1600. function Input () {
  1601. this.semantic = "";
  1602. this.offset = 0;
  1603. this.source = "";
  1604. this.set = 0;
  1605. };
  1606. Input.prototype.parse = function ( element ) {
  1607. this.semantic = element.getAttribute('semantic');
  1608. this.source = element.getAttribute('source').replace(/^#/, '');
  1609. this.set = _attr_as_int(element, 'set', -1);
  1610. this.offset = _attr_as_int(element, 'offset', 0);
  1611. if ( this.semantic == 'TEXCOORD' && this.set < 0 ) {
  1612. this.set = 0;
  1613. }
  1614. return this;
  1615. };
  1616. function Source ( id ) {
  1617. this.id = id;
  1618. this.type = null;
  1619. };
  1620. Source.prototype.parse = function ( element ) {
  1621. this.id = element.getAttribute( 'id' );
  1622. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1623. var child = element.childNodes[i];
  1624. switch ( child.nodeName ) {
  1625. case 'bool_array':
  1626. this.data = _bools( child.textContent );
  1627. this.type = child.nodeName;
  1628. break;
  1629. case 'float_array':
  1630. this.data = _floats( child.textContent );
  1631. this.type = child.nodeName;
  1632. break;
  1633. case 'int_array':
  1634. this.data = _ints( child.textContent );
  1635. this.type = child.nodeName;
  1636. break;
  1637. case 'IDREF_array':
  1638. case 'Name_array':
  1639. this.data = _strings( child.textContent );
  1640. this.type = child.nodeName;
  1641. break;
  1642. case 'technique_common':
  1643. for ( var j = 0; j < child.childNodes.length; j ++ ) {
  1644. if ( child.childNodes[ j ].nodeName == 'accessor' ) {
  1645. this.accessor = ( new Accessor() ).parse( child.childNodes[ j ] );
  1646. break;
  1647. }
  1648. }
  1649. break;
  1650. default:
  1651. // console.log(child.nodeName);
  1652. break;
  1653. }
  1654. }
  1655. return this;
  1656. };
  1657. Source.prototype.read = function () {
  1658. var result = [];
  1659. //for (var i = 0; i < this.accessor.params.length; i++) {
  1660. var param = this.accessor.params[ 0 ];
  1661. //console.log(param.name + " " + param.type);
  1662. switch ( param.type ) {
  1663. case 'IDREF':
  1664. case 'Name': case 'name':
  1665. case 'float':
  1666. return this.data;
  1667. case 'float4x4':
  1668. for ( var j = 0; j < this.data.length; j += 16 ) {
  1669. var s = this.data.slice( j, j + 16 );
  1670. var m = getConvertedMat4( s );
  1671. result.push( m );
  1672. }
  1673. break;
  1674. default:
  1675. console.log( 'ColladaLoader: Source: Read dont know how to read ' + param.type + '.' );
  1676. break;
  1677. }
  1678. //}
  1679. return result;
  1680. };
  1681. function Material () {
  1682. this.id = "";
  1683. this.name = "";
  1684. this.instance_effect = null;
  1685. };
  1686. Material.prototype.parse = function ( element ) {
  1687. this.id = element.getAttribute( 'id' );
  1688. this.name = element.getAttribute( 'name' );
  1689. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1690. if ( element.childNodes[ i ].nodeName == 'instance_effect' ) {
  1691. this.instance_effect = ( new InstanceEffect() ).parse( element.childNodes[ i ] );
  1692. break;
  1693. }
  1694. }
  1695. return this;
  1696. };
  1697. function ColorOrTexture () {
  1698. this.color = new THREE.Color( 0 );
  1699. this.color.setRGB( Math.random(), Math.random(), Math.random() );
  1700. this.color.a = 1.0;
  1701. this.texture = null;
  1702. this.texcoord = null;
  1703. this.texOpts = null;
  1704. };
  1705. ColorOrTexture.prototype.isColor = function () {
  1706. return ( this.texture == null );
  1707. };
  1708. ColorOrTexture.prototype.isTexture = function () {
  1709. return ( this.texture != null );
  1710. };
  1711. ColorOrTexture.prototype.parse = function ( element ) {
  1712. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1713. var child = element.childNodes[ i ];
  1714. if ( child.nodeType != 1 ) continue;
  1715. switch ( child.nodeName ) {
  1716. case 'color':
  1717. var rgba = _floats( child.textContent );
  1718. this.color = new THREE.Color(0);
  1719. this.color.setRGB( rgba[0], rgba[1], rgba[2] );
  1720. this.color.a = rgba[3];
  1721. break;
  1722. case 'texture':
  1723. this.texture = child.getAttribute('texture');
  1724. this.texcoord = child.getAttribute('texcoord');
  1725. // Defaults from:
  1726. // https://collada.org/mediawiki/index.php/Maya_texture_placement_MAYA_extension
  1727. this.texOpts = {
  1728. offsetU: 0,
  1729. offsetV: 0,
  1730. repeatU: 1,
  1731. repeatV: 1,
  1732. wrapU: 1,
  1733. wrapV: 1,
  1734. };
  1735. this.parseTexture( child );
  1736. break;
  1737. default:
  1738. break;
  1739. }
  1740. }
  1741. return this;
  1742. };
  1743. ColorOrTexture.prototype.parseTexture = function ( element ) {
  1744. if ( ! element.childNodes ) return this;
  1745. // This should be supported by Maya, 3dsMax, and MotionBuilder
  1746. if ( element.childNodes[1] && element.childNodes[1].nodeName === 'extra' ) {
  1747. element = element.childNodes[1];
  1748. if ( element.childNodes[1] && element.childNodes[1].nodeName === 'technique' ) {
  1749. element = element.childNodes[1];
  1750. }
  1751. }
  1752. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1753. var child = element.childNodes[ i ];
  1754. switch ( child.nodeName ) {
  1755. case 'offsetU':
  1756. case 'offsetV':
  1757. case 'repeatU':
  1758. case 'repeatV':
  1759. this.texOpts[ child.nodeName ] = parseFloat( child.textContent );
  1760. break;
  1761. case 'wrapU':
  1762. case 'wrapV':
  1763. this.texOpts[ child.nodeName ] = parseInt( child.textContent );
  1764. break;
  1765. default:
  1766. this.texOpts[ child.nodeName ] = child.textContent;
  1767. break;
  1768. }
  1769. }
  1770. return this;
  1771. };
  1772. function Shader ( type, effect ) {
  1773. this.type = type;
  1774. this.effect = effect;
  1775. this.material = null;
  1776. };
  1777. Shader.prototype.parse = function ( element ) {
  1778. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1779. var child = element.childNodes[ i ];
  1780. if ( child.nodeType != 1 ) continue;
  1781. switch ( child.nodeName ) {
  1782. case 'ambient':
  1783. case 'emission':
  1784. case 'diffuse':
  1785. case 'specular':
  1786. case 'transparent':
  1787. this[ child.nodeName ] = ( new ColorOrTexture() ).parse( child );
  1788. break;
  1789. case 'shininess':
  1790. case 'reflectivity':
  1791. case 'transparency':
  1792. var f = evaluateXPath( child, './/dae:float' );
  1793. if ( f.length > 0 )
  1794. this[ child.nodeName ] = parseFloat( f[ 0 ].textContent );
  1795. break;
  1796. default:
  1797. break;
  1798. }
  1799. }
  1800. this.create();
  1801. return this;
  1802. };
  1803. Shader.prototype.create = function() {
  1804. var props = {};
  1805. var transparent = ( this['transparency'] !== undefined && this['transparency'] < 1.0 );
  1806. for ( var prop in this ) {
  1807. switch ( prop ) {
  1808. case 'ambient':
  1809. case 'emission':
  1810. case 'diffuse':
  1811. case 'specular':
  1812. var cot = this[prop];
  1813. if ( cot instanceof ColorOrTexture ) {
  1814. if ( cot.isTexture() ) {
  1815. if ( this.effect.sampler && this.effect.surface ) {
  1816. if ( this.effect.sampler.source == this.effect.surface.sid ) {
  1817. var image = images[this.effect.surface.init_from];
  1818. if ( image ) {
  1819. var texture = THREE.ImageUtils.loadTexture(baseUrl + image.init_from);
  1820. texture.wrapS = cot.texOpts.wrapU ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  1821. texture.wrapT = cot.texOpts.wrapV ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  1822. texture.offset.x = cot.texOpts.offsetU;
  1823. texture.offset.y = cot.texOpts.offsetV;
  1824. texture.repeat.x = cot.texOpts.repeatU;
  1825. texture.repeat.y = cot.texOpts.repeatV;
  1826. props['map'] = texture;
  1827. // Texture with baked lighting?
  1828. if ( prop == 'emission' ) props[ 'emissive' ] = 0xffffff;
  1829. }
  1830. }
  1831. }
  1832. } else if ( prop == 'diffuse' || !transparent ) {
  1833. if ( prop == 'emission' ) {
  1834. props[ 'emissive' ] = cot.color.getHex();
  1835. } else {
  1836. props[ prop ] = cot.color.getHex();
  1837. }
  1838. }
  1839. }
  1840. break;
  1841. case 'shininess':
  1842. case 'reflectivity':
  1843. props[ prop ] = this[ prop ];
  1844. break;
  1845. case 'transparency':
  1846. if ( transparent ) {
  1847. props[ 'transparent' ] = true;
  1848. props[ 'opacity' ] = this[ prop ];
  1849. transparent = true;
  1850. }
  1851. break;
  1852. default:
  1853. break;
  1854. }
  1855. }
  1856. props[ 'shading' ] = preferredShading;
  1857. switch ( this.type ) {
  1858. case 'constant':
  1859. props.color = props.emission;
  1860. this.material = new THREE.MeshBasicMaterial( props );
  1861. break;
  1862. case 'phong':
  1863. case 'blinn':
  1864. props.color = props.diffuse;
  1865. this.material = new THREE.MeshPhongMaterial( props );
  1866. break;
  1867. case 'lambert':
  1868. default:
  1869. props.color = props.diffuse;
  1870. this.material = new THREE.MeshLambertMaterial( props );
  1871. break;
  1872. }
  1873. return this.material;
  1874. };
  1875. function Surface ( effect ) {
  1876. this.effect = effect;
  1877. this.init_from = null;
  1878. this.format = null;
  1879. };
  1880. Surface.prototype.parse = function ( element ) {
  1881. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1882. var child = element.childNodes[ i ];
  1883. if ( child.nodeType != 1 ) continue;
  1884. switch ( child.nodeName ) {
  1885. case 'init_from':
  1886. this.init_from = child.textContent;
  1887. break;
  1888. case 'format':
  1889. this.format = child.textContent;
  1890. break;
  1891. default:
  1892. console.log( "unhandled Surface prop: " + child.nodeName );
  1893. break;
  1894. }
  1895. }
  1896. return this;
  1897. };
  1898. function Sampler2D ( effect ) {
  1899. this.effect = effect;
  1900. this.source = null;
  1901. this.wrap_s = null;
  1902. this.wrap_t = null;
  1903. this.minfilter = null;
  1904. this.magfilter = null;
  1905. this.mipfilter = null;
  1906. };
  1907. Sampler2D.prototype.parse = function ( element ) {
  1908. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1909. var child = element.childNodes[ i ];
  1910. if ( child.nodeType != 1 ) continue;
  1911. switch ( child.nodeName ) {
  1912. case 'source':
  1913. this.source = child.textContent;
  1914. break;
  1915. case 'minfilter':
  1916. this.minfilter = child.textContent;
  1917. break;
  1918. case 'magfilter':
  1919. this.magfilter = child.textContent;
  1920. break;
  1921. case 'mipfilter':
  1922. this.mipfilter = child.textContent;
  1923. break;
  1924. case 'wrap_s':
  1925. this.wrap_s = child.textContent;
  1926. break;
  1927. case 'wrap_t':
  1928. this.wrap_t = child.textContent;
  1929. break;
  1930. default:
  1931. console.log( "unhandled Sampler2D prop: " + child.nodeName );
  1932. break;
  1933. }
  1934. }
  1935. return this;
  1936. };
  1937. function Effect () {
  1938. this.id = "";
  1939. this.name = "";
  1940. this.shader = null;
  1941. this.surface = null;
  1942. this.sampler = null;
  1943. };
  1944. Effect.prototype.create = function () {
  1945. if ( this.shader == null ) {
  1946. return null;
  1947. }
  1948. };
  1949. Effect.prototype.parse = function ( element ) {
  1950. this.id = element.getAttribute( 'id' );
  1951. this.name = element.getAttribute( 'name' );
  1952. this.shader = null;
  1953. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1954. var child = element.childNodes[ i ];
  1955. if ( child.nodeType != 1 ) continue;
  1956. switch ( child.nodeName ) {
  1957. case 'profile_COMMON':
  1958. this.parseTechnique( this.parseProfileCOMMON( child ) );
  1959. break;
  1960. default:
  1961. break;
  1962. }
  1963. }
  1964. return this;
  1965. };
  1966. Effect.prototype.parseNewparam = function ( element ) {
  1967. var sid = element.getAttribute( 'sid' );
  1968. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1969. var child = element.childNodes[ i ];
  1970. if ( child.nodeType != 1 ) continue;
  1971. switch ( child.nodeName ) {
  1972. case 'surface':
  1973. this.surface = ( new Surface( this ) ).parse( child );
  1974. this.surface.sid = sid;
  1975. break;
  1976. case 'sampler2D':
  1977. this.sampler = ( new Sampler2D( this ) ).parse( child );
  1978. this.sampler.sid = sid;
  1979. break;
  1980. case 'extra':
  1981. break;
  1982. default:
  1983. console.log( child.nodeName );
  1984. break;
  1985. }
  1986. }
  1987. };
  1988. Effect.prototype.parseProfileCOMMON = function ( element ) {
  1989. var technique;
  1990. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  1991. var child = element.childNodes[ i ];
  1992. if ( child.nodeType != 1 ) continue;
  1993. switch ( child.nodeName ) {
  1994. case 'profile_COMMON':
  1995. this.parseProfileCOMMON( child );
  1996. break;
  1997. case 'technique':
  1998. technique = child;
  1999. break;
  2000. case 'newparam':
  2001. this.parseNewparam( child );
  2002. break;
  2003. case 'image':
  2004. var _image = ( new _Image() ).parse( child );
  2005. images[ _image.id ] = _image;
  2006. break;
  2007. case 'extra':
  2008. break;
  2009. default:
  2010. console.log( child.nodeName );
  2011. break;
  2012. }
  2013. }
  2014. return technique;
  2015. };
  2016. Effect.prototype.parseTechnique= function ( element ) {
  2017. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2018. var child = element.childNodes[i];
  2019. if ( child.nodeType != 1 ) continue;
  2020. switch ( child.nodeName ) {
  2021. case 'constant':
  2022. case 'lambert':
  2023. case 'blinn':
  2024. case 'phong':
  2025. this.shader = ( new Shader( child.nodeName, this ) ).parse( child );
  2026. break;
  2027. default:
  2028. break;
  2029. }
  2030. }
  2031. };
  2032. function InstanceEffect () {
  2033. this.url = "";
  2034. };
  2035. InstanceEffect.prototype.parse = function ( element ) {
  2036. this.url = element.getAttribute( 'url' ).replace( /^#/, '' );
  2037. return this;
  2038. };
  2039. function Animation() {
  2040. this.id = "";
  2041. this.name = "";
  2042. this.source = {};
  2043. this.sampler = [];
  2044. this.channel = [];
  2045. };
  2046. Animation.prototype.parse = function ( element ) {
  2047. this.id = element.getAttribute( 'id' );
  2048. this.name = element.getAttribute( 'name' );
  2049. this.source = {};
  2050. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2051. var child = element.childNodes[ i ];
  2052. if ( child.nodeType != 1 ) continue;
  2053. switch ( child.nodeName ) {
  2054. case 'animation':
  2055. var anim = ( new Animation() ).parse( child );
  2056. for ( var src in anim.source ) {
  2057. this.source[ src ] = anim.source[ src ];
  2058. }
  2059. for ( var j = 0; j < anim.channel.length; j ++ ) {
  2060. this.channel.push( anim.channel[ j ] );
  2061. this.sampler.push( anim.sampler[ j ] );
  2062. }
  2063. break;
  2064. case 'source':
  2065. var src = ( new Source() ).parse( child );
  2066. this.source[ src.id ] = src;
  2067. break;
  2068. case 'sampler':
  2069. this.sampler.push( ( new Sampler( this ) ).parse( child ) );
  2070. break;
  2071. case 'channel':
  2072. this.channel.push( ( new Channel( this ) ).parse( child ) );
  2073. break;
  2074. default:
  2075. break;
  2076. }
  2077. }
  2078. return this;
  2079. };
  2080. function Channel( animation ) {
  2081. this.animation = animation;
  2082. this.source = "";
  2083. this.target = "";
  2084. this.fullSid = null;
  2085. this.sid = null;
  2086. this.dotSyntax = null;
  2087. this.arrSyntax = null;
  2088. this.arrIndices = null;
  2089. this.member = null;
  2090. };
  2091. Channel.prototype.parse = function ( element ) {
  2092. this.source = element.getAttribute( 'source' ).replace( /^#/, '' );
  2093. this.target = element.getAttribute( 'target' );
  2094. var parts = this.target.split( '/' );
  2095. var id = parts.shift();
  2096. var sid = parts.shift();
  2097. var dotSyntax = ( sid.indexOf(".") >= 0 );
  2098. var arrSyntax = ( sid.indexOf("(") >= 0 );
  2099. if ( dotSyntax ) {
  2100. parts = sid.split(".");
  2101. this.sid = parts.shift();
  2102. this.member = parts.shift();
  2103. } else if ( arrSyntax ) {
  2104. var arrIndices = sid.split("(");
  2105. this.sid = arrIndices.shift();
  2106. for (var j = 0; j < arrIndices.length; j ++ ) {
  2107. arrIndices[j] = parseInt( arrIndices[j].replace(/\)/, '') );
  2108. }
  2109. this.arrIndices = arrIndices;
  2110. } else {
  2111. this.sid = sid;
  2112. }
  2113. this.fullSid = sid;
  2114. this.dotSyntax = dotSyntax;
  2115. this.arrSyntax = arrSyntax;
  2116. return this;
  2117. };
  2118. function Sampler ( animation ) {
  2119. this.id = "";
  2120. this.animation = animation;
  2121. this.inputs = [];
  2122. this.input = null;
  2123. this.output = null;
  2124. this.strideOut = null;
  2125. this.interpolation = null;
  2126. this.startTime = null;
  2127. this.endTime = null;
  2128. this.duration = 0;
  2129. };
  2130. Sampler.prototype.parse = function ( element ) {
  2131. this.id = element.getAttribute( 'id' );
  2132. this.inputs = [];
  2133. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2134. var child = element.childNodes[ i ];
  2135. if ( child.nodeType != 1 ) continue;
  2136. switch ( child.nodeName ) {
  2137. case 'input':
  2138. this.inputs.push( (new Input()).parse( child ) );
  2139. break;
  2140. default:
  2141. break;
  2142. }
  2143. }
  2144. return this;
  2145. };
  2146. Sampler.prototype.create = function () {
  2147. for ( var i = 0; i < this.inputs.length; i ++ ) {
  2148. var input = this.inputs[ i ];
  2149. var source = this.animation.source[ input.source ];
  2150. switch ( input.semantic ) {
  2151. case 'INPUT':
  2152. this.input = source.read();
  2153. break;
  2154. case 'OUTPUT':
  2155. this.output = source.read();
  2156. this.strideOut = source.accessor.stride;
  2157. break;
  2158. case 'INTERPOLATION':
  2159. this.interpolation = source.read();
  2160. break;
  2161. case 'IN_TANGENT':
  2162. break;
  2163. case 'OUT_TANGENT':
  2164. break;
  2165. default:
  2166. console.log(input.semantic);
  2167. break;
  2168. }
  2169. }
  2170. this.startTime = 0;
  2171. this.endTime = 0;
  2172. this.duration = 0;
  2173. if ( this.input.length ) {
  2174. this.startTime = 100000000;
  2175. this.endTime = -100000000;
  2176. for ( var i = 0; i < this.input.length; i ++ ) {
  2177. this.startTime = Math.min( this.startTime, this.input[ i ] );
  2178. this.endTime = Math.max( this.endTime, this.input[ i ] );
  2179. }
  2180. this.duration = this.endTime - this.startTime;
  2181. }
  2182. };
  2183. Sampler.prototype.getData = function ( type, ndx ) {
  2184. var data;
  2185. if ( type === 'matrix' && this.strideOut === 16 ) {
  2186. data = this.output[ ndx ];
  2187. } else if ( this.strideOut > 1 ) {
  2188. data = [];
  2189. ndx *= this.strideOut;
  2190. for ( var i = 0; i < this.strideOut; ++i ) {
  2191. data[ i ] = this.output[ ndx + i ];
  2192. }
  2193. if ( this.strideOut === 3 ) {
  2194. switch ( type ) {
  2195. case 'rotate':
  2196. case 'translate':
  2197. fixCoords( data, -1 );
  2198. break;
  2199. case 'scale':
  2200. fixCoords( data, 1 );
  2201. break;
  2202. }
  2203. } else if ( this.strideOut === 4 && type === 'matrix' ) {
  2204. fixCoords( data, -1 );
  2205. }
  2206. } else {
  2207. data = this.output[ ndx ];
  2208. }
  2209. return data;
  2210. };
  2211. function Key ( time ) {
  2212. this.targets = [];
  2213. this.time = time;
  2214. };
  2215. Key.prototype.addTarget = function ( fullSid, transform, member, data ) {
  2216. this.targets.push( {
  2217. sid: fullSid,
  2218. member: member,
  2219. transform: transform,
  2220. data: data
  2221. } );
  2222. };
  2223. Key.prototype.apply = function ( opt_sid ) {
  2224. for ( var i = 0; i < this.targets.length; ++i ) {
  2225. var target = this.targets[ i ];
  2226. if ( !opt_sid || target.sid === opt_sid ) {
  2227. target.transform.update( target.data, target.member );
  2228. }
  2229. }
  2230. };
  2231. Key.prototype.getTarget = function ( fullSid ) {
  2232. for ( var i = 0; i < this.targets.length; ++i ) {
  2233. if ( this.targets[ i ].sid === fullSid ) {
  2234. return this.targets[ i ];
  2235. }
  2236. }
  2237. return null;
  2238. };
  2239. Key.prototype.hasTarget = function ( fullSid ) {
  2240. for ( var i = 0; i < this.targets.length; ++i ) {
  2241. if ( this.targets[ i ].sid === fullSid ) {
  2242. return true;
  2243. }
  2244. }
  2245. return false;
  2246. };
  2247. // TODO: Currently only doing linear interpolation. Should support full COLLADA spec.
  2248. Key.prototype.interpolate = function ( nextKey, time ) {
  2249. for ( var i = 0; i < this.targets.length; ++i ) {
  2250. var target = this.targets[ i ],
  2251. nextTarget = nextKey.getTarget( target.sid ),
  2252. data;
  2253. if ( target.transform.type !== 'matrix' && nextTarget ) {
  2254. var scale = ( time - this.time ) / ( nextKey.time - this.time ),
  2255. nextData = nextTarget.data,
  2256. prevData = target.data;
  2257. // check scale error
  2258. if ( scale < 0 || scale > 1 ) {
  2259. console.log( "Key.interpolate: Warning! Scale out of bounds:" + scale );
  2260. scale = scale < 0 ? 0 : 1;
  2261. }
  2262. if ( prevData.length ) {
  2263. data = [];
  2264. for ( var j = 0; j < prevData.length; ++j ) {
  2265. data[ j ] = prevData[ j ] + ( nextData[ j ] - prevData[ j ] ) * scale;
  2266. }
  2267. } else {
  2268. data = prevData + ( nextData - prevData ) * scale;
  2269. }
  2270. } else {
  2271. data = target.data;
  2272. }
  2273. target.transform.update( data, target.member );
  2274. }
  2275. };
  2276. function Camera() {
  2277. this.id = "";
  2278. this.name = "";
  2279. this.technique = "";
  2280. };
  2281. Camera.prototype.parse = function ( element ) {
  2282. this.id = element.getAttribute( 'id' );
  2283. this.name = element.getAttribute( 'name' );
  2284. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2285. var child = element.childNodes[ i ];
  2286. if ( child.nodeType != 1 ) continue;
  2287. switch ( child.nodeName ) {
  2288. case 'optics':
  2289. this.parseOptics( child );
  2290. break;
  2291. default:
  2292. break;
  2293. }
  2294. }
  2295. return this;
  2296. };
  2297. Camera.prototype.parseOptics = function ( element ) {
  2298. for ( var i = 0; i < element.childNodes.length; i ++ ) {
  2299. if ( element.childNodes[ i ].nodeName == 'technique_common' ) {
  2300. var technique = element.childNodes[ i ];
  2301. for ( var j = 0; j < technique.childNodes.length; j ++ ) {
  2302. this.technique = technique.childNodes[ j ].nodeName;
  2303. if ( this.technique == 'perspective' ) {
  2304. var perspective = technique.childNodes[ j ];
  2305. for ( var k = 0; k < perspective.childNodes.length; k ++ ) {
  2306. var param = perspective.childNodes[ k ];
  2307. switch ( param.nodeName ) {
  2308. case 'yfov':
  2309. this.yfov = param.textContent;
  2310. break;
  2311. case 'xfov':
  2312. this.xfov = param.textContent;
  2313. break;
  2314. case 'znear':
  2315. this.znear = param.textContent;
  2316. break;
  2317. case 'zfar':
  2318. this.zfar = param.textContent;
  2319. break;
  2320. case 'aspect_ratio':
  2321. this.aspect_ratio = param.textContent;
  2322. break;
  2323. }
  2324. }
  2325. } else if ( this.technique == 'orthographic' ) {
  2326. var orthographic = technique.childNodes[ j ];
  2327. for ( var k = 0; k < orthographic.childNodes.length; k ++ ) {
  2328. var param = orthographic.childNodes[ k ];
  2329. switch ( param.nodeName ) {
  2330. case 'xmag':
  2331. this.xmag = param.textContent;
  2332. break;
  2333. case 'ymag':
  2334. this.ymag = param.textContent;
  2335. break;
  2336. case 'znear':
  2337. this.znear = param.textContent;
  2338. break;
  2339. case 'zfar':
  2340. this.zfar = param.textContent;
  2341. break;
  2342. case 'aspect_ratio':
  2343. this.aspect_ratio = param.textContent;
  2344. break;
  2345. }
  2346. }
  2347. }
  2348. }
  2349. }
  2350. }
  2351. return this;
  2352. };
  2353. function InstanceCamera() {
  2354. this.url = "";
  2355. };
  2356. InstanceCamera.prototype.parse = function ( element ) {
  2357. this.url = element.getAttribute('url').replace(/^#/, '');
  2358. return this;
  2359. };
  2360. function _source( element ) {
  2361. var id = element.getAttribute( 'id' );
  2362. if ( sources[ id ] != undefined ) {
  2363. return sources[ id ];
  2364. }
  2365. sources[ id ] = ( new Source(id )).parse( element );
  2366. return sources[ id ];
  2367. };
  2368. function _nsResolver( nsPrefix ) {
  2369. if ( nsPrefix == "dae" ) {
  2370. return "http://www.collada.org/2005/11/COLLADASchema";
  2371. }
  2372. return null;
  2373. };
  2374. function _bools( str ) {
  2375. var raw = _strings( str );
  2376. var data = [];
  2377. for ( var i = 0, l = raw.length; i < l; i ++ ) {
  2378. data.push( (raw[i] == 'true' || raw[i] == '1') ? true : false );
  2379. }
  2380. return data;
  2381. };
  2382. function _floats( str ) {
  2383. var raw = _strings(str);
  2384. var data = [];
  2385. for ( var i = 0, l = raw.length; i < l; i ++ ) {
  2386. data.push( parseFloat( raw[ i ] ) );
  2387. }
  2388. return data;
  2389. };
  2390. function _ints( str ) {
  2391. var raw = _strings( str );
  2392. var data = [];
  2393. for ( var i = 0, l = raw.length; i < l; i ++ ) {
  2394. data.push( parseInt( raw[ i ], 10 ) );
  2395. }
  2396. return data;
  2397. };
  2398. function _strings( str ) {
  2399. return ( str.length > 0 ) ? _trimString( str ).split( /\s+/ ) : [];
  2400. };
  2401. function _trimString( str ) {
  2402. return str.replace( /^\s+/, "" ).replace( /\s+$/, "" );
  2403. };
  2404. function _attr_as_float( element, name, defaultValue ) {
  2405. if ( element.hasAttribute( name ) ) {
  2406. return parseFloat( element.getAttribute( name ) );
  2407. } else {
  2408. return defaultValue;
  2409. }
  2410. };
  2411. function _attr_as_int( element, name, defaultValue ) {
  2412. if ( element.hasAttribute( name ) ) {
  2413. return parseInt( element.getAttribute( name ), 10) ;
  2414. } else {
  2415. return defaultValue;
  2416. }
  2417. };
  2418. function _attr_as_string( element, name, defaultValue ) {
  2419. if ( element.hasAttribute( name ) ) {
  2420. return element.getAttribute( name );
  2421. } else {
  2422. return defaultValue;
  2423. }
  2424. };
  2425. function _format_float( f, num ) {
  2426. if ( f === undefined ) {
  2427. var s = '0.';
  2428. while ( s.length < num + 2 ) {
  2429. s += '0';
  2430. }
  2431. return s;
  2432. }
  2433. num = num || 2;
  2434. var parts = f.toString().split( '.' );
  2435. parts[ 1 ] = parts.length > 1 ? parts[ 1 ].substr( 0, num ) : "0";
  2436. while( parts[ 1 ].length < num ) {
  2437. parts[ 1 ] += '0';
  2438. }
  2439. return parts.join( '.' );
  2440. };
  2441. function evaluateXPath( node, query ) {
  2442. var instances = COLLADA.evaluate( query, node, _nsResolver, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
  2443. var inst = instances.iterateNext();
  2444. var result = [];
  2445. while ( inst ) {
  2446. result.push( inst );
  2447. inst = instances.iterateNext();
  2448. }
  2449. return result;
  2450. };
  2451. // Up axis conversion
  2452. function setUpConversion() {
  2453. if ( !options.convertUpAxis || colladaUp === options.upAxis ) {
  2454. upConversion = null;
  2455. } else {
  2456. switch ( colladaUp ) {
  2457. case 'X':
  2458. upConversion = options.upAxis === 'Y' ? 'XtoY' : 'XtoZ';
  2459. break;
  2460. case 'Y':
  2461. upConversion = options.upAxis === 'X' ? 'YtoX' : 'YtoZ';
  2462. break;
  2463. case 'Z':
  2464. upConversion = options.upAxis === 'X' ? 'ZtoX' : 'ZtoY';
  2465. break;
  2466. }
  2467. }
  2468. };
  2469. function fixCoords( data, sign ) {
  2470. if ( !options.convertUpAxis || colladaUp === options.upAxis ) {
  2471. return;
  2472. }
  2473. switch ( upConversion ) {
  2474. case 'XtoY':
  2475. var tmp = data[ 0 ];
  2476. data[ 0 ] = sign * data[ 1 ];
  2477. data[ 1 ] = tmp;
  2478. break;
  2479. case 'XtoZ':
  2480. var tmp = data[ 2 ];
  2481. data[ 2 ] = data[ 1 ];
  2482. data[ 1 ] = data[ 0 ];
  2483. data[ 0 ] = tmp;
  2484. break;
  2485. case 'YtoX':
  2486. var tmp = data[ 0 ];
  2487. data[ 0 ] = data[ 1 ];
  2488. data[ 1 ] = sign * tmp;
  2489. break;
  2490. case 'YtoZ':
  2491. var tmp = data[ 1 ];
  2492. data[ 1 ] = sign * data[ 2 ];
  2493. data[ 2 ] = tmp;
  2494. break;
  2495. case 'ZtoX':
  2496. var tmp = data[ 0 ];
  2497. data[ 0 ] = data[ 1 ];
  2498. data[ 1 ] = data[ 2 ];
  2499. data[ 2 ] = tmp;
  2500. break;
  2501. case 'ZtoY':
  2502. var tmp = data[ 1 ];
  2503. data[ 1 ] = data[ 2 ];
  2504. data[ 2 ] = sign * tmp;
  2505. break;
  2506. }
  2507. };
  2508. function getConvertedVec3( data, offset ) {
  2509. var arr = [ data[ offset ], data[ offset + 1 ], data[ offset + 2 ] ];
  2510. fixCoords( arr, -1 );
  2511. return new THREE.Vector3( arr[ 0 ], arr[ 1 ], arr[ 2 ] );
  2512. };
  2513. function getConvertedMat4( data ) {
  2514. if ( options.convertUpAxis ) {
  2515. // First fix rotation and scale
  2516. // Columns first
  2517. var arr = [ data[ 0 ], data[ 4 ], data[ 8 ] ];
  2518. fixCoords( arr, -1 );
  2519. data[ 0 ] = arr[ 0 ];
  2520. data[ 4 ] = arr[ 1 ];
  2521. data[ 8 ] = arr[ 2 ];
  2522. arr = [ data[ 1 ], data[ 5 ], data[ 9 ] ];
  2523. fixCoords( arr, -1 );
  2524. data[ 1 ] = arr[ 0 ];
  2525. data[ 5 ] = arr[ 1 ];
  2526. data[ 9 ] = arr[ 2 ];
  2527. arr = [ data[ 2 ], data[ 6 ], data[ 10 ] ];
  2528. fixCoords( arr, -1 );
  2529. data[ 2 ] = arr[ 0 ];
  2530. data[ 6 ] = arr[ 1 ];
  2531. data[ 10 ] = arr[ 2 ];
  2532. // Rows second
  2533. arr = [ data[ 0 ], data[ 1 ], data[ 2 ] ];
  2534. fixCoords( arr, -1 );
  2535. data[ 0 ] = arr[ 0 ];
  2536. data[ 1 ] = arr[ 1 ];
  2537. data[ 2 ] = arr[ 2 ];
  2538. arr = [ data[ 4 ], data[ 5 ], data[ 6 ] ];
  2539. fixCoords( arr, -1 );
  2540. data[ 4 ] = arr[ 0 ];
  2541. data[ 5 ] = arr[ 1 ];
  2542. data[ 6 ] = arr[ 2 ];
  2543. arr = [ data[ 8 ], data[ 9 ], data[ 10 ] ];
  2544. fixCoords( arr, -1 );
  2545. data[ 8 ] = arr[ 0 ];
  2546. data[ 9 ] = arr[ 1 ];
  2547. data[ 10 ] = arr[ 2 ];
  2548. // Now fix translation
  2549. arr = [ data[ 3 ], data[ 7 ], data[ 11 ] ];
  2550. fixCoords( arr, -1 );
  2551. data[ 3 ] = arr[ 0 ];
  2552. data[ 7 ] = arr[ 1 ];
  2553. data[ 11 ] = arr[ 2 ];
  2554. }
  2555. return new THREE.Matrix4(
  2556. data[0], data[1], data[2], data[3],
  2557. data[4], data[5], data[6], data[7],
  2558. data[8], data[9], data[10], data[11],
  2559. data[12], data[13], data[14], data[15]
  2560. );
  2561. };
  2562. function getConvertedIndex( index ) {
  2563. if ( index > -1 && index < 3 ) {
  2564. var members = ['X', 'Y', 'Z'],
  2565. indices = { X: 0, Y: 1, Z: 2 };
  2566. index = getConvertedMember( members[ index ] );
  2567. index = indices[ index ];
  2568. }
  2569. return index;
  2570. };
  2571. function getConvertedMember( member ) {
  2572. if ( options.convertUpAxis ) {
  2573. switch ( member ) {
  2574. case 'X':
  2575. switch ( upConversion ) {
  2576. case 'XtoY':
  2577. case 'XtoZ':
  2578. case 'YtoX':
  2579. member = 'Y';
  2580. break;
  2581. case 'ZtoX':
  2582. member = 'Z';
  2583. break;
  2584. }
  2585. break;
  2586. case 'Y':
  2587. switch ( upConversion ) {
  2588. case 'XtoY':
  2589. case 'YtoX':
  2590. case 'ZtoX':
  2591. member = 'X';
  2592. break;
  2593. case 'XtoZ':
  2594. case 'YtoZ':
  2595. case 'ZtoY':
  2596. member = 'Z';
  2597. break;
  2598. }
  2599. break;
  2600. case 'Z':
  2601. switch ( upConversion ) {
  2602. case 'XtoZ':
  2603. member = 'X';
  2604. break;
  2605. case 'YtoZ':
  2606. case 'ZtoX':
  2607. case 'ZtoY':
  2608. member = 'Y';
  2609. break;
  2610. }
  2611. break;
  2612. }
  2613. }
  2614. return member;
  2615. };
  2616. return {
  2617. load: load,
  2618. parse: parse,
  2619. setPreferredShading: setPreferredShading,
  2620. applySkin: applySkin,
  2621. geometries : geometries,
  2622. options: options
  2623. };
  2624. };