JSONLoader.js 12 KB

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