LegacyJSONLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. import {
  6. AnimationClip,
  7. Color,
  8. DefaultLoadingManager,
  9. Face3,
  10. FileLoader,
  11. Geometry,
  12. Loader,
  13. LoaderUtils,
  14. Vector2,
  15. Vector3,
  16. Vector4
  17. } from "../../../../build/three.module.js";
  18. var LegacyJSONLoader = ( function () {
  19. function LegacyJSONLoader( manager ) {
  20. if ( typeof manager === 'boolean' ) {
  21. console.warn( 'THREE.JSONLoader: showStatus parameter has been removed from constructor.' );
  22. manager = undefined;
  23. }
  24. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  25. this.withCredentials = false;
  26. }
  27. Object.assign( LegacyJSONLoader.prototype, {
  28. crossOrigin: 'anonymous',
  29. load: function ( url, onLoad, onProgress, onError ) {
  30. var scope = this;
  31. var path = ( this.path === undefined ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  32. var loader = new FileLoader( this.manager );
  33. loader.setPath( this.path );
  34. loader.setWithCredentials( this.withCredentials );
  35. loader.load( url, function ( text ) {
  36. var json = JSON.parse( text );
  37. var metadata = json.metadata;
  38. if ( metadata !== undefined ) {
  39. var type = metadata.type;
  40. if ( type !== undefined ) {
  41. if ( type.toLowerCase() === 'object' ) {
  42. console.error( 'THREE.JSONLoader: ' + url + ' should be loaded with THREE.ObjectLoader instead.' );
  43. return;
  44. }
  45. }
  46. }
  47. var object = scope.parse( json, path );
  48. onLoad( object.geometry, object.materials );
  49. }, onProgress, onError );
  50. },
  51. setPath: function ( value ) {
  52. this.path = value;
  53. return this;
  54. },
  55. setResourcePath: function ( value ) {
  56. this.resourcePath = value;
  57. return this;
  58. },
  59. setCrossOrigin: function ( value ) {
  60. this.crossOrigin = value;
  61. return this;
  62. },
  63. parse: ( function () {
  64. function parseModel( json, geometry ) {
  65. function isBitSet( value, position ) {
  66. return value & ( 1 << position );
  67. }
  68. var i, j, fi,
  69. offset, zLength,
  70. colorIndex, normalIndex, uvIndex, materialIndex,
  71. type,
  72. isQuad,
  73. hasMaterial,
  74. hasFaceVertexUv,
  75. hasFaceNormal, hasFaceVertexNormal,
  76. hasFaceColor, hasFaceVertexColor,
  77. vertex, face, faceA, faceB, hex, normal,
  78. uvLayer, uv, u, v,
  79. faces = json.faces,
  80. vertices = json.vertices,
  81. normals = json.normals,
  82. colors = json.colors,
  83. scale = json.scale,
  84. nUvLayers = 0;
  85. if ( json.uvs !== undefined ) {
  86. // disregard empty arrays
  87. for ( i = 0; i < json.uvs.length; i ++ ) {
  88. if ( json.uvs[ i ].length ) nUvLayers ++;
  89. }
  90. for ( i = 0; i < nUvLayers; i ++ ) {
  91. geometry.faceVertexUvs[ i ] = [];
  92. }
  93. }
  94. offset = 0;
  95. zLength = vertices.length;
  96. while ( offset < zLength ) {
  97. vertex = new Vector3();
  98. vertex.x = vertices[ offset ++ ] * scale;
  99. vertex.y = vertices[ offset ++ ] * scale;
  100. vertex.z = vertices[ offset ++ ] * scale;
  101. geometry.vertices.push( vertex );
  102. }
  103. offset = 0;
  104. zLength = faces.length;
  105. while ( offset < zLength ) {
  106. type = faces[ offset ++ ];
  107. isQuad = isBitSet( type, 0 );
  108. hasMaterial = isBitSet( type, 1 );
  109. hasFaceVertexUv = isBitSet( type, 3 );
  110. hasFaceNormal = isBitSet( type, 4 );
  111. hasFaceVertexNormal = isBitSet( type, 5 );
  112. hasFaceColor = isBitSet( type, 6 );
  113. hasFaceVertexColor = isBitSet( type, 7 );
  114. // console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  115. if ( isQuad ) {
  116. faceA = new Face3();
  117. faceA.a = faces[ offset ];
  118. faceA.b = faces[ offset + 1 ];
  119. faceA.c = faces[ offset + 3 ];
  120. faceB = new Face3();
  121. faceB.a = faces[ offset + 1 ];
  122. faceB.b = faces[ offset + 2 ];
  123. faceB.c = faces[ offset + 3 ];
  124. offset += 4;
  125. if ( hasMaterial ) {
  126. materialIndex = faces[ offset ++ ];
  127. faceA.materialIndex = materialIndex;
  128. faceB.materialIndex = materialIndex;
  129. }
  130. // to get face <=> uv index correspondence
  131. fi = geometry.faces.length;
  132. if ( hasFaceVertexUv ) {
  133. for ( i = 0; i < nUvLayers; i ++ ) {
  134. uvLayer = json.uvs[ i ];
  135. geometry.faceVertexUvs[ i ][ fi ] = [];
  136. geometry.faceVertexUvs[ i ][ fi + 1 ] = [];
  137. for ( j = 0; j < 4; j ++ ) {
  138. uvIndex = faces[ offset ++ ];
  139. u = uvLayer[ uvIndex * 2 ];
  140. v = uvLayer[ uvIndex * 2 + 1 ];
  141. uv = new Vector2( u, v );
  142. if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
  143. if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );
  144. }
  145. }
  146. }
  147. if ( hasFaceNormal ) {
  148. normalIndex = faces[ offset ++ ] * 3;
  149. faceA.normal.set(
  150. normals[ normalIndex ++ ],
  151. normals[ normalIndex ++ ],
  152. normals[ normalIndex ]
  153. );
  154. faceB.normal.copy( faceA.normal );
  155. }
  156. if ( hasFaceVertexNormal ) {
  157. for ( i = 0; i < 4; i ++ ) {
  158. normalIndex = faces[ offset ++ ] * 3;
  159. normal = new Vector3(
  160. normals[ normalIndex ++ ],
  161. normals[ normalIndex ++ ],
  162. normals[ normalIndex ]
  163. );
  164. if ( i !== 2 ) faceA.vertexNormals.push( normal );
  165. if ( i !== 0 ) faceB.vertexNormals.push( normal );
  166. }
  167. }
  168. if ( hasFaceColor ) {
  169. colorIndex = faces[ offset ++ ];
  170. hex = colors[ colorIndex ];
  171. faceA.color.setHex( hex );
  172. faceB.color.setHex( hex );
  173. }
  174. if ( hasFaceVertexColor ) {
  175. for ( i = 0; i < 4; i ++ ) {
  176. colorIndex = faces[ offset ++ ];
  177. hex = colors[ colorIndex ];
  178. if ( i !== 2 ) faceA.vertexColors.push( new Color( hex ) );
  179. if ( i !== 0 ) faceB.vertexColors.push( new Color( hex ) );
  180. }
  181. }
  182. geometry.faces.push( faceA );
  183. geometry.faces.push( faceB );
  184. } else {
  185. face = new Face3();
  186. face.a = faces[ offset ++ ];
  187. face.b = faces[ offset ++ ];
  188. face.c = faces[ offset ++ ];
  189. if ( hasMaterial ) {
  190. materialIndex = faces[ offset ++ ];
  191. face.materialIndex = materialIndex;
  192. }
  193. // to get face <=> uv index correspondence
  194. fi = geometry.faces.length;
  195. if ( hasFaceVertexUv ) {
  196. for ( i = 0; i < nUvLayers; i ++ ) {
  197. uvLayer = json.uvs[ i ];
  198. geometry.faceVertexUvs[ i ][ fi ] = [];
  199. for ( j = 0; j < 3; j ++ ) {
  200. uvIndex = faces[ offset ++ ];
  201. u = uvLayer[ uvIndex * 2 ];
  202. v = uvLayer[ uvIndex * 2 + 1 ];
  203. uv = new Vector2( u, v );
  204. geometry.faceVertexUvs[ i ][ fi ].push( uv );
  205. }
  206. }
  207. }
  208. if ( hasFaceNormal ) {
  209. normalIndex = faces[ offset ++ ] * 3;
  210. face.normal.set(
  211. normals[ normalIndex ++ ],
  212. normals[ normalIndex ++ ],
  213. normals[ normalIndex ]
  214. );
  215. }
  216. if ( hasFaceVertexNormal ) {
  217. for ( i = 0; i < 3; i ++ ) {
  218. normalIndex = faces[ offset ++ ] * 3;
  219. normal = new Vector3(
  220. normals[ normalIndex ++ ],
  221. normals[ normalIndex ++ ],
  222. normals[ normalIndex ]
  223. );
  224. face.vertexNormals.push( normal );
  225. }
  226. }
  227. if ( hasFaceColor ) {
  228. colorIndex = faces[ offset ++ ];
  229. face.color.setHex( colors[ colorIndex ] );
  230. }
  231. if ( hasFaceVertexColor ) {
  232. for ( i = 0; i < 3; i ++ ) {
  233. colorIndex = faces[ offset ++ ];
  234. face.vertexColors.push( new Color( colors[ colorIndex ] ) );
  235. }
  236. }
  237. geometry.faces.push( face );
  238. }
  239. }
  240. }
  241. function parseSkin( json, geometry ) {
  242. var influencesPerVertex = ( json.influencesPerVertex !== undefined ) ? json.influencesPerVertex : 2;
  243. if ( json.skinWeights ) {
  244. for ( var i = 0, l = json.skinWeights.length; i < l; i += influencesPerVertex ) {
  245. var x = json.skinWeights[ i ];
  246. var y = ( influencesPerVertex > 1 ) ? json.skinWeights[ i + 1 ] : 0;
  247. var z = ( influencesPerVertex > 2 ) ? json.skinWeights[ i + 2 ] : 0;
  248. var w = ( influencesPerVertex > 3 ) ? json.skinWeights[ i + 3 ] : 0;
  249. geometry.skinWeights.push( new Vector4( x, y, z, w ) );
  250. }
  251. }
  252. if ( json.skinIndices ) {
  253. for ( var i = 0, l = json.skinIndices.length; i < l; i += influencesPerVertex ) {
  254. var a = json.skinIndices[ i ];
  255. var b = ( influencesPerVertex > 1 ) ? json.skinIndices[ i + 1 ] : 0;
  256. var c = ( influencesPerVertex > 2 ) ? json.skinIndices[ i + 2 ] : 0;
  257. var d = ( influencesPerVertex > 3 ) ? json.skinIndices[ i + 3 ] : 0;
  258. geometry.skinIndices.push( new Vector4( a, b, c, d ) );
  259. }
  260. }
  261. geometry.bones = json.bones;
  262. if ( geometry.bones && geometry.bones.length > 0 && ( geometry.skinWeights.length !== geometry.skinIndices.length || geometry.skinIndices.length !== geometry.vertices.length ) ) {
  263. console.warn( 'When skinning, number of vertices (' + geometry.vertices.length + '), skinIndices (' +
  264. geometry.skinIndices.length + '), and skinWeights (' + geometry.skinWeights.length + ') should match.' );
  265. }
  266. }
  267. function parseMorphing( json, geometry ) {
  268. var scale = json.scale;
  269. if ( json.morphTargets !== undefined ) {
  270. for ( var i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  271. geometry.morphTargets[ i ] = {};
  272. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  273. geometry.morphTargets[ i ].vertices = [];
  274. var dstVertices = geometry.morphTargets[ i ].vertices;
  275. var srcVertices = json.morphTargets[ i ].vertices;
  276. for ( var v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  277. var vertex = new Vector3();
  278. vertex.x = srcVertices[ v ] * scale;
  279. vertex.y = srcVertices[ v + 1 ] * scale;
  280. vertex.z = srcVertices[ v + 2 ] * scale;
  281. dstVertices.push( vertex );
  282. }
  283. }
  284. }
  285. if ( json.morphColors !== undefined && json.morphColors.length > 0 ) {
  286. console.warn( 'THREE.JSONLoader: "morphColors" no longer supported. Using them as face colors.' );
  287. var faces = geometry.faces;
  288. var morphColors = json.morphColors[ 0 ].colors;
  289. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  290. faces[ i ].color.fromArray( morphColors, i * 3 );
  291. }
  292. }
  293. }
  294. function parseAnimations( json, geometry ) {
  295. var outputAnimations = [];
  296. // parse old style Bone/Hierarchy animations
  297. var animations = [];
  298. if ( json.animation !== undefined ) {
  299. animations.push( json.animation );
  300. }
  301. if ( json.animations !== undefined ) {
  302. if ( json.animations.length ) {
  303. animations = animations.concat( json.animations );
  304. } else {
  305. animations.push( json.animations );
  306. }
  307. }
  308. for ( var i = 0; i < animations.length; i ++ ) {
  309. var clip = AnimationClip.parseAnimation( animations[ i ], geometry.bones );
  310. if ( clip ) outputAnimations.push( clip );
  311. }
  312. // parse implicit morph animations
  313. if ( geometry.morphTargets ) {
  314. // TODO: Figure out what an appropraite FPS is for morph target animations -- defaulting to 10, but really it is completely arbitrary.
  315. var morphAnimationClips = AnimationClip.CreateClipsFromMorphTargetSequences( geometry.morphTargets, 10 );
  316. outputAnimations = outputAnimations.concat( morphAnimationClips );
  317. }
  318. if ( outputAnimations.length > 0 ) geometry.animations = outputAnimations;
  319. }
  320. return function parse( json, path ) {
  321. if ( json.data !== undefined ) {
  322. // Geometry 4.0 spec
  323. json = json.data;
  324. }
  325. if ( json.scale !== undefined ) {
  326. json.scale = 1.0 / json.scale;
  327. } else {
  328. json.scale = 1.0;
  329. }
  330. var geometry = new Geometry();
  331. parseModel( json, geometry );
  332. parseSkin( json, geometry );
  333. parseMorphing( json, geometry );
  334. parseAnimations( json, geometry );
  335. geometry.computeFaceNormals();
  336. geometry.computeBoundingSphere();
  337. if ( json.materials === undefined || json.materials.length === 0 ) {
  338. return { geometry: geometry };
  339. } else {
  340. var materials = Loader.prototype.initMaterials( json.materials, this.resourcePath || path, this.crossOrigin );
  341. return { geometry: geometry, materials: materials };
  342. }
  343. };
  344. } )()
  345. } );
  346. return LegacyJSONLoader;
  347. } )();
  348. export { LegacyJSONLoader };