AMFLoader.js 10 KB

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