JSONLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.JSONLoader = function ( showStatus ) {
  6. THREE.Loader.call( this, showStatus );
  7. this.withCredentials = false;
  8. };
  9. THREE.JSONLoader.prototype = Object.create( THREE.Loader.prototype );
  10. THREE.JSONLoader.prototype.constructor = THREE.JSONLoader;
  11. THREE.JSONLoader.prototype.load = function ( url, callback, texturePath ) {
  12. var scope = this;
  13. // todo: unify load API to for easier SceneLoader use
  14. texturePath = texturePath && ( typeof texturePath === 'string' ) ? texturePath : this.extractUrlBase( url );
  15. this.onLoadStart();
  16. this.loadAjaxJSON( this, url, callback, texturePath );
  17. };
  18. THREE.JSONLoader.prototype.loadAjaxJSON = function ( context, url, callback, texturePath, callbackProgress ) {
  19. var xhr = new XMLHttpRequest();
  20. var length = 0;
  21. xhr.onreadystatechange = function () {
  22. if ( xhr.readyState === xhr.DONE ) {
  23. if ( xhr.status === 200 || xhr.status === 0 ) {
  24. if ( xhr.responseText ) {
  25. var json = JSON.parse( xhr.responseText );
  26. if ( json.metadata !== undefined && json.metadata.version >= 4 ) {
  27. console.error( 'THREE.JSONLoader: "' + url + '" should be loaded with THREE.ObjectLoader instead.' );
  28. return;
  29. }
  30. if ( json.metadata !== undefined && json.metadata.type === 'scene' ) {
  31. THREE.error( 'THREE.JSONLoader: "' + url + '" seems to be a Scene. Use THREE.SceneLoader instead.' );
  32. return;
  33. }
  34. var result = context.parse( json, texturePath );
  35. callback( result.geometry, result.materials );
  36. } else {
  37. THREE.error( 'THREE.JSONLoader: "' + url + '" seems to be unreachable or the file is empty.' );
  38. }
  39. // in context of more complex asset initialization
  40. // do not block on single failed file
  41. // maybe should go even one more level up
  42. context.onLoadComplete();
  43. } else {
  44. THREE.error( 'THREE.JSONLoader: Couldn\'t load "' + url + '" (' + xhr.status + ')' );
  45. }
  46. } else if ( xhr.readyState === xhr.LOADING ) {
  47. if ( callbackProgress ) {
  48. if ( length === 0 ) {
  49. length = xhr.getResponseHeader( 'Content-Length' );
  50. }
  51. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  52. }
  53. } else if ( xhr.readyState === xhr.HEADERS_RECEIVED ) {
  54. if ( callbackProgress !== undefined ) {
  55. length = xhr.getResponseHeader( 'Content-Length' );
  56. }
  57. }
  58. };
  59. xhr.open( 'GET', url, true );
  60. xhr.withCredentials = this.withCredentials;
  61. xhr.send( null );
  62. };
  63. THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
  64. var scope = this,
  65. geometry = new THREE.Geometry(),
  66. scale = ( json.scale !== undefined ) ? 1.0 / json.scale : 1.0;
  67. parseModel( scale );
  68. parseSkin();
  69. parseMorphing( scale );
  70. geometry.computeFaceNormals();
  71. geometry.computeBoundingSphere();
  72. function parseModel( scale ) {
  73. function isBitSet( value, position ) {
  74. return value & ( 1 << position );
  75. }
  76. var i, j, fi,
  77. offset, zLength,
  78. colorIndex, normalIndex, uvIndex, materialIndex,
  79. type,
  80. isQuad,
  81. hasMaterial,
  82. hasFaceVertexUv,
  83. hasFaceNormal, hasFaceVertexNormal,
  84. hasFaceColor, hasFaceVertexColor,
  85. vertex, face, faceA, faceB, color, hex, normal,
  86. uvLayer, uv, u, v,
  87. faces = json.faces,
  88. vertices = json.vertices,
  89. normals = json.normals,
  90. colors = json.colors,
  91. nUvLayers = 0;
  92. if ( json.uvs !== undefined ) {
  93. // disregard empty arrays
  94. for ( i = 0; i < json.uvs.length; i ++ ) {
  95. if ( json.uvs[ i ].length ) nUvLayers ++;
  96. }
  97. for ( i = 0; i < nUvLayers; i ++ ) {
  98. geometry.faceVertexUvs[ i ] = [];
  99. }
  100. }
  101. offset = 0;
  102. zLength = vertices.length;
  103. while ( offset < zLength ) {
  104. vertex = new THREE.Vector3();
  105. vertex.x = vertices[ offset ++ ] * scale;
  106. vertex.y = vertices[ offset ++ ] * scale;
  107. vertex.z = vertices[ offset ++ ] * scale;
  108. geometry.vertices.push( vertex );
  109. }
  110. offset = 0;
  111. zLength = faces.length;
  112. while ( offset < zLength ) {
  113. type = faces[ offset ++ ];
  114. isQuad = isBitSet( type, 0 );
  115. hasMaterial = isBitSet( type, 1 );
  116. hasFaceVertexUv = isBitSet( type, 3 );
  117. hasFaceNormal = isBitSet( type, 4 );
  118. hasFaceVertexNormal = isBitSet( type, 5 );
  119. hasFaceColor = isBitSet( type, 6 );
  120. hasFaceVertexColor = isBitSet( type, 7 );
  121. // console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  122. if ( isQuad ) {
  123. faceA = new THREE.Face3();
  124. faceA.a = faces[ offset ];
  125. faceA.b = faces[ offset + 1 ];
  126. faceA.c = faces[ offset + 3 ];
  127. faceB = new THREE.Face3();
  128. faceB.a = faces[ offset + 1 ];
  129. faceB.b = faces[ offset + 2 ];
  130. faceB.c = faces[ offset + 3 ];
  131. offset += 4;
  132. if ( hasMaterial ) {
  133. materialIndex = faces[ offset ++ ];
  134. faceA.materialIndex = materialIndex;
  135. faceB.materialIndex = materialIndex;
  136. }
  137. // to get face <=> uv index correspondence
  138. fi = geometry.faces.length;
  139. if ( hasFaceVertexUv ) {
  140. for ( i = 0; i < nUvLayers; i ++ ) {
  141. uvLayer = json.uvs[ i ];
  142. geometry.faceVertexUvs[ i ][ fi ] = [];
  143. geometry.faceVertexUvs[ i ][ fi + 1 ] = []
  144. for ( j = 0; j < 4; j ++ ) {
  145. uvIndex = faces[ offset ++ ];
  146. u = uvLayer[ uvIndex * 2 ];
  147. v = uvLayer[ uvIndex * 2 + 1 ];
  148. uv = new THREE.Vector2( u, v );
  149. if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
  150. if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );
  151. }
  152. }
  153. }
  154. if ( hasFaceNormal ) {
  155. normalIndex = faces[ offset ++ ] * 3;
  156. faceA.normal.set(
  157. normals[ normalIndex ++ ],
  158. normals[ normalIndex ++ ],
  159. normals[ normalIndex ]
  160. );
  161. faceB.normal.copy( faceA.normal );
  162. }
  163. if ( hasFaceVertexNormal ) {
  164. for ( i = 0; i < 4; i ++ ) {
  165. normalIndex = faces[ offset ++ ] * 3;
  166. normal = new THREE.Vector3(
  167. normals[ normalIndex ++ ],
  168. normals[ normalIndex ++ ],
  169. normals[ normalIndex ]
  170. );
  171. if ( i !== 2 ) faceA.vertexNormals.push( normal );
  172. if ( i !== 0 ) faceB.vertexNormals.push( normal );
  173. }
  174. }
  175. if ( hasFaceColor ) {
  176. colorIndex = faces[ offset ++ ];
  177. hex = colors[ colorIndex ];
  178. faceA.color.setHex( hex );
  179. faceB.color.setHex( hex );
  180. }
  181. if ( hasFaceVertexColor ) {
  182. for ( i = 0; i < 4; i ++ ) {
  183. colorIndex = faces[ offset ++ ];
  184. hex = colors[ colorIndex ];
  185. if ( i !== 2 ) faceA.vertexColors.push( new THREE.Color( hex ) );
  186. if ( i !== 0 ) faceB.vertexColors.push( new THREE.Color( hex ) );
  187. }
  188. }
  189. geometry.faces.push( faceA );
  190. geometry.faces.push( faceB );
  191. } else {
  192. face = new THREE.Face3();
  193. face.a = faces[ offset ++ ];
  194. face.b = faces[ offset ++ ];
  195. face.c = faces[ offset ++ ];
  196. if ( hasMaterial ) {
  197. materialIndex = faces[ offset ++ ];
  198. face.materialIndex = materialIndex;
  199. }
  200. // to get face <=> uv index correspondence
  201. fi = geometry.faces.length;
  202. if ( hasFaceVertexUv ) {
  203. for ( i = 0; i < nUvLayers; i ++ ) {
  204. uvLayer = json.uvs[ i ];
  205. geometry.faceVertexUvs[ i ][ fi ] = [];
  206. for ( j = 0; j < 3; j ++ ) {
  207. uvIndex = faces[ offset ++ ];
  208. u = uvLayer[ uvIndex * 2 ];
  209. v = uvLayer[ uvIndex * 2 + 1 ];
  210. uv = new THREE.Vector2( u, v );
  211. geometry.faceVertexUvs[ i ][ fi ].push( uv );
  212. }
  213. }
  214. }
  215. if ( hasFaceNormal ) {
  216. normalIndex = faces[ offset ++ ] * 3;
  217. face.normal.set(
  218. normals[ normalIndex ++ ],
  219. normals[ normalIndex ++ ],
  220. normals[ normalIndex ]
  221. );
  222. }
  223. if ( hasFaceVertexNormal ) {
  224. for ( i = 0; i < 3; i ++ ) {
  225. normalIndex = faces[ offset ++ ] * 3;
  226. normal = new THREE.Vector3(
  227. normals[ normalIndex ++ ],
  228. normals[ normalIndex ++ ],
  229. normals[ normalIndex ]
  230. );
  231. face.vertexNormals.push( normal );
  232. }
  233. }
  234. if ( hasFaceColor ) {
  235. colorIndex = faces[ offset ++ ];
  236. face.color.setHex( colors[ colorIndex ] );
  237. }
  238. if ( hasFaceVertexColor ) {
  239. for ( i = 0; i < 3; i ++ ) {
  240. colorIndex = faces[ offset ++ ];
  241. face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
  242. }
  243. }
  244. geometry.faces.push( face );
  245. }
  246. }
  247. };
  248. function parseSkin() {
  249. var influencesPerVertex = ( json.influencesPerVertex !== undefined ) ? json.influencesPerVertex : 2;
  250. if ( json.skinWeights ) {
  251. for ( var i = 0, l = json.skinWeights.length; i < l; i += influencesPerVertex ) {
  252. var x = json.skinWeights[ i ];
  253. var y = ( influencesPerVertex > 1 ) ? json.skinWeights[ i + 1 ] : 0;
  254. var z = ( influencesPerVertex > 2 ) ? json.skinWeights[ i + 2 ] : 0;
  255. var w = ( influencesPerVertex > 3 ) ? json.skinWeights[ i + 3 ] : 0;
  256. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  257. }
  258. }
  259. if ( json.skinIndices ) {
  260. for ( var i = 0, l = json.skinIndices.length; i < l; i += influencesPerVertex ) {
  261. var a = json.skinIndices[ i ];
  262. var b = ( influencesPerVertex > 1 ) ? json.skinIndices[ i + 1 ] : 0;
  263. var c = ( influencesPerVertex > 2 ) ? json.skinIndices[ i + 2 ] : 0;
  264. var d = ( influencesPerVertex > 3 ) ? json.skinIndices[ i + 3 ] : 0;
  265. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  266. }
  267. }
  268. geometry.bones = json.bones;
  269. if ( geometry.bones && geometry.bones.length > 0 && ( geometry.skinWeights.length !== geometry.skinIndices.length || geometry.skinIndices.length !== geometry.vertices.length ) ) {
  270. THREE.warn( 'THREE.JSONLoader: When skinning, number of vertices (' + geometry.vertices.length + '), skinIndices (' +
  271. geometry.skinIndices.length + '), and skinWeights (' + geometry.skinWeights.length + ') should match.' );
  272. }
  273. // could change this to json.animations[0] or remove completely
  274. geometry.animation = json.animation;
  275. geometry.animations = json.animations;
  276. };
  277. function parseMorphing( scale ) {
  278. if ( json.morphTargets !== undefined ) {
  279. var i, l, v, vl, dstVertices, srcVertices;
  280. for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  281. geometry.morphTargets[ i ] = {};
  282. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  283. geometry.morphTargets[ i ].vertices = [];
  284. dstVertices = geometry.morphTargets[ i ].vertices;
  285. srcVertices = json.morphTargets [ i ].vertices;
  286. for ( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  287. var vertex = new THREE.Vector3();
  288. vertex.x = srcVertices[ v ] * scale;
  289. vertex.y = srcVertices[ v + 1 ] * scale;
  290. vertex.z = srcVertices[ v + 2 ] * scale;
  291. dstVertices.push( vertex );
  292. }
  293. }
  294. }
  295. if ( json.morphColors !== undefined ) {
  296. var i, l, c, cl, dstColors, srcColors, color;
  297. for ( i = 0, l = json.morphColors.length; i < l; i ++ ) {
  298. geometry.morphColors[ i ] = {};
  299. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  300. geometry.morphColors[ i ].colors = [];
  301. dstColors = geometry.morphColors[ i ].colors;
  302. srcColors = json.morphColors [ i ].colors;
  303. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  304. color = new THREE.Color( 0xffaa00 );
  305. color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] );
  306. dstColors.push( color );
  307. }
  308. }
  309. }
  310. };
  311. if ( json.materials === undefined || json.materials.length === 0 ) {
  312. return { geometry: geometry };
  313. } else {
  314. var materials = this.initMaterials( json.materials, texturePath );
  315. if ( this.needsTangents( materials ) ) {
  316. geometry.computeTangents();
  317. }
  318. return { geometry: geometry, materials: materials };
  319. }
  320. };
粤ICP备19079148号