ColladaLoader.js 81 KB

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