AssimpJSONLoader.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. import {
  14. BufferGeometry,
  15. DefaultLoadingManager,
  16. FileLoader,
  17. Float32BufferAttribute,
  18. LoaderUtils,
  19. Matrix4,
  20. Mesh,
  21. MeshPhongMaterial,
  22. Object3D,
  23. RepeatWrapping,
  24. TextureLoader
  25. } from "../../../build/three.module.js";
  26. var AssimpJSONLoader = function ( manager ) {
  27. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  28. };
  29. AssimpJSONLoader.prototype = {
  30. constructor: AssimpJSONLoader,
  31. crossOrigin: 'anonymous',
  32. load: function ( url, onLoad, onProgress, onError ) {
  33. var scope = this;
  34. var path = ( scope.path === undefined ) ? LoaderUtils.extractUrlBase( url ) : scope.path;
  35. var loader = new FileLoader( this.manager );
  36. loader.setPath( scope.path );
  37. loader.load( url, function ( text ) {
  38. var json = JSON.parse( text );
  39. var metadata = json.__metadata__;
  40. // check if __metadata__ meta header is present
  41. // this header is used to disambiguate between different JSON-based file formats
  42. if ( typeof metadata !== 'undefined' ) {
  43. // check if assimp2json at all
  44. if ( metadata.format !== 'assimp2json' ) {
  45. onError( 'THREE.AssimpJSONLoader: Not an assimp2json scene.' );
  46. return;
  47. // check major format version
  48. } else if ( metadata.version < 100 && metadata.version >= 200 ) {
  49. onError( 'THREE.AssimpJSONLoader: Unsupported assimp2json file format version.' );
  50. return;
  51. }
  52. }
  53. onLoad( scope.parse( json, path ) );
  54. }, onProgress, onError );
  55. },
  56. setPath: function ( value ) {
  57. this.path = value;
  58. return this;
  59. },
  60. setResourcePath: function ( value ) {
  61. this.resourcePath = value;
  62. return this;
  63. },
  64. setCrossOrigin: function ( value ) {
  65. this.crossOrigin = value;
  66. return this;
  67. },
  68. parse: function ( json, path ) {
  69. function parseList( json, handler ) {
  70. var meshes = new Array( json.length );
  71. for ( var i = 0; i < json.length; ++ i ) {
  72. meshes[ i ] = handler.call( this, json[ i ] );
  73. }
  74. return meshes;
  75. }
  76. function parseMesh( json ) {
  77. var geometry = new BufferGeometry();
  78. var i, l, face;
  79. var indices = [];
  80. var vertices = json.vertices || [];
  81. var normals = json.normals || [];
  82. var uvs = json.texturecoords || [];
  83. var colors = json.colors || [];
  84. uvs = uvs[ 0 ] || []; // only support for a single set of uvs
  85. for ( i = 0, l = json.faces.length; i < l; i ++ ) {
  86. face = json.faces[ i ];
  87. indices.push( face[ 0 ], face[ 1 ], face[ 2 ] );
  88. }
  89. geometry.setIndex( indices );
  90. geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  91. if ( normals.length > 0 ) {
  92. geometry.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  93. }
  94. if ( uvs.length > 0 ) {
  95. geometry.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  96. }
  97. if ( colors.length > 0 ) {
  98. geometry.addAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  99. }
  100. geometry.computeBoundingSphere();
  101. return geometry;
  102. }
  103. function parseMaterial( json ) {
  104. var material = new MeshPhongMaterial();
  105. for ( var i in json.properties ) {
  106. var property = json.properties[ i ];
  107. var key = property.key;
  108. var value = property.value;
  109. switch ( key ) {
  110. case '$tex.file': {
  111. var semantic = property.semantic;
  112. // prop.semantic gives the type of the texture
  113. // 1: diffuse
  114. // 2: specular map
  115. // 4: emissive map
  116. // 5: height map (bumps)
  117. // 6: normal map
  118. // more values (i.e. environment, etc) are known by assimp and may be relevant
  119. if ( semantic === 1 || semantic === 2 || semantic === 4 || semantic === 5 || semantic === 6 ) {
  120. var keyname;
  121. switch ( semantic ) {
  122. case 1:
  123. keyname = 'map';
  124. break;
  125. case 2:
  126. keyname = 'specularMap';
  127. break;
  128. case 4:
  129. keyname = 'emissiveMap';
  130. break;
  131. case 5:
  132. keyname = 'bumpMap';
  133. break;
  134. case 6:
  135. keyname = 'normalMap';
  136. break;
  137. }
  138. var texture = textureLoader.load( value );
  139. // TODO: read texture settings from assimp.
  140. // Wrapping is the default, though.
  141. texture.wrapS = texture.wrapT = RepeatWrapping;
  142. material[ keyname ] = texture;
  143. }
  144. break;
  145. }
  146. case '?mat.name':
  147. material.name = value;
  148. break;
  149. case '$clr.diffuse':
  150. material.color.fromArray( value );
  151. break;
  152. case '$clr.specular':
  153. material.specular.fromArray( value );
  154. break;
  155. case '$clr.emissive':
  156. material.emissive.fromArray( value );
  157. break;
  158. case '$mat.shininess':
  159. material.shininess = value;
  160. break;
  161. case '$mat.shadingm':
  162. // aiShadingMode_Flat
  163. material.flatShading = ( value === 1 ) ? true : false;
  164. break;
  165. case '$mat.opacity':
  166. if ( value < 1 ) {
  167. material.opacity = value;
  168. material.transparent = true;
  169. }
  170. break;
  171. }
  172. }
  173. return material;
  174. }
  175. function parseObject( json, node, meshes, materials ) {
  176. var obj = new Object3D(), i, idx;
  177. obj.name = node.name || '';
  178. obj.matrix = new Matrix4().fromArray( node.transformation ).transpose();
  179. obj.matrix.decompose( obj.position, obj.quaternion, obj.scale );
  180. for ( i = 0; node.meshes && i < node.meshes.length; i ++ ) {
  181. idx = node.meshes[ i ];
  182. obj.add( new Mesh( meshes[ idx ], materials[ json.meshes[ idx ].materialindex ] ) );
  183. }
  184. for ( i = 0; node.children && i < node.children.length; i ++ ) {
  185. obj.add( parseObject( json, node.children[ i ], meshes, materials ) );
  186. }
  187. return obj;
  188. }
  189. var textureLoader = new TextureLoader( this.manager );
  190. textureLoader.setPath( this.resourcePath || path ).setCrossOrigin( this.crossOrigin );
  191. var meshes = parseList( json.meshes, parseMesh );
  192. var materials = parseList( json.materials, parseMaterial );
  193. return parseObject( json, json.rootnode, meshes, materials );
  194. }
  195. };
  196. export { AssimpJSONLoader };