PLYExporter.js 12 KB

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