AssimpJSONLoader.js 8.0 KB

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