AMFLoader.js 11 KB

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