JSONLoader.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. scale = ( json.scale !== undefined ) ? 1.0 / json.scale : 1.0;
  35. this.init_materials( geometry, json.materials, texture_path );
  36. parseModel( scale );
  37. parseSkin();
  38. parseMorphing( scale );
  39. parseEdges();
  40. geometry.computeCentroids();
  41. geometry.computeFaceNormals();
  42. if ( this.hasNormals( geometry ) ) geometry.computeTangents();
  43. // geometry.computeEdgeFaces();
  44. function parseModel( scale ) {
  45. if ( json.version === undefined || json.version != 2 ) {
  46. console.error( 'Deprecated file format.' );
  47. return;
  48. }
  49. function isBitSet( value, position ) {
  50. return value & ( 1 << position );
  51. };
  52. var i, j, fi,
  53. offset, zLength, nVertices,
  54. colorIndex, normalIndex, uvIndex, materialIndex,
  55. type,
  56. isQuad,
  57. hasMaterial,
  58. hasFaceUv, hasFaceVertexUv,
  59. hasFaceNormal, hasFaceVertexNormal,
  60. hasFaceColor, hasFaceVertexColor,
  61. vertex, face, color, normal,
  62. uvLayer, uvs, u, v,
  63. faces = json.faces,
  64. vertices = json.vertices,
  65. normals = json.normals,
  66. colors = json.colors,
  67. nUvLayers = 0;
  68. // disregard empty arrays
  69. for ( i = 0; i < json.uvs.length; i++ ) {
  70. if ( json.uvs[ i ].length ) nUvLayers ++;
  71. }
  72. for ( i = 0; i < nUvLayers; i++ ) {
  73. geometry.faceUvs[ i ] = [];
  74. geometry.faceVertexUvs[ i ] = [];
  75. }
  76. offset = 0;
  77. zLength = vertices.length;
  78. while ( offset < zLength ) {
  79. vertex = new THREE.Vertex();
  80. vertex.position.x = vertices[ offset ++ ] * scale;
  81. vertex.position.y = vertices[ offset ++ ] * scale;
  82. vertex.position.z = vertices[ offset ++ ] * scale;
  83. geometry.vertices.push( vertex );
  84. }
  85. offset = 0;
  86. zLength = faces.length;
  87. while ( offset < zLength ) {
  88. type = faces[ offset ++ ];
  89. isQuad = isBitSet( type, 0 );
  90. hasMaterial = isBitSet( type, 1 );
  91. hasFaceUv = isBitSet( type, 2 );
  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, hasFaceUv, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  98. if ( isQuad ) {
  99. face = new THREE.Face4();
  100. face.a = faces[ offset ++ ];
  101. face.b = faces[ offset ++ ];
  102. face.c = faces[ offset ++ ];
  103. face.d = faces[ offset ++ ];
  104. nVertices = 4;
  105. } else {
  106. face = new THREE.Face3();
  107. face.a = faces[ offset ++ ];
  108. face.b = faces[ offset ++ ];
  109. face.c = faces[ offset ++ ];
  110. nVertices = 3;
  111. }
  112. if ( hasMaterial ) {
  113. materialIndex = faces[ offset ++ ];
  114. face.materials = geometry.materials[ materialIndex ];
  115. }
  116. // to get face <=> uv index correspondence
  117. fi = geometry.faces.length;
  118. if ( hasFaceUv ) {
  119. for ( i = 0; i < nUvLayers; i++ ) {
  120. uvLayer = json.uvs[ i ];
  121. uvIndex = faces[ offset ++ ];
  122. u = uvLayer[ uvIndex * 2 ];
  123. v = uvLayer[ uvIndex * 2 + 1 ];
  124. geometry.faceUvs[ i ][ fi ] = new THREE.UV( u, v );
  125. }
  126. }
  127. if ( hasFaceVertexUv ) {
  128. for ( i = 0; i < nUvLayers; i++ ) {
  129. uvLayer = json.uvs[ i ];
  130. uvs = [];
  131. for ( j = 0; j < nVertices; j ++ ) {
  132. uvIndex = faces[ offset ++ ];
  133. u = uvLayer[ uvIndex * 2 ];
  134. v = uvLayer[ uvIndex * 2 + 1 ];
  135. uvs[ j ] = new THREE.UV( u, v );
  136. }
  137. geometry.faceVertexUvs[ i ][ fi ] = uvs;
  138. }
  139. }
  140. if ( hasFaceNormal ) {
  141. normalIndex = faces[ offset ++ ] * 3;
  142. normal = new THREE.Vector3();
  143. normal.x = normals[ normalIndex ++ ];
  144. normal.y = normals[ normalIndex ++ ];
  145. normal.z = normals[ normalIndex ];
  146. face.normal = normal;
  147. }
  148. if ( hasFaceVertexNormal ) {
  149. for ( i = 0; i < nVertices; i++ ) {
  150. normalIndex = faces[ offset ++ ] * 3;
  151. normal = new THREE.Vector3();
  152. normal.x = normals[ normalIndex ++ ];
  153. normal.y = normals[ normalIndex ++ ];
  154. normal.z = normals[ normalIndex ];
  155. face.vertexNormals.push( normal );
  156. }
  157. }
  158. if ( hasFaceColor ) {
  159. colorIndex = faces[ offset ++ ];
  160. color = new THREE.Color( colors[ colorIndex ] );
  161. face.color = color;
  162. }
  163. if ( hasFaceVertexColor ) {
  164. for ( i = 0; i < nVertices; i++ ) {
  165. colorIndex = faces[ offset ++ ];
  166. color = new THREE.Color( colors[ colorIndex ] );
  167. face.vertexColors.push( color );
  168. }
  169. }
  170. geometry.faces.push( face );
  171. }
  172. };
  173. function parseSkin() {
  174. var i, l, x, y, z, w, a, b, c, d;
  175. if ( json.skinWeights ) {
  176. for ( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
  177. x = json.skinWeights[ i ];
  178. y = json.skinWeights[ i + 1 ];
  179. z = 0;
  180. w = 0;
  181. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  182. }
  183. }
  184. if ( json.skinIndices ) {
  185. for ( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
  186. a = json.skinIndices[ i ];
  187. b = json.skinIndices[ i + 1 ];
  188. c = 0;
  189. d = 0;
  190. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  191. }
  192. }
  193. geometry.bones = json.bones;
  194. geometry.animation = json.animation;
  195. };
  196. function parseMorphing( scale ) {
  197. if ( json.morphTargets !== undefined ) {
  198. var i, l, v, vl, x, y, z, dstVertices, srcVertices;
  199. for ( i = 0, l = json.morphTargets.length; i < l; i++ ) {
  200. geometry.morphTargets[ i ] = {};
  201. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  202. geometry.morphTargets[ i ].vertices = [];
  203. dstVertices = geometry.morphTargets[ i ].vertices;
  204. srcVertices = json.morphTargets [ i ].vertices;
  205. for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  206. x = srcVertices[ v ] * scale;
  207. y = srcVertices[ v + 1 ] * scale;
  208. z = srcVertices[ v + 2 ] * scale;
  209. dstVertices.push( new THREE.Vertex( new THREE.Vector3( x, y, z ) ) );
  210. }
  211. }
  212. }
  213. if ( json.morphColors !== undefined ) {
  214. var i, l, c, cl, dstColors, srcColors, color;
  215. for ( i = 0, l = json.morphColors.length; i < l; i++ ) {
  216. geometry.morphColors[ i ] = {};
  217. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  218. geometry.morphColors[ i ].colors = [];
  219. dstColors = geometry.morphColors[ i ].colors;
  220. srcColors = json.morphColors [ i ].colors;
  221. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  222. color = new THREE.Color( 0xffaa00 );
  223. color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] );
  224. dstColors.push( color );
  225. }
  226. }
  227. }
  228. };
  229. function parseEdges() {
  230. if( json.edges !== undefined ) {
  231. var i, il, v1, v2;
  232. for ( i = 0; i < json.edges.length; i+= 2 ) {
  233. v1 = json.edges[ i ];
  234. v2 = json.edges[ i + 1 ];
  235. geometry.edges.push( new THREE.Edge( geometry.vertices[ v1 ], geometry.vertices[ v2 ], v1, v2 ) );
  236. }
  237. }
  238. };
  239. callback( geometry );
  240. }