ColladaLoader.js 80 KB

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