PLYExporter.js 11 KB

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