AMFLoader.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * @author tamarintech / https://tamarintech.com
  3. *
  4. * Description: Early release of an AMF Loader following the pattern of the
  5. * example loaders in the three.js project.
  6. *
  7. * More information about the AMF format: http://amf.wikispaces.com
  8. *
  9. * Usage:
  10. * var loader = new AMFLoader();
  11. * loader.load('/path/to/project.amf', function(objecttree) {
  12. * scene.add(objecttree);
  13. * });
  14. *
  15. * Materials now supported, material colors supported
  16. * Zip support, requires jszip
  17. * No constellation support (yet)!
  18. *
  19. */
  20. THREE.AMFLoader = function ( manager ) {
  21. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  22. };
  23. THREE.AMFLoader.prototype = {
  24. constructor: THREE.AMFLoader,
  25. load: function ( url, onLoad, onProgress, onError ) {
  26. var scope = this;
  27. var loader = new THREE.FileLoader( scope.manager );
  28. loader.setPath( scope.path );
  29. loader.setResponseType( 'arraybuffer' );
  30. loader.load( url, function ( text ) {
  31. onLoad( scope.parse( text ) );
  32. }, onProgress, onError );
  33. },
  34. setPath: function ( value ) {
  35. this.path = value;
  36. return this;
  37. },
  38. parse: function ( data ) {
  39. function loadDocument( data ) {
  40. var view = new DataView( data );
  41. var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) );
  42. if ( magic === 'PK' ) {
  43. var zip = null;
  44. var file = null;
  45. console.log( 'THREE.AMFLoader: Loading Zip' );
  46. try {
  47. zip = new JSZip( data ); // eslint-disable-line no-undef
  48. } catch ( e ) {
  49. if ( e instanceof ReferenceError ) {
  50. console.log( 'THREE.AMFLoader: jszip missing and file is compressed.' );
  51. return null;
  52. }
  53. }
  54. for ( file in zip.files ) {
  55. if ( file.toLowerCase().substr( - 4 ) === '.amf' ) {
  56. break;
  57. }
  58. }
  59. console.log( 'THREE.AMFLoader: Trying to load file asset: ' + file );
  60. view = new DataView( zip.file( file ).asArrayBuffer() );
  61. }
  62. var fileText = THREE.LoaderUtils.decodeText( view );
  63. var xmlData = new DOMParser().parseFromString( fileText, 'application/xml' );
  64. if ( xmlData.documentElement.nodeName.toLowerCase() !== 'amf' ) {
  65. console.log( 'THREE.AMFLoader: Error loading AMF - no AMF document found.' );
  66. return null;
  67. }
  68. return xmlData;
  69. }
  70. function loadDocumentScale( node ) {
  71. var scale = 1.0;
  72. var unit = 'millimeter';
  73. if ( node.documentElement.attributes.unit !== undefined ) {
  74. unit = node.documentElement.attributes.unit.value.toLowerCase();
  75. }
  76. var scaleUnits = {
  77. millimeter: 1.0,
  78. inch: 25.4,
  79. feet: 304.8,
  80. meter: 1000.0,
  81. micron: 0.001
  82. };
  83. if ( scaleUnits[ unit ] !== undefined ) {
  84. scale = scaleUnits[ unit ];
  85. }
  86. console.log( 'THREE.AMFLoader: Unit scale: ' + scale );
  87. return scale;
  88. }
  89. function loadMaterials( node ) {
  90. var matName = 'AMF Material';
  91. var matId = node.attributes.id.textContent;
  92. var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  93. var loadedMaterial = null;
  94. for ( var i = 0; i < node.childNodes.length; i ++ ) {
  95. var matChildEl = node.childNodes[ i ];
  96. if ( matChildEl.nodeName === 'metadata' && matChildEl.attributes.type !== undefined ) {
  97. if ( matChildEl.attributes.type.value === 'name' ) {
  98. matName = matChildEl.textContent;
  99. }
  100. } else if ( matChildEl.nodeName === 'color' ) {
  101. color = loadColor( matChildEl );
  102. }
  103. }
  104. loadedMaterial = new THREE.MeshPhongMaterial( {
  105. flatShading: true,
  106. color: new THREE.Color( color.r, color.g, color.b ),
  107. name: matName
  108. } );
  109. if ( color.a !== 1.0 ) {
  110. loadedMaterial.transparent = true;
  111. loadedMaterial.opacity = color.a;
  112. }
  113. return { id: matId, material: loadedMaterial };
  114. }
  115. function loadColor( node ) {
  116. var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  117. for ( var i = 0; i < node.childNodes.length; i ++ ) {
  118. var matColor = node.childNodes[ i ];
  119. if ( matColor.nodeName === 'r' ) {
  120. color.r = matColor.textContent;
  121. } else if ( matColor.nodeName === 'g' ) {
  122. color.g = matColor.textContent;
  123. } else if ( matColor.nodeName === 'b' ) {
  124. color.b = matColor.textContent;
  125. } else if ( matColor.nodeName === 'a' ) {
  126. color.a = matColor.textContent;
  127. }
  128. }
  129. return color;
  130. }
  131. function loadMeshVolume( node ) {
  132. var volume = { name: '', triangles: [], materialid: null };
  133. var currVolumeNode = node.firstElementChild;
  134. if ( node.attributes.materialid !== undefined ) {
  135. volume.materialId = node.attributes.materialid.nodeValue;
  136. }
  137. while ( currVolumeNode ) {
  138. if ( currVolumeNode.nodeName === 'metadata' ) {
  139. if ( currVolumeNode.attributes.type !== undefined ) {
  140. if ( currVolumeNode.attributes.type.value === 'name' ) {
  141. volume.name = currVolumeNode.textContent;
  142. }
  143. }
  144. } else if ( currVolumeNode.nodeName === 'triangle' ) {
  145. var v1 = currVolumeNode.getElementsByTagName( 'v1' )[ 0 ].textContent;
  146. var v2 = currVolumeNode.getElementsByTagName( 'v2' )[ 0 ].textContent;
  147. var v3 = currVolumeNode.getElementsByTagName( 'v3' )[ 0 ].textContent;
  148. volume.triangles.push( v1, v2, v3 );
  149. }
  150. currVolumeNode = currVolumeNode.nextElementSibling;
  151. }
  152. return volume;
  153. }
  154. function loadMeshVertices( node ) {
  155. var vertArray = [];
  156. var normalArray = [];
  157. var currVerticesNode = node.firstElementChild;
  158. while ( currVerticesNode ) {
  159. if ( currVerticesNode.nodeName === 'vertex' ) {
  160. var vNode = currVerticesNode.firstElementChild;
  161. while ( vNode ) {
  162. if ( vNode.nodeName === 'coordinates' ) {
  163. var x = vNode.getElementsByTagName( 'x' )[ 0 ].textContent;
  164. var y = vNode.getElementsByTagName( 'y' )[ 0 ].textContent;
  165. var z = vNode.getElementsByTagName( 'z' )[ 0 ].textContent;
  166. vertArray.push( x, y, z );
  167. } else if ( vNode.nodeName === 'normal' ) {
  168. var nx = vNode.getElementsByTagName( 'nx' )[ 0 ].textContent;
  169. var ny = vNode.getElementsByTagName( 'ny' )[ 0 ].textContent;
  170. var nz = vNode.getElementsByTagName( 'nz' )[ 0 ].textContent;
  171. normalArray.push( nx, ny, nz );
  172. }
  173. vNode = vNode.nextElementSibling;
  174. }
  175. }
  176. currVerticesNode = currVerticesNode.nextElementSibling;
  177. }
  178. return { 'vertices': vertArray, 'normals': normalArray };
  179. }
  180. function loadObject( node ) {
  181. var objId = node.attributes.id.textContent;
  182. var loadedObject = { name: 'amfobject', meshes: [] };
  183. var currColor = null;
  184. var currObjNode = node.firstElementChild;
  185. while ( currObjNode ) {
  186. if ( currObjNode.nodeName === 'metadata' ) {
  187. if ( currObjNode.attributes.type !== undefined ) {
  188. if ( currObjNode.attributes.type.value === 'name' ) {
  189. loadedObject.name = currObjNode.textContent;
  190. }
  191. }
  192. } else if ( currObjNode.nodeName === 'color' ) {
  193. currColor = loadColor( currObjNode );
  194. } else if ( currObjNode.nodeName === 'mesh' ) {
  195. var currMeshNode = currObjNode.firstElementChild;
  196. var mesh = { vertices: [], normals: [], volumes: [], color: currColor };
  197. while ( currMeshNode ) {
  198. if ( currMeshNode.nodeName === 'vertices' ) {
  199. var loadedVertices = loadMeshVertices( currMeshNode );
  200. mesh.normals = mesh.normals.concat( loadedVertices.normals );
  201. mesh.vertices = mesh.vertices.concat( loadedVertices.vertices );
  202. } else if ( currMeshNode.nodeName === 'volume' ) {
  203. mesh.volumes.push( loadMeshVolume( currMeshNode ) );
  204. }
  205. currMeshNode = currMeshNode.nextElementSibling;
  206. }
  207. loadedObject.meshes.push( mesh );
  208. }
  209. currObjNode = currObjNode.nextElementSibling;
  210. }
  211. return { 'id': objId, 'obj': loadedObject };
  212. }
  213. var xmlData = loadDocument( data );
  214. var amfName = '';
  215. var amfAuthor = '';
  216. var amfScale = loadDocumentScale( xmlData );
  217. var amfMaterials = {};
  218. var amfObjects = {};
  219. var childNodes = xmlData.documentElement.childNodes;
  220. var i, j;
  221. for ( i = 0; i < childNodes.length; i ++ ) {
  222. var child = childNodes[ i ];
  223. if ( child.nodeName === 'metadata' ) {
  224. if ( child.attributes.type !== undefined ) {
  225. if ( child.attributes.type.value === 'name' ) {
  226. amfName = child.textContent;
  227. } else if ( child.attributes.type.value === 'author' ) {
  228. amfAuthor = child.textContent;
  229. }
  230. }
  231. } else if ( child.nodeName === 'material' ) {
  232. var loadedMaterial = loadMaterials( child );
  233. amfMaterials[ loadedMaterial.id ] = loadedMaterial.material;
  234. } else if ( child.nodeName === 'object' ) {
  235. var loadedObject = loadObject( child );
  236. amfObjects[ loadedObject.id ] = loadedObject.obj;
  237. }
  238. }
  239. var sceneObject = new THREE.Group();
  240. var defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaff, flatShading: true } );
  241. sceneObject.name = amfName;
  242. sceneObject.userData.author = amfAuthor;
  243. sceneObject.userData.loader = 'AMF';
  244. for ( var id in amfObjects ) {
  245. var part = amfObjects[ id ];
  246. var meshes = part.meshes;
  247. var newObject = new THREE.Group();
  248. newObject.name = part.name || '';
  249. for ( i = 0; i < meshes.length; i ++ ) {
  250. var objDefaultMaterial = defaultMaterial;
  251. var mesh = meshes[ i ];
  252. var vertices = new THREE.Float32BufferAttribute( mesh.vertices, 3 );
  253. var normals = null;
  254. if ( mesh.normals.length ) {
  255. normals = new THREE.Float32BufferAttribute( mesh.normals, 3 );
  256. }
  257. if ( mesh.color ) {
  258. var color = mesh.color;
  259. objDefaultMaterial = defaultMaterial.clone();
  260. objDefaultMaterial.color = new THREE.Color( color.r, color.g, color.b );
  261. if ( color.a !== 1.0 ) {
  262. objDefaultMaterial.transparent = true;
  263. objDefaultMaterial.opacity = color.a;
  264. }
  265. }
  266. var volumes = mesh.volumes;
  267. for ( j = 0; j < volumes.length; j ++ ) {
  268. var volume = volumes[ j ];
  269. var newGeometry = new THREE.BufferGeometry();
  270. var material = objDefaultMaterial;
  271. newGeometry.setIndex( volume.triangles );
  272. newGeometry.addAttribute( 'position', vertices.clone() );
  273. if ( normals ) {
  274. newGeometry.addAttribute( 'normal', normals.clone() );
  275. }
  276. if ( amfMaterials[ volume.materialId ] !== undefined ) {
  277. material = amfMaterials[ volume.materialId ];
  278. }
  279. newGeometry.scale( amfScale, amfScale, amfScale );
  280. newObject.add( new THREE.Mesh( newGeometry, material.clone() ) );
  281. }
  282. }
  283. sceneObject.add( newObject );
  284. }
  285. return sceneObject;
  286. }
  287. };