AMFLoader.js 10 KB

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