AMFLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. 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( "Loading Zip" );
  43. try {
  44. zip = new JSZip( data );
  45. } catch ( e ) {
  46. if ( e instanceof ReferenceError ) {
  47. console.log( " 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( " Trying to load file asset: " + file );
  57. view = new DataView( zip.file( file ).asArrayBuffer() );
  58. }
  59. if ( TextDecoder === undefined ) {
  60. console.log( " TextDecoder not present. Please use TextDecoder polyfill." );
  61. return null;
  62. }
  63. var fileText = new TextDecoder( 'utf-8' ).decode( view );
  64. var xmlData = new DOMParser().parseFromString( fileText, 'application/xml' );
  65. if ( xmlData.documentElement.nodeName.toLowerCase() !== "amf" ) {
  66. console.log( " Error loading AMF - no AMF document found." );
  67. return null;
  68. }
  69. return xmlData;
  70. }
  71. function loadDocumentScale( node ) {
  72. var scale = 1.0;
  73. var unit = 'millimeter';
  74. if ( node.documentElement.attributes[ 'unit' ] !== undefined ) {
  75. unit = node.documentElement.attributes[ 'unit' ].value.toLowerCase();
  76. }
  77. var scaleUnits = {
  78. 'millimeter': 1.0,
  79. 'inch': 25.4,
  80. 'feet': 304.8,
  81. 'meter': 1000.0,
  82. 'micron': 0.001
  83. };
  84. if ( scaleUnits[ unit ] !== undefined ) {
  85. scale = scaleUnits[ unit ];
  86. }
  87. console.log( " Unit scale: " + scale );
  88. return scale;
  89. }
  90. function loadMaterials( node ) {
  91. var matName = "AMF Material";
  92. var matId = node.attributes[ 'id' ].textContent;
  93. var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  94. var loadedMaterial = null;
  95. for ( var i = 0; i < node.children.length; i ++ ) {
  96. var matChildEl = node.children[ i ];
  97. if ( matChildEl.nodeName === "metadata" && matChildEl.attributes[ 'type' ] !== undefined ) {
  98. if ( matChildEl.attributes[ 'type' ].value === 'name' ) {
  99. matname = matChildEl.textContent;
  100. }
  101. } else if ( matChildEl.nodeName === 'color' ) {
  102. color = loadColor( matChildEl );
  103. }
  104. }
  105. loadedMaterial = new THREE.MeshPhongMaterial( {
  106. shading: THREE.FlatShading,
  107. color: new THREE.Color( color.r, color.g, color.b ),
  108. name: matName
  109. } );
  110. if ( color.a !== 1.0 ) {
  111. loadedMaterial.transparent = true;
  112. loadedMaterial.opacity = color.a;
  113. }
  114. return { 'id': matId, 'material': loadedMaterial };
  115. }
  116. function loadColor( node ) {
  117. var color = { 'r': 1.0, 'g': 1.0, 'b': 1.0, 'a': 1.0 };
  118. for ( var i = 0; i < node.children.length; i ++ ) {
  119. var matColor = node.children[ i ];
  120. if ( matColor.nodeName === 'r' ) {
  121. color.r = matColor.textContent;
  122. } else if ( matColor.nodeName === 'g' ) {
  123. color.g = matColor.textContent;
  124. } else if ( matColor.nodeName === 'b' ) {
  125. color.b = matColor.textContent;
  126. } else if ( matColor.nodeName === 'a' ) {
  127. color.a = matColor.textContent;
  128. }
  129. }
  130. return color;
  131. }
  132. function loadMeshVolume( node ) {
  133. var volume = { "name": "", "triangles": [], "materialid": null };
  134. var currVolumeNode = node.firstElementChild;
  135. if ( node.attributes[ 'materialid' ] !== undefined ) {
  136. volume.materialId = node.attributes[ 'materialid' ].nodeValue;
  137. }
  138. while ( currVolumeNode ) {
  139. if ( currVolumeNode.nodeName === "metadata" ) {
  140. if ( currVolumeNode.attributes[ 'type' ] !== undefined ) {
  141. if ( currVolumeNode.attributes[ 'type' ].value === 'name' ) {
  142. volume.name = currVolumeNode.textContent;
  143. }
  144. }
  145. } else if ( currVolumeNode.nodeName === "triangle" ) {
  146. var v1 = currVolumeNode.getElementsByTagName("v1")[0].textContent;
  147. var v2 = currVolumeNode.getElementsByTagName("v2")[0].textContent;
  148. var v3 = currVolumeNode.getElementsByTagName("v3")[0].textContent;
  149. volume.triangles.push( v1 );
  150. volume.triangles.push( v2 );
  151. volume.triangles.push( v3 );
  152. }
  153. currVolumeNode = currVolumeNode.nextElementSibling;
  154. }
  155. return volume;
  156. }
  157. function loadMeshVertices( node ) {
  158. var vertArray = [];
  159. var normalArray = [];
  160. var currVerticesNode = node.firstElementChild;
  161. while ( currVerticesNode ) {
  162. if ( currVerticesNode.nodeName === "vertex" ) {
  163. var vNode = currVerticesNode.firstElementChild;
  164. while ( vNode ) {
  165. if ( vNode.nodeName === "coordinates" ) {
  166. var x = vNode.getElementsByTagName("x")[0].textContent;
  167. var y = vNode.getElementsByTagName("y")[0].textContent;
  168. var z = vNode.getElementsByTagName("z")[0].textContent;
  169. vertArray.push(x);
  170. vertArray.push(y);
  171. vertArray.push(z);
  172. } else if ( vNode.nodeName === "normal" ) {
  173. var nx = vNode.getElementsByTagName("nx")[0].textContent;
  174. var ny = vNode.getElementsByTagName("ny")[0].textContent;
  175. var nz = vNode.getElementsByTagName("nz")[0].textContent;
  176. normalArray.push(nx);
  177. normalArray.push(ny);
  178. normalArray.push(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 children = xmlData.documentElement.children;
  227. for ( var i = 0; i < children.length; i ++ ) {
  228. var child = children[ 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, shading: THREE.FlatShading } );
  247. sceneObject.name = amfName;
  248. sceneObject.userData.author = amfAuthor;
  249. sceneObject.userData.loader = "AMF";
  250. for ( var id in amfObjects ) {
  251. var meshes = amfObjects[ id ].meshes;
  252. var newObject = new THREE.Group();
  253. for ( var i = 0; i < meshes.length; i ++ ) {
  254. var objDefaultMaterial = defaultMaterial;
  255. var mesh = meshes[ i ];
  256. var meshVertices = Float32Array.from( mesh.vertices );
  257. var vertices = new THREE.BufferAttribute( Float32Array.from( meshVertices ), 3 );
  258. var meshNormals = null;
  259. var normals = null;
  260. if ( mesh.normals.length ) {
  261. meshNormals = Float32Array.from( mesh.normals );
  262. normals = new THREE.BufferAttribute( Float32Array.from( meshNormals ), 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 ( var j = 0; j < volumes.length; j ++ ) {
  275. var volume = volumes[ j ];
  276. var newGeometry = new THREE.BufferGeometry();
  277. var indexes = Uint32Array.from( volume.triangles );
  278. var material = objDefaultMaterial;
  279. newGeometry.setIndex( new THREE.BufferAttribute( indexes, 1 ) );
  280. newGeometry.addAttribute( 'position', vertices.clone() );
  281. if( normals ) {
  282. newGeometry.addAttribute( 'normal', normals.clone() );
  283. }
  284. if ( amfMaterials[ volume.materialId ] !== undefined ) {
  285. material = amfMaterials[ volume.materialId ];
  286. }
  287. newGeometry.scale( amfScale, amfScale, amfScale );
  288. newObject.add( new THREE.Mesh( newGeometry, material.clone() ) );
  289. }
  290. }
  291. sceneObject.add( newObject );
  292. }
  293. return sceneObject;
  294. }
  295. };