JSONLoader.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. 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, nVertices,
  70. colorIndex, normalIndex, uvIndex, materialIndex,
  71. type,
  72. isQuad,
  73. hasMaterial,
  74. hasFaceVertexUv,
  75. hasFaceNormal, hasFaceVertexNormal,
  76. hasFaceColor, hasFaceVertexColor,
  77. vertex, face, color, normal,
  78. uvLayer, uvs, 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. face = new THREE.Face4();
  116. face.a = faces[ offset ++ ];
  117. face.b = faces[ offset ++ ];
  118. face.c = faces[ offset ++ ];
  119. face.d = faces[ offset ++ ];
  120. nVertices = 4;
  121. } else {
  122. face = new THREE.Face3();
  123. face.a = faces[ offset ++ ];
  124. face.b = faces[ offset ++ ];
  125. face.c = faces[ offset ++ ];
  126. nVertices = 3;
  127. }
  128. if ( hasMaterial ) {
  129. materialIndex = faces[ offset ++ ];
  130. face.materialIndex = materialIndex;
  131. }
  132. // to get face <=> uv index correspondence
  133. fi = geometry.faces.length;
  134. if ( hasFaceVertexUv ) {
  135. for ( i = 0; i < nUvLayers; i++ ) {
  136. uvLayer = json.uvs[ i ];
  137. uvs = [];
  138. for ( j = 0; j < nVertices; j ++ ) {
  139. uvIndex = faces[ offset ++ ];
  140. u = uvLayer[ uvIndex * 2 ];
  141. v = uvLayer[ uvIndex * 2 + 1 ];
  142. uvs[ j ] = new THREE.Vector2( u, v );
  143. }
  144. geometry.faceVertexUvs[ i ][ fi ] = uvs;
  145. }
  146. }
  147. if ( hasFaceNormal ) {
  148. normalIndex = faces[ offset ++ ] * 3;
  149. face.normal.set(
  150. normals[ normalIndex ++ ],
  151. normals[ normalIndex ++ ],
  152. normals[ normalIndex ]
  153. );
  154. }
  155. if ( hasFaceVertexNormal ) {
  156. for ( i = 0; i < nVertices; i++ ) {
  157. normalIndex = faces[ offset ++ ] * 3;
  158. normal = new THREE.Vector3(
  159. normals[ normalIndex ++ ],
  160. normals[ normalIndex ++ ],
  161. normals[ normalIndex ]
  162. );
  163. face.vertexNormals.push( normal );
  164. }
  165. }
  166. if ( hasFaceColor ) {
  167. colorIndex = faces[ offset ++ ];
  168. face.color.setHex( colors[ colorIndex ] );
  169. }
  170. if ( hasFaceVertexColor ) {
  171. for ( i = 0; i < nVertices; i++ ) {
  172. colorIndex = faces[ offset ++ ];
  173. face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
  174. }
  175. }
  176. geometry.faces.push( face );
  177. }
  178. };
  179. function parseSkin() {
  180. var i, l, x, y, z, w, a, b, c, d;
  181. if ( json.skinWeights ) {
  182. for ( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
  183. x = json.skinWeights[ i ];
  184. y = json.skinWeights[ i + 1 ];
  185. z = 0;
  186. w = 0;
  187. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  188. }
  189. }
  190. if ( json.skinIndices ) {
  191. for ( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
  192. a = json.skinIndices[ i ];
  193. b = json.skinIndices[ i + 1 ];
  194. c = 0;
  195. d = 0;
  196. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  197. }
  198. }
  199. geometry.bones = json.bones;
  200. geometry.animation = json.animation;
  201. };
  202. function parseMorphing( scale ) {
  203. if ( json.morphTargets !== undefined ) {
  204. var i, l, v, vl, dstVertices, srcVertices;
  205. for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  206. geometry.morphTargets[ i ] = {};
  207. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  208. geometry.morphTargets[ i ].vertices = [];
  209. dstVertices = geometry.morphTargets[ i ].vertices;
  210. srcVertices = json.morphTargets [ i ].vertices;
  211. for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  212. var vertex = new THREE.Vector3();
  213. vertex.x = srcVertices[ v ] * scale;
  214. vertex.y = srcVertices[ v + 1 ] * scale;
  215. vertex.z = srcVertices[ v + 2 ] * scale;
  216. dstVertices.push( vertex );
  217. }
  218. }
  219. }
  220. if ( json.morphColors !== undefined ) {
  221. var i, l, c, cl, dstColors, srcColors, color;
  222. for ( i = 0, l = json.morphColors.length; i < l; i++ ) {
  223. geometry.morphColors[ i ] = {};
  224. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  225. geometry.morphColors[ i ].colors = [];
  226. dstColors = geometry.morphColors[ i ].colors;
  227. srcColors = json.morphColors [ i ].colors;
  228. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  229. color = new THREE.Color( 0xffaa00 );
  230. color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] );
  231. dstColors.push( color );
  232. }
  233. }
  234. }
  235. };
  236. if ( json.materials === undefined ) {
  237. return { geometry: geometry };
  238. } else {
  239. var materials = this.initMaterials( json.materials, texturePath );
  240. if ( this.needsTangents( materials ) ) {
  241. geometry.computeTangents();
  242. }
  243. return { geometry: geometry, materials: materials };
  244. }
  245. };
粤ICP备19079148号