ColladaLoader.js 67 KB

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