JSONLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.JSONLoader = function ( manager ) {
  6. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7. this.withCredentials = false;
  8. };
  9. THREE.JSONLoader.prototype = {
  10. constructor: THREE.JSONLoader,
  11. load: function( url, onLoad, onProgress, onError ) {
  12. var scope = this;
  13. var texturePath = this.texturePath && ( typeof this.texturePath === "string" ) ? this.texturePath : THREE.Loader.prototype.extractUrlBase( url );
  14. var loader = new THREE.XHRLoader( this.manager );
  15. loader.setCrossOrigin( this.crossOrigin );
  16. loader.setWithCredentials( this.withCredentials );
  17. loader.load( url, function ( text ) {
  18. var json = JSON.parse( text );
  19. var metadata = json.metadata;
  20. if ( metadata !== undefined ) {
  21. if ( metadata.type === 'object' ) {
  22. console.error( 'THREE.JSONLoader: ' + url + ' should be loaded with THREE.ObjectLoader instead.' );
  23. return;
  24. }
  25. if ( metadata.type === 'scene' ) {
  26. console.error( 'THREE.JSONLoader: ' + url + ' should be loaded with THREE.SceneLoader instead.' );
  27. return;
  28. }
  29. }
  30. var object = scope.parse( json, texturePath );
  31. onLoad( object.geometry, object.materials );
  32. } );
  33. },
  34. setCrossOrigin: function ( value ) {
  35. this.crossOrigin = value;
  36. },
  37. setTexturePath: function ( value ) {
  38. this.texturePath = value;
  39. },
  40. parse: function ( json, texturePath ) {
  41. var geometry = new THREE.Geometry(),
  42. scale = ( json.scale !== undefined ) ? 1.0 / json.scale : 1.0;
  43. parseModel( scale );
  44. parseSkin();
  45. parseMorphing( scale );
  46. geometry.computeFaceNormals();
  47. geometry.computeBoundingSphere();
  48. function parseModel( scale ) {
  49. function isBitSet( value, position ) {
  50. return value & ( 1 << position );
  51. }
  52. var i, j, fi,
  53. offset, zLength,
  54. colorIndex, normalIndex, uvIndex, materialIndex,
  55. type,
  56. isQuad,
  57. hasMaterial,
  58. hasFaceVertexUv,
  59. hasFaceNormal, hasFaceVertexNormal,
  60. hasFaceColor, hasFaceVertexColor,
  61. vertex, face, faceA, faceB, hex, normal,
  62. uvLayer, uv, u, v,
  63. faces = json.faces,
  64. vertices = json.vertices,
  65. normals = json.normals,
  66. colors = json.colors,
  67. nUvLayers = 0;
  68. if ( json.uvs !== undefined ) {
  69. // disregard empty arrays
  70. for ( i = 0; i < json.uvs.length; i ++ ) {
  71. if ( json.uvs[ i ].length ) nUvLayers ++;
  72. }
  73. for ( i = 0; i < nUvLayers; i ++ ) {
  74. geometry.faceVertexUvs[ i ] = [];
  75. }
  76. }
  77. offset = 0;
  78. zLength = vertices.length;
  79. while ( offset < zLength ) {
  80. vertex = new THREE.Vector3();
  81. vertex.x = vertices[ offset ++ ] * scale;
  82. vertex.y = vertices[ offset ++ ] * scale;
  83. vertex.z = vertices[ offset ++ ] * scale;
  84. geometry.vertices.push( vertex );
  85. }
  86. offset = 0;
  87. zLength = faces.length;
  88. while ( offset < zLength ) {
  89. type = faces[ offset ++ ];
  90. isQuad = isBitSet( type, 0 );
  91. hasMaterial = isBitSet( type, 1 );
  92. hasFaceVertexUv = isBitSet( type, 3 );
  93. hasFaceNormal = isBitSet( type, 4 );
  94. hasFaceVertexNormal = isBitSet( type, 5 );
  95. hasFaceColor = isBitSet( type, 6 );
  96. hasFaceVertexColor = isBitSet( type, 7 );
  97. // console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  98. if ( isQuad ) {
  99. faceA = new THREE.Face3();
  100. faceA.a = faces[ offset ];
  101. faceA.b = faces[ offset + 1 ];
  102. faceA.c = faces[ offset + 3 ];
  103. faceB = new THREE.Face3();
  104. faceB.a = faces[ offset + 1 ];
  105. faceB.b = faces[ offset + 2 ];
  106. faceB.c = faces[ offset + 3 ];
  107. offset += 4;
  108. if ( hasMaterial ) {
  109. materialIndex = faces[ offset ++ ];
  110. faceA.materialIndex = materialIndex;
  111. faceB.materialIndex = materialIndex;
  112. }
  113. // to get face <=> uv index correspondence
  114. fi = geometry.faces.length;
  115. if ( hasFaceVertexUv ) {
  116. for ( i = 0; i < nUvLayers; i ++ ) {
  117. uvLayer = json.uvs[ i ];
  118. geometry.faceVertexUvs[ i ][ fi ] = [];
  119. geometry.faceVertexUvs[ i ][ fi + 1 ] = [];
  120. for ( j = 0; j < 4; j ++ ) {
  121. uvIndex = faces[ offset ++ ];
  122. u = uvLayer[ uvIndex * 2 ];
  123. v = uvLayer[ uvIndex * 2 + 1 ];
  124. uv = new THREE.Vector2( u, v );
  125. if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
  126. if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );
  127. }
  128. }
  129. }
  130. if ( hasFaceNormal ) {
  131. normalIndex = faces[ offset ++ ] * 3;
  132. faceA.normal.set(
  133. normals[ normalIndex ++ ],
  134. normals[ normalIndex ++ ],
  135. normals[ normalIndex ]
  136. );
  137. faceB.normal.copy( faceA.normal );
  138. }
  139. if ( hasFaceVertexNormal ) {
  140. for ( i = 0; i < 4; i ++ ) {
  141. normalIndex = faces[ offset ++ ] * 3;
  142. normal = new THREE.Vector3(
  143. normals[ normalIndex ++ ],
  144. normals[ normalIndex ++ ],
  145. normals[ normalIndex ]
  146. );
  147. if ( i !== 2 ) faceA.vertexNormals.push( normal );
  148. if ( i !== 0 ) faceB.vertexNormals.push( normal );
  149. }
  150. }
  151. if ( hasFaceColor ) {
  152. colorIndex = faces[ offset ++ ];
  153. hex = colors[ colorIndex ];
  154. faceA.color.setHex( hex );
  155. faceB.color.setHex( hex );
  156. }
  157. if ( hasFaceVertexColor ) {
  158. for ( i = 0; i < 4; i ++ ) {
  159. colorIndex = faces[ offset ++ ];
  160. hex = colors[ colorIndex ];
  161. if ( i !== 2 ) faceA.vertexColors.push( new THREE.Color( hex ) );
  162. if ( i !== 0 ) faceB.vertexColors.push( new THREE.Color( hex ) );
  163. }
  164. }
  165. geometry.faces.push( faceA );
  166. geometry.faces.push( faceB );
  167. } else {
  168. face = new THREE.Face3();
  169. face.a = faces[ offset ++ ];
  170. face.b = faces[ offset ++ ];
  171. face.c = faces[ offset ++ ];
  172. if ( hasMaterial ) {
  173. materialIndex = faces[ offset ++ ];
  174. face.materialIndex = materialIndex;
  175. }
  176. // to get face <=> uv index correspondence
  177. fi = geometry.faces.length;
  178. if ( hasFaceVertexUv ) {
  179. for ( i = 0; i < nUvLayers; i ++ ) {
  180. uvLayer = json.uvs[ i ];
  181. geometry.faceVertexUvs[ i ][ fi ] = [];
  182. for ( j = 0; j < 3; j ++ ) {
  183. uvIndex = faces[ offset ++ ];
  184. u = uvLayer[ uvIndex * 2 ];
  185. v = uvLayer[ uvIndex * 2 + 1 ];
  186. uv = new THREE.Vector2( u, v );
  187. geometry.faceVertexUvs[ i ][ fi ].push( uv );
  188. }
  189. }
  190. }
  191. if ( hasFaceNormal ) {
  192. normalIndex = faces[ offset ++ ] * 3;
  193. face.normal.set(
  194. normals[ normalIndex ++ ],
  195. normals[ normalIndex ++ ],
  196. normals[ normalIndex ]
  197. );
  198. }
  199. if ( hasFaceVertexNormal ) {
  200. for ( i = 0; i < 3; i ++ ) {
  201. normalIndex = faces[ offset ++ ] * 3;
  202. normal = new THREE.Vector3(
  203. normals[ normalIndex ++ ],
  204. normals[ normalIndex ++ ],
  205. normals[ normalIndex ]
  206. );
  207. face.vertexNormals.push( normal );
  208. }
  209. }
  210. if ( hasFaceColor ) {
  211. colorIndex = faces[ offset ++ ];
  212. face.color.setHex( colors[ colorIndex ] );
  213. }
  214. if ( hasFaceVertexColor ) {
  215. for ( i = 0; i < 3; i ++ ) {
  216. colorIndex = faces[ offset ++ ];
  217. face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
  218. }
  219. }
  220. geometry.faces.push( face );
  221. }
  222. }
  223. };
  224. function parseSkin() {
  225. var influencesPerVertex = ( json.influencesPerVertex !== undefined ) ? json.influencesPerVertex : 2;
  226. if ( json.skinWeights ) {
  227. for ( var i = 0, l = json.skinWeights.length; i < l; i += influencesPerVertex ) {
  228. var x = json.skinWeights[ i ];
  229. var y = ( influencesPerVertex > 1 ) ? json.skinWeights[ i + 1 ] : 0;
  230. var z = ( influencesPerVertex > 2 ) ? json.skinWeights[ i + 2 ] : 0;
  231. var w = ( influencesPerVertex > 3 ) ? json.skinWeights[ i + 3 ] : 0;
  232. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  233. }
  234. }
  235. if ( json.skinIndices ) {
  236. for ( var i = 0, l = json.skinIndices.length; i < l; i += influencesPerVertex ) {
  237. var a = json.skinIndices[ i ];
  238. var b = ( influencesPerVertex > 1 ) ? json.skinIndices[ i + 1 ] : 0;
  239. var c = ( influencesPerVertex > 2 ) ? json.skinIndices[ i + 2 ] : 0;
  240. var d = ( influencesPerVertex > 3 ) ? json.skinIndices[ i + 3 ] : 0;
  241. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  242. }
  243. }
  244. geometry.bones = json.bones;
  245. if ( geometry.bones && geometry.bones.length > 0 && ( geometry.skinWeights.length !== geometry.skinIndices.length || geometry.skinIndices.length !== geometry.vertices.length ) ) {
  246. console.warn( 'When skinning, number of vertices (' + geometry.vertices.length + '), skinIndices (' +
  247. geometry.skinIndices.length + '), and skinWeights (' + geometry.skinWeights.length + ') should match.' );
  248. }
  249. // could change this to json.animations[0] or remove completely
  250. geometry.animation = json.animation;
  251. geometry.animations = json.animations;
  252. };
  253. function parseMorphing( scale ) {
  254. if ( json.morphTargets !== undefined ) {
  255. var i, l, v, vl, dstVertices, srcVertices;
  256. for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  257. geometry.morphTargets[ i ] = {};
  258. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  259. geometry.morphTargets[ i ].vertices = [];
  260. dstVertices = geometry.morphTargets[ i ].vertices;
  261. srcVertices = json.morphTargets[ i ].vertices;
  262. for ( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  263. var vertex = new THREE.Vector3();
  264. vertex.x = srcVertices[ v ] * scale;
  265. vertex.y = srcVertices[ v + 1 ] * scale;
  266. vertex.z = srcVertices[ v + 2 ] * scale;
  267. dstVertices.push( vertex );
  268. }
  269. }
  270. }
  271. if ( json.morphColors !== undefined ) {
  272. var i, l, c, cl, dstColors, srcColors, color;
  273. for ( i = 0, l = json.morphColors.length; i < l; i ++ ) {
  274. geometry.morphColors[ i ] = {};
  275. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  276. geometry.morphColors[ i ].colors = [];
  277. dstColors = geometry.morphColors[ i ].colors;
  278. srcColors = json.morphColors[ i ].colors;
  279. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  280. color = new THREE.Color( 0xffaa00 );
  281. color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] );
  282. dstColors.push( color );
  283. }
  284. }
  285. }
  286. };
  287. if ( json.materials === undefined || json.materials.length === 0 ) {
  288. return { geometry: geometry };
  289. } else {
  290. var materials = THREE.Loader.prototype.initMaterials( json.materials, texturePath, this.crossOrigin );
  291. if ( THREE.Loader.prototype.needsTangents( materials ) ) {
  292. geometry.computeTangents();
  293. }
  294. return { geometry: geometry, materials: materials };
  295. }
  296. }
  297. };
粤ICP备19079148号