JSONLoader.js 12 KB

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