PLYExporter.js 12 KB

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