AMFLoader.js 11 KB

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