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. crossOrigin: 'Anonymous',
  19. load: function ( url, onLoad, onProgress, onError ) {
  20. var scope = this;
  21. this.texturePath = this.texturePath && ( typeof this.texturePath === "string" ) ? this.texturePath : this.extractUrlBase( url );
  22. var loader = new THREE.FileLoader( this.manager );
  23. loader.load( url, function ( text ) {
  24. var json = JSON.parse( text ), scene, metadata;
  25. // Check __metadata__ meta header if present
  26. // This header is used to disambiguate between
  27. // different JSON-based file formats.
  28. metadata = json.__metadata__;
  29. if ( typeof metadata !== 'undefined' ) {
  30. // Check if assimp2json at all
  31. if ( metadata.format !== 'assimp2json' ) {
  32. onError( 'Not an assimp2json scene' );
  33. return;
  34. }
  35. // Check major format version
  36. else if ( metadata.version < 100 && metadata.version >= 200 ) {
  37. onError( 'Unsupported assimp2json file format version' );
  38. return;
  39. }
  40. }
  41. scene = scope.parse( json );
  42. onLoad( scene );
  43. }, onProgress, onError );
  44. },
  45. setCrossOrigin: function ( value ) {
  46. this.crossOrigin = value;
  47. },
  48. setTexturePath: function ( value ) {
  49. this.texturePath = value;
  50. },
  51. extractUrlBase: function ( url ) {
  52. // from three/src/loaders/Loader.js
  53. var parts = url.split( '/' );
  54. parts.pop();
  55. return ( parts.length < 1 ? '.' : parts.join( '/' ) ) + '/';
  56. },
  57. parse: function ( json ) {
  58. var meshes = this.parseList ( json.meshes, this.parseMesh );
  59. var materials = this.parseList ( json.materials, this.parseMaterial );
  60. return this.parseObject( json, json.rootnode, meshes, materials );
  61. },
  62. parseList : function( json, handler ) {
  63. var meshes = new Array( json.length );
  64. for ( var i = 0; i < json.length; ++ i ) {
  65. meshes[ i ] = handler.call( this, json[ i ] );
  66. }
  67. return meshes;
  68. },
  69. parseMesh : function( json ) {
  70. var geometry = new THREE.BufferGeometry();
  71. var i, l, face;
  72. var indices = [];
  73. var vertices = json.vertices || [];
  74. var normals = json.normals || [];
  75. var uvs = json.texturecoords || [];
  76. var colors = json.colors || [];
  77. uvs = uvs[ 0 ] || []; // only support for a single set of uvs
  78. for ( i = 0, l = json.faces.length; i < l; i ++ ) {
  79. face = json.faces[ i ];
  80. indices.push( face[ 0 ], face[ 1 ], face[ 2 ] );
  81. }
  82. geometry.setIndex( indices );
  83. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  84. if ( normals.length > 0 ) {
  85. geometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  86. }
  87. if ( uvs.length > 0 ) {
  88. geometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  89. }
  90. if ( colors.length > 0 ) {
  91. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  92. }
  93. geometry.computeBoundingSphere();
  94. return geometry;
  95. },
  96. parseMaterial : function( json ) {
  97. var mat = null;
  98. var scope = this;
  99. var i, prop, has_textures = [],
  100. init_props = {};
  101. function toColor( value_arr ) {
  102. var col = new THREE.Color();
  103. col.setRGB( value_arr[ 0 ], value_arr[ 1 ], value_arr[ 2 ] );
  104. return col;
  105. }
  106. function defaultTexture() {
  107. var im = new Image();
  108. im.width = 1;
  109. im.height = 1;
  110. return new THREE.Texture( im );
  111. }
  112. for ( i in json.properties ) {
  113. prop = json.properties[ i ];
  114. if ( prop.key === '$tex.file' ) {
  115. // prop.semantic gives the type of the texture
  116. // 1: diffuse
  117. // 2: specular mao
  118. // 5: height map (bumps)
  119. // 6: normal map
  120. // more values (i.e. emissive, environment) are known by assimp and may be relevant
  121. if ( prop.semantic === 1 || prop.semantic === 5 || prop.semantic === 6 || prop.semantic === 2 ) {
  122. ( function( semantic ) {
  123. var loader = new THREE.TextureLoader( scope.manager ),
  124. keyname;
  125. if ( semantic === 1 ) {
  126. keyname = 'map';
  127. } else if ( semantic === 5 ) {
  128. keyname = 'bumpMap';
  129. } else if ( semantic === 6 ) {
  130. keyname = 'normalMap';
  131. } else if ( semantic === 2 ) {
  132. keyname = 'specularMap';
  133. }
  134. has_textures.push( keyname );
  135. loader.setCrossOrigin( this.crossOrigin );
  136. var material_url = scope.texturePath + '/' + prop.value;
  137. material_url = material_url.replace( /\\/g, '/' );
  138. loader.load( material_url, function( tex ) {
  139. if ( tex ) {
  140. // TODO: read texture settings from assimp.
  141. // Wrapping is the default, though.
  142. tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
  143. mat[ keyname ] = tex;
  144. mat.needsUpdate = true;
  145. }
  146. } );
  147. } )( prop.semantic );
  148. }
  149. } else if ( prop.key === '?mat.name' ) {
  150. init_props.name = prop.value;
  151. } else if ( prop.key === '$clr.diffuse' ) {
  152. init_props.color = toColor( prop.value );
  153. } else if ( prop.key === '$clr.specular' ) {
  154. init_props.specular = toColor( prop.value );
  155. } else if ( prop.key === '$clr.emissive' ) {
  156. init_props.emissive = toColor( prop.value );
  157. } else if ( prop.key === '$mat.shadingm' ) {
  158. // aiShadingMode_Flat
  159. if ( prop.value === 1 ) {
  160. init_props.flatShading = true;
  161. }
  162. } else if ( prop.key === '$mat.shininess' ) {
  163. init_props.shininess = prop.value;
  164. }
  165. }
  166. // note: three.js does not like it when a texture is added after the geometry
  167. // has been rendered once, see http://stackoverflow.com/questions/16531759/.
  168. // for this reason we fill all slots upfront with default textures
  169. if ( has_textures.length ) {
  170. for ( i = has_textures.length - 1; i >= 0; -- i ) {
  171. init_props[ has_textures[ i ]] = defaultTexture();
  172. }
  173. }
  174. mat = new THREE.MeshPhongMaterial( init_props );
  175. return mat;
  176. },
  177. parseObject : function( json, node, meshes, materials ) {
  178. var obj = new THREE.Object3D(), i, idx;
  179. obj.name = node.name || "";
  180. obj.matrix = new THREE.Matrix4().fromArray( node.transformation ).transpose();
  181. obj.matrix.decompose( obj.position, obj.quaternion, obj.scale );
  182. for ( i = 0; node.meshes && i < node.meshes.length; ++ i ) {
  183. idx = node.meshes[ i ];
  184. obj.add( new THREE.Mesh( meshes[ idx ], materials[ json.meshes[ idx ].materialindex ] ) );
  185. }
  186. for ( i = 0; node.children && i < node.children.length; ++ i ) {
  187. obj.add( this.parseObject( json, node.children[ i ], meshes, materials ) );
  188. }
  189. return obj;
  190. }
  191. };