PLYExporter.js 11 KB

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