AMFLoader.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /**
  2. * Description: Early release of an AMF Loader following the pattern of the
  3. * example loaders in the three.js project.
  4. *
  5. * More information about the AMF format: http://amf.wikispaces.com
  6. *
  7. * Usage:
  8. * var loader = new AMFLoader();
  9. * loader.load('/path/to/project.amf', function(objecttree) {
  10. * scene.add(objecttree);
  11. * });
  12. *
  13. * Materials now supported, material colors supported
  14. * Zip support, requires fflate
  15. * No constellation support (yet)!
  16. *
  17. */
  18. THREE.AMFLoader = function ( manager ) {
  19. THREE.Loader.call( this, manager );
  20. };
  21. THREE.AMFLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
  22. constructor: THREE.AMFLoader,
  23. load: function ( url, onLoad, onProgress, onError ) {
  24. var scope = this;
  25. var loader = new THREE.FileLoader( scope.manager );
  26. loader.setPath( scope.path );
  27. loader.setResponseType( 'arraybuffer' );
  28. loader.setRequestHeader( scope.requestHeader );
  29. loader.setWithCredentials( scope.withCredentials );
  30. loader.load( url, function ( text ) {
  31. try {
  32. onLoad( scope.parse( text ) );
  33. } catch ( e ) {
  34. if ( onError ) {
  35. onError( e );
  36. } else {
  37. console.error( e );
  38. }
  39. scope.manager.itemError( url );
  40. }
  41. }, onProgress, onError );
  42. },
  43. parse: function ( data ) {
  44. function loadDocument( data ) {
  45. var view = new DataView( data );
  46. var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) );
  47. if ( magic === 'PK' ) {
  48. var zip = null;
  49. var file = null;
  50. console.log( 'THREE.AMFLoader: Loading Zip' );
  51. try {
  52. zip = fflate.unzipSync( new Uint8Array( data ) ); // eslint-disable-line no-undef
  53. } catch ( e ) {
  54. if ( e instanceof ReferenceError ) {
  55. console.log( 'THREE.AMFLoader: fflate missing and file is compressed.' );
  56. return null;
  57. }
  58. }
  59. for ( var file in zip ) {
  60. if ( file.toLowerCase().substr( - 4 ) === '.amf' ) {
  61. break;
  62. }
  63. }
  64. console.log( 'THREE.AMFLoader: Trying to load file asset: ' + file );
  65. view = new DataView( zip[ file ].buffer );
  66. }
  67. var fileText = THREE.LoaderUtils.decodeText( view );
  68. var xmlData = new DOMParser().parseFromString( fileText, 'application/xml' );
  69. if ( xmlData.documentElement.nodeName.toLowerCase() !== 'amf' ) {
  70. console.log( 'THREE.AMFLoader: Error loading AMF - no AMF document found.' );
  71. return null;
  72. }
  73. return xmlData;
  74. }
  75. function loadDocumentScale( node ) {
  76. var scale = 1.0;
  77. var unit = 'millimeter';
  78. if ( node.documentElement.attributes.unit !== undefined ) {
  79. unit = node.documentElement.attributes.unit.value.toLowerCase();
  80. }
  81. var scaleUnits = {
  82. millimeter: 1.0,
  83. inch: 25.4,
  84. feet: 304.8,
  85. meter: 1000.0,
  86. micron: 0.001
  87. };
  88. if ( scaleUnits[ unit ] !== undefined ) {
  89. scale = scaleUnits[ unit ];
  90. }
  91. console.log( 'THREE.AMFLoader: Unit scale: ' + scale );
  92. return scale;
  93. }
  94. function loadMaterials( node ) {
  95. var matName = 'AMF Material';
  96. var matId = node.attributes.id.textContent;
  97. var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  98. var loadedMaterial = null;
  99. for ( var i = 0; i < node.childNodes.length; i ++ ) {
  100. var matChildEl = node.childNodes[ i ];
  101. if ( matChildEl.nodeName === 'metadata' && matChildEl.attributes.type !== undefined ) {
  102. if ( matChildEl.attributes.type.value === 'name' ) {
  103. matName = matChildEl.textContent;
  104. }
  105. } else if ( matChildEl.nodeName === 'color' ) {
  106. color = loadColor( matChildEl );
  107. }
  108. }
  109. loadedMaterial = new THREE.MeshPhongMaterial( {
  110. flatShading: true,
  111. color: new THREE.Color( color.r, color.g, color.b ),
  112. name: matName
  113. } );
  114. if ( color.a !== 1.0 ) {
  115. loadedMaterial.transparent = true;
  116. loadedMaterial.opacity = color.a;
  117. }
  118. return { id: matId, material: loadedMaterial };
  119. }
  120. function loadColor( node ) {
  121. var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
  122. for ( var i = 0; i < node.childNodes.length; i ++ ) {
  123. var matColor = node.childNodes[ i ];
  124. if ( matColor.nodeName === 'r' ) {
  125. color.r = matColor.textContent;
  126. } else if ( matColor.nodeName === 'g' ) {
  127. color.g = matColor.textContent;
  128. } else if ( matColor.nodeName === 'b' ) {
  129. color.b = matColor.textContent;
  130. } else if ( matColor.nodeName === 'a' ) {
  131. color.a = matColor.textContent;
  132. }
  133. }
  134. return color;
  135. }
  136. function loadMeshVolume( node ) {
  137. var volume = { name: '', triangles: [], materialid: null };
  138. var currVolumeNode = node.firstElementChild;
  139. if ( node.attributes.materialid !== undefined ) {
  140. volume.materialId = node.attributes.materialid.nodeValue;
  141. }
  142. while ( currVolumeNode ) {
  143. if ( currVolumeNode.nodeName === 'metadata' ) {
  144. if ( currVolumeNode.attributes.type !== undefined ) {
  145. if ( currVolumeNode.attributes.type.value === 'name' ) {
  146. volume.name = currVolumeNode.textContent;
  147. }
  148. }
  149. } else if ( currVolumeNode.nodeName === 'triangle' ) {
  150. var v1 = currVolumeNode.getElementsByTagName( 'v1' )[ 0 ].textContent;
  151. var v2 = currVolumeNode.getElementsByTagName( 'v2' )[ 0 ].textContent;
  152. var v3 = currVolumeNode.getElementsByTagName( 'v3' )[ 0 ].textContent;
  153. volume.triangles.push( v1, v2, v3 );
  154. }
  155. currVolumeNode = currVolumeNode.nextElementSibling;
  156. }
  157. return volume;
  158. }
  159. function loadMeshVertices( node ) {
  160. var vertArray = [];
  161. var normalArray = [];
  162. var currVerticesNode = node.firstElementChild;
  163. while ( currVerticesNode ) {
  164. if ( currVerticesNode.nodeName === 'vertex' ) {
  165. var vNode = currVerticesNode.firstElementChild;
  166. while ( vNode ) {
  167. if ( vNode.nodeName === 'coordinates' ) {
  168. var x = vNode.getElementsByTagName( 'x' )[ 0 ].textContent;
  169. var y = vNode.getElementsByTagName( 'y' )[ 0 ].textContent;
  170. var z = vNode.getElementsByTagName( 'z' )[ 0 ].textContent;
  171. vertArray.push( x, y, 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, ny, nz );
  177. }
  178. vNode = vNode.nextElementSibling;
  179. }
  180. }
  181. currVerticesNode = currVerticesNode.nextElementSibling;
  182. }
  183. return { 'vertices': vertArray, 'normals': normalArray };
  184. }
  185. function loadObject( node ) {
  186. var objId = node.attributes.id.textContent;
  187. var loadedObject = { name: 'amfobject', meshes: [] };
  188. var currColor = null;
  189. var currObjNode = node.firstElementChild;
  190. while ( currObjNode ) {
  191. if ( currObjNode.nodeName === 'metadata' ) {
  192. if ( currObjNode.attributes.type !== undefined ) {
  193. if ( currObjNode.attributes.type.value === 'name' ) {
  194. loadedObject.name = currObjNode.textContent;
  195. }
  196. }
  197. } else if ( currObjNode.nodeName === 'color' ) {
  198. currColor = loadColor( currObjNode );
  199. } else if ( currObjNode.nodeName === 'mesh' ) {
  200. var currMeshNode = currObjNode.firstElementChild;
  201. var mesh = { vertices: [], normals: [], volumes: [], color: currColor };
  202. while ( currMeshNode ) {
  203. if ( currMeshNode.nodeName === 'vertices' ) {
  204. var loadedVertices = loadMeshVertices( currMeshNode );
  205. mesh.normals = mesh.normals.concat( loadedVertices.normals );
  206. mesh.vertices = mesh.vertices.concat( loadedVertices.vertices );
  207. } else if ( currMeshNode.nodeName === 'volume' ) {
  208. mesh.volumes.push( loadMeshVolume( currMeshNode ) );
  209. }
  210. currMeshNode = currMeshNode.nextElementSibling;
  211. }
  212. loadedObject.meshes.push( mesh );
  213. }
  214. currObjNode = currObjNode.nextElementSibling;
  215. }
  216. return { 'id': objId, 'obj': loadedObject };
  217. }
  218. var xmlData = loadDocument( data );
  219. var amfName = '';
  220. var amfAuthor = '';
  221. var amfScale = loadDocumentScale( xmlData );
  222. var amfMaterials = {};
  223. var amfObjects = {};
  224. var childNodes = xmlData.documentElement.childNodes;
  225. var i, j;
  226. for ( i = 0; i < childNodes.length; i ++ ) {
  227. var child = childNodes[ 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, flatShading: true } );
  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 ( i = 0; i < meshes.length; i ++ ) {
  255. var objDefaultMaterial = defaultMaterial;
  256. var mesh = meshes[ i ];
  257. var vertices = new THREE.Float32BufferAttribute( mesh.vertices, 3 );
  258. var normals = null;
  259. if ( mesh.normals.length ) {
  260. normals = new THREE.Float32BufferAttribute( 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 ( j = 0; j < volumes.length; j ++ ) {
  273. var volume = volumes[ j ];
  274. var newGeometry = new THREE.BufferGeometry();
  275. var material = objDefaultMaterial;
  276. newGeometry.setIndex( volume.triangles );
  277. newGeometry.setAttribute( 'position', vertices.clone() );
  278. if ( normals ) {
  279. newGeometry.setAttribute( 'normal', normals.clone() );
  280. }
  281. if ( amfMaterials[ volume.materialId ] !== undefined ) {
  282. material = amfMaterials[ volume.materialId ];
  283. }
  284. newGeometry.scale( amfScale, amfScale, amfScale );
  285. newObject.add( new THREE.Mesh( newGeometry, material.clone() ) );
  286. }
  287. }
  288. sceneObject.add( newObject );
  289. }
  290. return sceneObject;
  291. }
  292. } );