AMFLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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.FileLoader( scope.manager );
  29. loader.setResponseType( 'arraybuffer' );
  30. loader.load( url, function( text ) {
  31. onLoad( scope.parse( text ) );
  32. }, onProgress, onError );
  33. },
  34. parse: function ( data ) {
  35. function loadDocument( data ) {
  36. var view = new DataView( data );
  37. var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) );
  38. if ( magic === "PK" ) {
  39. var zip = null;
  40. var file = null;
  41. console.log( "Loading Zip" );
  42. try {
  43. zip = new JSZip( data );
  44. } catch ( e ) {
  45. if ( e instanceof ReferenceError ) {
  46. console.log( " jszip missing and file is compressed." );
  47. return null;
  48. }
  49. }
  50. for ( file in zip.files ) {
  51. if ( file.toLowerCase().substr( - 4 ) === '.amf' ) {
  52. break;
  53. }
  54. }
  55. console.log( " Trying to load file asset: " + file );
  56. view = new DataView( zip.file( file ).asArrayBuffer() );
  57. }
  58. if ( TextDecoder === undefined ) {
  59. console.log( " TextDecoder not present. Please use TextDecoder polyfill." );
  60. return null;
  61. }
  62. var fileText = new TextDecoder( 'utf-8' ).decode( view );
  63. var xmlData = new DOMParser().parseFromString( fileText, 'application/xml' );
  64. if ( xmlData.documentElement.nodeName.toLowerCase() !== "amf" ) {
  65. console.log( " Error loading AMF - no AMF document found." );
  66. return null;
  67. }
  68. return xmlData;
  69. }
  70. function loadDocumentScale( node ) {
  71. var scale = 1.0;
  72. var unit = 'millimeter';
  73. if ( node.documentElement.attributes[ 'unit' ] !== undefined ) {
  74. unit = node.documentElement.attributes[ 'unit' ].value.toLowerCase();
  75. }
  76. var scaleUnits = {
  77. 'millimeter': 1.0,
  78. 'inch': 25.4,
  79. 'feet': 304.8,
  80. 'meter': 1000.0,
  81. 'micron': 0.001
  82. };
  83. if ( scaleUnits[ unit ] !== undefined ) {
  84. scale = scaleUnits[ unit ];
  85. }
  86. console.log( " Unit scale: " + scale );
  87. return scale;
  88. }
  89. function loadMaterials( node ) {
  90. var matName = "AMF Material";
  91. var matId = node.attributes[ 'id' ].textContent;
  92. var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  93. var loadedMaterial = null;
  94. for ( var i = 0; i < node.children.length; i ++ ) {
  95. var matChildEl = node.children[ i ];
  96. if ( matChildEl.nodeName === "metadata" && matChildEl.attributes[ 'type' ] !== undefined ) {
  97. if ( matChildEl.attributes[ 'type' ].value === 'name' ) {
  98. matname = matChildEl.textContent;
  99. }
  100. } else if ( matChildEl.nodeName === 'color' ) {
  101. color = loadColor( matChildEl );
  102. }
  103. }
  104. loadedMaterial = new THREE.MeshPhongMaterial( {
  105. shading: THREE.FlatShading,
  106. color: new THREE.Color( color.r, color.g, color.b ),
  107. name: matName
  108. } );
  109. if ( color.a !== 1.0 ) {
  110. loadedMaterial.transparent = true;
  111. loadedMaterial.opacity = color.a;
  112. }
  113. return { 'id': matId, 'material': loadedMaterial };
  114. }
  115. function loadColor( node ) {
  116. var color = { 'r': 1.0, 'g': 1.0, 'b': 1.0, 'a': 1.0 };
  117. for ( var i = 0; i < node.children.length; i ++ ) {
  118. var matColor = node.children[ i ];
  119. if ( matColor.nodeName === 'r' ) {
  120. color.r = matColor.textContent;
  121. } else if ( matColor.nodeName === 'g' ) {
  122. color.g = matColor.textContent;
  123. } else if ( matColor.nodeName === 'b' ) {
  124. color.b = matColor.textContent;
  125. } else if ( matColor.nodeName === 'a' ) {
  126. color.a = matColor.textContent;
  127. }
  128. }
  129. return color;
  130. }
  131. function loadMeshVolume( node ) {
  132. var volume = { "name": "", "triangles": [], "materialid": null };
  133. var currVolumeNode = node.firstElementChild;
  134. if ( node.attributes[ 'materialid' ] !== undefined ) {
  135. volume.materialId = node.attributes[ 'materialid' ].nodeValue;
  136. }
  137. while ( currVolumeNode ) {
  138. if ( currVolumeNode.nodeName === "metadata" ) {
  139. if ( currVolumeNode.attributes[ 'type' ] !== undefined ) {
  140. if ( currVolumeNode.attributes[ 'type' ].value === 'name' ) {
  141. volume.name = currVolumeNode.textContent;
  142. }
  143. }
  144. } else if ( currVolumeNode.nodeName === "triangle" ) {
  145. var v1 = currVolumeNode.getElementsByTagName("v1")[0].textContent;
  146. var v2 = currVolumeNode.getElementsByTagName("v2")[0].textContent;
  147. var v3 = currVolumeNode.getElementsByTagName("v3")[0].textContent;
  148. volume.triangles.push( v1 );
  149. volume.triangles.push( v2 );
  150. volume.triangles.push( v3 );
  151. }
  152. currVolumeNode = currVolumeNode.nextElementSibling;
  153. }
  154. return volume;
  155. }
  156. function loadMeshVertices( node ) {
  157. var vertArray = [];
  158. var normalArray = [];
  159. var currVerticesNode = node.firstElementChild;
  160. while ( currVerticesNode ) {
  161. if ( currVerticesNode.nodeName === "vertex" ) {
  162. var vNode = currVerticesNode.firstElementChild;
  163. while ( vNode ) {
  164. if ( vNode.nodeName === "coordinates" ) {
  165. var x = vNode.getElementsByTagName("x")[0].textContent;
  166. var y = vNode.getElementsByTagName("y")[0].textContent;
  167. var z = vNode.getElementsByTagName("z")[0].textContent;
  168. vertArray.push(x);
  169. vertArray.push(y);
  170. vertArray.push(z);
  171. } else if ( vNode.nodeName === "normal" ) {
  172. var nx = vNode.getElementsByTagName("nx")[0].textContent;
  173. var ny = vNode.getElementsByTagName("ny")[0].textContent;
  174. var nz = vNode.getElementsByTagName("nz")[0].textContent;
  175. normalArray.push(nx);
  176. normalArray.push(ny);
  177. normalArray.push(nz);
  178. }
  179. vNode = vNode.nextElementSibling;
  180. }
  181. }
  182. currVerticesNode = currVerticesNode.nextElementSibling;
  183. }
  184. return { "vertices": vertArray, "normals": normalArray };
  185. }
  186. function loadObject( node ) {
  187. var objId = node.attributes[ 'id' ].textContent;
  188. var loadedObject = { "name": "amfobject", "meshes": [] };
  189. var currColor = null;
  190. var currObjNode = node.firstElementChild;
  191. while ( currObjNode ) {
  192. if ( currObjNode.nodeName === "metadata" ) {
  193. if ( currObjNode.attributes[ 'type' ] !== undefined ) {
  194. if ( currObjNode.attributes[ 'type' ].value === 'name' ) {
  195. loadedObject.name = currObjNode.textContent;
  196. }
  197. }
  198. } else if ( currObjNode.nodeName === "color" ) {
  199. currColor = loadColor( currObjNode );
  200. } else if ( currObjNode.nodeName === "mesh" ) {
  201. var currMeshNode = currObjNode.firstElementChild;
  202. var mesh = { "vertices": [], "normals": [], "volumes": [], "color": currColor };
  203. while ( currMeshNode ) {
  204. if ( currMeshNode.nodeName === "vertices" ) {
  205. var loadedVertices = loadMeshVertices( currMeshNode );
  206. mesh.normals = mesh.normals.concat( loadedVertices.normals );
  207. mesh.vertices = mesh.vertices.concat( loadedVertices.vertices );
  208. } else if ( currMeshNode.nodeName === "volume" ) {
  209. mesh.volumes.push( loadMeshVolume( currMeshNode ) );
  210. }
  211. currMeshNode = currMeshNode.nextElementSibling;
  212. }
  213. loadedObject.meshes.push( mesh );
  214. }
  215. currObjNode = currObjNode.nextElementSibling;
  216. }
  217. return { 'id': objId, 'obj': loadedObject };
  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. var defaultMaterial = new THREE.MeshPhongMaterial( { color: 0xaaaaff, shading: THREE.FlatShading } );
  246. sceneObject.name = amfName;
  247. sceneObject.userData.author = amfAuthor;
  248. sceneObject.userData.loader = "AMF";
  249. for ( var id in amfObjects ) {
  250. var part = amfObjects[ id ];
  251. var meshes = part.meshes;
  252. var newObject = new THREE.Group();
  253. newObject.name = part.name || '';
  254. for ( var i = 0; i < meshes.length; i ++ ) {
  255. var objDefaultMaterial = defaultMaterial;
  256. var mesh = meshes[ i ];
  257. var vertices = new THREE.BufferAttribute( new Float32Array( mesh.vertices ), 3 );
  258. var normals = null;
  259. if ( mesh.normals.length ) {
  260. normals = new THREE.BufferAttribute( new Float32Array( mesh.normals ), 3 );
  261. }
  262. if ( mesh.color ) {
  263. var color = mesh.color;
  264. objDefaultMaterial = defaultMaterial.clone();
  265. objDefaultMaterial.color = new THREE.Color( color.r, color.g, color.b );
  266. if ( color.a !== 1.0 ) {
  267. objDefaultMaterial.transparent = true;
  268. objDefaultMaterial.opacity = color.a;
  269. }
  270. }
  271. var volumes = mesh.volumes;
  272. for ( var j = 0; j < volumes.length; j ++ ) {
  273. var volume = volumes[ j ];
  274. var newGeometry = new THREE.BufferGeometry();
  275. var indexes = new Uint32Array( volume.triangles );
  276. var material = objDefaultMaterial;
  277. newGeometry.setIndex( new THREE.BufferAttribute( indexes, 1 ) );
  278. newGeometry.addAttribute( 'position', vertices.clone() );
  279. if( normals ) {
  280. newGeometry.addAttribute( 'normal', normals.clone() );
  281. }
  282. if ( amfMaterials[ volume.materialId ] !== undefined ) {
  283. material = amfMaterials[ volume.materialId ];
  284. }
  285. newGeometry.scale( amfScale, amfScale, amfScale );
  286. newObject.add( new THREE.Mesh( newGeometry, material.clone() ) );
  287. }
  288. }
  289. sceneObject.add( newObject );
  290. }
  291. return sceneObject;
  292. }
  293. };