JSONLoader.js 7.0 KB

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