PLYExporter.js 11 KB

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