JSONLoader.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. THREE.JSONLoader.prototype = {
  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. load: function ( parameters ) {
  18. var url = parameters.model,
  19. callback = parameters.callback,
  20. texture_path = parameters.texture_path ? parameters.texture_path : THREE.Loader.prototype.extractUrlbase( url ),
  21. s = (new Date).getTime(),
  22. worker = new Worker( url );
  23. worker.onmessage = function( event ) {
  24. THREE.JSONLoader.prototype.createModel( event.data, callback, texture_path );
  25. };
  26. worker.postMessage( s );
  27. },
  28. createModel: function ( json, callback, texture_path ) {
  29. var Model = function ( texture_path ) {
  30. var scope = this;
  31. THREE.Geometry.call( this );
  32. THREE.Loader.prototype.init_materials( scope, json.materials, texture_path );
  33. parse();
  34. init_skin();
  35. this.computeCentroids();
  36. this.computeFaceNormals();
  37. function parse() {
  38. if ( json.version === undefined || json.version != 2 ) {
  39. console.error( 'Deprecated file format.' );
  40. return;
  41. }
  42. function isBitSet( value, position ) {
  43. return value & ( 1 << position );
  44. };
  45. var i, j,
  46. offset, zLength,
  47. type,
  48. isQuad,
  49. hasMaterial,
  50. hasFaceUv, hasFaceVertexUv,
  51. hasFaceNormal, hasFaceVertexNormal,
  52. hasFaceColor, hasFaceVertexColor,
  53. vertex, face,
  54. faces = json.faces,
  55. vertices = json.vertices,
  56. normals = json.normals,
  57. colors = json.colors,
  58. nUvLayers = 0;
  59. // disregard empty arrays
  60. for ( i = 0; i < json.uvs.length; i++ ) {
  61. if ( json.uvs[ i ].length ) nUvLayers ++;
  62. }
  63. for ( i = 0; i < nUvLayers; i++ ) {
  64. scope.faceUvs[ i ] = [];
  65. scope.faceVertexUvs[ i ] = [];
  66. }
  67. offset = 0;
  68. zLength = vertices.length;
  69. while ( offset < zLength ) {
  70. vertex = new THREE.Vertex();
  71. vertex.position.x = vertices[ offset ++ ];
  72. vertex.position.y = vertices[ offset ++ ];
  73. vertex.position.z = vertices[ offset ++ ];
  74. scope.vertices.push( vertex );
  75. }
  76. offset = 0;
  77. zLength = faces.length;
  78. while ( offset < zLength ) {
  79. type = faces[ offset ++ ];
  80. isQuad = isBitSet( type, 0 );
  81. hasMaterial = isBitSet( type, 1 );
  82. hasFaceUv = isBitSet( type, 2 );
  83. hasFaceVertexUv = isBitSet( type, 3 );
  84. hasFaceNormal = isBitSet( type, 4 );
  85. hasFaceVertexNormal = isBitSet( type, 5 );
  86. hasFaceColor = isBitSet( type, 6 );
  87. hasFaceVertexColor = isBitSet( type, 7 );
  88. //console.log("type", type, "bits", isQuad, hasMaterial, hasFaceUv, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  89. if ( isQuad ) {
  90. face = new THREE.Face4();
  91. face.a = faces[ offset ++ ];
  92. face.b = faces[ offset ++ ];
  93. face.c = faces[ offset ++ ];
  94. face.d = faces[ offset ++ ];
  95. nVertices = 4;
  96. } else {
  97. face = new THREE.Face3();
  98. face.a = faces[ offset ++ ];
  99. face.b = faces[ offset ++ ];
  100. face.c = faces[ offset ++ ];
  101. nVertices = 3;
  102. }
  103. if ( hasMaterial ) {
  104. materialIndex = faces[ offset ++ ];
  105. face.materials = scope.materials[ materialIndex ];
  106. }
  107. if ( hasFaceUv ) {
  108. for ( i = 0; i < nUvLayers; i++ ) {
  109. uvLayer = json.uvs[ i ];
  110. uvIndex = faces[ offset ++ ];
  111. u = uvLayer[ uvIndex * 2 ];
  112. v = uvLayer[ uvIndex * 2 + 1 ];
  113. scope.faceUvs[ i ].push( new THREE.UV( u, v ) );
  114. }
  115. }
  116. if ( hasFaceVertexUv ) {
  117. for ( i = 0; i < nUvLayers; i++ ) {
  118. uvLayer = json.uvs[ i ];
  119. uvs = [];
  120. for ( j = 0; j < nVertices; j ++ ) {
  121. uvIndex = faces[ offset ++ ];
  122. u = uvLayer[ uvIndex * 2 ];
  123. v = uvLayer[ uvIndex * 2 + 1 ];
  124. uvs[ j ] = new THREE.UV( u, v );
  125. }
  126. scope.faceVertexUvs[ i ].push( uvs );
  127. }
  128. }
  129. if ( hasFaceNormal ) {
  130. normalIndex = faces[ offset ++ ] * 3;
  131. normal = new THREE.Vector3();
  132. normal.x = normals[ normalIndex ++ ];
  133. normal.y = normals[ normalIndex ++ ];
  134. normal.z = normals[ normalIndex ];
  135. face.normal = normal;
  136. }
  137. if ( hasFaceVertexNormal ) {
  138. for ( i = 0; i < nVertices; i++ ) {
  139. normalIndex = faces[ offset ++ ] * 3;
  140. normal = new THREE.Vector3();
  141. normal.x = normals[ normalIndex ++ ];
  142. normal.y = normals[ normalIndex ++ ];
  143. normal.z = normals[ normalIndex ];
  144. face.vertexNormals.push( normal );
  145. }
  146. }
  147. if ( hasFaceColor ) {
  148. color = new THREE.Color( faces[ offset ++ ] );
  149. face.color = color;
  150. }
  151. if ( hasFaceVertexColor ) {
  152. for ( i = 0; i < nVertices; i++ ) {
  153. colorIndex = faces[ offset ++ ];
  154. color = new THREE.Color( colors[ colorIndex ] );
  155. face.vertexColors.push( color );
  156. }
  157. }
  158. scope.faces.push( face );
  159. }
  160. };
  161. function init_skin() {
  162. var i, l, x, y, z, w, a, b, c, d;
  163. if ( json.skinWeights ) {
  164. for( i = 0, l = json.skinWeights.length; i < l; i += 2 ) {
  165. x = json.skinWeights[ i ];
  166. y = json.skinWeights[ i + 1 ];
  167. z = 0;
  168. w = 0;
  169. scope.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  170. }
  171. }
  172. if ( json.skinIndices ) {
  173. for( i = 0, l = json.skinIndices.length; i < l; i += 2 ) {
  174. a = json.skinIndices[ i ];
  175. b = json.skinIndices[ i + 1 ];
  176. c = 0;
  177. d = 0;
  178. scope.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  179. }
  180. }
  181. scope.bones = json.bones;
  182. scope.animation = json.animation;
  183. };
  184. };
  185. Model.prototype = new THREE.Geometry();
  186. Model.prototype.constructor = Model;
  187. callback( new Model( texture_path ) );
  188. }
  189. };