LegacyJSONLoader.js 20 KB

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