JSONLoader.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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.load = function ( url, callback, texturePath ) {
  11. var scope = this;
  12. // todo: unify load API to for easier SceneLoader use
  13. texturePath = texturePath && ( typeof texturePath === "string" ) ? texturePath : this.extractUrlBase( url );
  14. this.onLoadStart();
  15. this.loadAjaxJSON( this, url, callback, texturePath );
  16. };
  17. THREE.JSONLoader.prototype.loadAjaxJSON = function ( context, url, callback, texturePath, callbackProgress ) {
  18. var xhr = new XMLHttpRequest();
  19. var length = 0;
  20. xhr.onreadystatechange = function () {
  21. if ( xhr.readyState === xhr.DONE ) {
  22. if ( xhr.status === 200 || xhr.status === 0 ) {
  23. if ( xhr.responseText ) {
  24. var json = JSON.parse( xhr.responseText );
  25. var result = context.parse( json, texturePath );
  26. callback( result.geometry, result.materials );
  27. } else {
  28. console.warn( "THREE.JSONLoader: [" + url + "] seems to be unreachable or file there is empty" );
  29. }
  30. // in context of more complex asset initialization
  31. // do not block on single failed file
  32. // maybe should go even one more level up
  33. context.onLoadComplete();
  34. } else {
  35. console.error( "THREE.JSONLoader: Couldn't load [" + url + "] [" + xhr.status + "]" );
  36. }
  37. } else if ( xhr.readyState === xhr.LOADING ) {
  38. if ( callbackProgress ) {
  39. if ( length === 0 ) {
  40. length = xhr.getResponseHeader( "Content-Length" );
  41. }
  42. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  43. }
  44. } else if ( xhr.readyState === xhr.HEADERS_RECEIVED ) {
  45. if ( callbackProgress !== undefined ) {
  46. length = xhr.getResponseHeader( "Content-Length" );
  47. }
  48. }
  49. };
  50. xhr.open( "GET", url, true );
  51. xhr.withCredentials = this.withCredentials;
  52. xhr.send( null );
  53. };
  54. THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
  55. var scope = this,
  56. geometry = new THREE.Geometry(),
  57. scale = ( json.scale !== undefined ) ? 1.0 / json.scale : 1.0;
  58. parseModel( scale );
  59. parseSkin();
  60. parseMorphing( scale );
  61. geometry.computeCentroids();
  62. geometry.computeFaceNormals();
  63. function parseModel( scale ) {
  64. function isBitSet( value, position ) {
  65. return value & ( 1 << position );
  66. }
  67. var i, j, fi,
  68. offset, zLength, nVertices,
  69. colorIndex, normalIndex, uvIndex, materialIndex,
  70. type,
  71. isQuad,
  72. hasMaterial,
  73. hasFaceUv, hasFaceVertexUv,
  74. hasFaceNormal, hasFaceVertexNormal,
  75. hasFaceColor, hasFaceVertexColor,
  76. vertex, face, color, normal,
  77. uvLayer, uvs, u, v,
  78. faces = json.faces,
  79. vertices = json.vertices,
  80. normals = json.normals,
  81. colors = json.colors,
  82. nUvLayers = 0;
  83. // disregard empty arrays
  84. for ( i = 0; i < json.uvs.length; i++ ) {
  85. if ( json.uvs[ i ].length ) nUvLayers ++;
  86. }
  87. for ( i = 0; i < nUvLayers; i++ ) {
  88. geometry.faceUvs[ i ] = [];
  89. geometry.faceVertexUvs[ i ] = [];
  90. }
  91. offset = 0;
  92. zLength = vertices.length;
  93. while ( offset < zLength ) {
  94. vertex = new THREE.Vector3();
  95. vertex.x = vertices[ offset ++ ] * scale;
  96. vertex.y = vertices[ offset ++ ] * scale;
  97. vertex.z = vertices[ offset ++ ] * scale;
  98. geometry.vertices.push( vertex );
  99. }
  100. offset = 0;
  101. zLength = faces.length;
  102. while ( offset < zLength ) {
  103. type = faces[ offset ++ ];
  104. isQuad = isBitSet( type, 0 );
  105. hasMaterial = isBitSet( type, 1 );
  106. hasFaceUv = isBitSet( type, 2 );
  107. hasFaceVertexUv = isBitSet( type, 3 );
  108. hasFaceNormal = isBitSet( type, 4 );
  109. hasFaceVertexNormal = isBitSet( type, 5 );
  110. hasFaceColor = isBitSet( type, 6 );
  111. hasFaceVertexColor = isBitSet( type, 7 );
  112. //console.log("type", type, "bits", isQuad, hasMaterial, hasFaceUv, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  113. if ( isQuad ) {
  114. face = new THREE.Face4();
  115. face.a = faces[ offset ++ ];
  116. face.b = faces[ offset ++ ];
  117. face.c = faces[ offset ++ ];
  118. face.d = faces[ offset ++ ];
  119. nVertices = 4;
  120. } else {
  121. face = new THREE.Face3();
  122. face.a = faces[ offset ++ ];
  123. face.b = faces[ offset ++ ];
  124. face.c = faces[ offset ++ ];
  125. nVertices = 3;
  126. }
  127. if ( hasMaterial ) {
  128. materialIndex = faces[ offset ++ ];
  129. face.materialIndex = materialIndex;
  130. }
  131. // to get face <=> uv index correspondence
  132. fi = geometry.faces.length;
  133. if ( hasFaceUv ) {
  134. for ( i = 0; i < nUvLayers; i++ ) {
  135. uvLayer = json.uvs[ i ];
  136. uvIndex = faces[ offset ++ ];
  137. u = uvLayer[ uvIndex * 2 ];
  138. v = uvLayer[ uvIndex * 2 + 1 ];
  139. geometry.faceUvs[ i ][ fi ] = new THREE.Vector2( u, v );
  140. }
  141. }
  142. if ( hasFaceVertexUv ) {
  143. for ( i = 0; i < nUvLayers; i++ ) {
  144. uvLayer = json.uvs[ i ];
  145. uvs = [];
  146. for ( j = 0; j < nVertices; j ++ ) {
  147. uvIndex = faces[ offset ++ ];
  148. u = uvLayer[ uvIndex * 2 ];
  149. v = uvLayer[ uvIndex * 2 + 1 ];
  150. uvs[ j ] = new THREE.Vector2( u, v );
  151. }
  152. geometry.faceVertexUvs[ i ][ fi ] = uvs;
  153. }
  154. }
  155. if ( hasFaceNormal ) {
  156. normalIndex = faces[ offset ++ ] * 3;
  157. normal = new THREE.Vector3();
  158. normal.x = normals[ normalIndex ++ ];
  159. normal.y = normals[ normalIndex ++ ];
  160. normal.z = normals[ normalIndex ];
  161. face.normal = normal;
  162. }
  163. if ( hasFaceVertexNormal ) {
  164. for ( i = 0; i < nVertices; i++ ) {
  165. normalIndex = faces[ offset ++ ] * 3;
  166. normal = new THREE.Vector3();
  167. normal.x = normals[ normalIndex ++ ];
  168. normal.y = normals[ normalIndex ++ ];
  169. normal.z = normals[ normalIndex ];
  170. face.vertexNormals.push( normal );
  171. }
  172. }
  173. if ( hasFaceColor ) {
  174. colorIndex = faces[ offset ++ ];
  175. color = new THREE.Color( colors[ colorIndex ] );
  176. face.color = color;
  177. }
  178. if ( hasFaceVertexColor ) {
  179. for ( i = 0; i < nVertices; i++ ) {
  180. colorIndex = faces[ offset ++ ];
  181. color = new THREE.Color( colors[ colorIndex ] );
  182. face.vertexColors.push( color );
  183. }
  184. }
  185. geometry.faces.push( face );
  186. }
  187. };
  188. function parseSkin() {
  189. var i, l, x, y, z, w, a, b, c, d;
  190. if ( json.skinWeights ) {
  191. for ( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
  192. x = json.skinWeights[ i ];
  193. y = json.skinWeights[ i + 1 ];
  194. z = 0;
  195. w = 0;
  196. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  197. }
  198. }
  199. if ( json.skinIndices ) {
  200. for ( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
  201. a = json.skinIndices[ i ];
  202. b = json.skinIndices[ i + 1 ];
  203. c = 0;
  204. d = 0;
  205. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  206. }
  207. }
  208. geometry.bones = json.bones;
  209. geometry.animation = json.animation;
  210. };
  211. function parseMorphing( scale ) {
  212. if ( json.morphTargets !== undefined ) {
  213. var i, l, v, vl, dstVertices, srcVertices;
  214. for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  215. geometry.morphTargets[ i ] = {};
  216. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  217. geometry.morphTargets[ i ].vertices = [];
  218. dstVertices = geometry.morphTargets[ i ].vertices;
  219. srcVertices = json.morphTargets [ i ].vertices;
  220. for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  221. var vertex = new THREE.Vector3();
  222. vertex.x = srcVertices[ v ] * scale;
  223. vertex.y = srcVertices[ v + 1 ] * scale;
  224. vertex.z = srcVertices[ v + 2 ] * scale;
  225. dstVertices.push( vertex );
  226. }
  227. }
  228. }
  229. if ( json.morphColors !== undefined ) {
  230. var i, l, c, cl, dstColors, srcColors, color;
  231. for ( i = 0, l = json.morphColors.length; i < l; i++ ) {
  232. geometry.morphColors[ i ] = {};
  233. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  234. geometry.morphColors[ i ].colors = [];
  235. dstColors = geometry.morphColors[ i ].colors;
  236. srcColors = json.morphColors [ i ].colors;
  237. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  238. color = new THREE.Color( 0xffaa00 );
  239. color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] );
  240. dstColors.push( color );
  241. }
  242. }
  243. }
  244. };
  245. if ( json.materials === undefined ) {
  246. return { geometry: geometry };
  247. } else {
  248. var materials = this.initMaterials( json.materials, texturePath );
  249. if ( this.needsTangents( materials ) ) {
  250. geometry.computeTangents();
  251. }
  252. return { geometry: geometry, materials: materials };
  253. }
  254. };
粤ICP备19079148号