JSONLoader.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. geometry.computeCentroids();
  39. geometry.computeFaceNormals();
  40. function parse() {
  41. if ( json.version === undefined || json.version != 2 ) {
  42. console.error( 'Deprecated file format.' );
  43. return;
  44. }
  45. function isBitSet( value, position ) {
  46. return value & ( 1 << position );
  47. };
  48. var i, j, fi,
  49. offset, zLength,
  50. colorIndex, normalIndex, uvIndex, materialIndex,
  51. type,
  52. isQuad,
  53. hasMaterial,
  54. hasFaceUv, hasFaceVertexUv,
  55. hasFaceNormal, hasFaceVertexNormal,
  56. hasFaceColor, hasFaceVertexColor,
  57. vertex, face,
  58. faces = json.faces,
  59. vertices = json.vertices,
  60. normals = json.normals,
  61. colors = json.colors,
  62. nUvLayers = 0;
  63. // disregard empty arrays
  64. for ( i = 0; i < json.uvs.length; i++ ) {
  65. if ( json.uvs[ i ].length ) nUvLayers ++;
  66. }
  67. for ( i = 0; i < nUvLayers; i++ ) {
  68. geometry.faceUvs[ i ] = [];
  69. geometry.faceVertexUvs[ i ] = [];
  70. }
  71. offset = 0;
  72. zLength = vertices.length;
  73. while ( offset < zLength ) {
  74. vertex = new THREE.Vertex();
  75. vertex.position.x = vertices[ offset ++ ];
  76. vertex.position.y = vertices[ offset ++ ];
  77. vertex.position.z = vertices[ offset ++ ];
  78. geometry.vertices.push( vertex );
  79. }
  80. offset = 0;
  81. zLength = faces.length;
  82. while ( offset < zLength ) {
  83. type = faces[ offset ++ ];
  84. isQuad = isBitSet( type, 0 );
  85. hasMaterial = isBitSet( type, 1 );
  86. hasFaceUv = isBitSet( type, 2 );
  87. hasFaceVertexUv = isBitSet( type, 3 );
  88. hasFaceNormal = isBitSet( type, 4 );
  89. hasFaceVertexNormal = isBitSet( type, 5 );
  90. hasFaceColor = isBitSet( type, 6 );
  91. hasFaceVertexColor = isBitSet( type, 7 );
  92. //console.log("type", type, "bits", isQuad, hasMaterial, hasFaceUv, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  93. if ( isQuad ) {
  94. face = new THREE.Face4();
  95. face.a = faces[ offset ++ ];
  96. face.b = faces[ offset ++ ];
  97. face.c = faces[ offset ++ ];
  98. face.d = faces[ offset ++ ];
  99. nVertices = 4;
  100. } else {
  101. face = new THREE.Face3();
  102. face.a = faces[ offset ++ ];
  103. face.b = faces[ offset ++ ];
  104. face.c = faces[ offset ++ ];
  105. nVertices = 3;
  106. }
  107. if ( hasMaterial ) {
  108. materialIndex = faces[ offset ++ ];
  109. face.materials = geometry.materials[ materialIndex ];
  110. }
  111. // to get face <=> uv index correspondence
  112. fi = geometry.faces.length;
  113. if ( hasFaceUv ) {
  114. for ( i = 0; i < nUvLayers; i++ ) {
  115. uvLayer = json.uvs[ i ];
  116. uvIndex = faces[ offset ++ ];
  117. u = uvLayer[ uvIndex * 2 ];
  118. v = uvLayer[ uvIndex * 2 + 1 ];
  119. geometry.faceUvs[ i ][ fi ] = new THREE.UV( u, v );
  120. }
  121. }
  122. if ( hasFaceVertexUv ) {
  123. for ( i = 0; i < nUvLayers; i++ ) {
  124. uvLayer = json.uvs[ i ];
  125. uvs = [];
  126. for ( j = 0; j < nVertices; j ++ ) {
  127. uvIndex = faces[ offset ++ ];
  128. u = uvLayer[ uvIndex * 2 ];
  129. v = uvLayer[ uvIndex * 2 + 1 ];
  130. uvs[ j ] = new THREE.UV( u, v );
  131. }
  132. geometry.faceVertexUvs[ i ][ fi ] = uvs;
  133. }
  134. }
  135. if ( hasFaceNormal ) {
  136. normalIndex = faces[ offset ++ ] * 3;
  137. normal = new THREE.Vector3();
  138. normal.x = normals[ normalIndex ++ ];
  139. normal.y = normals[ normalIndex ++ ];
  140. normal.z = normals[ normalIndex ];
  141. face.normal = normal;
  142. }
  143. if ( hasFaceVertexNormal ) {
  144. for ( i = 0; i < nVertices; i++ ) {
  145. normalIndex = faces[ offset ++ ] * 3;
  146. normal = new THREE.Vector3();
  147. normal.x = normals[ normalIndex ++ ];
  148. normal.y = normals[ normalIndex ++ ];
  149. normal.z = normals[ normalIndex ];
  150. face.vertexNormals.push( normal );
  151. }
  152. }
  153. if ( hasFaceColor ) {
  154. color = new THREE.Color( faces[ offset ++ ] );
  155. face.color = color;
  156. }
  157. if ( hasFaceVertexColor ) {
  158. for ( i = 0; i < nVertices; i++ ) {
  159. colorIndex = faces[ offset ++ ];
  160. color = new THREE.Color( colors[ colorIndex ] );
  161. face.vertexColors.push( color );
  162. }
  163. }
  164. geometry.faces.push( face );
  165. }
  166. };
  167. function init_skin() {
  168. var i, l, x, y, z, w, a, b, c, d;
  169. if ( json.skinWeights ) {
  170. for( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
  171. x = json.skinWeights[ i ];
  172. y = json.skinWeights[ i + 1 ];
  173. z = 0;
  174. w = 0;
  175. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  176. }
  177. }
  178. if ( json.skinIndices ) {
  179. for( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
  180. a = json.skinIndices[ i ];
  181. b = json.skinIndices[ i + 1 ];
  182. c = 0;
  183. d = 0;
  184. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  185. }
  186. }
  187. geometry.bones = json.bones;
  188. geometry.animation = json.animation;
  189. };
  190. function init_morphing() {
  191. if( json.morphTargets !== undefined ) {
  192. var i, l, v, vl;
  193. for( i = 0, l = json.morphTargets.length; i < l; i++ ) {
  194. geometry.morphTargets[ i ] = {};
  195. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  196. geometry.morphTargets[ i ].vertices = [];
  197. dstVertices = geometry.morphTargets[ i ].vertices;
  198. srcVertices = json.morphTargets [ i ].vertices;
  199. for( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  200. dstVertices.push( new THREE.Vertex( new THREE.Vector3( srcVertices[ v ], srcVertices[ v + 1 ], srcVertices[ v + 2 ] ) ) );
  201. }
  202. }
  203. }
  204. };
  205. callback( geometry );
  206. }