JSONLoader.js 8.6 KB

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