AMFLoader.js 11 KB

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