ColladaLoader.js 87 KB

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