PLYExporter.js 12 KB

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