AssimpJSONLoader.js 6.0 KB

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