AMFLoader.js 10 KB

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