PLYExporter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /**
  2. * @author Garrett Johnson / http://gkjohnson.github.io/
  3. * https://github.com/gkjohnson/ply-exporter-js
  4. *
  5. * Usage:
  6. * var exporter = new PLYExporter();
  7. *
  8. * // second argument is a list of options
  9. * exporter.parse(mesh, data => console.log(data), { binary: true, excludeAttributes: [ 'color' ] });
  10. *
  11. * Format Definition:
  12. * http://paulbourke.net/dataformats/ply/
  13. */
  14. import {
  15. BufferGeometry,
  16. Matrix3,
  17. Vector3
  18. } from "../../../build/three.module.js";
  19. var PLYExporter = function () {};
  20. PLYExporter.prototype = {
  21. constructor: PLYExporter,
  22. parse: function ( object, onDone, options ) {
  23. if ( onDone && typeof onDone === 'object' ) {
  24. console.warn( 'THREE.PLYExporter: The options parameter is now the third argument to the "parse" function. See the documentation for the new API.' );
  25. options = onDone;
  26. onDone = undefined;
  27. }
  28. // Iterate over the valid meshes in the object
  29. function traverseMeshes( cb ) {
  30. object.traverse( function ( child ) {
  31. if ( child.isMesh === true ) {
  32. var mesh = child;
  33. var geometry = mesh.geometry;
  34. if ( geometry.isGeometry === true ) {
  35. geometry = geomToBufferGeom.get( geometry );
  36. }
  37. if ( geometry.isBufferGeometry === true ) {
  38. if ( geometry.getAttribute( 'position' ) !== undefined ) {
  39. cb( mesh, geometry );
  40. }
  41. }
  42. }
  43. } );
  44. }
  45. // Default options
  46. var defaultOptions = {
  47. binary: false,
  48. excludeAttributes: [] // normal, uv, color, index
  49. };
  50. options = Object.assign( defaultOptions, options );
  51. var excludeAttributes = options.excludeAttributes;
  52. var geomToBufferGeom = new WeakMap();
  53. var includeNormals = false;
  54. var includeColors = false;
  55. var includeUVs = false;
  56. // count the vertices, check which properties are used,
  57. // and cache the BufferGeometry
  58. var vertexCount = 0;
  59. var faceCount = 0;
  60. object.traverse( function ( child ) {
  61. if ( child.isMesh === true ) {
  62. var mesh = child;
  63. var geometry = mesh.geometry;
  64. if ( geometry.isGeometry === true ) {
  65. var bufferGeometry = geomToBufferGeom.get( geometry ) || new BufferGeometry().setFromObject( mesh );
  66. geomToBufferGeom.set( geometry, bufferGeometry );
  67. geometry = bufferGeometry;
  68. }
  69. if ( geometry.isBufferGeometry === true ) {
  70. var vertices = geometry.getAttribute( 'position' );
  71. var normals = geometry.getAttribute( 'normal' );
  72. var uvs = geometry.getAttribute( 'uv' );
  73. var colors = geometry.getAttribute( 'color' );
  74. var indices = geometry.getIndex();
  75. if ( vertices === undefined ) {
  76. return;
  77. }
  78. vertexCount += vertices.count;
  79. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  80. if ( normals !== undefined ) includeNormals = true;
  81. if ( uvs !== undefined ) includeUVs = true;
  82. if ( colors !== undefined ) includeColors = true;
  83. }
  84. }
  85. } );
  86. var includeIndices = excludeAttributes.indexOf( 'index' ) === - 1;
  87. includeNormals = includeNormals && excludeAttributes.indexOf( 'normal' ) === - 1;
  88. includeColors = includeColors && excludeAttributes.indexOf( 'color' ) === - 1;
  89. includeUVs = includeUVs && excludeAttributes.indexOf( 'uv' ) === - 1;
  90. if ( includeIndices && faceCount !== Math.floor( faceCount ) ) {
  91. // point cloud meshes will not have an index array and may not have a
  92. // number of vertices that is divisble by 3 (and therefore representable
  93. // as triangles)
  94. console.error(
  95. 'PLYExporter: Failed to generate a valid PLY file with triangle indices because the ' +
  96. 'number of indices is not divisible by 3.'
  97. );
  98. return null;
  99. }
  100. var indexByteCount = 4;
  101. var header =
  102. 'ply\n' +
  103. `format ${ options.binary ? 'binary_big_endian' : 'ascii' } 1.0\n` +
  104. `element vertex ${vertexCount}\n` +
  105. // position
  106. 'property float x\n' +
  107. 'property float y\n' +
  108. 'property float z\n';
  109. if ( includeNormals === true ) {
  110. // normal
  111. header +=
  112. 'property float nx\n' +
  113. 'property float ny\n' +
  114. 'property float nz\n';
  115. }
  116. if ( includeUVs === true ) {
  117. // uvs
  118. header +=
  119. 'property float s\n' +
  120. 'property float t\n';
  121. }
  122. if ( includeColors === true ) {
  123. // colors
  124. header +=
  125. 'property uchar red\n' +
  126. 'property uchar green\n' +
  127. 'property uchar blue\n';
  128. }
  129. if ( includeIndices === true ) {
  130. // faces
  131. header +=
  132. `element face ${faceCount}\n` +
  133. `property list uchar int vertex_index\n`;
  134. }
  135. header += 'end_header\n';
  136. // Generate attribute data
  137. var vertex = new Vector3();
  138. var normalMatrixWorld = new Matrix3();
  139. var result = null;
  140. if ( options.binary === true ) {
  141. // Binary File Generation
  142. var headerBin = new TextEncoder().encode( header );
  143. // 3 position values at 4 bytes
  144. // 3 normal values at 4 bytes
  145. // 3 color channels with 1 byte
  146. // 2 uv values at 4 bytes
  147. var vertexListLength = vertexCount * ( 4 * 3 + ( includeNormals ? 4 * 3 : 0 ) + ( includeColors ? 3 : 0 ) + ( includeUVs ? 4 * 2 : 0 ) );
  148. // 1 byte shape desciptor
  149. // 3 vertex indices at ${indexByteCount} bytes
  150. var faceListLength = includeIndices ? faceCount * ( indexByteCount * 3 + 1 ) : 0;
  151. var output = new DataView( new ArrayBuffer( headerBin.length + vertexListLength + faceListLength ) );
  152. new Uint8Array( output.buffer ).set( headerBin, 0 );
  153. var vOffset = headerBin.length;
  154. var fOffset = headerBin.length + vertexListLength;
  155. var writtenVertices = 0;
  156. traverseMeshes( function ( mesh, geometry ) {
  157. var vertices = geometry.getAttribute( 'position' );
  158. var normals = geometry.getAttribute( 'normal' );
  159. var uvs = geometry.getAttribute( 'uv' );
  160. var colors = geometry.getAttribute( 'color' );
  161. var indices = geometry.getIndex();
  162. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  163. for ( var i = 0, l = vertices.count; i < l; i ++ ) {
  164. vertex.x = vertices.getX( i );
  165. vertex.y = vertices.getY( i );
  166. vertex.z = vertices.getZ( i );
  167. vertex.applyMatrix4( mesh.matrixWorld );
  168. // Position information
  169. output.setFloat32( vOffset, vertex.x );
  170. vOffset += 4;
  171. output.setFloat32( vOffset, vertex.y );
  172. vOffset += 4;
  173. output.setFloat32( vOffset, vertex.z );
  174. vOffset += 4;
  175. // Normal information
  176. if ( includeNormals === true ) {
  177. if ( normals != null ) {
  178. vertex.x = normals.getX( i );
  179. vertex.y = normals.getY( i );
  180. vertex.z = normals.getZ( i );
  181. vertex.applyMatrix3( normalMatrixWorld ).normalize();
  182. output.setFloat32( vOffset, vertex.x );
  183. vOffset += 4;
  184. output.setFloat32( vOffset, vertex.y );
  185. vOffset += 4;
  186. output.setFloat32( vOffset, vertex.z );
  187. vOffset += 4;
  188. } else {
  189. output.setFloat32( vOffset, 0 );
  190. vOffset += 4;
  191. output.setFloat32( vOffset, 0 );
  192. vOffset += 4;
  193. output.setFloat32( vOffset, 0 );
  194. vOffset += 4;
  195. }
  196. }
  197. // UV information
  198. if ( includeUVs === true ) {
  199. if ( uvs != null ) {
  200. output.setFloat32( vOffset, uvs.getX( i ) );
  201. vOffset += 4;
  202. output.setFloat32( vOffset, uvs.getY( i ) );
  203. vOffset += 4;
  204. } else if ( includeUVs !== false ) {
  205. output.setFloat32( vOffset, 0 );
  206. vOffset += 4;
  207. output.setFloat32( vOffset, 0 );
  208. vOffset += 4;
  209. }
  210. }
  211. // Color information
  212. if ( includeColors === true ) {
  213. if ( colors != null ) {
  214. output.setUint8( vOffset, Math.floor( colors.getX( i ) * 255 ) );
  215. vOffset += 1;
  216. output.setUint8( vOffset, Math.floor( colors.getY( i ) * 255 ) );
  217. vOffset += 1;
  218. output.setUint8( vOffset, Math.floor( colors.getZ( i ) * 255 ) );
  219. vOffset += 1;
  220. } else {
  221. output.setUint8( vOffset, 255 );
  222. vOffset += 1;
  223. output.setUint8( vOffset, 255 );
  224. vOffset += 1;
  225. output.setUint8( vOffset, 255 );
  226. vOffset += 1;
  227. }
  228. }
  229. }
  230. if ( includeIndices === true ) {
  231. // Create the face list
  232. if ( indices !== null ) {
  233. for ( var i = 0, l = indices.count; i < l; i += 3 ) {
  234. output.setUint8( fOffset, 3 );
  235. fOffset += 1;
  236. output.setUint32( fOffset, indices.getX( i + 0 ) + writtenVertices );
  237. fOffset += indexByteCount;
  238. output.setUint32( fOffset, indices.getX( i + 1 ) + writtenVertices );
  239. fOffset += indexByteCount;
  240. output.setUint32( fOffset, indices.getX( i + 2 ) + writtenVertices );
  241. fOffset += indexByteCount;
  242. }
  243. } else {
  244. for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
  245. output.setUint8( fOffset, 3 );
  246. fOffset += 1;
  247. output.setUint32( fOffset, writtenVertices + i );
  248. fOffset += indexByteCount;
  249. output.setUint32( fOffset, writtenVertices + i + 1 );
  250. fOffset += indexByteCount;
  251. output.setUint32( fOffset, writtenVertices + i + 2 );
  252. fOffset += indexByteCount;
  253. }
  254. }
  255. }
  256. // Save the amount of verts we've already written so we can offset
  257. // the face index on the next mesh
  258. writtenVertices += vertices.count;
  259. } );
  260. result = output.buffer;
  261. } else {
  262. // Ascii File Generation
  263. // count the number of vertices
  264. var writtenVertices = 0;
  265. var vertexList = '';
  266. var faceList = '';
  267. traverseMeshes( function ( mesh, geometry ) {
  268. var vertices = geometry.getAttribute( 'position' );
  269. var normals = geometry.getAttribute( 'normal' );
  270. var uvs = geometry.getAttribute( 'uv' );
  271. var colors = geometry.getAttribute( 'color' );
  272. var indices = geometry.getIndex();
  273. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  274. // form each line
  275. for ( var i = 0, l = vertices.count; i < l; i ++ ) {
  276. vertex.x = vertices.getX( i );
  277. vertex.y = vertices.getY( i );
  278. vertex.z = vertices.getZ( i );
  279. vertex.applyMatrix4( mesh.matrixWorld );
  280. // Position information
  281. var line =
  282. vertex.x + ' ' +
  283. vertex.y + ' ' +
  284. vertex.z;
  285. // Normal information
  286. if ( includeNormals === true ) {
  287. if ( normals != null ) {
  288. vertex.x = normals.getX( i );
  289. vertex.y = normals.getY( i );
  290. vertex.z = normals.getZ( i );
  291. vertex.applyMatrix3( normalMatrixWorld ).normalize();
  292. line += ' ' +
  293. vertex.x + ' ' +
  294. vertex.y + ' ' +
  295. vertex.z;
  296. } else {
  297. line += ' 0 0 0';
  298. }
  299. }
  300. // UV information
  301. if ( includeUVs === true ) {
  302. if ( uvs != null ) {
  303. line += ' ' +
  304. uvs.getX( i ) + ' ' +
  305. uvs.getY( i );
  306. } else if ( includeUVs !== false ) {
  307. line += ' 0 0';
  308. }
  309. }
  310. // Color information
  311. if ( includeColors === true ) {
  312. if ( colors != null ) {
  313. line += ' ' +
  314. Math.floor( colors.getX( i ) * 255 ) + ' ' +
  315. Math.floor( colors.getY( i ) * 255 ) + ' ' +
  316. Math.floor( colors.getZ( i ) * 255 );
  317. } else {
  318. line += ' 255 255 255';
  319. }
  320. }
  321. vertexList += line + '\n';
  322. }
  323. // Create the face list
  324. if ( includeIndices === true ) {
  325. if ( indices !== null ) {
  326. for ( var i = 0, l = indices.count; i < l; i += 3 ) {
  327. faceList += `3 ${ indices.getX( i + 0 ) + writtenVertices }`;
  328. faceList += ` ${ indices.getX( i + 1 ) + writtenVertices }`;
  329. faceList += ` ${ indices.getX( i + 2 ) + writtenVertices }\n`;
  330. }
  331. } else {
  332. for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
  333. faceList += `3 ${ writtenVertices + i } ${ writtenVertices + i + 1 } ${ writtenVertices + i + 2 }\n`;
  334. }
  335. }
  336. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  337. }
  338. writtenVertices += vertices.count;
  339. } );
  340. result = `${ header }${vertexList}${ includeIndices ? `${faceList}\n` : '\n' }`;
  341. }
  342. if ( typeof onDone === 'function' ) requestAnimationFrame( () => onDone( result ) );
  343. return result;
  344. }
  345. };
  346. export { PLYExporter };