JSONLoader.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. length = xhr.getResponseHeader( "Content-Length" );
  46. }
  47. };
  48. xhr.open( "GET", url, true );
  49. xhr.withCredentials = this.withCredentials;
  50. xhr.send( null );
  51. };
  52. THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
  53. var scope = this,
  54. geometry = new THREE.Geometry(),
  55. scale = ( json.scale !== undefined ) ? 1.0 / json.scale : 1.0;
  56. parseModel( scale );
  57. parseSkin();
  58. parseMorphing( scale );
  59. geometry.computeCentroids();
  60. geometry.computeFaceNormals();
  61. function parseModel( scale ) {
  62. function isBitSet( value, position ) {
  63. return value & ( 1 << position );
  64. }
  65. var i, j, fi,
  66. offset, zLength, nVertices,
  67. colorIndex, normalIndex, uvIndex, materialIndex,
  68. type,
  69. isQuad,
  70. hasMaterial,
  71. hasFaceUv, hasFaceVertexUv,
  72. hasFaceNormal, hasFaceVertexNormal,
  73. hasFaceColor, hasFaceVertexColor,
  74. vertex, face, color, normal,
  75. uvLayer, uvs, u, v,
  76. faces = json.faces,
  77. vertices = json.vertices,
  78. normals = json.normals,
  79. colors = json.colors,
  80. nUvLayers = 0;
  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.faceUvs[ i ] = [];
  87. geometry.faceVertexUvs[ i ] = [];
  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. hasFaceUv = isBitSet( type, 2 );
  105. hasFaceVertexUv = isBitSet( type, 3 );
  106. hasFaceNormal = isBitSet( type, 4 );
  107. hasFaceVertexNormal = isBitSet( type, 5 );
  108. hasFaceColor = isBitSet( type, 6 );
  109. hasFaceVertexColor = isBitSet( type, 7 );
  110. //console.log("type", type, "bits", isQuad, hasMaterial, hasFaceUv, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  111. if ( isQuad ) {
  112. face = new THREE.Face4();
  113. face.a = faces[ offset ++ ];
  114. face.b = faces[ offset ++ ];
  115. face.c = faces[ offset ++ ];
  116. face.d = faces[ offset ++ ];
  117. nVertices = 4;
  118. } else {
  119. face = new THREE.Face3();
  120. face.a = faces[ offset ++ ];
  121. face.b = faces[ offset ++ ];
  122. face.c = faces[ offset ++ ];
  123. nVertices = 3;
  124. }
  125. if ( hasMaterial ) {
  126. materialIndex = faces[ offset ++ ];
  127. face.materialIndex = materialIndex;
  128. }
  129. // to get face <=> uv index correspondence
  130. fi = geometry.faces.length;
  131. if ( hasFaceUv ) {
  132. for ( i = 0; i < nUvLayers; i++ ) {
  133. uvLayer = json.uvs[ i ];
  134. uvIndex = faces[ offset ++ ];
  135. u = uvLayer[ uvIndex * 2 ];
  136. v = uvLayer[ uvIndex * 2 + 1 ];
  137. geometry.faceUvs[ i ][ fi ] = new THREE.Vector2( u, v );
  138. }
  139. }
  140. if ( hasFaceVertexUv ) {
  141. for ( i = 0; i < nUvLayers; i++ ) {
  142. uvLayer = json.uvs[ i ];
  143. uvs = [];
  144. for ( j = 0; j < nVertices; j ++ ) {
  145. uvIndex = faces[ offset ++ ];
  146. u = uvLayer[ uvIndex * 2 ];
  147. v = uvLayer[ uvIndex * 2 + 1 ];
  148. uvs[ j ] = new THREE.Vector2( u, v );
  149. }
  150. geometry.faceVertexUvs[ i ][ fi ] = uvs;
  151. }
  152. }
  153. if ( hasFaceNormal ) {
  154. normalIndex = faces[ offset ++ ] * 3;
  155. normal = new THREE.Vector3();
  156. normal.x = normals[ normalIndex ++ ];
  157. normal.y = normals[ normalIndex ++ ];
  158. normal.z = normals[ normalIndex ];
  159. face.normal = normal;
  160. }
  161. if ( hasFaceVertexNormal ) {
  162. for ( i = 0; i < nVertices; i++ ) {
  163. normalIndex = faces[ offset ++ ] * 3;
  164. normal = new THREE.Vector3();
  165. normal.x = normals[ normalIndex ++ ];
  166. normal.y = normals[ normalIndex ++ ];
  167. normal.z = normals[ normalIndex ];
  168. face.vertexNormals.push( normal );
  169. }
  170. }
  171. if ( hasFaceColor ) {
  172. colorIndex = faces[ offset ++ ];
  173. color = new THREE.Color( colors[ colorIndex ] );
  174. face.color = color;
  175. }
  176. if ( hasFaceVertexColor ) {
  177. for ( i = 0; i < nVertices; i++ ) {
  178. colorIndex = faces[ offset ++ ];
  179. color = new THREE.Color( colors[ colorIndex ] );
  180. face.vertexColors.push( color );
  181. }
  182. }
  183. geometry.faces.push( face );
  184. }
  185. };
  186. function parseSkin() {
  187. var i, l, x, y, z, w, a, b, c, d;
  188. if ( json.skinWeights ) {
  189. for ( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
  190. x = json.skinWeights[ i ];
  191. y = json.skinWeights[ i + 1 ];
  192. z = 0;
  193. w = 0;
  194. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  195. }
  196. }
  197. if ( json.skinIndices ) {
  198. for ( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
  199. a = json.skinIndices[ i ];
  200. b = json.skinIndices[ i + 1 ];
  201. c = 0;
  202. d = 0;
  203. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  204. }
  205. }
  206. geometry.bones = json.bones;
  207. geometry.animation = json.animation;
  208. };
  209. function parseMorphing( scale ) {
  210. if ( json.morphTargets !== undefined ) {
  211. var i, l, v, vl, dstVertices, srcVertices;
  212. for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  213. geometry.morphTargets[ i ] = {};
  214. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  215. geometry.morphTargets[ i ].vertices = [];
  216. dstVertices = geometry.morphTargets[ i ].vertices;
  217. srcVertices = json.morphTargets [ i ].vertices;
  218. for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  219. var vertex = new THREE.Vector3();
  220. vertex.x = srcVertices[ v ] * scale;
  221. vertex.y = srcVertices[ v + 1 ] * scale;
  222. vertex.z = srcVertices[ v + 2 ] * scale;
  223. dstVertices.push( vertex );
  224. }
  225. }
  226. }
  227. if ( json.morphColors !== undefined ) {
  228. var i, l, c, cl, dstColors, srcColors, color;
  229. for ( i = 0, l = json.morphColors.length; i < l; i++ ) {
  230. geometry.morphColors[ i ] = {};
  231. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  232. geometry.morphColors[ i ].colors = [];
  233. dstColors = geometry.morphColors[ i ].colors;
  234. srcColors = json.morphColors [ i ].colors;
  235. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  236. color = new THREE.Color( 0xffaa00 );
  237. color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] );
  238. dstColors.push( color );
  239. }
  240. }
  241. }
  242. };
  243. var materials = this.initMaterials( json.materials, texturePath );
  244. if ( this.needsTangents( materials ) ) {
  245. geometry.computeTangents();
  246. }
  247. return {
  248. geometry: geometry,
  249. materials: materials
  250. };
  251. };