ColladaLoader2.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author Mugen87 / https://github.com/Mugen87
  4. */
  5. THREE.ColladaLoader = function ( manager ) {
  6. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7. };
  8. THREE.ColladaLoader.prototype = {
  9. constructor: THREE.ColladaLoader,
  10. load: function ( url, onLoad, onProgress, onError ) {
  11. var scope = this;
  12. var resourceDirectory = url.split( /[\\\/]/ );
  13. resourceDirectory.pop();
  14. resourceDirectory = resourceDirectory.join( '/' ) + '/';
  15. var loader = new THREE.FileLoader( scope.manager );
  16. loader.load( url, function ( text ) {
  17. onLoad( scope.parse( text, resourceDirectory ) );
  18. }, onProgress, onError );
  19. },
  20. options: {
  21. set convertUpAxis( value ) {
  22. console.log( 'THREE.ColladaLoader.options.convertUpAxis: TODO' );
  23. }
  24. },
  25. setCrossOrigin: function ( value ) {
  26. this.crossOrigin = value;
  27. },
  28. parse: function ( text, resourceDirectory ) {
  29. function getElementsByTagName( xml, name ) {
  30. // Non recursive xml.getElementsByTagName() ...
  31. var array = [];
  32. var childNodes = xml.childNodes;
  33. for ( var i = 0, l = childNodes.length; i < l; i ++ ) {
  34. var child = childNodes[ i ];
  35. if ( child.nodeName === name ) {
  36. array.push( child );
  37. }
  38. }
  39. return array;
  40. }
  41. function parseStrings( text ) {
  42. if ( text.length === 0 ) return [];
  43. var parts = text.trim().split( /\s+/ );
  44. var array = new Array( parts.length );
  45. for ( var i = 0, l = parts.length; i < l; i ++ ) {
  46. array[ i ] = parts[ i ];
  47. }
  48. return array;
  49. }
  50. function parseFloats( text ) {
  51. if ( text.length === 0 ) return [];
  52. var parts = text.trim().split( /\s+/ );
  53. var array = new Array( parts.length );
  54. for ( var i = 0, l = parts.length; i < l; i ++ ) {
  55. array[ i ] = parseFloat( parts[ i ] );
  56. }
  57. return array;
  58. }
  59. function parseInts( text ) {
  60. if ( text.length === 0 ) return [];
  61. var parts = text.trim().split( /\s+/ );
  62. var array = new Array( parts.length );
  63. for ( var i = 0, l = parts.length; i < l; i ++ ) {
  64. array[ i ] = parseInt( parts[ i ] );
  65. }
  66. return array;
  67. }
  68. function parseId( text ) {
  69. return text.substring( 1 );
  70. }
  71. // asset
  72. function parseAsset( xml ) {
  73. return {
  74. unit: parseAssetUnit( getElementsByTagName( xml, 'unit' )[ 0 ] ),
  75. upAxis: parseAssetUpAxis( getElementsByTagName( xml, 'up_axis' )[ 0 ] )
  76. };
  77. }
  78. function parseAssetUnit( xml ) {
  79. return xml !== undefined ? parseFloat( xml.getAttribute( 'meter' ) ) : 1;
  80. }
  81. function parseAssetUpAxis( xml ) {
  82. return xml !== undefined ? xml.textContent : 'Y_UP';
  83. }
  84. // library
  85. function parseLibrary( xml, libraryName, nodeName, parser ) {
  86. var library = getElementsByTagName( xml, libraryName )[ 0 ];
  87. if ( library !== undefined ) {
  88. var elements = getElementsByTagName( library, nodeName );
  89. for ( var i = 0; i < elements.length; i ++ ) {
  90. parser( elements[ i ] );
  91. }
  92. }
  93. }
  94. function buildLibrary( data, builder ) {
  95. for ( var name in data ) {
  96. var object = data[ name ];
  97. object.build = builder( data[ name ] );
  98. }
  99. }
  100. // get
  101. function getBuild( data, builder ) {
  102. if ( data.build !== undefined ) return data.build;
  103. data.build = builder( data );
  104. return data.build;
  105. }
  106. // animation
  107. function parseAnimation( xml ) {
  108. var data = {
  109. sources: {},
  110. samplers: {},
  111. channels: {}
  112. };
  113. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  114. var child = xml.childNodes[ i ];
  115. if ( child.nodeType !== 1 ) continue;
  116. var id;
  117. switch ( child.nodeName ) {
  118. case 'source':
  119. id = child.getAttribute( 'id' );
  120. data.sources[ id ] = parseSource( child );
  121. break;
  122. case 'sampler':
  123. id = child.getAttribute( 'id' );
  124. data.samplers[ id ] = parseAnimationSampler( child );
  125. break;
  126. case 'channel':
  127. id = child.getAttribute( 'target' );
  128. data.channels[ id ] = parseAnimationChannel( child );
  129. break;
  130. default:
  131. console.log( child );
  132. }
  133. }
  134. library.animations[ xml.getAttribute( 'id' ) ] = data;
  135. }
  136. function parseAnimationSampler( xml ) {
  137. var data = {
  138. inputs: {},
  139. };
  140. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  141. var child = xml.childNodes[ i ];
  142. if ( child.nodeType !== 1 ) continue;
  143. switch ( child.nodeName ) {
  144. case 'input':
  145. var id = parseId( child.getAttribute( 'source' ) );
  146. var semantic = child.getAttribute( 'semantic' );
  147. data.inputs[ semantic ] = id;
  148. break;
  149. }
  150. }
  151. return data;
  152. }
  153. function parseAnimationChannel( xml ) {
  154. var data = {};
  155. var target = xml.getAttribute( 'target' );
  156. // parsing SID Addressing Syntax
  157. var parts = target.split( '/' );
  158. var id = parts.shift();
  159. var sid = parts.shift();
  160. // check selection syntax
  161. var arraySyntax = ( sid.indexOf( '(' ) !== - 1 );
  162. var memberSyntax = ( sid.indexOf( '.' ) !== - 1 );
  163. if ( memberSyntax ) {
  164. // member selection access
  165. parts = sid.split( '.' );
  166. sid = parts.shift();
  167. data.member = parts.shift();
  168. } else if ( arraySyntax ) {
  169. // array-access syntax. can be used to express fields in one-dimensional vectors or two-dimensional matrices.
  170. var indices = sid.split( '(' );
  171. sid = indices.shift();
  172. for ( var i = 0; i < indices.length; i ++ ) {
  173. indices[ i ] = parseInt( indices[ i ].replace( /\)/, '' ) );
  174. }
  175. data.indices = indices;
  176. }
  177. data.id = id;
  178. data.sid = sid;
  179. data.arraySyntax = arraySyntax;
  180. data.memberSyntax = memberSyntax;
  181. data.sampler = parseId( xml.getAttribute( 'source' ) );
  182. return data;
  183. }
  184. // controller
  185. function parseController( xml ) {
  186. var data = {};
  187. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  188. var child = xml.childNodes[ i ];
  189. if ( child.nodeType !== 1 ) continue;
  190. switch ( child.nodeName ) {
  191. case 'skin':
  192. // there is exactly one skin per controller
  193. data.id = parseId( child.getAttribute( 'source' ) );
  194. data.skin = parseSkin( child );
  195. break;
  196. }
  197. }
  198. library.controllers[ xml.getAttribute( 'id' ) ] = data;
  199. }
  200. function parseSkin( xml ) {
  201. var data = {
  202. sources: {}
  203. }
  204. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  205. var child = xml.childNodes[ i ];
  206. if ( child.nodeType !== 1 ) continue;
  207. switch ( child.nodeName ) {
  208. case 'bind_shape_matrix':
  209. data.bindShapeMatrix = parseFloats( child.textContent );
  210. break;
  211. case 'source':
  212. var id = child.getAttribute( 'id' );
  213. data.sources[ id ] = parseSource( child );
  214. break;
  215. case 'joints':
  216. data.joints = parseJoints( child );
  217. break;
  218. case 'vertex_weights':
  219. data.vertexWeights = parseVertexWeights( child );
  220. break;
  221. }
  222. }
  223. return data;
  224. }
  225. function parseJoints( xml ) {
  226. var data = {
  227. inputs: {}
  228. }
  229. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  230. var child = xml.childNodes[ i ];
  231. if ( child.nodeType !== 1 ) continue;
  232. switch ( child.nodeName ) {
  233. case 'input':
  234. var semantic = child.getAttribute( 'semantic' );
  235. var id = parseId( child.getAttribute( 'source' ) );
  236. data.inputs[ semantic ] = id;
  237. break;
  238. }
  239. }
  240. return data;
  241. }
  242. function parseVertexWeights( xml ) {
  243. var data = {
  244. inputs: {}
  245. }
  246. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  247. var child = xml.childNodes[ i ];
  248. if ( child.nodeType !== 1 ) continue;
  249. switch ( child.nodeName ) {
  250. case 'input':
  251. var semantic = child.getAttribute( 'semantic' );
  252. var id = parseId( child.getAttribute( 'source' ) );
  253. var offset = parseInt( child.getAttribute( 'offset' ) );
  254. data.inputs[ semantic ] = { id: id, offset: offset };
  255. break;
  256. case 'vcount':
  257. data.vcount = parseInts( child.textContent );
  258. break;
  259. case 'v':
  260. data.v = parseInts( child.textContent );
  261. break;
  262. }
  263. }
  264. return data;
  265. }
  266. function buildController( data ) {
  267. var build = {};
  268. build.id = data.id;
  269. build.skin = buildSkin( data.skin );
  270. // we enhance the 'sources' property of the corresponding geometry with our skin data
  271. var geometry = library.geometries[ build.id ];
  272. geometry.sources.skinIndices = build.skin.indices;
  273. geometry.sources.skinWeights = build.skin.weights;
  274. return build;
  275. }
  276. function buildSkin( data ) {
  277. var BONE_LIMIT = 4;
  278. var build = {
  279. bones: {},
  280. indices: {
  281. array: [],
  282. stride: BONE_LIMIT
  283. },
  284. weights: {
  285. array: [],
  286. stride: BONE_LIMIT
  287. }
  288. };
  289. var sources = data.sources;
  290. var vertexWeights = data.vertexWeights;
  291. var vcount = vertexWeights.vcount;
  292. var v = vertexWeights.v;
  293. var jointOffset = vertexWeights.inputs.JOINT.offset;
  294. var weightOffset = vertexWeights.inputs.WEIGHT.offset;
  295. var jointSource = data.sources[ data.joints.inputs.JOINT ];
  296. var inverseSource = data.sources[ data.joints.inputs.INV_BIND_MATRIX ];
  297. var weights = sources[ vertexWeights.inputs.WEIGHT.id ].array;
  298. var stride = 0;
  299. var i, j, l;
  300. // procces skin data for each vertex
  301. for ( i = 0, l = vcount.length; i < l; i ++ ) {
  302. var jointCount = vcount[ i ]; // this is the amount of joints that affect a single vertex
  303. var vertexSkinData = [];
  304. for ( j = 0; j < jointCount; j ++ ) {
  305. var skinIndex = v[ stride + jointOffset ];
  306. var weightId = v[ stride + weightOffset ];
  307. var skinWeight = weights[ weightId ];
  308. vertexSkinData.push( { index: skinIndex, weight: skinWeight } );
  309. stride += 2;
  310. }
  311. // we sort the joints in descending order based on the weights.
  312. // this ensures, we only procced the most important joints of the vertex
  313. vertexSkinData.sort( descending );
  314. // now we provide for each vertex a set of four index and weight values.
  315. // the order of the skin data matches the order of vertices
  316. for ( j = 0; j < BONE_LIMIT; j ++ ) {
  317. var d = vertexSkinData[ j ];
  318. if ( d !== undefined ) {
  319. build.indices.array.push( d.index );
  320. build.weights.array.push( d.weight );
  321. } else {
  322. build.indices.array.push( 0 );
  323. build.weights.array.push( 0 );
  324. }
  325. }
  326. }
  327. // setup bind matrix
  328. build.bindMatrix = new THREE.Matrix4().fromArray( data.bindShapeMatrix ).transpose();
  329. // process bones and inverse bind matrix data
  330. for ( i = 0, l = jointSource.array.length; i < l; i ++ ) {
  331. var name = jointSource.array[ i ];
  332. var boneInverse = new THREE.Matrix4().fromArray( inverseSource.array, i * inverseSource.stride ).transpose();
  333. build.bones[ name ] = boneInverse;
  334. }
  335. return build;
  336. // array sort function
  337. function descending( a, b ) {
  338. return b.weight - a.weight;
  339. }
  340. }
  341. function getController( id ) {
  342. return getBuild( library.controllers[ id ], buildController );
  343. }
  344. // image
  345. function parseImage( xml ) {
  346. var data = {
  347. init_from: getElementsByTagName( xml, 'init_from' )[ 0 ].textContent
  348. };
  349. library.images[ xml.getAttribute( 'id' ) ] = data;
  350. }
  351. function buildImage( data ) {
  352. if ( data.build !== undefined ) return data.build;
  353. return data.init_from;
  354. }
  355. function getImage( id ) {
  356. return getBuild( library.images[ id ], buildImage );
  357. }
  358. // effect
  359. function parseEffect( xml ) {
  360. var data = {};
  361. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  362. var child = xml.childNodes[ i ];
  363. if ( child.nodeType !== 1 ) continue;
  364. switch ( child.nodeName ) {
  365. case 'profile_COMMON':
  366. data.profile = parseEffectProfileCOMMON( child );
  367. break;
  368. }
  369. }
  370. library.effects[ xml.getAttribute( 'id' ) ] = data;
  371. }
  372. function parseEffectProfileCOMMON( xml ) {
  373. var data = {
  374. surfaces: {},
  375. samplers: {}
  376. };
  377. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  378. var child = xml.childNodes[ i ];
  379. if ( child.nodeType !== 1 ) continue;
  380. switch ( child.nodeName ) {
  381. case 'newparam':
  382. parseEffectNewparam( child, data );
  383. break;
  384. case 'technique':
  385. data.technique = parseEffectTechnique( child );
  386. break;
  387. }
  388. }
  389. return data;
  390. }
  391. function parseEffectNewparam( xml, data ) {
  392. var sid = xml.getAttribute( 'sid' );
  393. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  394. var child = xml.childNodes[ i ];
  395. if ( child.nodeType !== 1 ) continue;
  396. switch ( child.nodeName ) {
  397. case 'surface':
  398. data.surfaces[ sid ] = parseEffectSurface( child );
  399. break;
  400. case 'sampler2D':
  401. data.samplers[ sid ] = parseEffectSampler( child );
  402. break;
  403. }
  404. }
  405. }
  406. function parseEffectSurface( xml ) {
  407. var data = {};
  408. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  409. var child = xml.childNodes[ i ];
  410. if ( child.nodeType !== 1 ) continue;
  411. switch ( child.nodeName ) {
  412. case 'init_from':
  413. data.init_from = child.textContent;
  414. break;
  415. }
  416. }
  417. return data;
  418. }
  419. function parseEffectSampler( xml ) {
  420. var data = {};
  421. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  422. var child = xml.childNodes[ i ];
  423. if ( child.nodeType !== 1 ) continue;
  424. switch ( child.nodeName ) {
  425. case 'source':
  426. data.source = child.textContent;
  427. break;
  428. }
  429. }
  430. return data;
  431. }
  432. function parseEffectTechnique( xml ) {
  433. var data = {};
  434. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  435. var child = xml.childNodes[ i ];
  436. if ( child.nodeType !== 1 ) continue;
  437. switch ( child.nodeName ) {
  438. case 'constant':
  439. case 'lambert':
  440. case 'blinn':
  441. case 'phong':
  442. data.type = child.nodeName;
  443. data.parameters = parseEffectParameters( child );
  444. break;
  445. }
  446. }
  447. return data;
  448. }
  449. function parseEffectParameters( xml ) {
  450. var data = {};
  451. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  452. var child = xml.childNodes[ i ];
  453. if ( child.nodeType !== 1 ) continue;
  454. switch ( child.nodeName ) {
  455. case 'emission':
  456. case 'diffuse':
  457. case 'specular':
  458. case 'shininess':
  459. case 'transparent':
  460. case 'transparency':
  461. data[ child.nodeName ] = parseEffectParameter( child );
  462. break;
  463. }
  464. }
  465. return data;
  466. }
  467. function parseEffectParameter( xml ) {
  468. var data = {};
  469. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  470. var child = xml.childNodes[ i ];
  471. if ( child.nodeType !== 1 ) continue;
  472. switch ( child.nodeName ) {
  473. case 'color':
  474. data[ child.nodeName ] = parseFloats( child.textContent );
  475. break;
  476. case 'float':
  477. data[ child.nodeName ] = parseFloat( child.textContent );
  478. break;
  479. case 'texture':
  480. data[ child.nodeName ] = { id: child.getAttribute( 'texture' ), extra: parseEffectParameterTexture( child ) };
  481. break;
  482. }
  483. }
  484. return data;
  485. }
  486. function parseEffectParameterTexture( xml ) {
  487. var data = {};
  488. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  489. var child = xml.childNodes[ i ];
  490. if ( child.nodeType !== 1 ) continue;
  491. switch ( child.nodeName ) {
  492. case 'extra':
  493. data = parseEffectParameterTextureExtra( child );
  494. break;
  495. }
  496. }
  497. return data;
  498. }
  499. function parseEffectParameterTextureExtra( xml ) {
  500. var data = {};
  501. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  502. var child = xml.childNodes[ i ];
  503. if ( child.nodeType !== 1 ) continue;
  504. switch ( child.nodeName ) {
  505. case 'technique':
  506. data[ child.nodeName ] = parseEffectParameterTextureExtraTechnique( child );
  507. break;
  508. }
  509. }
  510. return data;
  511. }
  512. function parseEffectParameterTextureExtraTechnique( xml ) {
  513. var data = {};
  514. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  515. var child = xml.childNodes[ i ];
  516. if ( child.nodeType !== 1 ) continue;
  517. switch ( child.nodeName ) {
  518. case 'repeatU':
  519. case 'repeatV':
  520. case 'offsetU':
  521. case 'offsetV':
  522. data[ child.nodeName ] = parseFloat( child.textContent );
  523. break;
  524. case 'wrapU':
  525. case 'wrapV':
  526. data[ child.nodeName ] = parseInt( child.textContent );
  527. break;
  528. }
  529. }
  530. return data;
  531. }
  532. function buildEffect( data ) {
  533. return data;
  534. }
  535. function getEffect( id ) {
  536. return getBuild( library.effects[ id ], buildEffect );
  537. }
  538. // material
  539. function parseMaterial( xml ) {
  540. var data = {
  541. name: xml.getAttribute( 'name' )
  542. };
  543. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  544. var child = xml.childNodes[ i ];
  545. if ( child.nodeType !== 1 ) continue;
  546. switch ( child.nodeName ) {
  547. case 'instance_effect':
  548. data.url = parseId( child.getAttribute( 'url' ) );
  549. break;
  550. }
  551. }
  552. library.materials[ xml.getAttribute( 'id' ) ] = data;
  553. }
  554. function buildMaterial( data ) {
  555. var effect = getEffect( data.url );
  556. var technique = effect.profile.technique;
  557. var material;
  558. switch ( technique.type ) {
  559. case 'phong':
  560. case 'blinn':
  561. material = new THREE.MeshPhongMaterial();
  562. break;
  563. case 'lambert':
  564. material = new THREE.MeshLambertMaterial();
  565. break;
  566. default:
  567. material = new THREE.MeshBasicMaterial();
  568. break;
  569. }
  570. material.name = data.name;
  571. function getTexture( textureObject ) {
  572. var sampler = effect.profile.samplers[ textureObject.id ];
  573. if ( sampler !== undefined ) {
  574. var surface = effect.profile.surfaces[ sampler.source ];
  575. var texture = textureLoader.load( getImage( surface.init_from ) );
  576. var extra = textureObject.extra;
  577. if ( extra !== undefined && extra.technique !== undefined ) {
  578. var technique = extra.technique;
  579. texture.wrapS = technique.wrapU ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  580. texture.wrapT = technique.wrapV ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  581. texture.offset.set( technique.offsetU || 0, technique.offsetV || 0 );
  582. texture.repeat.set( technique.repeatU || 1, technique.repeatV || 1 );
  583. } else {
  584. texture.wrapS = THREE.RepeatWrapping;
  585. texture.wrapT = THREE.RepeatWrapping;
  586. }
  587. return texture;
  588. }
  589. console.error( 'THREE.ColladaLoader: Undefined sampler', textureObject.id );
  590. return null;
  591. }
  592. var parameters = technique.parameters;
  593. for ( var key in parameters ) {
  594. var parameter = parameters[ key ];
  595. switch ( key ) {
  596. case 'diffuse':
  597. if ( parameter.color ) material.color.fromArray( parameter.color );
  598. if ( parameter.texture ) material.map = getTexture( parameter.texture );
  599. break;
  600. case 'specular':
  601. if ( parameter.color && material.specular )
  602. material.specular.fromArray( parameter.color );
  603. break;
  604. case 'shininess':
  605. if ( parameter.float && material.shininess )
  606. material.shininess = parameter.float;
  607. break;
  608. case 'emission':
  609. if ( parameter.color && material.emissive )
  610. material.emissive.fromArray( parameter.color );
  611. break;
  612. case 'transparent':
  613. // if ( parameter.texture ) material.alphaMap = getTexture( parameter.texture );
  614. material.transparent = true;
  615. break;
  616. case 'transparency':
  617. if ( parameter.float !== undefined ) material.opacity = parameter.float;
  618. material.transparent = true;
  619. break;
  620. }
  621. }
  622. return material;
  623. }
  624. function getMaterial( id ) {
  625. return getBuild( library.materials[ id ], buildMaterial );
  626. }
  627. // camera
  628. function parseCamera( xml ) {
  629. var data = {
  630. name: xml.getAttribute( 'name' )
  631. };
  632. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  633. var child = xml.childNodes[ i ];
  634. if ( child.nodeType !== 1 ) continue;
  635. switch ( child.nodeName ) {
  636. case 'optics':
  637. data.optics = parseCameraOptics( child );
  638. break;
  639. }
  640. }
  641. library.cameras[ xml.getAttribute( 'id' ) ] = data;
  642. }
  643. function parseCameraOptics( xml ) {
  644. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  645. var child = xml.childNodes[ i ];
  646. switch ( child.nodeName ) {
  647. case 'technique_common':
  648. return parseCameraTechnique( child );
  649. }
  650. }
  651. return {};
  652. }
  653. function parseCameraTechnique( xml ) {
  654. var data = {};
  655. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  656. var child = xml.childNodes[ i ];
  657. switch ( child.nodeName ) {
  658. case 'perspective':
  659. case 'orthographic':
  660. data.technique = child.nodeName;
  661. data.parameters = parseCameraParameters( child );
  662. break;
  663. }
  664. }
  665. return data;
  666. }
  667. function parseCameraParameters( xml ) {
  668. var data = {};
  669. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  670. var child = xml.childNodes[ i ];
  671. switch ( child.nodeName ) {
  672. case 'xfov':
  673. case 'yfov':
  674. case 'xmag':
  675. case 'ymag':
  676. case 'znear':
  677. case 'zfar':
  678. case 'aspect_ratio':
  679. data[ child.nodeName ] = parseFloat( child.textContent );
  680. break;
  681. }
  682. }
  683. return data;
  684. }
  685. function buildCamera( data ) {
  686. var camera;
  687. switch ( data.optics.technique ) {
  688. case 'perspective':
  689. camera = new THREE.PerspectiveCamera(
  690. data.optics.parameters.yfov,
  691. data.optics.parameters.aspect_ratio,
  692. data.optics.parameters.znear,
  693. data.optics.parameters.zfar
  694. );
  695. break;
  696. case 'orthographic':
  697. var ymag = data.optics.parameters.ymag;
  698. var xmag = data.optics.parameters.xmag;
  699. var aspectRatio = data.optics.parameters.aspect_ratio;
  700. xmag = ( xmag === undefined ) ? ( ymag * aspectRatio ) : xmag;
  701. ymag = ( ymag === undefined ) ? ( xmag / aspectRatio ) : ymag;
  702. xmag *= 0.5;
  703. ymag *= 0.5;
  704. camera = new THREE.OrthographicCamera(
  705. - xmag, xmag, ymag, - ymag, // left, right, top, bottom
  706. data.optics.parameters.znear,
  707. data.optics.parameters.zfar
  708. );
  709. break;
  710. default:
  711. camera = new THREE.PerspectiveCamera();
  712. break;
  713. }
  714. camera.name = data.name;
  715. return camera;
  716. }
  717. function getCamera( id ) {
  718. return getBuild( library.cameras[ id ], buildCamera );
  719. }
  720. // light
  721. function parseLight( xml ) {
  722. var data = {};
  723. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  724. var child = xml.childNodes[ i ];
  725. if ( child.nodeType !== 1 ) continue;
  726. switch ( child.nodeName ) {
  727. case 'technique_common':
  728. data = parseLightTechnique( child );
  729. break;
  730. }
  731. }
  732. library.lights[ xml.getAttribute( 'id' ) ] = data;
  733. }
  734. function parseLightTechnique( xml ) {
  735. var data = {};
  736. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  737. var child = xml.childNodes[ i ];
  738. if ( child.nodeType !== 1 ) continue;
  739. switch ( child.nodeName ) {
  740. case 'directional':
  741. case 'point':
  742. case 'spot':
  743. case 'ambient':
  744. data.technique = child.nodeName;
  745. data.parameters = parseLightParameters( child );
  746. }
  747. }
  748. return data;
  749. }
  750. function parseLightParameters( xml ) {
  751. var data = {};
  752. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  753. var child = xml.childNodes[ i ];
  754. if ( child.nodeType !== 1 ) continue;
  755. switch ( child.nodeName ) {
  756. case 'color':
  757. var array = parseFloats( child.textContent );
  758. data.color = new THREE.Color().fromArray( array );
  759. break;
  760. case 'falloff_angle':
  761. data.falloffAngle = parseFloat( child.textContent );
  762. break;
  763. case 'quadratic_attenuation':
  764. var f = parseFloat( child.textContent );
  765. data.distance = f ? Math.sqrt( 1 / f ) : 0;
  766. break;
  767. }
  768. }
  769. return data;
  770. }
  771. function buildLight( data ) {
  772. var light;
  773. switch ( data.technique ) {
  774. case 'directional':
  775. light = new THREE.DirectionalLight();
  776. break;
  777. case 'point':
  778. light = new THREE.PointLight();
  779. break;
  780. case 'spot':
  781. light = new THREE.SpotLight();
  782. break;
  783. case 'ambient':
  784. light = new THREE.AmbientLight();
  785. break;
  786. }
  787. if ( data.parameters.color ) light.color.copy( data.parameters.color );
  788. if ( data.parameters.distance ) light.distance = data.parameters.distance;
  789. return light;
  790. }
  791. function getLight( id ) {
  792. return getBuild( library.lights[ id ], buildLight );
  793. }
  794. // geometry
  795. function parseGeometry( xml ) {
  796. var data = {
  797. name: xml.getAttribute( 'name' ),
  798. sources: {},
  799. vertices: {},
  800. primitives: []
  801. };
  802. var mesh = getElementsByTagName( xml, 'mesh' )[ 0 ];
  803. for ( var i = 0; i < mesh.childNodes.length; i ++ ) {
  804. var child = mesh.childNodes[ i ];
  805. if ( child.nodeType !== 1 ) continue;
  806. var id = child.getAttribute( 'id' );
  807. switch ( child.nodeName ) {
  808. case 'source':
  809. data.sources[ id ] = parseSource( child );
  810. break;
  811. case 'vertices':
  812. // data.sources[ id ] = data.sources[ parseId( getElementsByTagName( child, 'input' )[ 0 ].getAttribute( 'source' ) ) ];
  813. data.vertices = parseGeometryVertices( child );
  814. break;
  815. case 'polygons':
  816. console.warn( 'THREE.ColladaLoader: Unsupported primitive type: ', child.nodeName );
  817. break;
  818. case 'lines':
  819. case 'linestrips':
  820. case 'polylist':
  821. case 'triangles':
  822. data.primitives.push( parseGeometryPrimitive( child ) );
  823. break;
  824. default:
  825. console.log( child );
  826. }
  827. }
  828. library.geometries[ xml.getAttribute( 'id' ) ] = data;
  829. }
  830. function parseSource( xml ) {
  831. var data = {
  832. array: [],
  833. stride: 3
  834. };
  835. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  836. var child = xml.childNodes[ i ];
  837. if ( child.nodeType !== 1 ) continue;
  838. switch ( child.nodeName ) {
  839. case 'float_array':
  840. data.array = parseFloats( child.textContent );
  841. break;
  842. case 'Name_array':
  843. data.array = parseStrings( child.textContent );
  844. break;
  845. case 'technique_common':
  846. var accessor = getElementsByTagName( child, 'accessor' )[ 0 ];
  847. if ( accessor !== undefined ) {
  848. data.stride = parseInt( accessor.getAttribute( 'stride' ) );
  849. }
  850. break;
  851. }
  852. }
  853. return data;
  854. }
  855. function parseGeometryVertices( xml ) {
  856. var data = {};
  857. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  858. var child = xml.childNodes[ i ];
  859. if ( child.nodeType !== 1 ) continue;
  860. data[ child.getAttribute( 'semantic' ) ] = parseId( child.getAttribute( 'source' ) );
  861. }
  862. return data;
  863. }
  864. function parseGeometryPrimitive( xml ) {
  865. var primitive = {
  866. type: xml.nodeName,
  867. material: xml.getAttribute( 'material' ),
  868. inputs: {},
  869. stride: 0
  870. };
  871. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  872. var child = xml.childNodes[ i ];
  873. if ( child.nodeType !== 1 ) continue;
  874. switch ( child.nodeName ) {
  875. case 'input':
  876. var id = parseId( child.getAttribute( 'source' ) );
  877. var semantic = child.getAttribute( 'semantic' );
  878. var offset = parseInt( child.getAttribute( 'offset' ) );
  879. primitive.inputs[ semantic ] = { id: id, offset: offset };
  880. primitive.stride = Math.max( primitive.stride, offset + 1 );
  881. break;
  882. case 'vcount':
  883. primitive.vcount = parseInts( child.textContent );
  884. break;
  885. case 'p':
  886. primitive.p = parseInts( child.textContent );
  887. break;
  888. }
  889. }
  890. return primitive;
  891. }
  892. var DEFAULT_LINEMATERIAL = new THREE.LineBasicMaterial();
  893. var DEFAULT_MESHMATERIAL = new THREE.MeshPhongMaterial();
  894. function buildGeometry( data ) {
  895. var group = {};
  896. var sources = data.sources;
  897. var vertices = data.vertices;
  898. var primitives = data.primitives;
  899. var skinning = false;
  900. if ( primitives.length === 0 ) return group;
  901. for ( var p = 0; p < primitives.length; p ++ ) {
  902. var primitive = primitives[ p ];
  903. var inputs = primitive.inputs;
  904. var geometry = new THREE.BufferGeometry();
  905. if ( data.name ) geometry.name = data.name;
  906. for ( var name in inputs ) {
  907. var input = inputs[ name ];
  908. switch ( name ) {
  909. case 'VERTEX':
  910. for ( var key in vertices ) {
  911. geometry.addAttribute( key.toLowerCase(), buildGeometryAttribute( primitive, sources[ vertices[ key ] ], input.offset ) );
  912. if ( key.toLowerCase() === 'position' && sources.skinWeights && sources.skinIndices ) {
  913. geometry.addAttribute( 'skinWeight', buildGeometryAttribute( primitive, sources.skinWeights, input.offset ) );
  914. geometry.addAttribute( 'skinIndex', buildGeometryAttribute( primitive, sources.skinIndices, input.offset ) );
  915. skinning = true;
  916. }
  917. }
  918. break;
  919. case 'NORMAL':
  920. geometry.addAttribute( 'normal', buildGeometryAttribute( primitive, sources[ input.id ], input.offset ) );
  921. break;
  922. case 'COLOR':
  923. geometry.addAttribute( 'color', buildGeometryAttribute( primitive, sources[ input.id ], input.offset ) );
  924. break;
  925. case 'TEXCOORD':
  926. geometry.addAttribute( 'uv', buildGeometryAttribute( primitive, sources[ input.id ], input.offset ) );
  927. break;
  928. }
  929. }
  930. var object;
  931. switch ( primitive.type ) {
  932. case 'lines':
  933. object = new THREE.LineSegments( geometry, DEFAULT_LINEMATERIAL );
  934. break;
  935. case 'linestrips':
  936. object = new THREE.Line( geometry, DEFAULT_LINEMATERIAL );
  937. break;
  938. case 'triangles':
  939. case 'polylist':
  940. object = ( skinning === true ) ? new THREE.SkinnedMesh( geometry, DEFAULT_MESHMATERIAL ) : new THREE.Mesh( geometry, DEFAULT_MESHMATERIAL );
  941. break;
  942. }
  943. group[ primitive.material ] = object;
  944. }
  945. return group;
  946. }
  947. function buildGeometryAttribute( primitive, source, offset ) {
  948. var indices = primitive.p;
  949. var stride = primitive.stride;
  950. var vcount = primitive.vcount;
  951. function pushVector( i ) {
  952. var index = indices[ i + offset ] * sourceStride;
  953. var length = index + sourceStride;
  954. for ( ; index < length; index ++ ) {
  955. array.push( sourceArray[ index ] );
  956. }
  957. }
  958. var maxcount = 0;
  959. var sourceArray = source.array;
  960. var sourceStride = source.stride;
  961. var array = [];
  962. if ( primitive.vcount !== undefined ) {
  963. var index = 0;
  964. for ( var i = 0, l = vcount.length; i < l; i ++ ) {
  965. var count = vcount[ i ];
  966. if ( count === 4 ) {
  967. var a = index + stride * 0;
  968. var b = index + stride * 1;
  969. var c = index + stride * 2;
  970. var d = index + stride * 3;
  971. pushVector( a ); pushVector( b ); pushVector( d );
  972. pushVector( b ); pushVector( c ); pushVector( d );
  973. } else if ( count === 3 ) {
  974. var a = index + stride * 0;
  975. var b = index + stride * 1;
  976. var c = index + stride * 2;
  977. pushVector( a ); pushVector( b ); pushVector( c );
  978. } else {
  979. maxcount = Math.max( maxcount, count );
  980. }
  981. index += stride * count;
  982. }
  983. if ( maxcount > 0 ) {
  984. console.log( 'THREE.ColladaLoader: Geometry has faces with more than 4 vertices.' );
  985. }
  986. } else {
  987. for ( var i = 0, l = indices.length; i < l; i += stride ) {
  988. pushVector( i );
  989. }
  990. }
  991. return new THREE.Float32BufferAttribute( array, sourceStride );
  992. }
  993. function getGeometry( id ) {
  994. return getBuild( library.geometries[ id ], buildGeometry );
  995. }
  996. // nodes
  997. var matrix = new THREE.Matrix4();
  998. var vector = new THREE.Vector3();
  999. function parseNode( xml ) {
  1000. var data = {
  1001. name: xml.getAttribute( 'name' ),
  1002. type: xml.getAttribute( 'type' ),
  1003. sid: xml.getAttribute( 'sid' ),
  1004. matrix: new THREE.Matrix4(),
  1005. nodes: [],
  1006. instanceCameras: [],
  1007. instanceControllers: [],
  1008. instanceLights: [],
  1009. instanceGeometries: [],
  1010. instanceNodes: []
  1011. };
  1012. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  1013. var child = xml.childNodes[ i ];
  1014. if ( child.nodeType !== 1 ) continue;
  1015. switch ( child.nodeName ) {
  1016. case 'node':
  1017. if ( child.hasAttribute( 'id' ) ) {
  1018. data.nodes.push( child.getAttribute( 'id' ) );
  1019. parseNode( child );
  1020. }
  1021. break;
  1022. case 'instance_camera':
  1023. data.instanceCameras.push( parseId( child.getAttribute( 'url' ) ) );
  1024. break;
  1025. case 'instance_controller':
  1026. data.instanceControllers.push( parseNodeInstance( child ) );
  1027. break;
  1028. case 'instance_light':
  1029. data.instanceLights.push( parseId( child.getAttribute( 'url' ) ) );
  1030. break;
  1031. case 'instance_geometry':
  1032. data.instanceGeometries.push( parseNodeInstance( child ) );
  1033. break;
  1034. case 'instance_node':
  1035. data.instanceNodes.push( parseId( child.getAttribute( 'url' ) ) );
  1036. break;
  1037. case 'matrix':
  1038. var array = parseFloats( child.textContent );
  1039. data.matrix.multiply( matrix.fromArray( array ).transpose() ); // .transpose() when Z_UP?
  1040. break;
  1041. case 'translate':
  1042. var array = parseFloats( child.textContent );
  1043. vector.fromArray( array );
  1044. data.matrix.multiply( matrix.makeTranslation( vector.x, vector.y, vector.z ) );
  1045. break;
  1046. case 'rotate':
  1047. var array = parseFloats( child.textContent );
  1048. var angle = THREE.Math.degToRad( array[ 3 ] );
  1049. data.matrix.multiply( matrix.makeRotationAxis( vector.fromArray( array ), angle ) );
  1050. break;
  1051. case 'scale':
  1052. var array = parseFloats( child.textContent );
  1053. data.matrix.scale( vector.fromArray( array ) );
  1054. break;
  1055. case 'extra':
  1056. break;
  1057. default:
  1058. console.log( child );
  1059. }
  1060. }
  1061. if ( xml.hasAttribute( 'id' ) ) {
  1062. library.nodes[ xml.getAttribute( 'id' ) ] = data;
  1063. }
  1064. return data;
  1065. }
  1066. function parseNodeInstance( xml ) {
  1067. var data = {
  1068. id: parseId( xml.getAttribute( 'url' ) ),
  1069. materials: {}
  1070. };
  1071. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  1072. var child = xml.childNodes[ i ];
  1073. switch ( child.nodeName ) {
  1074. case 'bind_material':
  1075. var instances = child.getElementsByTagName( 'instance_material' );
  1076. for ( var j = 0; j < instances.length; j ++ ) {
  1077. var instance = instances[ j ];
  1078. var symbol = instance.getAttribute( 'symbol' );
  1079. var target = instance.getAttribute( 'target' );
  1080. data.materials[ symbol ] = parseId( target );
  1081. }
  1082. break;
  1083. case 'skeleton':
  1084. data.skeleton = parseId( child.textContent );;
  1085. break;
  1086. default:
  1087. break;
  1088. }
  1089. }
  1090. return data;
  1091. }
  1092. function getSkeleton( root, controller ) {
  1093. var boneArray = [];
  1094. var boneInverseArray = [];
  1095. root.traverse( function( bone ) {
  1096. boneArray.push( bone );
  1097. boneInverseArray.push( controller.skin.bones[ bone.name ] );
  1098. } );
  1099. return new THREE.Skeleton( boneArray, boneInverseArray );
  1100. }
  1101. function buildNode( data ) {
  1102. var objects = [];
  1103. var matrix = data.matrix;
  1104. var nodes = data.nodes;
  1105. var type = data.type;
  1106. var instanceCameras = data.instanceCameras;
  1107. var instanceControllers = data.instanceControllers;
  1108. var instanceLights = data.instanceLights;
  1109. var instanceGeometries = data.instanceGeometries;
  1110. var instanceNodes = data.instanceNodes;
  1111. for ( var i = 0, l = nodes.length; i < l; i ++ ) {
  1112. objects.push( getNode( nodes[ i ] ).clone() );
  1113. }
  1114. for ( var i = 0, l = instanceCameras.length; i < l; i ++ ) {
  1115. objects.push( getCamera( instanceCameras[ i ] ).clone() );
  1116. }
  1117. for ( var i = 0, l = instanceControllers.length; i < l; i ++ ) {
  1118. var instance = instanceControllers[ i ];
  1119. var controller = getController( instance.id );
  1120. var geometries = getGeometry( controller.id );
  1121. var node = getNode( instance.skeleton );
  1122. var skeleton = getSkeleton( node, controller );
  1123. for ( var key in geometries ) {
  1124. var object = geometries[ key ].clone();
  1125. if ( instance.materials[ key ] !== undefined ) {
  1126. object.bind( skeleton, controller.skin.bindMatrix );
  1127. object.normalizeSkinWeights();
  1128. object.add( node ); // bone hierarchy is a child of the skinned mesh
  1129. object.material = getMaterial( instance.materials[ key ] );
  1130. object.material.skinning = true;
  1131. }
  1132. objects.push( object );
  1133. }
  1134. }
  1135. for ( var i = 0, l = instanceLights.length; i < l; i ++ ) {
  1136. objects.push( getLight( instanceLights[ i ] ).clone() );
  1137. }
  1138. for ( var i = 0, l = instanceGeometries.length; i < l; i ++ ) {
  1139. var instance = instanceGeometries[ i ];
  1140. var geometries = getGeometry( instance.id );
  1141. for ( var key in geometries ) {
  1142. var object = geometries[ key ].clone();
  1143. if ( instance.materials[ key ] !== undefined ) {
  1144. object.material = getMaterial( instance.materials[ key ] );
  1145. }
  1146. objects.push( object );
  1147. }
  1148. }
  1149. for ( var i = 0, l = instanceNodes.length; i < l; i ++ ) {
  1150. objects.push( getNode( instanceNodes[ i ] ).clone() );
  1151. }
  1152. var object;
  1153. if ( nodes.length === 0 && objects.length === 1 ) {
  1154. object = objects[ 0 ];
  1155. } else {
  1156. object = ( type === 'JOINT' ) ? new THREE.Bone() : new THREE.Group();
  1157. for ( var i = 0; i < objects.length; i ++ ) {
  1158. object.add( objects[ i ] );
  1159. }
  1160. }
  1161. object.name = ( type === 'JOINT' ) ? data.sid : data.name;
  1162. object.matrix.copy( matrix );
  1163. object.matrix.decompose( object.position, object.quaternion, object.scale );
  1164. return object;
  1165. }
  1166. function getNode( id ) {
  1167. return getBuild( library.nodes[ id ], buildNode );
  1168. }
  1169. // visual scenes
  1170. function parseVisualScene( xml ) {
  1171. var data = {
  1172. name: xml.getAttribute( 'name' ),
  1173. children: []
  1174. };
  1175. var elements = getElementsByTagName( xml, 'node' );
  1176. for ( var i = 0; i < elements.length; i ++ ) {
  1177. data.children.push( parseNode( elements[ i ] ) );
  1178. }
  1179. library.visualScenes[ xml.getAttribute( 'id' ) ] = data;
  1180. }
  1181. function buildVisualScene( data ) {
  1182. var group = new THREE.Group();
  1183. group.name = data.name;
  1184. var children = data.children;
  1185. for ( var i = 0; i < children.length; i ++ ) {
  1186. group.add( buildNode( children[ i ] ) );
  1187. }
  1188. return group;
  1189. }
  1190. function getVisualScene( id ) {
  1191. return getBuild( library.visualScenes[ id ], buildVisualScene );
  1192. }
  1193. // scenes
  1194. function parseScene( xml ) {
  1195. var instance = getElementsByTagName( xml, 'instance_visual_scene' )[ 0 ];
  1196. return getVisualScene( parseId( instance.getAttribute( 'url' ) ) );
  1197. }
  1198. console.time( 'THREE.ColladaLoader' );
  1199. if ( text.length === 0 ) {
  1200. return { scene: new THREE.Scene() };
  1201. }
  1202. console.time( 'THREE.ColladaLoader: DOMParser' );
  1203. var xml = new DOMParser().parseFromString( text, 'application/xml' );
  1204. console.timeEnd( 'THREE.ColladaLoader: DOMParser' );
  1205. var collada = getElementsByTagName( xml, 'COLLADA' )[ 0 ];
  1206. // metadata
  1207. var version = collada.getAttribute( 'version' );
  1208. console.log( 'THREE.ColladaLoader: File version', version );
  1209. var asset = parseAsset( getElementsByTagName( collada, 'asset' )[ 0 ] );
  1210. var textureLoader = new THREE.TextureLoader( this.manager ).setPath( resourceDirectory );
  1211. //
  1212. var library = {
  1213. animations: {},
  1214. controllers: {},
  1215. images: {},
  1216. effects: {},
  1217. materials: {},
  1218. cameras: {},
  1219. lights: {},
  1220. geometries: {},
  1221. nodes: {},
  1222. visualScenes: {}
  1223. };
  1224. console.time( 'THREE.ColladaLoader: Parse' );
  1225. // parseLibrary( collada, 'library_animations', 'animation', parseAnimation );
  1226. parseLibrary( collada, 'library_controllers', 'controller', parseController );
  1227. parseLibrary( collada, 'library_images', 'image', parseImage );
  1228. parseLibrary( collada, 'library_effects', 'effect', parseEffect );
  1229. parseLibrary( collada, 'library_materials', 'material', parseMaterial );
  1230. parseLibrary( collada, 'library_cameras', 'camera', parseCamera );
  1231. parseLibrary( collada, 'library_lights', 'light', parseLight );
  1232. parseLibrary( collada, 'library_geometries', 'geometry', parseGeometry );
  1233. parseLibrary( collada, 'library_nodes', 'node', parseNode );
  1234. parseLibrary( collada, 'library_visual_scenes', 'visual_scene', parseVisualScene );
  1235. console.timeEnd( 'THREE.ColladaLoader: Parse' );
  1236. console.time( 'THREE.ColladaLoader: Build' );
  1237. buildLibrary( library.controllers, buildController );
  1238. buildLibrary( library.images, buildImage );
  1239. buildLibrary( library.effects, buildEffect );
  1240. buildLibrary( library.materials, buildMaterial );
  1241. buildLibrary( library.cameras, buildCamera );
  1242. buildLibrary( library.lights, buildLight );
  1243. buildLibrary( library.geometries, buildGeometry );
  1244. buildLibrary( library.visualScenes, buildVisualScene );
  1245. console.timeEnd( 'THREE.ColladaLoader: Build' );
  1246. // console.log( library );
  1247. var scene = parseScene( getElementsByTagName( collada, 'scene' )[ 0 ] );
  1248. if ( asset.upAxis === 'Z_UP' ) {
  1249. scene.rotation.x = - Math.PI / 2;
  1250. }
  1251. scene.scale.multiplyScalar( asset.unit );
  1252. console.timeEnd( 'THREE.ColladaLoader' );
  1253. // console.log( scene );
  1254. return {
  1255. animations: [],
  1256. kinematics: { joints: [] },
  1257. library: library,
  1258. scene: scene
  1259. };
  1260. }
  1261. };