JSONLoader.js 12 KB

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