LegacyJSONLoader.js 20 KB

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