AMFLoader.js 10 KB

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