JSONLoader.js 12 KB

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