PLYExporter.js 12 KB

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