JSONLoader.js 12 KB

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