2
0

AssimpJSONLoader.js 5.8 KB

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