AMFLoader.js 11 KB

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