AssimpJSONLoader.js 7.5 KB

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