LegacyJSONLoader.js 20 KB

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