AssimpJSONLoader.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * @author Alexander Gessler / http://www.greentoken.de/
  3. * https://github.com/acgessler
  4. *
  5. * Loader for models imported with Open Asset Import Library (http://assimp.sf.net)
  6. * through assimp2json (https://github.com/acgessler/assimp2json).
  7. *
  8. * Supports any input format that assimp supports, including 3ds, obj, dae, blend,
  9. * fbx, x, ms3d, lwo (and many more).
  10. *
  11. * See webgl_loader_assimp2json example.
  12. */
  13. THREE.AssimpJSONLoader = function ( manager ) {
  14. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  15. };
  16. THREE.AssimpJSONLoader.prototype = {
  17. constructor: THREE.AssimpJSONLoader,
  18. load: function ( url, onLoad, onProgress, onError ) {
  19. var scope = this;
  20. this.texturePath = this.texturePath && ( typeof this.texturePath === "string" ) ? this.texturePath : this.extractUrlBase( url );
  21. var loader = new THREE.XHRLoader( this.manager );
  22. loader.load( url, function ( text ) {
  23. var json = JSON.parse( text ), scene, metadata;
  24. // Check __metadata__ meta header if present
  25. // This header is used to disambiguate between
  26. // different JSON-based file formats.
  27. metadata = json.__metadata__;
  28. if ( typeof metadata !== 'undefined' ) {
  29. // Check if assimp2json at all
  30. if ( metadata.format !== 'assimp2json' ) {
  31. onError( 'Not an assimp2json scene' );
  32. return;
  33. }
  34. // Check major format version
  35. else if ( metadata.version < 100 && metadata.version >= 200 ) {
  36. onError( 'Unsupported assimp2json file format version' );
  37. return;
  38. }
  39. }
  40. scene = scope.parse( json );
  41. onLoad( scene );
  42. }, onProgress, onError );
  43. },
  44. setCrossOrigin: function ( value ) {
  45. this.crossOrigin = value;
  46. },
  47. setTexturePath: function ( value ) {
  48. this.texturePath = value;
  49. },
  50. extractUrlBase: function ( url ) {
  51. // from three/src/loaders/Loader.js
  52. var parts = url.split( '/' );
  53. parts.pop();
  54. return ( parts.length < 1 ? '.' : parts.join( '/' ) ) + '/';
  55. },
  56. parse: function ( json ) {
  57. var meshes = this.parseList ( json.meshes, this.parseMesh );
  58. var materials = this.parseList ( json.materials, this.parseMaterial );
  59. return this.parseObject( json, json.rootnode, meshes, materials );
  60. },
  61. parseList : function( json, handler ) {
  62. var meshes = new Array( json.length );
  63. for ( var i = 0; i < json.length; ++ i ) {
  64. meshes[ i ] = handler.call( this, json[ i ] );
  65. }
  66. return meshes;
  67. },
  68. parseMesh : function( json ) {
  69. var vertex, geometry, i, e, in_data, src;
  70. geometry = new THREE.Geometry();
  71. // read vertex positions
  72. for ( in_data = json.vertices, i = 0, e = in_data.length; i < e; ) {
  73. geometry.vertices.push( new THREE.Vector3( in_data[ i ++ ], in_data[ i ++ ], in_data[ i ++ ] ) );
  74. }
  75. // read faces
  76. var cnt = 0;
  77. for ( in_data = json.faces, i = 0, e = in_data.length; i < e; ++ i ) {
  78. src = in_data[ i ];
  79. face = new THREE.Face3( src[ 0 ], src[ 1 ], src[ 2 ] );
  80. geometry.faces.push( face );
  81. }
  82. // read texture coordinates - three.js attaches them to its faces
  83. json.texturecoords = json.texturecoords || [];
  84. for ( i = 0, e = json.texturecoords.length; i < e; ++ i ) {
  85. function convertTextureCoords( in_uv, out_faces, out_vertex_uvs ) {
  86. var i, e, face, a, b, c;
  87. for ( i = 0, e = out_faces.length; i < e; ++ i ) {
  88. face = out_faces[ i ];
  89. a = face.a * 2;
  90. b = face.b * 2;
  91. c = face.c * 2;
  92. out_vertex_uvs.push( [
  93. new THREE.Vector2( in_uv[ a ], in_uv[ a + 1 ] ),
  94. new THREE.Vector2( in_uv[ b ], in_uv[ b + 1 ] ),
  95. new THREE.Vector2( in_uv[ c ], in_uv[ c + 1 ] )
  96. ] );
  97. }
  98. }
  99. convertTextureCoords( json.texturecoords[ i ], geometry.faces, geometry.faceVertexUvs[ i ] );
  100. }
  101. // read normals - three.js also attaches them to its faces
  102. if ( json.normals ) {
  103. function convertNormals( in_nor, out_faces ) {
  104. var i, e, face, a, b, c;
  105. for ( i = 0, e = out_faces.length; i < e; ++ i ) {
  106. face = out_faces[ i ];
  107. a = face.a * 3;
  108. b = face.b * 3;
  109. c = face.c * 3;
  110. face.vertexNormals = [
  111. new THREE.Vector3( in_nor[ a ], in_nor[ a + 1 ], in_nor[ a + 2 ] ),
  112. new THREE.Vector3( in_nor[ b ], in_nor[ b + 1 ], in_nor[ b + 2 ] ),
  113. new THREE.Vector3( in_nor[ c ], in_nor[ c + 1 ], in_nor[ c + 2 ] )
  114. ];
  115. }
  116. }
  117. convertNormals( json.normals, geometry.faces );
  118. }
  119. // read vertex colors - three.js also attaches them to its faces
  120. if ( json.colors && json.colors[ 0 ] ) {
  121. function convertColors( in_color, out_faces ) {
  122. for ( var i = 0, e = out_faces.length; i < e; ++ i ) {
  123. var face = out_faces[ i ];
  124. var a = face.a * 4;
  125. var b = face.b * 4;
  126. var c = face.c * 4;
  127. face.vertexColors = [
  128. new THREE.Color().fromArray( a ),
  129. new THREE.Color().fromArray( b ),
  130. new THREE.Color().fromArray( c )
  131. ];
  132. }
  133. }
  134. convertColors( json.colors[ 0 ], geometry.faces );
  135. }
  136. //geometry.computeFaceNormals();
  137. //geometry.computeVertexNormals();
  138. geometry.computeBoundingSphere();
  139. return geometry;
  140. },
  141. parseMaterial : function( json ) {
  142. var mat = null,
  143. scope = this, i, prop, has_textures = [],
  144. init_props = {
  145. shading : THREE.SmoothShading
  146. };
  147. function toColor( value_arr ) {
  148. var col = new THREE.Color();
  149. col.setRGB( value_arr[ 0 ], value_arr[ 1 ], value_arr[ 2 ] );
  150. return col;
  151. }
  152. function defaultTexture() {
  153. var im = new Image();
  154. im.width = 1;
  155. im.height = 1;
  156. return new THREE.Texture( im );
  157. }
  158. for ( var i in json.properties ) {
  159. prop = json.properties[ i ];
  160. if ( prop.key === '$tex.file' ) {
  161. // prop.semantic gives the type of the texture
  162. // 1: diffuse
  163. // 2: specular mao
  164. // 5: height map (bumps)
  165. // 6: normal map
  166. // more values (i.e. emissive, environment) are known by assimp and may be relevant
  167. if ( prop.semantic === 1 || prop.semantic === 5 || prop.semantic === 6 || prop.semantic === 2 ) {
  168. ( function( semantic ) {
  169. var loader = new THREE.TextureLoader( scope.manager ),
  170. keyname;
  171. if ( semantic === 1 ) {
  172. keyname = 'map';
  173. } else if ( semantic === 5 ) {
  174. keyname = 'bumpMap';
  175. } else if ( semantic === 6 ) {
  176. keyname = 'normalMap';
  177. } else if ( semantic === 2 ) {
  178. keyname = 'specularMap';
  179. }
  180. has_textures.push( keyname );
  181. loader.setCrossOrigin( this.crossOrigin );
  182. var material_url = scope.texturePath + '/' + prop.value;
  183. material_url = material_url.replace( /\\/g, '/' );
  184. loader.load( material_url, function( tex ) {
  185. if ( tex ) {
  186. // TODO: read texture settings from assimp.
  187. // Wrapping is the default, though.
  188. tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
  189. mat[ keyname ] = tex;
  190. mat.needsUpdate = true;
  191. }
  192. } );
  193. } )( prop.semantic );
  194. }
  195. } else if ( prop.key === '?mat.name' ) {
  196. init_props.name = prop.value;
  197. } else if ( prop.key === '$clr.diffuse' ) {
  198. init_props.color = toColor( prop.value );
  199. } else if ( prop.key === '$clr.specular' ) {
  200. init_props.specular = toColor( prop.value );
  201. } else if ( prop.key === '$clr.emissive' ) {
  202. init_props.emissive = toColor( prop.value );
  203. } else if ( prop.key === '$mat.shadingm' ) {
  204. // aiShadingMode_Flat
  205. if ( prop.value === 1 ) {
  206. init_props.shading = THREE.FlatShading;
  207. }
  208. } else if ( prop.key === '$mat.shininess' ) {
  209. init_props.shininess = prop.value;
  210. }
  211. }
  212. // note: three.js does not like it when a texture is added after the geometry
  213. // has been rendered once, see http://stackoverflow.com/questions/16531759/.
  214. // for this reason we fill all slots upfront with default textures
  215. if ( has_textures.length ) {
  216. for ( i = has_textures.length - 1; i >= 0; -- i ) {
  217. init_props[ has_textures[ i ]] = defaultTexture();
  218. }
  219. }
  220. mat = new THREE.MeshPhongMaterial( init_props );
  221. return mat;
  222. },
  223. parseObject : function( json, node, meshes, materials ) {
  224. var obj = new THREE.Object3D()
  225. , i
  226. , idx
  227. ;
  228. obj.name = node.name || "";
  229. obj.matrix = new THREE.Matrix4().fromArray( node.transformation ).transpose();
  230. obj.matrix.decompose( obj.position, obj.quaternion, obj.scale );
  231. for ( i = 0; node.meshes && i < node.meshes.length; ++ i ) {
  232. idx = node.meshes[ i ];
  233. obj.add( new THREE.Mesh( meshes[ idx ], materials[ json.meshes[ idx ].materialindex ] ) );
  234. }
  235. for ( i = 0; node.children && i < node.children.length; ++ i ) {
  236. obj.add( this.parseObject( json, node.children[ i ], meshes, materials ) );
  237. }
  238. return obj;
  239. },
  240. };