ColladaLoader.js 89 KB

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