AMFLoader.js 11 KB

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