JSONLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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,
  70. colorIndex, normalIndex, uvIndex, materialIndex,
  71. type,
  72. isQuad,
  73. hasMaterial,
  74. hasFaceVertexUv,
  75. hasFaceNormal, hasFaceVertexNormal,
  76. hasFaceColor, hasFaceVertexColor,
  77. vertex, face, faceA, faceB, color, hex, normal,
  78. uvLayer, uv, 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. faceA = new THREE.Face3();
  116. faceA.a = faces[ offset ];
  117. faceA.b = faces[ offset + 1 ];
  118. faceA.c = faces[ offset + 3 ];
  119. faceB = new THREE.Face3();
  120. faceB.a = faces[ offset + 1 ];
  121. faceB.b = faces[ offset + 2 ];
  122. faceB.c = faces[ offset + 3 ];
  123. offset += 4;
  124. if ( hasMaterial ) {
  125. materialIndex = faces[ offset ++ ];
  126. faceA.materialIndex = materialIndex;
  127. faceB.materialIndex = materialIndex;
  128. }
  129. // to get face <=> uv index correspondence
  130. fi = geometry.faces.length;
  131. if ( hasFaceVertexUv ) {
  132. for ( i = 0; i < nUvLayers; i++ ) {
  133. uvLayer = json.uvs[ i ];
  134. geometry.faceVertexUvs[ i ][ fi ] = [];
  135. geometry.faceVertexUvs[ i ][ fi + 1 ] = []
  136. for ( j = 0; j < 4; j ++ ) {
  137. uvIndex = faces[ offset ++ ];
  138. u = uvLayer[ uvIndex * 2 ];
  139. v = uvLayer[ uvIndex * 2 + 1 ];
  140. uv = new THREE.Vector2( u, v );
  141. if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
  142. if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );
  143. }
  144. }
  145. }
  146. if ( hasFaceNormal ) {
  147. normalIndex = faces[ offset ++ ] * 3;
  148. faceA.normal.set(
  149. normals[ normalIndex ++ ],
  150. normals[ normalIndex ++ ],
  151. normals[ normalIndex ]
  152. );
  153. faceB.normal.copy( faceA.normal );
  154. }
  155. if ( hasFaceVertexNormal ) {
  156. for ( i = 0; i < 4; i++ ) {
  157. normalIndex = faces[ offset ++ ] * 3;
  158. normal = new THREE.Vector3(
  159. normals[ normalIndex ++ ],
  160. normals[ normalIndex ++ ],
  161. normals[ normalIndex ]
  162. );
  163. if ( i !== 2 ) faceA.vertexNormals.push( normal );
  164. if ( i !== 0 ) faceB.vertexNormals.push( normal );
  165. }
  166. }
  167. if ( hasFaceColor ) {
  168. colorIndex = faces[ offset ++ ];
  169. hex = colors[ colorIndex ];
  170. faceA.color.setHex( hex );
  171. faceB.color.setHex( hex );
  172. }
  173. if ( hasFaceVertexColor ) {
  174. for ( i = 0; i < 4; i++ ) {
  175. colorIndex = faces[ offset ++ ];
  176. hex = colors[ colorIndex ];
  177. if ( i !== 2 ) faceA.vertexColors.push( new THREE.Color( hex ) );
  178. if ( i !== 0 ) faceB.vertexColors.push( new THREE.Color( hex ) );
  179. }
  180. }
  181. geometry.faces.push( faceA );
  182. geometry.faces.push( faceB );
  183. } else {
  184. face = new THREE.Face3();
  185. face.a = faces[ offset ++ ];
  186. face.b = faces[ offset ++ ];
  187. face.c = faces[ offset ++ ];
  188. if ( hasMaterial ) {
  189. materialIndex = faces[ offset ++ ];
  190. face.materialIndex = materialIndex;
  191. }
  192. // to get face <=> uv index correspondence
  193. fi = geometry.faces.length;
  194. if ( hasFaceVertexUv ) {
  195. for ( i = 0; i < nUvLayers; i++ ) {
  196. uvLayer = json.uvs[ i ];
  197. geometry.faceVertexUvs[ i ][ fi ] = [];
  198. for ( j = 0; j < 3; j ++ ) {
  199. uvIndex = faces[ offset ++ ];
  200. u = uvLayer[ uvIndex * 2 ];
  201. v = uvLayer[ uvIndex * 2 + 1 ];
  202. uv = new THREE.Vector2( u, v );
  203. geometry.faceVertexUvs[ i ][ fi ].push( uv );
  204. }
  205. }
  206. }
  207. if ( hasFaceNormal ) {
  208. normalIndex = faces[ offset ++ ] * 3;
  209. face.normal.set(
  210. normals[ normalIndex ++ ],
  211. normals[ normalIndex ++ ],
  212. normals[ normalIndex ]
  213. );
  214. }
  215. if ( hasFaceVertexNormal ) {
  216. for ( i = 0; i < 3; i++ ) {
  217. normalIndex = faces[ offset ++ ] * 3;
  218. normal = new THREE.Vector3(
  219. normals[ normalIndex ++ ],
  220. normals[ normalIndex ++ ],
  221. normals[ normalIndex ]
  222. );
  223. face.vertexNormals.push( normal );
  224. }
  225. }
  226. if ( hasFaceColor ) {
  227. colorIndex = faces[ offset ++ ];
  228. face.color.setHex( colors[ colorIndex ] );
  229. }
  230. if ( hasFaceVertexColor ) {
  231. for ( i = 0; i < 3; i++ ) {
  232. colorIndex = faces[ offset ++ ];
  233. face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
  234. }
  235. }
  236. geometry.faces.push( face );
  237. }
  238. }
  239. };
  240. function parseSkin() {
  241. var i, l, x, y, z, w, a, b, c, d;
  242. if ( json.skinWeights ) {
  243. for ( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
  244. x = json.skinWeights[ i ];
  245. y = json.skinWeights[ i + 1 ];
  246. z = 0;
  247. w = 0;
  248. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  249. }
  250. }
  251. if ( json.skinIndices ) {
  252. for ( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
  253. a = json.skinIndices[ i ];
  254. b = json.skinIndices[ i + 1 ];
  255. c = 0;
  256. d = 0;
  257. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  258. }
  259. }
  260. geometry.bones = json.bones;
  261. // could change this to json.animations[0] or remove completely
  262. geometry.animation = json.animation;
  263. geometry.animations = json.animations;
  264. };
  265. function parseMorphing( scale ) {
  266. if ( json.morphTargets !== undefined ) {
  267. var i, l, v, vl, dstVertices, srcVertices;
  268. for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  269. geometry.morphTargets[ i ] = {};
  270. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  271. geometry.morphTargets[ i ].vertices = [];
  272. dstVertices = geometry.morphTargets[ i ].vertices;
  273. srcVertices = json.morphTargets [ i ].vertices;
  274. for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  275. var vertex = new THREE.Vector3();
  276. vertex.x = srcVertices[ v ] * scale;
  277. vertex.y = srcVertices[ v + 1 ] * scale;
  278. vertex.z = srcVertices[ v + 2 ] * scale;
  279. dstVertices.push( vertex );
  280. }
  281. }
  282. }
  283. if ( json.morphColors !== undefined ) {
  284. var i, l, c, cl, dstColors, srcColors, color;
  285. for ( i = 0, l = json.morphColors.length; i < l; i++ ) {
  286. geometry.morphColors[ i ] = {};
  287. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  288. geometry.morphColors[ i ].colors = [];
  289. dstColors = geometry.morphColors[ i ].colors;
  290. srcColors = json.morphColors [ i ].colors;
  291. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  292. color = new THREE.Color( 0xffaa00 );
  293. color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] );
  294. dstColors.push( color );
  295. }
  296. }
  297. }
  298. };
  299. if ( json.materials === undefined ) {
  300. return { geometry: geometry };
  301. } else {
  302. var materials = this.initMaterials( json.materials, texturePath );
  303. if ( this.needsTangents( materials ) ) {
  304. geometry.computeTangents();
  305. }
  306. return { geometry: geometry, materials: materials };
  307. }
  308. };
粤ICP备19079148号