JSONLoader.js 8.9 KB

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