JSONLoader.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /**
  12. * Load models generated by slim OBJ converter with ASCII option (converter_obj_three_slim.py -t ascii)
  13. * - parameters
  14. * - model (required)
  15. * - callback (required)
  16. * - texture_path (optional: if not specified, textures will be assumed to be in the same folder as JS model file)
  17. */
  18. THREE.JSONLoader.prototype.load = function ( parameters ) {
  19. var scope = this,
  20. url = parameters.model,
  21. callback = parameters.callback,
  22. texture_path = parameters.texture_path ? parameters.texture_path : this.extractUrlbase( url ),
  23. worker = new Worker( url );
  24. worker.onmessage = function ( event ) {
  25. scope.createModel( event.data, callback, texture_path );
  26. scope.onLoadComplete();
  27. };
  28. this.onLoadStart();
  29. worker.postMessage( new Date().getTime() );
  30. };
  31. THREE.JSONLoader.prototype.createModel = function ( json, callback, texture_path ) {
  32. var scope = this,
  33. geometry = new THREE.Geometry();
  34. this.init_materials( geometry, json.materials, texture_path );
  35. parse();
  36. init_skin();
  37. init_morphing();
  38. init_edges();
  39. geometry.computeCentroids();
  40. geometry.computeFaceNormals();
  41. geometry.computeEdgeFaces();
  42. function parse() {
  43. if ( json.version === undefined || json.version != 2 ) {
  44. console.error( 'Deprecated file format.' );
  45. return;
  46. }
  47. function isBitSet( value, position ) {
  48. return value & ( 1 << position );
  49. };
  50. var i, j, fi,
  51. offset, zLength, nVertices,
  52. colorIndex, normalIndex, uvIndex, materialIndex,
  53. type,
  54. isQuad,
  55. hasMaterial,
  56. hasFaceUv, hasFaceVertexUv,
  57. hasFaceNormal, hasFaceVertexNormal,
  58. hasFaceColor, hasFaceVertexColor,
  59. vertex, face, color, normal,
  60. uvLayer, uvs, u, v,
  61. faces = json.faces,
  62. vertices = json.vertices,
  63. normals = json.normals,
  64. colors = json.colors,
  65. scale = ( json.scale !== undefined ) ? json.scale : 1.0,
  66. nUvLayers = 0;
  67. // disregard empty arrays
  68. for ( i = 0; i < json.uvs.length; i++ ) {
  69. if ( json.uvs[ i ].length ) nUvLayers ++;
  70. }
  71. for ( i = 0; i < nUvLayers; i++ ) {
  72. geometry.faceUvs[ i ] = [];
  73. geometry.faceVertexUvs[ i ] = [];
  74. }
  75. offset = 0;
  76. zLength = vertices.length;
  77. while ( offset < zLength ) {
  78. vertex = new THREE.Vertex();
  79. vertex.position.x = vertices[ offset ++ ] / scale;
  80. vertex.position.y = vertices[ offset ++ ] / scale;
  81. vertex.position.z = vertices[ offset ++ ] / scale;
  82. geometry.vertices.push( vertex );
  83. }
  84. offset = 0;
  85. zLength = faces.length;
  86. while ( offset < zLength ) {
  87. type = faces[ offset ++ ];
  88. isQuad = isBitSet( type, 0 );
  89. hasMaterial = isBitSet( type, 1 );
  90. hasFaceUv = isBitSet( type, 2 );
  91. hasFaceVertexUv = isBitSet( type, 3 );
  92. hasFaceNormal = isBitSet( type, 4 );
  93. hasFaceVertexNormal = isBitSet( type, 5 );
  94. hasFaceColor = isBitSet( type, 6 );
  95. hasFaceVertexColor = isBitSet( type, 7 );
  96. //console.log("type", type, "bits", isQuad, hasMaterial, hasFaceUv, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  97. if ( isQuad ) {
  98. face = new THREE.Face4();
  99. face.a = faces[ offset ++ ];
  100. face.b = faces[ offset ++ ];
  101. face.c = faces[ offset ++ ];
  102. face.d = faces[ offset ++ ];
  103. nVertices = 4;
  104. } else {
  105. face = new THREE.Face3();
  106. face.a = faces[ offset ++ ];
  107. face.b = faces[ offset ++ ];
  108. face.c = faces[ offset ++ ];
  109. nVertices = 3;
  110. }
  111. if ( hasMaterial ) {
  112. materialIndex = faces[ offset ++ ];
  113. face.materials = geometry.materials[ materialIndex ];
  114. }
  115. // to get face <=> uv index correspondence
  116. fi = geometry.faces.length;
  117. if ( hasFaceUv ) {
  118. for ( i = 0; i < nUvLayers; i++ ) {
  119. uvLayer = json.uvs[ i ];
  120. uvIndex = faces[ offset ++ ];
  121. u = uvLayer[ uvIndex * 2 ];
  122. v = uvLayer[ uvIndex * 2 + 1 ];
  123. geometry.faceUvs[ i ][ fi ] = new THREE.UV( u, v );
  124. }
  125. }
  126. if ( hasFaceVertexUv ) {
  127. for ( i = 0; i < nUvLayers; i++ ) {
  128. uvLayer = json.uvs[ i ];
  129. uvs = [];
  130. for ( j = 0; j < nVertices; j ++ ) {
  131. uvIndex = faces[ offset ++ ];
  132. u = uvLayer[ uvIndex * 2 ];
  133. v = uvLayer[ uvIndex * 2 + 1 ];
  134. uvs[ j ] = new THREE.UV( u, v );
  135. }
  136. geometry.faceVertexUvs[ i ][ fi ] = uvs;
  137. }
  138. }
  139. if ( hasFaceNormal ) {
  140. normalIndex = faces[ offset ++ ] * 3;
  141. normal = new THREE.Vector3();
  142. normal.x = normals[ normalIndex ++ ];
  143. normal.y = normals[ normalIndex ++ ];
  144. normal.z = normals[ normalIndex ];
  145. face.normal = normal;
  146. }
  147. if ( hasFaceVertexNormal ) {
  148. for ( i = 0; i < nVertices; i++ ) {
  149. normalIndex = faces[ offset ++ ] * 3;
  150. normal = new THREE.Vector3();
  151. normal.x = normals[ normalIndex ++ ];
  152. normal.y = normals[ normalIndex ++ ];
  153. normal.z = normals[ normalIndex ];
  154. face.vertexNormals.push( normal );
  155. }
  156. }
  157. if ( hasFaceColor ) {
  158. colorIndex = faces[ offset ++ ];
  159. color = new THREE.Color( colors[ colorIndex ] );
  160. face.color = color;
  161. }
  162. if ( hasFaceVertexColor ) {
  163. for ( i = 0; i < nVertices; i++ ) {
  164. colorIndex = faces[ offset ++ ];
  165. color = new THREE.Color( colors[ colorIndex ] );
  166. face.vertexColors.push( color );
  167. }
  168. }
  169. geometry.faces.push( face );
  170. }
  171. };
  172. function init_skin() {
  173. var i, l, x, y, z, w, a, b, c, d;
  174. if ( json.skinWeights ) {
  175. for ( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
  176. x = json.skinWeights[ i ];
  177. y = json.skinWeights[ i + 1 ];
  178. z = 0;
  179. w = 0;
  180. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  181. }
  182. }
  183. if ( json.skinIndices ) {
  184. for ( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
  185. a = json.skinIndices[ i ];
  186. b = json.skinIndices[ i + 1 ];
  187. c = 0;
  188. d = 0;
  189. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  190. }
  191. }
  192. geometry.bones = json.bones;
  193. geometry.animation = json.animation;
  194. };
  195. function init_morphing() {
  196. if ( json.morphTargets !== undefined ) {
  197. var i, l, v, vl, dstVertices, srcVertices;
  198. for ( i = 0, l = json.morphTargets.length; i < l; i++ ) {
  199. geometry.morphTargets[ i ] = {};
  200. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  201. geometry.morphTargets[ i ].vertices = [];
  202. dstVertices = geometry.morphTargets[ i ].vertices;
  203. srcVertices = json.morphTargets [ i ].vertices;
  204. for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  205. dstVertices.push( new THREE.Vertex( new THREE.Vector3( srcVertices[ v ], srcVertices[ v + 1 ], srcVertices[ v + 2 ] ) ) );
  206. }
  207. }
  208. }
  209. if ( json.morphColors !== undefined ) {
  210. var i, l, c, cl, dstColors, srcColors, color;
  211. for ( i = 0, l = json.morphColors.length; i < l; i++ ) {
  212. geometry.morphColors[ i ] = {};
  213. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  214. geometry.morphColors[ i ].colors = [];
  215. dstColors = geometry.morphColors[ i ].colors;
  216. srcColors = json.morphColors [ i ].colors;
  217. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  218. color = new THREE.Color( 0xffaa00 );
  219. color.setRGB( srcColors[ v ], srcColors[ v + 1 ], srcColors[ v + 2 ] );
  220. dstColors.push( color );
  221. }
  222. }
  223. }
  224. };
  225. function init_edges() {
  226. if( json.edges !== undefined ) {
  227. var i, il, v1, v2;
  228. for ( i = 0; i < json.edges.length; i+= 2 ) {
  229. v1 = json.edges[ i ];
  230. v2 = json.edges[ i + 1 ];
  231. geometry.edges.push( new THREE.Edge( geometry.vertices[ v1 ], geometry.vertices[ v2 ], v1, v2 ) );
  232. }
  233. }
  234. };
  235. callback( geometry );
  236. }
粤ICP备19079148号