ColladaLoader.js 81 KB

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