JSONLoader.js 8.4 KB

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