AMFLoader.js 11 KB

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