PLYExporter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. if ( onDone && typeof onDone === 'object' ) {
  21. console.warn( 'THREE.PLYExporter: The options parameter is now the third argument to the "parse" function. See the documentation for the new API.' );
  22. options = onDone;
  23. onDone = undefined;
  24. }
  25. // Iterate over the valid meshes in the object
  26. function traverseMeshes( cb ) {
  27. object.traverse( function ( child ) {
  28. if ( child.isMesh === true ) {
  29. const mesh = child;
  30. const geometry = mesh.geometry;
  31. if ( geometry.isBufferGeometry !== true ) {
  32. throw new Error( 'THREE.PLYExporter: Geometry is not of type THREE.BufferGeometry.' );
  33. }
  34. if ( geometry.hasAttribute( 'position' ) === true ) {
  35. cb( mesh, geometry );
  36. }
  37. }
  38. } );
  39. }
  40. // Default options
  41. const defaultOptions = {
  42. binary: false,
  43. excludeAttributes: [], // normal, uv, color, index
  44. littleEndian: false
  45. };
  46. options = Object.assign( defaultOptions, options );
  47. const excludeAttributes = options.excludeAttributes;
  48. let includeNormals = false;
  49. let includeColors = false;
  50. let includeUVs = false;
  51. // count the vertices, check which properties are used,
  52. // and cache the BufferGeometry
  53. let vertexCount = 0;
  54. let faceCount = 0;
  55. object.traverse( function ( child ) {
  56. if ( child.isMesh === true ) {
  57. const mesh = child;
  58. const geometry = mesh.geometry;
  59. if ( geometry.isBufferGeometry !== true ) {
  60. throw new Error( 'THREE.PLYExporter: Geometry is not of type THREE.BufferGeometry.' );
  61. }
  62. const vertices = geometry.getAttribute( 'position' );
  63. const normals = geometry.getAttribute( 'normal' );
  64. const uvs = geometry.getAttribute( 'uv' );
  65. const colors = geometry.getAttribute( 'color' );
  66. const indices = geometry.getIndex();
  67. if ( vertices === undefined ) {
  68. return;
  69. }
  70. vertexCount += vertices.count;
  71. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  72. if ( normals !== undefined ) includeNormals = true;
  73. if ( uvs !== undefined ) includeUVs = true;
  74. if ( colors !== undefined ) includeColors = true;
  75. }
  76. } );
  77. const tempColor = new Color();
  78. const 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.x = vertices.getX( i );
  157. vertex.y = vertices.getY( i );
  158. vertex.z = vertices.getZ( i );
  159. vertex.applyMatrix4( mesh.matrixWorld );
  160. // Position information
  161. output.setFloat32( vOffset, vertex.x, options.littleEndian );
  162. vOffset += 4;
  163. output.setFloat32( vOffset, vertex.y, options.littleEndian );
  164. vOffset += 4;
  165. output.setFloat32( vOffset, vertex.z, options.littleEndian );
  166. vOffset += 4;
  167. // Normal information
  168. if ( includeNormals === true ) {
  169. if ( normals != null ) {
  170. vertex.x = normals.getX( i );
  171. vertex.y = normals.getY( i );
  172. vertex.z = normals.getZ( i );
  173. vertex.applyMatrix3( normalMatrixWorld ).normalize();
  174. output.setFloat32( vOffset, vertex.x, options.littleEndian );
  175. vOffset += 4;
  176. output.setFloat32( vOffset, vertex.y, options.littleEndian );
  177. vOffset += 4;
  178. output.setFloat32( vOffset, vertex.z, options.littleEndian );
  179. vOffset += 4;
  180. } else {
  181. output.setFloat32( vOffset, 0, options.littleEndian );
  182. vOffset += 4;
  183. output.setFloat32( vOffset, 0, options.littleEndian );
  184. vOffset += 4;
  185. output.setFloat32( vOffset, 0, options.littleEndian );
  186. vOffset += 4;
  187. }
  188. }
  189. // UV information
  190. if ( includeUVs === true ) {
  191. if ( uvs != null ) {
  192. output.setFloat32( vOffset, uvs.getX( i ), options.littleEndian );
  193. vOffset += 4;
  194. output.setFloat32( vOffset, uvs.getY( i ), options.littleEndian );
  195. vOffset += 4;
  196. } else {
  197. output.setFloat32( vOffset, 0, options.littleEndian );
  198. vOffset += 4;
  199. output.setFloat32( vOffset, 0, options.littleEndian );
  200. vOffset += 4;
  201. }
  202. }
  203. // Color information
  204. if ( includeColors === true ) {
  205. if ( colors != null ) {
  206. tempColor
  207. .fromBufferAttribute( colors, i )
  208. .convertLinearToSRGB();
  209. output.setUint8( vOffset, Math.floor( tempColor.r * 255 ) );
  210. vOffset += 1;
  211. output.setUint8( vOffset, Math.floor( tempColor.g * 255 ) );
  212. vOffset += 1;
  213. output.setUint8( vOffset, Math.floor( tempColor.b * 255 ) );
  214. vOffset += 1;
  215. } else {
  216. output.setUint8( vOffset, 255 );
  217. vOffset += 1;
  218. output.setUint8( vOffset, 255 );
  219. vOffset += 1;
  220. output.setUint8( vOffset, 255 );
  221. vOffset += 1;
  222. }
  223. }
  224. }
  225. if ( includeIndices === true ) {
  226. // Create the face list
  227. if ( indices !== null ) {
  228. for ( let i = 0, l = indices.count; i < l; i += 3 ) {
  229. output.setUint8( fOffset, 3 );
  230. fOffset += 1;
  231. output.setUint32( fOffset, indices.getX( i + 0 ) + writtenVertices, options.littleEndian );
  232. fOffset += indexByteCount;
  233. output.setUint32( fOffset, indices.getX( i + 1 ) + writtenVertices, options.littleEndian );
  234. fOffset += indexByteCount;
  235. output.setUint32( fOffset, indices.getX( i + 2 ) + writtenVertices, options.littleEndian );
  236. fOffset += indexByteCount;
  237. }
  238. } else {
  239. for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
  240. output.setUint8( fOffset, 3 );
  241. fOffset += 1;
  242. output.setUint32( fOffset, writtenVertices + i, options.littleEndian );
  243. fOffset += indexByteCount;
  244. output.setUint32( fOffset, writtenVertices + i + 1, options.littleEndian );
  245. fOffset += indexByteCount;
  246. output.setUint32( fOffset, writtenVertices + i + 2, options.littleEndian );
  247. fOffset += indexByteCount;
  248. }
  249. }
  250. }
  251. // Save the amount of verts we've already written so we can offset
  252. // the face index on the next mesh
  253. writtenVertices += vertices.count;
  254. } );
  255. result = output.buffer;
  256. } else {
  257. // Ascii File Generation
  258. // count the number of vertices
  259. let writtenVertices = 0;
  260. let vertexList = '';
  261. let faceList = '';
  262. traverseMeshes( function ( mesh, geometry ) {
  263. const vertices = geometry.getAttribute( 'position' );
  264. const normals = geometry.getAttribute( 'normal' );
  265. const uvs = geometry.getAttribute( 'uv' );
  266. const colors = geometry.getAttribute( 'color' );
  267. const indices = geometry.getIndex();
  268. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  269. // form each line
  270. for ( let i = 0, l = vertices.count; i < l; i ++ ) {
  271. vertex.x = vertices.getX( i );
  272. vertex.y = vertices.getY( i );
  273. vertex.z = vertices.getZ( i );
  274. vertex.applyMatrix4( mesh.matrixWorld );
  275. // Position information
  276. let line =
  277. vertex.x + ' ' +
  278. vertex.y + ' ' +
  279. vertex.z;
  280. // Normal information
  281. if ( includeNormals === true ) {
  282. if ( normals != null ) {
  283. vertex.x = normals.getX( i );
  284. vertex.y = normals.getY( i );
  285. vertex.z = normals.getZ( i );
  286. vertex.applyMatrix3( normalMatrixWorld ).normalize();
  287. line += ' ' +
  288. vertex.x + ' ' +
  289. vertex.y + ' ' +
  290. vertex.z;
  291. } else {
  292. line += ' 0 0 0';
  293. }
  294. }
  295. // UV information
  296. if ( includeUVs === true ) {
  297. if ( uvs != null ) {
  298. line += ' ' +
  299. uvs.getX( i ) + ' ' +
  300. uvs.getY( i );
  301. } else {
  302. line += ' 0 0';
  303. }
  304. }
  305. // Color information
  306. if ( includeColors === true ) {
  307. if ( colors != null ) {
  308. tempColor
  309. .fromBufferAttribute( colors, i )
  310. .convertLinearToSRGB();
  311. line += ' ' +
  312. Math.floor( tempColor.r * 255 ) + ' ' +
  313. Math.floor( tempColor.g * 255 ) + ' ' +
  314. Math.floor( tempColor.b * 255 );
  315. } else {
  316. line += ' 255 255 255';
  317. }
  318. }
  319. vertexList += line + '\n';
  320. }
  321. // Create the face list
  322. if ( includeIndices === true ) {
  323. if ( indices !== null ) {
  324. for ( let i = 0, l = indices.count; i < l; i += 3 ) {
  325. faceList += `3 ${ indices.getX( i + 0 ) + writtenVertices }`;
  326. faceList += ` ${ indices.getX( i + 1 ) + writtenVertices }`;
  327. faceList += ` ${ indices.getX( i + 2 ) + writtenVertices }\n`;
  328. }
  329. } else {
  330. for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
  331. faceList += `3 ${ writtenVertices + i } ${ writtenVertices + i + 1 } ${ writtenVertices + i + 2 }\n`;
  332. }
  333. }
  334. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  335. }
  336. writtenVertices += vertices.count;
  337. } );
  338. result = `${ header }${vertexList}${ includeIndices ? `${faceList}\n` : '\n' }`;
  339. }
  340. if ( typeof onDone === 'function' ) requestAnimationFrame( () => onDone( result ) );
  341. return result;
  342. }
  343. }
  344. export { PLYExporter };