JSONLoader.js 7.4 KB

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