AMFLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. console.warn( "THREE.AMFLoader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. * Description: Early release of an AMF 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 jszip
  16. * No constellation support (yet)!
  17. *
  18. */
  19. THREE.AMFLoader = function ( manager ) {
  20. THREE.Loader.call( this, manager );
  21. };
  22. THREE.AMFLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  23. constructor: THREE.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 = new JSZip( data ); // eslint-disable-line no-undef
  54. } catch ( e ) {
  55. if ( e instanceof ReferenceError ) {
  56. console.log( 'THREE.AMFLoader: jszip missing and file is compressed.' );
  57. return null;
  58. }
  59. }
  60. for ( file in zip.files ) {
  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( file ).asArrayBuffer() );
  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 = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  99. var loadedMaterial = null;
  100. for ( var i = 0; i < node.childNodes.length; i ++ ) {
  101. var matChildEl = node.childNodes[ i ];
  102. if ( matChildEl.nodeName === 'metadata' && matChildEl.attributes.type !== undefined ) {
  103. if ( matChildEl.attributes.type.value === 'name' ) {
  104. matName = matChildEl.textContent;
  105. }
  106. } else if ( matChildEl.nodeName === 'color' ) {
  107. color = loadColor( matChildEl );
  108. }
  109. }
  110. loadedMaterial = new THREE.MeshPhongMaterial( {
  111. flatShading: true,
  112. color: new THREE.Color( color.r, color.g, color.b ),
  113. name: matName
  114. } );
  115. if ( color.a !== 1.0 ) {
  116. loadedMaterial.transparent = true;
  117. loadedMaterial.opacity = color.a;
  118. }
  119. return { id: matId, material: loadedMaterial };
  120. }
  121. function loadColor( node ) {
  122. var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  123. for ( var i = 0; i < node.childNodes.length; i ++ ) {
  124. var matColor = node.childNodes[ i ];
  125. if ( matColor.nodeName === 'r' ) {
  126. color.r = matColor.textContent;
  127. } else if ( matColor.nodeName === 'g' ) {
  128. color.g = matColor.textContent;
  129. } else if ( matColor.nodeName === 'b' ) {
  130. color.b = matColor.textContent;
  131. } else if ( matColor.nodeName === 'a' ) {
  132. color.a = matColor.textContent;
  133. }
  134. }
  135. return color;
  136. }
  137. function loadMeshVolume( node ) {
  138. var volume = { name: '', triangles: [], materialid: null };
  139. var currVolumeNode = node.firstElementChild;
  140. if ( node.attributes.materialid !== undefined ) {
  141. volume.materialId = node.attributes.materialid.nodeValue;
  142. }
  143. while ( currVolumeNode ) {
  144. if ( currVolumeNode.nodeName === 'metadata' ) {
  145. if ( currVolumeNode.attributes.type !== undefined ) {
  146. if ( currVolumeNode.attributes.type.value === 'name' ) {
  147. volume.name = currVolumeNode.textContent;
  148. }
  149. }
  150. } else if ( currVolumeNode.nodeName === 'triangle' ) {
  151. var v1 = currVolumeNode.getElementsByTagName( 'v1' )[ 0 ].textContent;
  152. var v2 = currVolumeNode.getElementsByTagName( 'v2' )[ 0 ].textContent;
  153. var v3 = currVolumeNode.getElementsByTagName( 'v3' )[ 0 ].textContent;
  154. volume.triangles.push( v1, v2, v3 );
  155. }
  156. currVolumeNode = currVolumeNode.nextElementSibling;
  157. }
  158. return volume;
  159. }
  160. function loadMeshVertices( node ) {
  161. var vertArray = [];
  162. var normalArray = [];
  163. var currVerticesNode = node.firstElementChild;
  164. while ( currVerticesNode ) {
  165. if ( currVerticesNode.nodeName === 'vertex' ) {
  166. var vNode = currVerticesNode.firstElementChild;
  167. while ( vNode ) {
  168. if ( vNode.nodeName === 'coordinates' ) {
  169. var x = vNode.getElementsByTagName( 'x' )[ 0 ].textContent;
  170. var y = vNode.getElementsByTagName( 'y' )[ 0 ].textContent;
  171. var z = vNode.getElementsByTagName( 'z' )[ 0 ].textContent;
  172. vertArray.push( x, y, z );
  173. } else if ( vNode.nodeName === 'normal' ) {
  174. var nx = vNode.getElementsByTagName( 'nx' )[ 0 ].textContent;
  175. var ny = vNode.getElementsByTagName( 'ny' )[ 0 ].textContent;
  176. var nz = vNode.getElementsByTagName( 'nz' )[ 0 ].textContent;
  177. normalArray.push( nx, ny, nz );
  178. }
  179. vNode = vNode.nextElementSibling;
  180. }
  181. }
  182. currVerticesNode = currVerticesNode.nextElementSibling;
  183. }
  184. return { 'vertices': vertArray, 'normals': normalArray };
  185. }
  186. function loadObject( node ) {
  187. var objId = node.attributes.id.textContent;
  188. var loadedObject = { name: 'amfobject', meshes: [] };
  189. var currColor = null;
  190. var currObjNode = node.firstElementChild;
  191. while ( currObjNode ) {
  192. if ( currObjNode.nodeName === 'metadata' ) {
  193. if ( currObjNode.attributes.type !== undefined ) {
  194. if ( currObjNode.attributes.type.value === 'name' ) {
  195. loadedObject.name = currObjNode.textContent;
  196. }
  197. }
  198. } else if ( currObjNode.nodeName === 'color' ) {
  199. currColor = loadColor( currObjNode );
  200. } else if ( currObjNode.nodeName === 'mesh' ) {
  201. var currMeshNode = currObjNode.firstElementChild;
  202. var mesh = { vertices: [], normals: [], volumes: [], color: currColor };
  203. while ( currMeshNode ) {
  204. if ( currMeshNode.nodeName === 'vertices' ) {
  205. var loadedVertices = loadMeshVertices( currMeshNode );
  206. mesh.normals = mesh.normals.concat( loadedVertices.normals );
  207. mesh.vertices = mesh.vertices.concat( loadedVertices.vertices );
  208. } else if ( currMeshNode.nodeName === 'volume' ) {
  209. mesh.volumes.push( loadMeshVolume( currMeshNode ) );
  210. }
  211. currMeshNode = currMeshNode.nextElementSibling;
  212. }
  213. loadedObject.meshes.push( mesh );
  214. }
  215. currObjNode = currObjNode.nextElementSibling;
  216. }
  217. return { 'id': objId, 'obj': loadedObject };
  218. }
  219. var xmlData = loadDocument( data );
  220. var amfName = '';
  221. var amfAuthor = '';
  222. var amfScale = loadDocumentScale( xmlData );
  223. var amfMaterials = {};
  224. var amfObjects = {};
  225. var childNodes = xmlData.documentElement.childNodes;
  226. var i, j;
  227. for ( i = 0; i < childNodes.length; i ++ ) {
  228. var child = childNodes[ i ];
  229. if ( child.nodeName === 'metadata' ) {
  230. if ( child.attributes.type !== undefined ) {
  231. if ( child.attributes.type.value === 'name' ) {
  232. amfName = child.textContent;
  233. } else if ( child.attributes.type.value === 'author' ) {
  234. amfAuthor = child.textContent;
  235. }
  236. }
  237. } else if ( child.nodeName === 'material' ) {
  238. var loadedMaterial = loadMaterials( child );
  239. amfMaterials[ loadedMaterial.id ] = loadedMaterial.material;
  240. } else if ( child.nodeName === 'object' ) {
  241. var loadedObject = loadObject( child );
  242. amfObjects[ loadedObject.id ] = loadedObject.obj;
  243. }
  244. }
  245. var sceneObject = new THREE.Group();
  246. var defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaff, flatShading: true } );
  247. sceneObject.name = amfName;
  248. sceneObject.userData.author = amfAuthor;
  249. sceneObject.userData.loader = 'AMF';
  250. for ( var id in amfObjects ) {
  251. var part = amfObjects[ id ];
  252. var meshes = part.meshes;
  253. var newObject = new THREE.Group();
  254. newObject.name = part.name || '';
  255. for ( i = 0; i < meshes.length; i ++ ) {
  256. var objDefaultMaterial = defaultMaterial;
  257. var mesh = meshes[ i ];
  258. var vertices = new THREE.Float32BufferAttribute( mesh.vertices, 3 );
  259. var normals = null;
  260. if ( mesh.normals.length ) {
  261. normals = new THREE.Float32BufferAttribute( mesh.normals, 3 );
  262. }
  263. if ( mesh.color ) {
  264. var color = mesh.color;
  265. objDefaultMaterial = defaultMaterial.clone();
  266. objDefaultMaterial.color = new THREE.Color( color.r, color.g, color.b );
  267. if ( color.a !== 1.0 ) {
  268. objDefaultMaterial.transparent = true;
  269. objDefaultMaterial.opacity = color.a;
  270. }
  271. }
  272. var volumes = mesh.volumes;
  273. for ( j = 0; j < volumes.length; j ++ ) {
  274. var volume = volumes[ j ];
  275. var newGeometry = new THREE.BufferGeometry();
  276. var material = objDefaultMaterial;
  277. newGeometry.setIndex( volume.triangles );
  278. newGeometry.setAttribute( 'position', vertices.clone() );
  279. if ( normals ) {
  280. newGeometry.setAttribute( 'normal', normals.clone() );
  281. }
  282. if ( amfMaterials[ volume.materialId ] !== undefined ) {
  283. material = amfMaterials[ volume.materialId ];
  284. }
  285. newGeometry.scale( amfScale, amfScale, amfScale );
  286. newObject.add( new THREE.Mesh( newGeometry, material.clone() ) );
  287. }
  288. }
  289. sceneObject.add( newObject );
  290. }
  291. return sceneObject;
  292. }
  293. } );