PageRenderTime 74ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/js/loaders/AssimpJSONLoader.js

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