AMFLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. var zip = null;
  42. var file = null;
  43. console.log( "Loading Zip" );
  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( node ) {
  73. var scale = 1.0;
  74. var unit = 'millimeter';
  75. if ( node.documentElement.attributes[ 'unit' ] !== undefined ) {
  76. unit = node.documentElement.attributes[ 'unit' ].value.toLowerCase();
  77. }
  78. var scaleUnits = {
  79. 'millimeter': 1.0,
  80. 'inch': 25.4,
  81. 'feet': 304.8,
  82. 'meter': 1000.0,
  83. 'micron': 0.001
  84. };
  85. if ( scaleUnits[ unit ] !== undefined ) {
  86. scale = scaleUnits[ unit ];
  87. }
  88. console.log( " Unit scale: " + scale );
  89. return scale;
  90. }
  91. function loadMaterials( node ) {
  92. var matName = "AMF Material";
  93. var matId = node.attributes[ 'id' ].textContent;
  94. var color;
  95. var loadedMaterial = null;
  96. for ( var i = 0; i < node.children.length; i ++ ) {
  97. var matChildEl = node.children[ i ];
  98. if ( matChildEl.nodeName === "metadata" && matChildEl.attributes[ 'type' ] !== undefined ) {
  99. if ( matChildEl.attributes[ 'type' ].value === 'name' ) {
  100. matname = matChildEl.textContent;
  101. }
  102. } else if ( matChildEl.nodeName === 'color' ) {
  103. color = loadColor( matChildEl );
  104. }
  105. }
  106. loadedMaterial = new THREE.MeshPhongMaterial( {
  107. shading: THREE.FlatShading,
  108. color: new THREE.Color( color.r, color.g, color.b ),
  109. name: matName
  110. } );
  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 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 coordNode = vNode.firstElementChild;
  171. while ( coordNode ) {
  172. if ( coordNode.nodeName === "x" ||
  173. coordNode.nodeName === "y" ||
  174. coordNode.nodeName === "z" ) {
  175. vertArray.push( coordNode.textContent );
  176. }
  177. coordNode = coordNode.nextElementSibling;
  178. }
  179. } else if ( vNode.nodeName === "normal" ) {
  180. var nx = vNode.getElementsByTagName("nx")[0].textContent;
  181. var ny = vNode.getElementsByTagName("ny")[0].textContent;
  182. var nz = vNode.getElementsByTagName("nz")[0].textContent;
  183. normalArray.push(nx);
  184. normalArray.push(ny);
  185. normalArray.push(nz);
  186. }
  187. vNode = vNode.nextElementSibling;
  188. }
  189. }
  190. currVerticesNode = currVerticesNode.nextElementSibling;
  191. }
  192. return { "vertices": vertArray, "normals": normalArray };
  193. }
  194. function loadObject( node ) {
  195. var objId = node.attributes[ 'id' ].textContent;
  196. var loadedObject = { "name": "amfobject", "meshes": [] };
  197. var currColor = null;
  198. var currObjNode = node.firstElementChild;
  199. while ( currObjNode ) {
  200. if ( currObjNode.nodeName === "metadata" ) {
  201. if ( currObjNode.attributes[ 'type' ] !== undefined ) {
  202. if ( currObjNode.attributes[ 'type' ].value === 'name' ) {
  203. loadedObject.name = currObjNode.textContent;
  204. }
  205. }
  206. } else if ( currObjNode.nodeName === "color" ) {
  207. currColor = loadColor( currObjNode );
  208. } else if ( currObjNode.nodeName === "mesh" ) {
  209. var currMeshNode = currObjNode.firstElementChild;
  210. var mesh = { "vertices": [], "normals": [], "volumes": [], "color": currColor };
  211. while ( currMeshNode ) {
  212. if ( currMeshNode.nodeName === "vertices" ) {
  213. var loadedVertices = loadMeshVertices( currMeshNode );
  214. mesh.normals = mesh.normals.concat( loadedVertices.normals );
  215. mesh.vertices = mesh.vertices.concat( loadedVertices.vertices );
  216. } else if ( currMeshNode.nodeName === "volume" ) {
  217. mesh.volumes.push( loadMeshVolume( currMeshNode ) );
  218. }
  219. currMeshNode = currMeshNode.nextElementSibling;
  220. }
  221. loadedObject.meshes.push( mesh );
  222. }
  223. currObjNode = currObjNode.nextElementSibling;
  224. }
  225. return { 'id': objId, 'obj': loadedObject };
  226. }
  227. var xmlData = loadDocument( data );
  228. var amfName = "";
  229. var amfAuthor = "";
  230. var amfScale = loadDocumentScale( xmlData );
  231. var amfMaterials = {};
  232. var amfObjects = {};
  233. var children = xmlData.documentElement.children;
  234. for ( var i = 0; i < children.length; i ++ ) {
  235. var child = children[ i ];
  236. if ( child.nodeName === 'metadata' ) {
  237. if ( child.attributes[ 'type' ] !== undefined ) {
  238. if ( child.attributes[ 'type' ].value === 'name' ) {
  239. amfName = child.textContent;
  240. } else if ( child.attributes[ 'type' ].value === 'author' ) {
  241. amfAuthor = child.textContent;
  242. }
  243. }
  244. } else if ( child.nodeName === 'material' ) {
  245. var loadedMaterial = loadMaterials( child );
  246. amfMaterials[ loadedMaterial.id ] = loadedMaterial.material;
  247. } else if ( child.nodeName === 'object' ) {
  248. var loadedObject = loadObject( child );
  249. amfObjects[ loadedObject.id ] = loadedObject.obj;
  250. }
  251. }
  252. var sceneObject = new THREE.Group();
  253. var defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaff, shading: THREE.FlatShading } );
  254. sceneObject.name = amfName;
  255. sceneObject.userData.author = amfAuthor;
  256. sceneObject.userData.loader = "AMF";
  257. for ( var id in amfObjects ) {
  258. var meshes = amfObjects[ id ].meshes;
  259. var newObject = new THREE.Group();
  260. for ( var i = 0; i < meshes.length; i ++ ) {
  261. var objDefaultMaterial = defaultMaterial;
  262. var mesh = meshes[ i ];
  263. var meshVertices = Float32Array.from( mesh.vertices );
  264. var vertices = new THREE.BufferAttribute( Float32Array.from( meshVertices ), 3 );
  265. var meshNormals = null;
  266. var normals = null;
  267. if ( mesh.normals.length ) {
  268. meshNormals = Float32Array.from( mesh.normals );
  269. normals = new THREE.BufferAttribute( Float32Array.from( meshNormals ), 3 );
  270. }
  271. if ( mesh.color ) {
  272. var color = mesh.color;
  273. objDefaultMaterial = defaultMaterial.clone();
  274. objDefaultMaterial.color = new THREE.Color( color.r, color.g, color.b );
  275. if ( color.a !== 1.0 ) {
  276. objDefaultMaterial.transparent = true;
  277. objDefaultMaterial.opacity = color.a;
  278. }
  279. }
  280. var volumes = mesh.volumes;
  281. for ( var j = 0; j < volumes.length; j ++ ) {
  282. var volume = volumes[ j ];
  283. var newGeometry = new THREE.BufferGeometry();
  284. var indexes = Uint32Array.from( volume.triangles );
  285. var material = objDefaultMaterial;
  286. newGeometry.setIndex( new THREE.BufferAttribute( indexes, 1 ) );
  287. newGeometry.addAttribute( 'position', vertices.clone() );
  288. if( normals ) {
  289. newGeometry.addAttribute( 'normal', normals.clone() );
  290. }
  291. if ( amfMaterials[ volume.materialid ] !== undefined ) {
  292. material = amfMaterials[ volume.materialid ];
  293. }
  294. newGeometry.scale( amfScale, amfScale, amfScale );
  295. newObject.add( new THREE.Mesh( newGeometry, material.clone() ) );
  296. }
  297. }
  298. sceneObject.add( newObject );
  299. }
  300. return sceneObject;
  301. }
  302. };