LegacyJSONLoader.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.LegacyJSONLoader = ( function () {
  6. function LegacyJSONLoader( manager ) {
  7. if ( typeof manager === 'boolean' ) {
  8. console.warn( 'THREE.JSONLoader: showStatus parameter has been removed from constructor.' );
  9. manager = undefined;
  10. }
  11. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  12. this.withCredentials = false;
  13. }
  14. Object.assign( LegacyJSONLoader.prototype, {
  15. crossOrigin: 'anonymous',
  16. load: function ( url, onLoad, onProgress, onError ) {
  17. var scope = this;
  18. var path = ( this.path === undefined ) ? THREE.LoaderUtils.extractUrlBase( url ) : this.path;
  19. var loader = new THREE.FileLoader( this.manager );
  20. loader.setPath( this.path );
  21. loader.setWithCredentials( this.withCredentials );
  22. loader.load( url, function ( text ) {
  23. var json = JSON.parse( text );
  24. var metadata = json.metadata;
  25. if ( metadata !== undefined ) {
  26. var type = metadata.type;
  27. if ( type !== undefined ) {
  28. if ( type.toLowerCase() === 'object' ) {
  29. console.error( 'THREE.JSONLoader: ' + url + ' should be loaded with THREE.ObjectLoader instead.' );
  30. return;
  31. }
  32. }
  33. }
  34. var object = scope.parse( json, path );
  35. onLoad( object.geometry, object.materials );
  36. }, onProgress, onError );
  37. },
  38. setPath: function ( value ) {
  39. this.path = value;
  40. return this;
  41. },
  42. setResourcePath: function ( value ) {
  43. this.resourcePath = value;
  44. return this;
  45. },
  46. setCrossOrigin: function ( value ) {
  47. this.crossOrigin = value;
  48. return this;
  49. },
  50. parse: ( function () {
  51. var _BlendingMode = {
  52. NoBlending: THREE.NoBlending,
  53. NormalBlending: THREE.NormalBlending,
  54. AdditiveBlending: THREE.AdditiveBlending,
  55. SubtractiveBlending: THREE.SubtractiveBlending,
  56. MultiplyBlending: THREE.MultiplyBlending,
  57. CustomBlending: THREE.CustomBlending
  58. };
  59. var _color = new THREE.Color();
  60. var _textureLoader = new THREE.TextureLoader();
  61. var _materialLoader = new THREE.MaterialLoader();
  62. function initMaterials( materials, texturePath, crossOrigin ) {
  63. var array = [];
  64. for ( var i = 0; i < materials.length; ++ i ) {
  65. array[ i ] = createMaterial( materials[ i ], texturePath, crossOrigin );
  66. }
  67. return array;
  68. }
  69. function createMaterial( m, texturePath, crossOrigin ) {
  70. // convert from old material format
  71. var textures = {};
  72. //
  73. var json = {
  74. uuid: THREE.Math.generateUUID(),
  75. type: 'MeshLambertMaterial'
  76. };
  77. for ( var name in m ) {
  78. var value = m[ name ];
  79. switch ( name ) {
  80. case 'DbgColor':
  81. case 'DbgIndex':
  82. case 'opticalDensity':
  83. case 'illumination':
  84. break;
  85. case 'DbgName':
  86. json.name = value;
  87. break;
  88. case 'blending':
  89. json.blending = _BlendingMode[ value ];
  90. break;
  91. case 'colorAmbient':
  92. case 'mapAmbient':
  93. console.warn( 'THREE.Loader.createMaterial:', name, 'is no longer supported.' );
  94. break;
  95. case 'colorDiffuse':
  96. json.color = _color.fromArray( value ).getHex();
  97. break;
  98. case 'colorSpecular':
  99. json.specular = _color.fromArray( value ).getHex();
  100. break;
  101. case 'colorEmissive':
  102. json.emissive = _color.fromArray( value ).getHex();
  103. break;
  104. case 'specularCoef':
  105. json.shininess = value;
  106. break;
  107. case 'shading':
  108. if ( value.toLowerCase() === 'basic' ) json.type = 'MeshBasicMaterial';
  109. if ( value.toLowerCase() === 'phong' ) json.type = 'MeshPhongMaterial';
  110. if ( value.toLowerCase() === 'standard' ) json.type = 'MeshStandardMaterial';
  111. break;
  112. case 'mapDiffuse':
  113. json.map = loadTexture( value, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy, textures, texturePath, crossOrigin );
  114. break;
  115. case 'mapDiffuseRepeat':
  116. case 'mapDiffuseOffset':
  117. case 'mapDiffuseWrap':
  118. case 'mapDiffuseAnisotropy':
  119. break;
  120. case 'mapEmissive':
  121. json.emissiveMap = loadTexture( value, m.mapEmissiveRepeat, m.mapEmissiveOffset, m.mapEmissiveWrap, m.mapEmissiveAnisotropy, textures, texturePath, crossOrigin );
  122. break;
  123. case 'mapEmissiveRepeat':
  124. case 'mapEmissiveOffset':
  125. case 'mapEmissiveWrap':
  126. case 'mapEmissiveAnisotropy':
  127. break;
  128. case 'mapLight':
  129. json.lightMap = loadTexture( value, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy, textures, texturePath, crossOrigin );
  130. break;
  131. case 'mapLightRepeat':
  132. case 'mapLightOffset':
  133. case 'mapLightWrap':
  134. case 'mapLightAnisotropy':
  135. break;
  136. case 'mapAO':
  137. json.aoMap = loadTexture( value, m.mapAORepeat, m.mapAOOffset, m.mapAOWrap, m.mapAOAnisotropy, textures, texturePath, crossOrigin );
  138. break;
  139. case 'mapAORepeat':
  140. case 'mapAOOffset':
  141. case 'mapAOWrap':
  142. case 'mapAOAnisotropy':
  143. break;
  144. case 'mapBump':
  145. json.bumpMap = loadTexture( value, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy, textures, texturePath, crossOrigin );
  146. break;
  147. case 'mapBumpScale':
  148. json.bumpScale = value;
  149. break;
  150. case 'mapBumpRepeat':
  151. case 'mapBumpOffset':
  152. case 'mapBumpWrap':
  153. case 'mapBumpAnisotropy':
  154. break;
  155. case 'mapNormal':
  156. json.normalMap = loadTexture( value, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy, textures, texturePath, crossOrigin );
  157. break;
  158. case 'mapNormalFactor':
  159. json.normalScale = value;
  160. break;
  161. case 'mapNormalRepeat':
  162. case 'mapNormalOffset':
  163. case 'mapNormalWrap':
  164. case 'mapNormalAnisotropy':
  165. break;
  166. case 'mapSpecular':
  167. json.specularMap = loadTexture( value, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy, textures, texturePath, crossOrigin );
  168. break;
  169. case 'mapSpecularRepeat':
  170. case 'mapSpecularOffset':
  171. case 'mapSpecularWrap':
  172. case 'mapSpecularAnisotropy':
  173. break;
  174. case 'mapMetalness':
  175. json.metalnessMap = loadTexture( value, m.mapMetalnessRepeat, m.mapMetalnessOffset, m.mapMetalnessWrap, m.mapMetalnessAnisotropy, textures, texturePath, crossOrigin );
  176. break;
  177. case 'mapMetalnessRepeat':
  178. case 'mapMetalnessOffset':
  179. case 'mapMetalnessWrap':
  180. case 'mapMetalnessAnisotropy':
  181. break;
  182. case 'mapRoughness':
  183. json.roughnessMap = loadTexture( value, m.mapRoughnessRepeat, m.mapRoughnessOffset, m.mapRoughnessWrap, m.mapRoughnessAnisotropy, textures, texturePath, crossOrigin );
  184. break;
  185. case 'mapRoughnessRepeat':
  186. case 'mapRoughnessOffset':
  187. case 'mapRoughnessWrap':
  188. case 'mapRoughnessAnisotropy':
  189. break;
  190. case 'mapAlpha':
  191. json.alphaMap = loadTexture( value, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy, textures, texturePath, crossOrigin );
  192. break;
  193. case 'mapAlphaRepeat':
  194. case 'mapAlphaOffset':
  195. case 'mapAlphaWrap':
  196. case 'mapAlphaAnisotropy':
  197. break;
  198. case 'flipSided':
  199. json.side = THREE.BackSide;
  200. break;
  201. case 'doubleSided':
  202. json.side = THREE.DoubleSide;
  203. break;
  204. case 'transparency':
  205. console.warn( 'THREE.Loader.createMaterial: transparency has been renamed to opacity' );
  206. json.opacity = value;
  207. break;
  208. case 'depthTest':
  209. case 'depthWrite':
  210. case 'colorWrite':
  211. case 'opacity':
  212. case 'reflectivity':
  213. case 'transparent':
  214. case 'visible':
  215. case 'wireframe':
  216. json[ name ] = value;
  217. break;
  218. case 'vertexColors':
  219. if ( value === true ) json.vertexColors = THREE.VertexColors;
  220. if ( value === 'face' ) json.vertexColors = THREE.FaceColors;
  221. break;
  222. default:
  223. console.error( 'THREE.Loader.createMaterial: Unsupported', name, value );
  224. break;
  225. }
  226. }
  227. if ( json.type === 'MeshBasicMaterial' ) delete json.emissive;
  228. if ( json.type !== 'MeshPhongMaterial' ) delete json.specular;
  229. if ( json.opacity < 1 ) json.transparent = true;
  230. _materialLoader.setTextures( textures );
  231. return _materialLoader.parse( json );
  232. }
  233. function loadTexture( path, repeat, offset, wrap, anisotropy, textures, texturePath, crossOrigin ) {
  234. var fullPath = texturePath + path;
  235. var loader = THREE.Loader.Handlers.get( fullPath );
  236. var texture;
  237. if ( loader !== null ) {
  238. texture = loader.load( fullPath );
  239. } else {
  240. _textureLoader.setCrossOrigin( crossOrigin );
  241. texture = _textureLoader.load( fullPath );
  242. }
  243. if ( repeat !== undefined ) {
  244. texture.repeat.fromArray( repeat );
  245. if ( repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  246. if ( repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  247. }
  248. if ( offset !== undefined ) {
  249. texture.offset.fromArray( offset );
  250. }
  251. if ( wrap !== undefined ) {
  252. if ( wrap[ 0 ] === 'repeat' ) texture.wrapS = THREE.RepeatWrapping;
  253. if ( wrap[ 0 ] === 'mirror' ) texture.wrapS = THREE.MirroredRepeatWrapping;
  254. if ( wrap[ 1 ] === 'repeat' ) texture.wrapT = THREE.RepeatWrapping;
  255. if ( wrap[ 1 ] === 'mirror' ) texture.wrapT = THREE.MirroredRepeatWrapping;
  256. }
  257. if ( anisotropy !== undefined ) {
  258. texture.anisotropy = anisotropy;
  259. }
  260. var uuid = THREE.Math.generateUUID();
  261. textures[ uuid ] = texture;
  262. return uuid;
  263. }
  264. function parseModel( json, geometry ) {
  265. function isBitSet( value, position ) {
  266. return value & ( 1 << position );
  267. }
  268. var i, j, fi,
  269. offset, zLength,
  270. colorIndex, normalIndex, uvIndex, materialIndex,
  271. type,
  272. isQuad,
  273. hasMaterial,
  274. hasFaceVertexUv,
  275. hasFaceNormal, hasFaceVertexNormal,
  276. hasFaceColor, hasFaceVertexColor,
  277. vertex, face, faceA, faceB, hex, normal,
  278. uvLayer, uv, u, v,
  279. faces = json.faces,
  280. vertices = json.vertices,
  281. normals = json.normals,
  282. colors = json.colors,
  283. scale = json.scale,
  284. nUvLayers = 0;
  285. if ( json.uvs !== undefined ) {
  286. // disregard empty arrays
  287. for ( i = 0; i < json.uvs.length; i ++ ) {
  288. if ( json.uvs[ i ].length ) nUvLayers ++;
  289. }
  290. for ( i = 0; i < nUvLayers; i ++ ) {
  291. geometry.faceVertexUvs[ i ] = [];
  292. }
  293. }
  294. offset = 0;
  295. zLength = vertices.length;
  296. while ( offset < zLength ) {
  297. vertex = new THREE.Vector3();
  298. vertex.x = vertices[ offset ++ ] * scale;
  299. vertex.y = vertices[ offset ++ ] * scale;
  300. vertex.z = vertices[ offset ++ ] * scale;
  301. geometry.vertices.push( vertex );
  302. }
  303. offset = 0;
  304. zLength = faces.length;
  305. while ( offset < zLength ) {
  306. type = faces[ offset ++ ];
  307. isQuad = isBitSet( type, 0 );
  308. hasMaterial = isBitSet( type, 1 );
  309. hasFaceVertexUv = isBitSet( type, 3 );
  310. hasFaceNormal = isBitSet( type, 4 );
  311. hasFaceVertexNormal = isBitSet( type, 5 );
  312. hasFaceColor = isBitSet( type, 6 );
  313. hasFaceVertexColor = isBitSet( type, 7 );
  314. // console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  315. if ( isQuad ) {
  316. faceA = new THREE.Face3();
  317. faceA.a = faces[ offset ];
  318. faceA.b = faces[ offset + 1 ];
  319. faceA.c = faces[ offset + 3 ];
  320. faceB = new THREE.Face3();
  321. faceB.a = faces[ offset + 1 ];
  322. faceB.b = faces[ offset + 2 ];
  323. faceB.c = faces[ offset + 3 ];
  324. offset += 4;
  325. if ( hasMaterial ) {
  326. materialIndex = faces[ offset ++ ];
  327. faceA.materialIndex = materialIndex;
  328. faceB.materialIndex = materialIndex;
  329. }
  330. // to get face <=> uv index correspondence
  331. fi = geometry.faces.length;
  332. if ( hasFaceVertexUv ) {
  333. for ( i = 0; i < nUvLayers; i ++ ) {
  334. uvLayer = json.uvs[ i ];
  335. geometry.faceVertexUvs[ i ][ fi ] = [];
  336. geometry.faceVertexUvs[ i ][ fi + 1 ] = [];
  337. for ( j = 0; j < 4; j ++ ) {
  338. uvIndex = faces[ offset ++ ];
  339. u = uvLayer[ uvIndex * 2 ];
  340. v = uvLayer[ uvIndex * 2 + 1 ];
  341. uv = new THREE.Vector2( u, v );
  342. if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
  343. if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );
  344. }
  345. }
  346. }
  347. if ( hasFaceNormal ) {
  348. normalIndex = faces[ offset ++ ] * 3;
  349. faceA.normal.set(
  350. normals[ normalIndex ++ ],
  351. normals[ normalIndex ++ ],
  352. normals[ normalIndex ]
  353. );
  354. faceB.normal.copy( faceA.normal );
  355. }
  356. if ( hasFaceVertexNormal ) {
  357. for ( i = 0; i < 4; i ++ ) {
  358. normalIndex = faces[ offset ++ ] * 3;
  359. normal = new THREE.Vector3(
  360. normals[ normalIndex ++ ],
  361. normals[ normalIndex ++ ],
  362. normals[ normalIndex ]
  363. );
  364. if ( i !== 2 ) faceA.vertexNormals.push( normal );
  365. if ( i !== 0 ) faceB.vertexNormals.push( normal );
  366. }
  367. }
  368. if ( hasFaceColor ) {
  369. colorIndex = faces[ offset ++ ];
  370. hex = colors[ colorIndex ];
  371. faceA.color.setHex( hex );
  372. faceB.color.setHex( hex );
  373. }
  374. if ( hasFaceVertexColor ) {
  375. for ( i = 0; i < 4; i ++ ) {
  376. colorIndex = faces[ offset ++ ];
  377. hex = colors[ colorIndex ];
  378. if ( i !== 2 ) faceA.vertexColors.push( new THREE.Color( hex ) );
  379. if ( i !== 0 ) faceB.vertexColors.push( new THREE.Color( hex ) );
  380. }
  381. }
  382. geometry.faces.push( faceA );
  383. geometry.faces.push( faceB );
  384. } else {
  385. face = new THREE.Face3();
  386. face.a = faces[ offset ++ ];
  387. face.b = faces[ offset ++ ];
  388. face.c = faces[ offset ++ ];
  389. if ( hasMaterial ) {
  390. materialIndex = faces[ offset ++ ];
  391. face.materialIndex = materialIndex;
  392. }
  393. // to get face <=> uv index correspondence
  394. fi = geometry.faces.length;
  395. if ( hasFaceVertexUv ) {
  396. for ( i = 0; i < nUvLayers; i ++ ) {
  397. uvLayer = json.uvs[ i ];
  398. geometry.faceVertexUvs[ i ][ fi ] = [];
  399. for ( j = 0; j < 3; j ++ ) {
  400. uvIndex = faces[ offset ++ ];
  401. u = uvLayer[ uvIndex * 2 ];
  402. v = uvLayer[ uvIndex * 2 + 1 ];
  403. uv = new THREE.Vector2( u, v );
  404. geometry.faceVertexUvs[ i ][ fi ].push( uv );
  405. }
  406. }
  407. }
  408. if ( hasFaceNormal ) {
  409. normalIndex = faces[ offset ++ ] * 3;
  410. face.normal.set(
  411. normals[ normalIndex ++ ],
  412. normals[ normalIndex ++ ],
  413. normals[ normalIndex ]
  414. );
  415. }
  416. if ( hasFaceVertexNormal ) {
  417. for ( i = 0; i < 3; i ++ ) {
  418. normalIndex = faces[ offset ++ ] * 3;
  419. normal = new THREE.Vector3(
  420. normals[ normalIndex ++ ],
  421. normals[ normalIndex ++ ],
  422. normals[ normalIndex ]
  423. );
  424. face.vertexNormals.push( normal );
  425. }
  426. }
  427. if ( hasFaceColor ) {
  428. colorIndex = faces[ offset ++ ];
  429. face.color.setHex( colors[ colorIndex ] );
  430. }
  431. if ( hasFaceVertexColor ) {
  432. for ( i = 0; i < 3; i ++ ) {
  433. colorIndex = faces[ offset ++ ];
  434. face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
  435. }
  436. }
  437. geometry.faces.push( face );
  438. }
  439. }
  440. }
  441. function parseSkin( json, geometry ) {
  442. var influencesPerVertex = ( json.influencesPerVertex !== undefined ) ? json.influencesPerVertex : 2;
  443. if ( json.skinWeights ) {
  444. for ( var i = 0, l = json.skinWeights.length; i < l; i += influencesPerVertex ) {
  445. var x = json.skinWeights[ i ];
  446. var y = ( influencesPerVertex > 1 ) ? json.skinWeights[ i + 1 ] : 0;
  447. var z = ( influencesPerVertex > 2 ) ? json.skinWeights[ i + 2 ] : 0;
  448. var w = ( influencesPerVertex > 3 ) ? json.skinWeights[ i + 3 ] : 0;
  449. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  450. }
  451. }
  452. if ( json.skinIndices ) {
  453. for ( var i = 0, l = json.skinIndices.length; i < l; i += influencesPerVertex ) {
  454. var a = json.skinIndices[ i ];
  455. var b = ( influencesPerVertex > 1 ) ? json.skinIndices[ i + 1 ] : 0;
  456. var c = ( influencesPerVertex > 2 ) ? json.skinIndices[ i + 2 ] : 0;
  457. var d = ( influencesPerVertex > 3 ) ? json.skinIndices[ i + 3 ] : 0;
  458. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  459. }
  460. }
  461. geometry.bones = json.bones;
  462. if ( geometry.bones && geometry.bones.length > 0 && ( geometry.skinWeights.length !== geometry.skinIndices.length || geometry.skinIndices.length !== geometry.vertices.length ) ) {
  463. console.warn( 'When skinning, number of vertices (' + geometry.vertices.length + '), skinIndices (' +
  464. geometry.skinIndices.length + '), and skinWeights (' + geometry.skinWeights.length + ') should match.' );
  465. }
  466. }
  467. function parseMorphing( json, geometry ) {
  468. var scale = json.scale;
  469. if ( json.morphTargets !== undefined ) {
  470. for ( var i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  471. geometry.morphTargets[ i ] = {};
  472. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  473. geometry.morphTargets[ i ].vertices = [];
  474. var dstVertices = geometry.morphTargets[ i ].vertices;
  475. var srcVertices = json.morphTargets[ i ].vertices;
  476. for ( var v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  477. var vertex = new THREE.Vector3();
  478. vertex.x = srcVertices[ v ] * scale;
  479. vertex.y = srcVertices[ v + 1 ] * scale;
  480. vertex.z = srcVertices[ v + 2 ] * scale;
  481. dstVertices.push( vertex );
  482. }
  483. }
  484. }
  485. if ( json.morphColors !== undefined && json.morphColors.length > 0 ) {
  486. console.warn( 'THREE.JSONLoader: "morphColors" no longer supported. Using them as face colors.' );
  487. var faces = geometry.faces;
  488. var morphColors = json.morphColors[ 0 ].colors;
  489. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  490. faces[ i ].color.fromArray( morphColors, i * 3 );
  491. }
  492. }
  493. }
  494. function parseAnimations( json, geometry ) {
  495. var outputAnimations = [];
  496. // parse old style Bone/Hierarchy animations
  497. var animations = [];
  498. if ( json.animation !== undefined ) {
  499. animations.push( json.animation );
  500. }
  501. if ( json.animations !== undefined ) {
  502. if ( json.animations.length ) {
  503. animations = animations.concat( json.animations );
  504. } else {
  505. animations.push( json.animations );
  506. }
  507. }
  508. for ( var i = 0; i < animations.length; i ++ ) {
  509. var clip = THREE.AnimationClip.parseAnimation( animations[ i ], geometry.bones );
  510. if ( clip ) outputAnimations.push( clip );
  511. }
  512. // parse implicit morph animations
  513. if ( geometry.morphTargets ) {
  514. // TODO: Figure out what an appropraite FPS is for morph target animations -- defaulting to 10, but really it is completely arbitrary.
  515. var morphAnimationClips = THREE.AnimationClip.CreateClipsFromMorphTargetSequences( geometry.morphTargets, 10 );
  516. outputAnimations = outputAnimations.concat( morphAnimationClips );
  517. }
  518. if ( outputAnimations.length > 0 ) geometry.animations = outputAnimations;
  519. }
  520. return function parse( json, path ) {
  521. if ( json.data !== undefined ) {
  522. // Geometry 4.0 spec
  523. json = json.data;
  524. }
  525. if ( json.scale !== undefined ) {
  526. json.scale = 1.0 / json.scale;
  527. } else {
  528. json.scale = 1.0;
  529. }
  530. var geometry = new THREE.Geometry();
  531. parseModel( json, geometry );
  532. parseSkin( json, geometry );
  533. parseMorphing( json, geometry );
  534. parseAnimations( json, geometry );
  535. geometry.computeFaceNormals();
  536. geometry.computeBoundingSphere();
  537. if ( json.materials === undefined || json.materials.length === 0 ) {
  538. return { geometry: geometry };
  539. } else {
  540. var materials = initMaterials( json.materials, this.resourcePath || path, this.crossOrigin );
  541. return { geometry: geometry, materials: materials };
  542. }
  543. };
  544. } )()
  545. } );
  546. return LegacyJSONLoader;
  547. } )();