AssimpJSONLoader.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. texturePath : '',
  19. load: function ( url, onLoad, onProgress, onError, texturePath ) {
  20. var scope = this;
  21. this.texturePath = texturePath && ( typeof texturePath === "string" ) ? texturePath : this.extractUrlBase( url );
  22. var loader = new THREE.XHRLoader( this.manager );
  23. loader.setCrossOrigin( this.crossOrigin );
  24. loader.load( url, function ( text ) {
  25. var json = JSON.parse( text ), scene, metadata;
  26. // Check __metadata__ meta header if present
  27. // This header is used to disambiguate between
  28. // different JSON-based file formats.
  29. metadata = json.__metadata__;
  30. if ( typeof metadata !== 'undefined' )
  31. {
  32. // Check if assimp2json at all
  33. if ( metadata.format !== 'assimp2json' ) {
  34. onError('Not an assimp2json scene');
  35. return;
  36. }
  37. // Check major format version
  38. else if ( metadata.version < 100 && metadata.version >= 200 ) {
  39. onError('Unsupported assimp2json file format version');
  40. return;
  41. }
  42. }
  43. scene = scope.parse( json );
  44. onLoad( scene );
  45. }, onProgress, onError );
  46. },
  47. setCrossOrigin: function ( value ) {
  48. this.crossOrigin = value;
  49. },
  50. extractUrlBase: function ( url ) { // from three/src/loaders/Loader.js
  51. var parts = url.split( '/' );
  52. parts.pop();
  53. return ( parts.length < 1 ? '.' : parts.join( '/' ) ) + '/';
  54. },
  55. parse: function ( json ) {
  56. var meshes = this.parseList ( json.meshes, this.parseMesh );
  57. var materials = this.parseList ( json.materials, this.parseMaterial );
  58. return this.parseObject( json, json.rootnode, meshes, materials );
  59. },
  60. parseList : function(json, handler) {
  61. var meshes = new Array(json.length);
  62. for(var i = 0; i < json.length; ++i) {
  63. meshes[i] = handler.call(this, json[i]);
  64. }
  65. return meshes;
  66. },
  67. parseMesh : function(json) {
  68. var vertex, geometry, i, e, in_data, src;
  69. geometry = new THREE.Geometry();
  70. // read vertex positions
  71. for(in_data = json.vertices, i = 0, e = in_data.length; i < e; ) {
  72. geometry.vertices.push( new THREE.Vector3( in_data[ i++ ], in_data[ i++ ], in_data[ i++ ] ) );
  73. }
  74. // read faces
  75. var cnt = 0;
  76. for(in_data = json.faces, i = 0, e = in_data.length; i < e; ++i) {
  77. face = new THREE.Face3();
  78. src = in_data[i];
  79. face.a = src[0];
  80. face.b = src[1];
  81. face.c = src[2];
  82. face.materialIndex = 0; //json.materialindex;
  83. geometry.faces.push(face);
  84. }
  85. // read texture coordinates - three.js attaches them to its faces
  86. json.texturecoords = json.texturecoords || [];
  87. for(i = 0, e = json.texturecoords.length; i < e; ++i) {
  88. function convertTextureCoords(in_uv, out_faces, out_vertex_uvs) {
  89. var i, e, face, a, b, c;
  90. for(i = 0, e = out_faces.length; i < e; ++i) {
  91. face = out_faces[i];
  92. a = face.a * 2;
  93. b = face.b * 2;
  94. c = face.c * 2;
  95. out_vertex_uvs.push([
  96. new THREE.Vector2( in_uv[ a ], in_uv[ a + 1 ] ),
  97. new THREE.Vector2( in_uv[ b ], in_uv[ b + 1 ] ),
  98. new THREE.Vector2( in_uv[ c ], in_uv[ c + 1 ] )
  99. ]);
  100. }
  101. }
  102. convertTextureCoords(json.texturecoords[i], geometry.faces, geometry.faceVertexUvs[i]);
  103. }
  104. // read normals - three.js also attaches them to its faces
  105. if(json.normals) {
  106. function convertNormals(in_nor, out_faces) {
  107. var i, e, face, a, b, c;
  108. for(i = 0, e = out_faces.length; i < e; ++i) {
  109. face = out_faces[i];
  110. a = face.a * 3;
  111. b = face.b * 3;
  112. c = face.c * 3;
  113. face.vertexNormals = [
  114. new THREE.Vector3( in_nor[ a ], in_nor[ a + 1 ], in_nor[ a + 2 ] ),
  115. new THREE.Vector3( in_nor[ b ], in_nor[ b + 1 ], in_nor[ b + 2 ] ),
  116. new THREE.Vector3( in_nor[ c ], in_nor[ c + 1 ], in_nor[ c + 2 ] )
  117. ];
  118. }
  119. }
  120. convertNormals(json.normals, geometry.faces);
  121. }
  122. // read vertex colors - three.js also attaches them to its faces
  123. if(json.colors && json.colors[0]) {
  124. function convertColors(in_color, out_faces) {
  125. var i, e, face, a, b, c;
  126. function makeColor(start) {
  127. var col = new THREE.Color( );
  128. col.setRGB( arr[0], arr[1], arr[2] );
  129. // TODO: what about alpha?
  130. return col;
  131. }
  132. for(i = 0, e = out_faces.length; i < e; ++i) {
  133. face = out_faces[i];
  134. a = face.a * 4;
  135. b = face.b * 4;
  136. c = face.c * 4;
  137. face.vertexColors = [
  138. makeColor( a ),
  139. makeColor( b ),
  140. makeColor( c )
  141. ];
  142. }
  143. }
  144. convertColors(json.colors[0], geometry.faces);
  145. }
  146. //geometry.computeFaceNormals();
  147. //geometry.computeVertexNormals();
  148. //geometry.computeTangents();
  149. geometry.computeBoundingSphere();
  150. // TODO: tangents
  151. return geometry;
  152. },
  153. parseMaterial : function(json) {
  154. var mat = null,
  155. scope = this, i, prop, has_textures = [],
  156. init_props = {
  157. shading : THREE.SmoothShading
  158. };
  159. function toColor(value_arr) {
  160. var col = new THREE.Color();
  161. col.setRGB(value_arr[0],value_arr[1],value_arr[2]);
  162. return col;
  163. }
  164. function defaultTexture() {
  165. var im = new Image();
  166. im.width = 1;
  167. im.height = 1;
  168. return new THREE.Texture(im);
  169. }
  170. for (var i in json.properties) {
  171. prop = json.properties[i];
  172. if(prop.key === '$tex.file') {
  173. // prop.semantic gives the type of the texture
  174. // 1: diffuse
  175. // 2: specular mao
  176. // 5: height map (bumps)
  177. // 6: normal map
  178. // more values (i.e. emissive, environment) are known by assimp and may be relevant
  179. if(prop.semantic === 1 || prop.semantic === 5 || prop.semantic === 6 || prop.semantic === 2) {
  180. (function(semantic) {
  181. var loader = new THREE.TextureLoader(scope.manager),
  182. keyname;
  183. if(semantic === 1) {
  184. keyname = 'map';
  185. }
  186. else if(semantic === 5) {
  187. keyname = 'bumpMap';
  188. }
  189. else if(semantic === 6) {
  190. keyname = 'normalMap';
  191. }
  192. else if(semantic === 2) {
  193. keyname = 'specularMap';
  194. }
  195. has_textures.push(keyname);
  196. loader.setCrossOrigin(this.crossOrigin);
  197. var material_url = scope.texturePath + '/' + prop.value
  198. material_url = material_url.replace(/\\/g, '/');
  199. loader.load(material_url, function(tex) {
  200. if(tex) {
  201. // TODO: read texture settings from assimp.
  202. // Wrapping is the default, though.
  203. tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
  204. mat[keyname] = tex;
  205. mat.needsUpdate = true;
  206. }
  207. });
  208. })(prop.semantic);
  209. }
  210. }
  211. else if(prop.key === '?mat.name') {
  212. init_props.name = prop.value;
  213. }
  214. else if(prop.key === '$clr.diffuse') {
  215. init_props.color = toColor(prop.value);
  216. }
  217. else if(prop.key === '$clr.specular') {
  218. init_props.specular = toColor(prop.value);
  219. }
  220. else if(prop.key === '$clr.ambient') {
  221. init_props.ambient = toColor(prop.value);
  222. }
  223. else if(prop.key === '$clr.emissive') {
  224. init_props.emissive = toColor(prop.value);
  225. }
  226. else if(prop.key === '$mat.shadingm') {
  227. // aiShadingMode_Flat
  228. if (prop.value === 1) {
  229. init_props.shading = THREE.FlatShading;
  230. }
  231. }
  232. else if (prop.key === '$mat.shininess') {
  233. init_props.shininess = prop.value;
  234. }
  235. }
  236. if(!init_props.ambient) {
  237. init_props.ambient = init_props.color;
  238. }
  239. // note: three.js does not like it when a texture is added after the geometry
  240. // has been rendered once, see http://stackoverflow.com/questions/16531759/.
  241. // for this reason we fill all slots upfront with default textures
  242. if(has_textures.length) {
  243. for(i = has_textures.length-1; i >= 0; --i) {
  244. init_props[has_textures[i]] = defaultTexture();
  245. }
  246. }
  247. mat = new THREE.MeshPhongMaterial( init_props );
  248. return mat;
  249. },
  250. parseObject : function(json, node, meshes, materials) {
  251. var obj = new THREE.Object3D()
  252. , i
  253. , idx
  254. ;
  255. obj.name = node.name || "";
  256. obj.matrix = new THREE.Matrix4().fromArray(node.transformation).transpose();
  257. obj.matrix.decompose( obj.position, obj.quaternion, obj.scale );
  258. for(i = 0; node.meshes && i < node.meshes.length; ++i) {
  259. idx = node.meshes[i];
  260. obj.add(new THREE.Mesh( meshes[idx], materials[json.meshes[idx].materialindex] ));
  261. }
  262. for(i = 0; node.children && i < node.children.length; ++i) {
  263. obj.add(this.parseObject(json, node.children[i], meshes, materials));
  264. }
  265. return obj;
  266. },
  267. };