AssimpJSONLoader.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.FileLoader( 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 geometry = new THREE.BufferGeometry();
  70. var i, l, face;
  71. var indices = [];
  72. var vertices = json.vertices || [];
  73. var normals = json.normals || [];
  74. var uvs = json.texturecoords || [];
  75. var colors = json.colors || [];
  76. uvs = uvs[ 0 ] || []; // only support for a single set of uvs
  77. for ( i = 0, l = json.faces.length; i < l; i ++ ) {
  78. face = json.faces[ i ];
  79. indices.push( face[ 0 ], face[ 1 ], face[ 2 ] );
  80. }
  81. geometry.setIndexArray( indices );
  82. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  83. if ( normals.length > 0 ) {
  84. geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  85. }
  86. if ( uvs.length > 0 ) {
  87. geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  88. }
  89. if ( colors.length > 0 ) {
  90. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  91. }
  92. geometry.computeBoundingSphere();
  93. return geometry;
  94. },
  95. parseMaterial : function( json ) {
  96. var mat = null;
  97. var scope = this;
  98. var i, prop, has_textures = [],
  99. init_props = {
  100. shading : THREE.SmoothShading
  101. };
  102. function toColor( value_arr ) {
  103. var col = new THREE.Color();
  104. col.setRGB( value_arr[ 0 ], value_arr[ 1 ], value_arr[ 2 ] );
  105. return col;
  106. }
  107. function defaultTexture() {
  108. var im = new Image();
  109. im.width = 1;
  110. im.height = 1;
  111. return new THREE.Texture( im );
  112. }
  113. for ( i in json.properties ) {
  114. prop = json.properties[ i ];
  115. if ( prop.key === '$tex.file' ) {
  116. // prop.semantic gives the type of the texture
  117. // 1: diffuse
  118. // 2: specular mao
  119. // 5: height map (bumps)
  120. // 6: normal map
  121. // more values (i.e. emissive, environment) are known by assimp and may be relevant
  122. if ( prop.semantic === 1 || prop.semantic === 5 || prop.semantic === 6 || prop.semantic === 2 ) {
  123. ( function( semantic ) {
  124. var loader = new THREE.TextureLoader( scope.manager ),
  125. keyname;
  126. if ( semantic === 1 ) {
  127. keyname = 'map';
  128. } else if ( semantic === 5 ) {
  129. keyname = 'bumpMap';
  130. } else if ( semantic === 6 ) {
  131. keyname = 'normalMap';
  132. } else if ( semantic === 2 ) {
  133. keyname = 'specularMap';
  134. }
  135. has_textures.push( keyname );
  136. loader.setCrossOrigin( this.crossOrigin );
  137. var material_url = scope.texturePath + '/' + prop.value;
  138. material_url = material_url.replace( /\\/g, '/' );
  139. loader.load( material_url, function( tex ) {
  140. if ( tex ) {
  141. // TODO: read texture settings from assimp.
  142. // Wrapping is the default, though.
  143. tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
  144. mat[ keyname ] = tex;
  145. mat.needsUpdate = true;
  146. }
  147. } );
  148. } )( prop.semantic );
  149. }
  150. } else if ( prop.key === '?mat.name' ) {
  151. init_props.name = prop.value;
  152. } else if ( prop.key === '$clr.diffuse' ) {
  153. init_props.color = toColor( prop.value );
  154. } else if ( prop.key === '$clr.specular' ) {
  155. init_props.specular = toColor( prop.value );
  156. } else if ( prop.key === '$clr.emissive' ) {
  157. init_props.emissive = toColor( prop.value );
  158. } else if ( prop.key === '$mat.shadingm' ) {
  159. // aiShadingMode_Flat
  160. if ( prop.value === 1 ) {
  161. init_props.shading = THREE.FlatShading;
  162. }
  163. } else if ( prop.key === '$mat.shininess' ) {
  164. init_props.shininess = prop.value;
  165. }
  166. }
  167. // note: three.js does not like it when a texture is added after the geometry
  168. // has been rendered once, see http://stackoverflow.com/questions/16531759/.
  169. // for this reason we fill all slots upfront with default textures
  170. if ( has_textures.length ) {
  171. for ( i = has_textures.length - 1; i >= 0; -- i ) {
  172. init_props[ has_textures[ i ]] = defaultTexture();
  173. }
  174. }
  175. mat = new THREE.MeshPhongMaterial( init_props );
  176. return mat;
  177. },
  178. parseObject : function( json, node, meshes, materials ) {
  179. var obj = new THREE.Object3D(), i, idx;
  180. obj.name = node.name || "";
  181. obj.matrix = new THREE.Matrix4().fromArray( node.transformation ).transpose();
  182. obj.matrix.decompose( obj.position, obj.quaternion, obj.scale );
  183. for ( i = 0; node.meshes && i < node.meshes.length; ++ i ) {
  184. idx = node.meshes[ i ];
  185. obj.add( new THREE.Mesh( meshes[ idx ], materials[ json.meshes[ idx ].materialindex ] ) );
  186. }
  187. for ( i = 0; node.children && i < node.children.length; ++ i ) {
  188. obj.add( this.parseObject( json, node.children[ i ], meshes, materials ) );
  189. }
  190. return obj;
  191. }
  192. };