AMFLoader.js 10 KB

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