ColladaLoader2.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.ColladaLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. };
  7. THREE.ColladaLoader.prototype = {
  8. constructor: THREE.ColladaLoader,
  9. load: function ( url, onLoad, onProgress, onError ) {
  10. var scope = this;
  11. var loader = new THREE.FileLoader( scope.manager );
  12. loader.load( url, function ( text ) {
  13. onLoad( scope.parse( text ) );
  14. }, onProgress, onError );
  15. },
  16. options: {
  17. set convertUpAxis( value ) {
  18. console.log( 'ColladaLoder.options.convertUpAxis: TODO' );
  19. }
  20. },
  21. setCrossOrigin: function ( value ) {
  22. this.crossOrigin = value;
  23. },
  24. parse: function ( text ) {
  25. function getElementsByTagName( xml, name ) {
  26. // Non recursive xml.getElementsByTagName() ...
  27. var array = [];
  28. var childNodes = xml.childNodes;
  29. for ( var i = 0, l = childNodes.length; i < l; i ++ ) {
  30. var child = childNodes[ i ];
  31. if ( child.nodeName === name ) {
  32. array.push( child );
  33. }
  34. }
  35. return array;
  36. }
  37. function parseFloats( text ) {
  38. if ( text.length === 0 ) return [];
  39. var parts = text.trim().split( /\s+/ );
  40. var array = new Array( parts.length );
  41. for ( var i = 0, l = parts.length; i < l; i ++ ) {
  42. array[ i ] = parseFloat( parts[ i ] );
  43. }
  44. return array;
  45. }
  46. function parseInts( text ) {
  47. if ( text.length === 0 ) return [];
  48. var parts = text.trim().split( /\s+/ );
  49. var array = new Array( parts.length );
  50. for ( var i = 0, l = parts.length; i < l; i ++ ) {
  51. array[ i ] = parseInt( parts[ i ] );
  52. }
  53. return array;
  54. }
  55. function parseId( text ) {
  56. return text.substring( 1 );
  57. }
  58. // asset
  59. function parseAsset( xml ) {
  60. return {
  61. unit: parseAssetUnit( getElementsByTagName( xml, 'unit' )[ 0 ] ),
  62. upAxis: parseAssetUpAxis( getElementsByTagName( xml, 'up_axis' )[ 0 ] )
  63. };
  64. }
  65. function parseAssetUnit( xml ) {
  66. return xml !== undefined ? parseFloat( xml.getAttribute( 'meter' ) ) : 1;
  67. }
  68. function parseAssetUpAxis( xml ) {
  69. return xml !== undefined ? xml.textContent : 'Y_UP';
  70. }
  71. // library
  72. function parseLibrary( xml, libraryName, nodeName, parser ) {
  73. var library = getElementsByTagName( xml, libraryName )[ 0 ];
  74. if ( library !== undefined ) {
  75. var elements = getElementsByTagName( library, nodeName );
  76. for ( var i = 0; i < elements.length; i ++ ) {
  77. parser( elements[ i ] );
  78. }
  79. }
  80. }
  81. function buildLibrary( data, builder ) {
  82. for ( var name in data ) {
  83. var object = data[ name ];
  84. object.build = builder( data[ name ] );
  85. }
  86. }
  87. // get
  88. function getBuild( data, builder ) {
  89. if ( data.build !== undefined ) return data.build;
  90. data.build = builder( data );
  91. return data.build;
  92. }
  93. // image
  94. function parseImage( xml ) {
  95. var data = {
  96. init_from: getElementsByTagName( xml, 'init_from' )[ 0 ].textContent
  97. };
  98. library.images[ xml.getAttribute( 'id' ) ] = data;
  99. }
  100. function buildImage( data ) {
  101. if ( data.build !== undefined ) return data.build;
  102. return new Image();
  103. }
  104. function getImage( id ) {
  105. return getBuild( library.images[ id ], buildImage );
  106. }
  107. // effect
  108. function parseEffect( xml ) {
  109. var data = {};
  110. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  111. var child = xml.childNodes[ i ];
  112. if ( child.nodeType !== 1 ) continue;
  113. switch ( child.nodeName ) {
  114. case 'profile_COMMON':
  115. data.profile = parseEffectProfileCOMMON( child );
  116. break;
  117. }
  118. }
  119. library.effects[ xml.getAttribute( 'id' ) ] = data;
  120. }
  121. function parseEffectProfileCOMMON( xml ) {
  122. var data = {
  123. surfaces: {},
  124. samplers: {}
  125. };
  126. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  127. var child = xml.childNodes[ i ];
  128. if ( child.nodeType !== 1 ) continue;
  129. switch ( child.nodeName ) {
  130. case 'newparam':
  131. parseEffectNewparam( child, data );
  132. break;
  133. case 'technique':
  134. data.technique = parseEffectTechnique( child );
  135. break;
  136. }
  137. }
  138. return data;
  139. }
  140. function parseEffectNewparam( xml, data ) {
  141. var sid = xml.getAttribute( 'sid' );
  142. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  143. var child = xml.childNodes[ i ];
  144. if ( child.nodeType !== 1 ) continue;
  145. switch ( child.nodeName ) {
  146. case 'surface':
  147. data.surfaces[ sid ] = parseEffectSurface( child );
  148. break;
  149. case 'sampler2D':
  150. data.samplers[ sid ] = parseEffectSampler( child );
  151. break;
  152. }
  153. }
  154. }
  155. function parseEffectSurface( xml ) {
  156. var data = {};
  157. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  158. var child = xml.childNodes[ i ];
  159. if ( child.nodeType !== 1 ) continue;
  160. switch ( child.nodeName ) {
  161. case 'init_from':
  162. data.init_from = child.textContent;
  163. break;
  164. }
  165. }
  166. return data;
  167. }
  168. function parseEffectSampler( xml ) {
  169. var data = {};
  170. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  171. var child = xml.childNodes[ i ];
  172. if ( child.nodeType !== 1 ) continue;
  173. switch ( child.nodeName ) {
  174. case 'source':
  175. data.source = child.textContent;
  176. break;
  177. }
  178. }
  179. return data;
  180. }
  181. function parseEffectTechnique( xml ) {
  182. var data = {};
  183. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  184. var child = xml.childNodes[ i ];
  185. if ( child.nodeType !== 1 ) continue;
  186. switch ( child.nodeName ) {
  187. case 'constant':
  188. case 'lambert':
  189. case 'blinn':
  190. case 'phong':
  191. data.type = child.nodeName;
  192. data.parameters = parseEffectParameters( child );
  193. break;
  194. }
  195. }
  196. return data;
  197. }
  198. function parseEffectParameters( xml ) {
  199. var data = {};
  200. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  201. var child = xml.childNodes[ i ];
  202. if ( child.nodeType !== 1 ) continue;
  203. switch ( child.nodeName ) {
  204. case 'emission':
  205. case 'diffuse':
  206. case 'specular':
  207. case 'shininess':
  208. case 'transparency':
  209. data[ child.nodeName ] = parseEffectParameter( child );
  210. break;
  211. }
  212. }
  213. return data;
  214. }
  215. function parseEffectParameter( xml ) {
  216. var data = {};
  217. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  218. var child = xml.childNodes[ i ];
  219. if ( child.nodeType !== 1 ) continue;
  220. switch ( child.nodeName ) {
  221. case 'color':
  222. data[ child.nodeName ] = parseFloats( child.textContent );
  223. break;
  224. case 'float':
  225. data[ child.nodeName ] = parseFloat( child.textContent );
  226. break;
  227. case 'texture':
  228. data[ child.nodeName ] = { id: child.getAttribute( 'texture' ), extra: parseEffectParameterTexture( child ) };
  229. break;
  230. }
  231. }
  232. return data;
  233. }
  234. function parseEffectParameterTexture( xml ) {
  235. var data = {};
  236. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  237. var child = xml.childNodes[ i ];
  238. if ( child.nodeType !== 1 ) continue;
  239. switch ( child.nodeName ) {
  240. case 'extra':
  241. data = parseEffectParameterTextureExtra( child );
  242. break;
  243. }
  244. }
  245. return data;
  246. }
  247. function parseEffectParameterTextureExtra( xml ) {
  248. var data = {};
  249. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  250. var child = xml.childNodes[ i ];
  251. if ( child.nodeType !== 1 ) continue;
  252. switch ( child.nodeName ) {
  253. case 'technique':
  254. data[ child.nodeName ] = parseEffectParameterTextureExtraTechnique( child );
  255. break;
  256. }
  257. }
  258. return data;
  259. }
  260. function parseEffectParameterTextureExtraTechnique( xml ) {
  261. var data = {};
  262. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  263. var child = xml.childNodes[ i ];
  264. if ( child.nodeType !== 1 ) continue;
  265. switch ( child.nodeName ) {
  266. case 'repeatU':
  267. case 'repeatV':
  268. case 'offsetU':
  269. case 'offsetV':
  270. data[ child.nodeName ] = parseFloat( child.textContent );
  271. break;
  272. case 'wrapU':
  273. case 'wrapV':
  274. data[ child.nodeName ] = parseInt( child.textContent );
  275. break;
  276. }
  277. }
  278. return data;
  279. }
  280. function buildEffect( data ) {
  281. return data;
  282. }
  283. function getEffect( id ) {
  284. return getBuild( library.effects[ id ], buildEffect );
  285. }
  286. // material
  287. function parseMaterial( xml ) {
  288. var data = {
  289. name: xml.getAttribute( 'name' )
  290. };
  291. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  292. var child = xml.childNodes[ i ];
  293. if ( child.nodeType !== 1 ) continue;
  294. switch ( child.nodeName ) {
  295. case 'instance_effect':
  296. data.url = parseId( child.getAttribute( 'url' ) );
  297. break;
  298. }
  299. }
  300. library.materials[ xml.getAttribute( 'id' ) ] = data;
  301. }
  302. function buildMaterial( data ) {
  303. var effect = getEffect( data.url );
  304. var technique = effect.profile.technique;
  305. var material;
  306. switch ( technique.type ) {
  307. case 'phong':
  308. case 'blinn':
  309. material = new THREE.MeshPhongMaterial();
  310. break;
  311. case 'lambert':
  312. material = new THREE.MeshLambertMaterial();
  313. break;
  314. default:
  315. material = new THREE.MeshBasicMaterial();
  316. break;
  317. }
  318. material.name = data.name;
  319. function getTexture( textureObject ) {
  320. var sampler = effect.profile.samplers[ textureObject.id ];
  321. if ( sampler !== undefined ) {
  322. var surface = effect.profile.surfaces[ sampler.source ];
  323. var texture = new THREE.Texture( getImage( surface.init_from ) );
  324. var extra = textureObject.extra;
  325. if ( extra !== undefined && extra.technique !== undefined ) {
  326. var technique = extra.technique;
  327. texture.wrapS = technique.wrapU ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  328. texture.wrapT = technique.wrapV ? THREE.RepeatWrapping : THREE.ClampToEdgeWrapping;
  329. texture.offset.set( technique.offsetU, technique.offsetV );
  330. texture.repeat.set( technique.repeatU, technique.repeatV );
  331. } else {
  332. texture.wrapS = THREE.RepeatWrapping;
  333. texture.wrapT = THREE.RepeatWrapping;
  334. }
  335. texture.needsUpdate = true;
  336. return texture;
  337. }
  338. console.error( 'ColladaLoder: Undefined sampler', textureObject.id );
  339. return null;
  340. }
  341. var parameters = technique.parameters;
  342. for ( var key in parameters ) {
  343. var parameter = parameters[ key ];
  344. switch ( key ) {
  345. case 'diffuse':
  346. if ( parameter.color ) material.color.fromArray( parameter.color );
  347. if ( parameter.texture ) material.map = getTexture( parameter.texture );
  348. break;
  349. case 'specular':
  350. if ( parameter.color && material.specular )
  351. material.specular.fromArray( parameter.color );
  352. break;
  353. case 'shininess':
  354. if ( parameter.float && material.shininess )
  355. material.shininess = parameter.float;
  356. break;
  357. case 'emission':
  358. if ( parameter.color && material.emissive )
  359. material.emissive.fromArray( parameter.color );
  360. break;
  361. case 'transparency':
  362. if ( parameter.float !== undefined ) material.opacity = parameter.float;
  363. material.transparent = true;
  364. break;
  365. }
  366. }
  367. return material;
  368. }
  369. function getMaterial( id ) {
  370. return getBuild( library.materials[ id ], buildMaterial );
  371. }
  372. // camera
  373. function parseCamera( xml ) {
  374. var data = {
  375. name: xml.getAttribute( 'name' )
  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 'optics':
  382. data.optics = parseCameraOptics( child );
  383. break;
  384. }
  385. }
  386. library.cameras[ xml.getAttribute( 'id' ) ] = data;
  387. }
  388. function parseCameraOptics( xml ) {
  389. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  390. var child = xml.childNodes[ i ];
  391. switch ( child.nodeName ) {
  392. case 'technique_common':
  393. return parseCameraTechnique( child );
  394. }
  395. }
  396. return {};
  397. }
  398. function parseCameraTechnique( xml ) {
  399. var data = {};
  400. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  401. var child = xml.childNodes[ i ];
  402. switch ( child.nodeName ) {
  403. case 'perspective':
  404. case 'orthographic':
  405. data.technique = child.nodeName;
  406. data.parameters = parseCameraParameters( child );
  407. break;
  408. }
  409. }
  410. return data;
  411. }
  412. function parseCameraParameters( xml ) {
  413. var data = {};
  414. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  415. var child = xml.childNodes[ i ];
  416. switch ( child.nodeName ) {
  417. case 'xfov':
  418. case 'yfov':
  419. case 'xmag':
  420. case 'ymag':
  421. case 'znear':
  422. case 'zfar':
  423. case 'aspect_ratio':
  424. data[ child.nodeName ] = parseFloat( child.textContent );
  425. break;
  426. }
  427. }
  428. return data;
  429. }
  430. function buildCamera( data ) {
  431. var camera;
  432. switch ( data.optics.technique ) {
  433. case 'perspective':
  434. camera = new THREE.PerspectiveCamera(
  435. data.optics.parameters.yfov,
  436. data.optics.parameters.aspect_ratio,
  437. data.optics.parameters.znear,
  438. data.optics.parameters.zfar
  439. );
  440. break;
  441. case 'orthographic':
  442. camera = new THREE.OrthographicCamera( /* TODO */ );
  443. break;
  444. default:
  445. camera = new THREE.PerspectiveCamera();
  446. break;
  447. }
  448. camera.name = data.name;
  449. return camera;
  450. }
  451. function getCamera( id ) {
  452. return getBuild( library.cameras[ id ], buildCamera );
  453. }
  454. // light
  455. function parseLight( xml ) {
  456. var data = {};
  457. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  458. var child = xml.childNodes[ i ];
  459. if ( child.nodeType !== 1 ) continue;
  460. switch ( child.nodeName ) {
  461. case 'technique_common':
  462. data = parseLightTechnique( child );
  463. break;
  464. }
  465. }
  466. library.lights[ xml.getAttribute( 'id' ) ] = data;
  467. }
  468. function parseLightTechnique( xml ) {
  469. var data = {};
  470. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  471. var child = xml.childNodes[ i ];
  472. if ( child.nodeType !== 1 ) continue;
  473. switch ( child.nodeName ) {
  474. case 'directional':
  475. case 'point':
  476. case 'spot':
  477. case 'ambient':
  478. data.technique = child.nodeName;
  479. data.parameters = parseLightParameters( child );
  480. }
  481. }
  482. return data;
  483. }
  484. function parseLightParameters( xml ) {
  485. var data = {};
  486. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  487. var child = xml.childNodes[ i ];
  488. if ( child.nodeType !== 1 ) continue;
  489. switch ( child.nodeName ) {
  490. case 'color':
  491. var array = parseFloats( child.textContent );
  492. data.color = new THREE.Color().fromArray( array );
  493. break;
  494. case 'falloff_angle':
  495. data.falloffAngle = parseFloat( child.textContent );
  496. break;
  497. case 'quadratic_attenuation':
  498. var f = parseFloat( child.textContent );
  499. data.distance = f ? Math.sqrt( 1 / f ) : 0;
  500. break;
  501. }
  502. }
  503. return data;
  504. }
  505. function buildLight( data ) {
  506. var light;
  507. switch ( data.technique ) {
  508. case 'directional':
  509. light = new THREE.DirectionalLight();
  510. break;
  511. case 'point':
  512. light = new THREE.PointLight();
  513. break;
  514. case 'spot':
  515. light = new THREE.SpotLight();
  516. break;
  517. case 'ambient':
  518. light = new THREE.AmbientLight();
  519. break;
  520. }
  521. if ( data.parameters.color ) light.color.copy( data.parameters.color );
  522. if ( data.parameters.distance ) light.distance = data.parameters.distance;
  523. return light;
  524. }
  525. function getLight( id ) {
  526. return getBuild( library.lights[ id ], buildLight );
  527. }
  528. // geometry
  529. function parseGeometry( xml ) {
  530. var data = {
  531. name: xml.getAttribute( 'name' ),
  532. sources: {},
  533. vertices: {},
  534. primitives: []
  535. };
  536. var mesh = getElementsByTagName( xml, 'mesh' )[ 0 ];
  537. for ( var i = 0; i < mesh.childNodes.length; i ++ ) {
  538. var child = mesh.childNodes[ i ];
  539. if ( child.nodeType !== 1 ) continue;
  540. var id = child.getAttribute( 'id' );
  541. switch ( child.nodeName ) {
  542. case 'source':
  543. data.sources[ id ] = parseGeometrySource( child );
  544. break;
  545. case 'vertices':
  546. // data.sources[ id ] = data.sources[ parseId( getElementsByTagName( child, 'input' )[ 0 ].getAttribute( 'source' ) ) ];
  547. data.vertices = parseGeometryVertices( child );
  548. break;
  549. case 'polygons':
  550. console.log( 'ColladaLoader: Unsupported primitive type: ', child.nodeName );
  551. break;
  552. case 'lines':
  553. case 'linestrips':
  554. case 'polylist':
  555. case 'triangles':
  556. data.primitives.push( parseGeometryPrimitive( child ) );
  557. break;
  558. default:
  559. console.log( child );
  560. }
  561. }
  562. library.geometries[ xml.getAttribute( 'id' ) ] = data;
  563. }
  564. function parseGeometrySource( xml ) {
  565. var data = {
  566. array: [],
  567. stride: 3
  568. };
  569. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  570. var child = xml.childNodes[ i ];
  571. if ( child.nodeType !== 1 ) continue;
  572. switch ( child.nodeName ) {
  573. case 'float_array':
  574. data.array = parseFloats( child.textContent );
  575. break;
  576. case 'technique_common':
  577. var accessor = getElementsByTagName( child, 'accessor' )[ 0 ];
  578. if ( accessor !== undefined ) {
  579. data.stride = parseInt( accessor.getAttribute( 'stride' ) );
  580. }
  581. break;
  582. default:
  583. console.log( child );
  584. }
  585. }
  586. return data;
  587. }
  588. function parseGeometryVertices( xml ) {
  589. var data = {};
  590. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  591. var child = xml.childNodes[ i ];
  592. if ( child.nodeType !== 1 ) continue;
  593. data[ child.getAttribute( 'semantic' ) ] = parseId( child.getAttribute( 'source' ) );
  594. }
  595. return data;
  596. }
  597. function parseGeometryPrimitive( xml ) {
  598. var primitive = {
  599. type: xml.nodeName,
  600. material: xml.getAttribute( 'material' ),
  601. inputs: {},
  602. stride: 0
  603. };
  604. for ( var i = 0, l = xml.childNodes.length; i < l; i ++ ) {
  605. var child = xml.childNodes[ i ];
  606. if ( child.nodeType !== 1 ) continue;
  607. switch ( child.nodeName ) {
  608. case 'input':
  609. var id = parseId( child.getAttribute( 'source' ) );
  610. var semantic = child.getAttribute( 'semantic' );
  611. var offset = parseInt( child.getAttribute( 'offset' ) );
  612. primitive.inputs[ semantic ] = { id: id, offset: offset };
  613. primitive.stride = Math.max( primitive.stride, offset + 1 );
  614. break;
  615. case 'vcount':
  616. primitive.vcount = parseInts( child.textContent );
  617. break;
  618. case 'p':
  619. primitive.p = parseInts( child.textContent );
  620. break;
  621. }
  622. }
  623. return primitive;
  624. }
  625. var DEFAULT_LINEMATERIAL = new THREE.LineBasicMaterial();
  626. var DEFAULT_MESHMATERIAL = new THREE.MeshPhongMaterial();
  627. function buildGeometry( data ) {
  628. var group = {};
  629. var sources = data.sources;
  630. var vertices = data.vertices;
  631. var primitives = data.primitives;
  632. if ( primitives.length === 0 ) return group;
  633. for ( var p = 0; p < primitives.length; p ++ ) {
  634. var primitive = primitives[ p ];
  635. var inputs = primitive.inputs;
  636. var geometry = new THREE.BufferGeometry();
  637. if ( data.name ) geometry.name = data.name;
  638. for ( var name in inputs ) {
  639. var input = inputs[ name ];
  640. switch ( name ) {
  641. case 'VERTEX':
  642. for ( var key in vertices ) {
  643. geometry.addAttribute( key.toLowerCase(), buildGeometryAttribute( primitive, sources[ vertices[ key ] ], input.offset ) );
  644. }
  645. break;
  646. case 'NORMAL':
  647. geometry.addAttribute( 'normal', buildGeometryAttribute( primitive, sources[ input.id ], input.offset ) );
  648. break;
  649. case 'COLOR':
  650. geometry.addAttribute( 'color', buildGeometryAttribute( primitive, sources[ input.id ], input.offset ) );
  651. break;
  652. case 'TEXCOORD':
  653. geometry.addAttribute( 'uv', buildGeometryAttribute( primitive, sources[ input.id ], input.offset ) );
  654. break;
  655. }
  656. }
  657. var object;
  658. switch ( primitive.type ) {
  659. case 'lines':
  660. object = new THREE.LineSegments( geometry, DEFAULT_LINEMATERIAL );
  661. break;
  662. case 'linestrips':
  663. object = new THREE.Line( geometry, DEFAULT_LINEMATERIAL );
  664. break;
  665. case 'triangles':
  666. case 'polylist':
  667. object = new THREE.Mesh( geometry, DEFAULT_MESHMATERIAL );
  668. break;
  669. }
  670. group[ primitive.material ] = object;
  671. }
  672. return group;
  673. }
  674. function buildGeometryAttribute( primitive, source, offset ) {
  675. var indices = primitive.p;
  676. var stride = primitive.stride;
  677. var vcount = primitive.vcount;
  678. function pushVector( i ) {
  679. var index = indices[ i + offset ] * sourceStride;
  680. var length = index + sourceStride;
  681. for ( ; index < length; index ++ ) {
  682. array.push( sourceArray[ index ] );
  683. }
  684. }
  685. var maxcount = 0;
  686. var sourceArray = source.array;
  687. var sourceStride = source.stride;
  688. var array = [];
  689. if ( primitive.vcount !== undefined ) {
  690. var index = 0;
  691. for ( var i = 0, l = vcount.length; i < l; i ++ ) {
  692. var count = vcount[ i ];
  693. if ( count === 4 ) {
  694. var a = index + stride * 0;
  695. var b = index + stride * 1;
  696. var c = index + stride * 2;
  697. var d = index + stride * 3;
  698. pushVector( a ); pushVector( b ); pushVector( d );
  699. pushVector( b ); pushVector( c ); pushVector( d );
  700. } else if ( count === 3 ) {
  701. var a = index + stride * 0;
  702. var b = index + stride * 1;
  703. var c = index + stride * 2;
  704. pushVector( a ); pushVector( b ); pushVector( c );
  705. } else {
  706. maxcount = Math.max( maxcount, count );
  707. }
  708. index += stride * count;
  709. }
  710. if ( maxcount > 0 ) {
  711. console.log( 'ColladaLoader: Geometry has faces with more than 4 vertices.' );
  712. }
  713. } else {
  714. for ( var i = 0, l = indices.length; i < l; i += stride ) {
  715. pushVector( i );
  716. }
  717. }
  718. return new THREE.Float32BufferAttribute( array, sourceStride );
  719. }
  720. function getGeometry( id ) {
  721. return getBuild( library.geometries[ id ], buildGeometry );
  722. }
  723. // nodes
  724. var matrix = new THREE.Matrix4();
  725. var vector = new THREE.Vector3();
  726. function parseNode( xml ) {
  727. var data = {
  728. name: xml.getAttribute( 'name' ),
  729. matrix: new THREE.Matrix4(),
  730. nodes: [],
  731. instanceCameras: [],
  732. instanceLights: [],
  733. instanceGeometries: [],
  734. instanceNodes: []
  735. };
  736. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  737. var child = xml.childNodes[ i ];
  738. if ( child.nodeType !== 1 ) continue;
  739. switch ( child.nodeName ) {
  740. case 'node':
  741. parseNode( child );
  742. data.nodes.push( child.getAttribute( 'id' ) );
  743. break;
  744. case 'instance_camera':
  745. data.instanceCameras.push( parseId( child.getAttribute( 'url' ) ) );
  746. break;
  747. case 'instance_light':
  748. data.instanceLights.push( parseId( child.getAttribute( 'url' ) ) );
  749. break;
  750. case 'instance_geometry':
  751. data.instanceGeometries.push( parseNodeInstanceGeometry( child ) );
  752. break;
  753. case 'instance_node':
  754. data.instanceNodes.push( parseId( child.getAttribute( 'url' ) ) );
  755. break;
  756. case 'matrix':
  757. var array = parseFloats( child.textContent );
  758. data.matrix.multiply( matrix.fromArray( array ).transpose() ); // .transpose() when Z_UP?
  759. break;
  760. case 'translate':
  761. var array = parseFloats( child.textContent );
  762. vector.fromArray( array );
  763. data.matrix.multiply( matrix.makeTranslation( vector.x, vector.y, vector.z ) );
  764. break;
  765. case 'rotate':
  766. var array = parseFloats( child.textContent );
  767. var angle = THREE.Math.degToRad( array[ 3 ] );
  768. data.matrix.multiply( matrix.makeRotationAxis( vector.fromArray( array ), angle ) );
  769. break;
  770. case 'scale':
  771. var array = parseFloats( child.textContent );
  772. data.matrix.scale( vector.fromArray( array ) );
  773. break;
  774. case 'extra':
  775. break;
  776. default:
  777. console.log( child );
  778. }
  779. }
  780. if ( xml.getAttribute( 'id' ) !== null ) {
  781. library.nodes[ xml.getAttribute( 'id' ) ] = data;
  782. }
  783. return data;
  784. }
  785. function parseNodeInstanceGeometry( xml ) {
  786. var data = {
  787. id: parseId( xml.getAttribute( 'url' ) ),
  788. materials: {}
  789. };
  790. for ( var i = 0; i < xml.childNodes.length; i ++ ) {
  791. var child = xml.childNodes[ i ];
  792. if ( child.nodeName === 'bind_material' ) {
  793. var instances = child.getElementsByTagName( 'instance_material' );
  794. for ( var j = 0; j < instances.length; j ++ ) {
  795. var instance = instances[ j ];
  796. var symbol = instance.getAttribute( 'symbol' );
  797. var target = instance.getAttribute( 'target' );
  798. data.materials[ symbol ] = parseId( target );
  799. }
  800. break;
  801. }
  802. }
  803. return data;
  804. }
  805. function buildNode( data ) {
  806. var objects = [];
  807. var matrix = data.matrix;
  808. var nodes = data.nodes;
  809. var instanceCameras = data.instanceCameras;
  810. var instanceLights = data.instanceLights;
  811. var instanceGeometries = data.instanceGeometries;
  812. var instanceNodes = data.instanceNodes;
  813. for ( var i = 0, l = nodes.length; i < l; i ++ ) {
  814. objects.push( getNode( nodes[ i ] ).clone() );
  815. }
  816. for ( var i = 0, l = instanceCameras.length; i < l; i ++ ) {
  817. objects.push( getCamera( instanceCameras[ i ] ).clone() );
  818. }
  819. for ( var i = 0, l = instanceLights.length; i < l; i ++ ) {
  820. objects.push( getLight( instanceLights[ i ] ).clone() );
  821. }
  822. for ( var i = 0, l = instanceGeometries.length; i < l; i ++ ) {
  823. var instance = instanceGeometries[ i ];
  824. var geometries = getGeometry( instance.id );
  825. for ( var key in geometries ) {
  826. var object = geometries[ key ].clone();
  827. if ( instance.materials[ key ] !== undefined ) {
  828. object.material = getMaterial( instance.materials[ key ] );
  829. }
  830. objects.push( object );
  831. }
  832. }
  833. for ( var i = 0, l = instanceNodes.length; i < l; i ++ ) {
  834. objects.push( getNode( instanceNodes[ i ] ).clone() );
  835. }
  836. var object;
  837. if ( nodes.length === 0 && objects.length === 1 ) {
  838. object = objects[ 0 ];
  839. } else {
  840. object = new THREE.Group();
  841. for ( var i = 0; i < objects.length; i ++ ) {
  842. object.add( objects[ i ] );
  843. }
  844. }
  845. object.name = data.name;
  846. matrix.decompose( object.position, object.quaternion, object.scale );
  847. return object;
  848. }
  849. function getNode( id ) {
  850. return getBuild( library.nodes[ id ], buildNode );
  851. }
  852. // visual scenes
  853. function parseVisualScene( xml ) {
  854. var data = {
  855. name: xml.getAttribute( 'name' ),
  856. children: []
  857. };
  858. var elements = getElementsByTagName( xml, 'node' );
  859. for ( var i = 0; i < elements.length; i ++ ) {
  860. data.children.push( parseNode( elements[ i ] ) );
  861. }
  862. library.visualScenes[ xml.getAttribute( 'id' ) ] = data;
  863. }
  864. function buildVisualScene( data ) {
  865. var group = new THREE.Group();
  866. group.name = data.name;
  867. var children = data.children;
  868. for ( var i = 0; i < children.length; i ++ ) {
  869. group.add( buildNode( children[ i ] ) );
  870. }
  871. return group;
  872. }
  873. function getVisualScene( id ) {
  874. return getBuild( library.visualScenes[ id ], buildVisualScene );
  875. }
  876. // scenes
  877. function parseScene( xml ) {
  878. var instance = getElementsByTagName( xml, 'instance_visual_scene' )[ 0 ];
  879. return getVisualScene( parseId( instance.getAttribute( 'url' ) ) );
  880. }
  881. console.time( 'ColladaLoader' );
  882. if ( text.length === 0 ) {
  883. return { scene: new THREE.Scene() };
  884. }
  885. console.time( 'ColladaLoader: DOMParser' );
  886. var xml = new DOMParser().parseFromString( text, 'application/xml' );
  887. console.timeEnd( 'ColladaLoader: DOMParser' );
  888. var collada = getElementsByTagName( xml, 'COLLADA' )[ 0 ];
  889. // metadata
  890. var version = collada.getAttribute( 'version' );
  891. console.log( 'ColladaLoader: File version', version );
  892. var asset = parseAsset( getElementsByTagName( collada, 'asset' )[ 0 ] );
  893. //
  894. var library = {
  895. images: {},
  896. effects: {},
  897. materials: {},
  898. cameras: {},
  899. lights: {},
  900. geometries: {},
  901. nodes: {},
  902. visualScenes: {}
  903. };
  904. console.time( 'ColladaLoader: Parse' );
  905. parseLibrary( collada, 'library_images', 'image', parseImage );
  906. parseLibrary( collada, 'library_effects', 'effect', parseEffect );
  907. parseLibrary( collada, 'library_materials', 'material', parseMaterial );
  908. parseLibrary( collada, 'library_cameras', 'camera', parseCamera );
  909. parseLibrary( collada, 'library_lights', 'light', parseLight );
  910. parseLibrary( collada, 'library_geometries', 'geometry', parseGeometry );
  911. parseLibrary( collada, 'library_nodes', 'node', parseNode );
  912. parseLibrary( collada, 'library_visual_scenes', 'visual_scene', parseVisualScene );
  913. console.timeEnd( 'ColladaLoader: Parse' );
  914. console.time( 'ColladaLoader: Build' );
  915. buildLibrary( library.images, buildImage );
  916. buildLibrary( library.effects, buildEffect );
  917. buildLibrary( library.materials, buildMaterial );
  918. buildLibrary( library.cameras, buildCamera );
  919. buildLibrary( library.lights, buildLight );
  920. buildLibrary( library.geometries, buildGeometry );
  921. buildLibrary( library.nodes, buildNode );
  922. buildLibrary( library.visualScenes, buildVisualScene );
  923. console.timeEnd( 'ColladaLoader: Build' );
  924. // console.log( library );
  925. var scene = parseScene( getElementsByTagName( collada, 'scene' )[ 0 ] );
  926. if ( asset.upAxis === 'Z_UP' ) {
  927. scene.rotation.x = - Math.PI / 2;
  928. }
  929. scene.scale.multiplyScalar( asset.unit );
  930. console.timeEnd( 'ColladaLoader' );
  931. // console.log( scene );
  932. return {
  933. animations: [],
  934. kinematics: { joints: [] },
  935. library: library,
  936. scene: scene
  937. };
  938. }
  939. };