AMFLoader.js 10 KB

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