JSONLoader.js 12 KB

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