AMFLoader.js 10 KB

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