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. * TextDecoder polyfill required by some browsers (particularly IE, Edge)
  18. * No constellation support (yet)!
  19. *
  20. */
  21. THREE.AMFLoader = function ( manager ) {
  22. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  23. };
  24. THREE.AMFLoader.prototype = {
  25. constructor: THREE.AMFLoader,
  26. load: function ( url, onLoad, onProgress, onError ) {
  27. var scope = this;
  28. var loader = new THREE.XHRLoader( scope.manager );
  29. loader.setCrossOrigin( this.crossOrigin );
  30. loader.setResponseType( 'arraybuffer' );
  31. loader.load( url, function( text ) {
  32. var amfObject = scope.parse( text );
  33. onLoad( amfObject );
  34. }, onProgress, onError );
  35. },
  36. parse: function ( data ) {
  37. function loadDocument( data ) {
  38. var view = new DataView( data );
  39. var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) );
  40. if ( magic === "PK" ) {
  41. console.log( "Loading Zip" );
  42. var zip = null;
  43. var file = null;
  44. try {
  45. zip = new JSZip( data );
  46. } catch ( e ) {
  47. if ( e instanceof ReferenceError ) {
  48. console.log( " jszip missing and file is compressed." );
  49. return null;
  50. }
  51. }
  52. for ( file in zip.files ) {
  53. if ( file.toLowerCase().endsWith( ".amf" ) ) {
  54. break;
  55. }
  56. }
  57. console.log( " Trying to load file asset: " + file );
  58. view = new DataView( zip.file( file ).asArrayBuffer() );
  59. }
  60. if ( TextDecoder === undefined ) {
  61. console.log( " TextDecoder not present. Please use TextDecoder polyfill." );
  62. return null;
  63. }
  64. var filetext = new TextDecoder( 'utf-8' ).decode( view );
  65. var xmldata = new DOMParser().parseFromString( filetext, 'application/xml' );
  66. if ( xmldata.documentElement.nodeName.toLowerCase() !== "amf" ) {
  67. console.log( " Error loading AMF - no AMF document found." );
  68. return null;
  69. }
  70. return xmldata;
  71. }
  72. function loadDocumentScale( xmldata ) {
  73. var scale = 1.0;
  74. var unit = 'millimeter';
  75. if ( xmldata.documentElement.attributes[ 'unit' ] !== undefined ) {
  76. unit = xmldata.documentElement.attributes[ 'unit' ].value.toLowerCase();
  77. }
  78. var scale_units = {
  79. 'millimeter': 1.0,
  80. 'inch': 25.4,
  81. 'feet': 304.8,
  82. 'meter': 1000.0,
  83. 'micron': 0.001
  84. };
  85. if ( scale_units[ unit ] !== undefined ) {
  86. scale = scale_units[ unit ];
  87. }
  88. console.log( " Unit scale: " + scale );
  89. return scale;
  90. }
  91. function loadMaterials( node ) {
  92. var mat = node;
  93. var loadedmaterial = null;
  94. var matname = "AMF Material";
  95. var matid = mat.attributes[ 'id' ].textContent;
  96. var color;
  97. for ( var i = 0; i < mat.children.length; i ++ ) {
  98. var matchildel = mat.children[ i ];
  99. if ( matchildel.nodeName === "metadata" && matchildel.attributes[ 'type' ] !== undefined ) {
  100. if ( matchildel.attributes[ 'type' ].value === 'name' ) {
  101. matname = matchildel.textContent;
  102. }
  103. } else if ( matchildel.nodeName === 'color' ) {
  104. color = loadColor( matchildel );
  105. }
  106. }
  107. loadedmaterial = new THREE.MeshPhongMaterial( {
  108. shading: THREE.FlatShading,
  109. color: new THREE.Color( color.r, color.g, color.b ),
  110. name: matname } );
  111. if ( color.opacity !== 1.0 ) {
  112. loadedmaterial.transparent = true;
  113. loadedmaterial.opacity = color.opacity;
  114. }
  115. return { 'id': matid, 'material': loadedmaterial };
  116. }
  117. function loadColor( node ) {
  118. var color = { 'r': 1.0, 'g': 1.0, 'b': 1.0, 'a': 1.0, opacity: 1.0 };
  119. for ( var i = 0; i < node.children.length; i ++ ) {
  120. var matcolor = node.children[ i ];
  121. if ( matcolor.nodeName === 'r' ) {
  122. color.r = matcolor.textContent;
  123. } else if ( matcolor.nodeName === 'g' ) {
  124. color.g = matcolor.textContent;
  125. } else if ( matcolor.nodeName === 'b' ) {
  126. color.b = matcolor.textContent;
  127. } else if ( matcolor.nodeName === 'a' ) {
  128. color.opacity = matcolor.textContent;
  129. }
  130. }
  131. return color;
  132. }
  133. function loadMeshVolume( node ) {
  134. var volume = { "name": "", "triangles": [], "materialid": null };
  135. var currvolumenode = node.firstElementChild;
  136. if ( node.attributes[ 'materialid' ] !== undefined ) {
  137. volume.materialid = node.attributes[ 'materialid' ].nodeValue;
  138. }
  139. while ( currvolumenode ) {
  140. if ( currvolumenode.nodeName === "metadata" ) {
  141. if ( currvolumenode.attributes[ 'type' ] !== undefined ) {
  142. if ( currvolumenode.attributes[ 'type' ].value === 'name' ) {
  143. volume.name = currvolumenode.textContent;
  144. }
  145. }
  146. } else if ( currvolumenode.nodeName === "triangle" ) {
  147. var trianglenode = currvolumenode.firstElementChild;
  148. while ( trianglenode ) {
  149. if ( trianglenode.nodeName === "v1" ||
  150. trianglenode.nodeName === "v2" ||
  151. trianglenode.nodeName === "v3" ) {
  152. volume.triangles.push( trianglenode.textContent );
  153. }
  154. trianglenode = trianglenode.nextElementSibling;
  155. }
  156. }
  157. currvolumenode = currvolumenode.nextElementSibling;
  158. }
  159. return volume;
  160. }
  161. function loadMeshVertices( node ) {
  162. var vert_array = [];
  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 coordnode = vnode.firstElementChild;
  170. while ( coordnode ) {
  171. if ( coordnode.nodeName === "x" ||
  172. coordnode.nodeName === "y" ||
  173. coordnode.nodeName === "z" ) {
  174. vert_array.push( coordnode.textContent );
  175. }
  176. coordnode = coordnode.nextElementSibling;
  177. }
  178. }
  179. vnode = vnode.nextElementSibling;
  180. }
  181. }
  182. currverticesnode = currverticesnode.nextElementSibling;
  183. }
  184. return vert_array;
  185. }
  186. function loadObject( node ) {
  187. "use strict";
  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": [], "volumes": [], "color": currcolor };
  204. while ( currmeshnode ) {
  205. if ( currmeshnode.nodeName === "vertices" ) {
  206. mesh.vertices = mesh.vertices.concat( loadMeshVertices( currmeshnode ) );
  207. } else if ( currmeshnode.nodeName === "volume" ) {
  208. mesh.volumes.push( loadMeshVolume( currmeshnode ) );
  209. }
  210. currmeshnode = currmeshnode.nextElementSibling;
  211. }
  212. loadedobject.meshes.push( mesh );
  213. }
  214. currobjnode = currobjnode.nextElementSibling;
  215. }
  216. return { 'id': objid, 'obj': loadedobject };
  217. }
  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 children = xmldata.documentElement.children;
  226. for ( var i = 0; i < children.length; i ++ ) {
  227. var child = children[ i ];
  228. if ( child.nodeName === 'metadata' ) {
  229. if ( child.attributes[ 'type' ] !== undefined ) {
  230. if ( child.attributes[ 'type' ].value === 'name' ) {
  231. amfName = child.textContent;
  232. } else if ( child.attributes[ 'type' ].value === 'author' ) {
  233. amfAuthor = child.textContent;
  234. }
  235. }
  236. } else if ( child.nodeName === 'material' ) {
  237. var loadedmaterial = loadMaterials( child );
  238. amfMaterials[ loadedmaterial.id ] = loadedmaterial.material;
  239. } else if ( child.nodeName === 'object' ) {
  240. var loadedobject = loadObject( child );
  241. amfObjects[ loadedobject.id ] = loadedobject.obj;
  242. }
  243. }
  244. var sceneobject = new THREE.Group();
  245. sceneobject.name = amfName;
  246. sceneobject.userData.author = amfAuthor;
  247. sceneobject.userData.loader = "AMF";
  248. var defaultmaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaff, shading: THREE.FlatShading } );
  249. for ( var id in amfObjects ) {
  250. var meshes = amfObjects[ id ].meshes;
  251. var newobject = new THREE.Group();
  252. for ( var i = 0; i < meshes.length; i ++ ) {
  253. var mesh = meshes[ i ];
  254. var meshvertices = Float32Array.from( mesh.vertices );
  255. var vertices = new THREE.BufferAttribute( Float32Array.from( meshvertices ), 3 );
  256. var objdefaultmaterial = defaultmaterial;
  257. if ( mesh.color ) {
  258. var color = mesh.color;
  259. objdefaultmaterial = defaultmaterial.clone();
  260. objdefaultmaterial.color = new THREE.Color( color.r, color.g, color.b );
  261. if ( color.a !== 1.0 ) {
  262. objdefaultmaterial.transparent = true;
  263. objdefaultmaterial.opacity = color.a;
  264. }
  265. }
  266. var volumes = mesh.volumes;
  267. for ( var j = 0; j < volumes.length; j ++ ) {
  268. var volume = volumes[ j ];
  269. var newgeometry = new THREE.BufferGeometry();
  270. var indexes = Uint32Array.from( volume.triangles );
  271. var normals = new Uint32Array( vertices.array.length );
  272. var material = objdefaultmaterial;
  273. newgeometry.setIndex( new THREE.BufferAttribute( indexes, 1 ) );
  274. newgeometry.addAttribute( 'position', vertices.clone() );
  275. if ( amfMaterials[ volume.materialid ] !== undefined ) {
  276. material = amfMaterials[ volume.materialid ];
  277. }
  278. newgeometry.scale( amfScale, amfScale, amfScale );
  279. newobject.add( new THREE.Mesh( newgeometry, material.clone() ) );
  280. }
  281. }
  282. sceneobject.add( newobject );
  283. }
  284. return sceneobject;
  285. }
  286. };