AssimpJSONLoader.js 5.7 KB

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