PLYExporter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 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` + // position
  84. 'property float x\n' + 'property float y\n' + 'property float z\n';
  85. if ( includeNormals === true ) {
  86. // normal
  87. header += 'property float nx\n' + 'property float ny\n' + 'property float nz\n';
  88. }
  89. if ( includeUVs === true ) {
  90. // uvs
  91. header += 'property float s\n' + 'property float t\n';
  92. }
  93. if ( includeColors === true ) {
  94. // colors
  95. header += 'property uchar red\n' + 'property uchar green\n' + 'property uchar blue\n';
  96. }
  97. if ( includeIndices === true ) {
  98. // faces
  99. header += `element face ${faceCount}\n` + 'property list uchar int vertex_index\n';
  100. }
  101. header += 'end_header\n'; // Generate attribute data
  102. const vertex = new THREE.Vector3();
  103. const normalMatrixWorld = new THREE.Matrix3();
  104. let result = null;
  105. if ( options.binary === true ) {
  106. // Binary File Generation
  107. const headerBin = new TextEncoder().encode( header ); // 3 position values at 4 bytes
  108. // 3 normal values at 4 bytes
  109. // 3 color channels with 1 byte
  110. // 2 uv values at 4 bytes
  111. const vertexListLength = vertexCount * ( 4 * 3 + ( includeNormals ? 4 * 3 : 0 ) + ( includeColors ? 3 : 0 ) + ( includeUVs ? 4 * 2 : 0 ) ); // 1 byte shape desciptor
  112. // 3 vertex indices at ${indexByteCount} bytes
  113. const faceListLength = includeIndices ? faceCount * ( indexByteCount * 3 + 1 ) : 0;
  114. const output = new DataView( new ArrayBuffer( headerBin.length + vertexListLength + faceListLength ) );
  115. new Uint8Array( output.buffer ).set( headerBin, 0 );
  116. let vOffset = headerBin.length;
  117. let fOffset = headerBin.length + vertexListLength;
  118. let writtenVertices = 0;
  119. traverseMeshes( function ( mesh, geometry ) {
  120. const vertices = geometry.getAttribute( 'position' );
  121. const normals = geometry.getAttribute( 'normal' );
  122. const uvs = geometry.getAttribute( 'uv' );
  123. const colors = geometry.getAttribute( 'color' );
  124. const indices = geometry.getIndex();
  125. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  126. for ( let i = 0, l = vertices.count; i < l; i ++ ) {
  127. vertex.x = vertices.getX( i );
  128. vertex.y = vertices.getY( i );
  129. vertex.z = vertices.getZ( i );
  130. vertex.applyMatrix4( mesh.matrixWorld ); // Position information
  131. output.setFloat32( vOffset, vertex.x, options.littleEndian );
  132. vOffset += 4;
  133. output.setFloat32( vOffset, vertex.y, options.littleEndian );
  134. vOffset += 4;
  135. output.setFloat32( vOffset, vertex.z, options.littleEndian );
  136. vOffset += 4; // Normal information
  137. if ( includeNormals === true ) {
  138. if ( normals != null ) {
  139. vertex.x = normals.getX( i );
  140. vertex.y = normals.getY( i );
  141. vertex.z = normals.getZ( i );
  142. vertex.applyMatrix3( normalMatrixWorld ).normalize();
  143. output.setFloat32( vOffset, vertex.x, options.littleEndian );
  144. vOffset += 4;
  145. output.setFloat32( vOffset, vertex.y, options.littleEndian );
  146. vOffset += 4;
  147. output.setFloat32( vOffset, vertex.z, options.littleEndian );
  148. vOffset += 4;
  149. } else {
  150. output.setFloat32( vOffset, 0, options.littleEndian );
  151. vOffset += 4;
  152. output.setFloat32( vOffset, 0, options.littleEndian );
  153. vOffset += 4;
  154. output.setFloat32( vOffset, 0, options.littleEndian );
  155. vOffset += 4;
  156. }
  157. } // UV information
  158. if ( includeUVs === true ) {
  159. if ( uvs != null ) {
  160. output.setFloat32( vOffset, uvs.getX( i ), options.littleEndian );
  161. vOffset += 4;
  162. output.setFloat32( vOffset, uvs.getY( i ), options.littleEndian );
  163. vOffset += 4;
  164. } else if ( includeUVs !== false ) {
  165. output.setFloat32( vOffset, 0, options.littleEndian );
  166. vOffset += 4;
  167. output.setFloat32( vOffset, 0, options.littleEndian );
  168. vOffset += 4;
  169. }
  170. } // Color information
  171. if ( includeColors === true ) {
  172. if ( colors != null ) {
  173. output.setUint8( vOffset, Math.floor( colors.getX( i ) * 255 ) );
  174. vOffset += 1;
  175. output.setUint8( vOffset, Math.floor( colors.getY( i ) * 255 ) );
  176. vOffset += 1;
  177. output.setUint8( vOffset, Math.floor( colors.getZ( i ) * 255 ) );
  178. vOffset += 1;
  179. } else {
  180. output.setUint8( vOffset, 255 );
  181. vOffset += 1;
  182. output.setUint8( vOffset, 255 );
  183. vOffset += 1;
  184. output.setUint8( vOffset, 255 );
  185. vOffset += 1;
  186. }
  187. }
  188. }
  189. if ( includeIndices === true ) {
  190. // Create the face list
  191. if ( indices !== null ) {
  192. for ( let i = 0, l = indices.count; i < l; i += 3 ) {
  193. output.setUint8( fOffset, 3 );
  194. fOffset += 1;
  195. output.setUint32( fOffset, indices.getX( i + 0 ) + writtenVertices, options.littleEndian );
  196. fOffset += indexByteCount;
  197. output.setUint32( fOffset, indices.getX( i + 1 ) + writtenVertices, options.littleEndian );
  198. fOffset += indexByteCount;
  199. output.setUint32( fOffset, indices.getX( i + 2 ) + writtenVertices, options.littleEndian );
  200. fOffset += indexByteCount;
  201. }
  202. } else {
  203. for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
  204. output.setUint8( fOffset, 3 );
  205. fOffset += 1;
  206. output.setUint32( fOffset, writtenVertices + i, options.littleEndian );
  207. fOffset += indexByteCount;
  208. output.setUint32( fOffset, writtenVertices + i + 1, options.littleEndian );
  209. fOffset += indexByteCount;
  210. output.setUint32( fOffset, writtenVertices + i + 2, options.littleEndian );
  211. fOffset += indexByteCount;
  212. }
  213. }
  214. } // Save the amount of verts we've already written so we can offset
  215. // the face index on the next mesh
  216. writtenVertices += vertices.count;
  217. } );
  218. result = output.buffer;
  219. } else {
  220. // Ascii File Generation
  221. // count the number of vertices
  222. let writtenVertices = 0;
  223. let vertexList = '';
  224. let faceList = '';
  225. traverseMeshes( function ( mesh, geometry ) {
  226. const vertices = geometry.getAttribute( 'position' );
  227. const normals = geometry.getAttribute( 'normal' );
  228. const uvs = geometry.getAttribute( 'uv' );
  229. const colors = geometry.getAttribute( 'color' );
  230. const indices = geometry.getIndex();
  231. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld ); // form each line
  232. for ( let i = 0, l = vertices.count; i < l; i ++ ) {
  233. vertex.x = vertices.getX( i );
  234. vertex.y = vertices.getY( i );
  235. vertex.z = vertices.getZ( i );
  236. vertex.applyMatrix4( mesh.matrixWorld ); // Position information
  237. let line = vertex.x + ' ' + vertex.y + ' ' + vertex.z; // Normal information
  238. if ( includeNormals === true ) {
  239. if ( normals != null ) {
  240. vertex.x = normals.getX( i );
  241. vertex.y = normals.getY( i );
  242. vertex.z = normals.getZ( i );
  243. vertex.applyMatrix3( normalMatrixWorld ).normalize();
  244. line += ' ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
  245. } else {
  246. line += ' 0 0 0';
  247. }
  248. } // UV information
  249. if ( includeUVs === true ) {
  250. if ( uvs != null ) {
  251. line += ' ' + uvs.getX( i ) + ' ' + uvs.getY( i );
  252. } else if ( includeUVs !== false ) {
  253. line += ' 0 0';
  254. }
  255. } // Color information
  256. if ( includeColors === true ) {
  257. if ( colors != null ) {
  258. line += ' ' + Math.floor( colors.getX( i ) * 255 ) + ' ' + Math.floor( colors.getY( i ) * 255 ) + ' ' + Math.floor( colors.getZ( i ) * 255 );
  259. } else {
  260. line += ' 255 255 255';
  261. }
  262. }
  263. vertexList += line + '\n';
  264. } // Create the face list
  265. if ( includeIndices === true ) {
  266. if ( indices !== null ) {
  267. for ( let i = 0, l = indices.count; i < l; i += 3 ) {
  268. faceList += `3 ${indices.getX( i + 0 ) + writtenVertices}`;
  269. faceList += ` ${indices.getX( i + 1 ) + writtenVertices}`;
  270. faceList += ` ${indices.getX( i + 2 ) + writtenVertices}\n`;
  271. }
  272. } else {
  273. for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
  274. faceList += `3 ${writtenVertices + i} ${writtenVertices + i + 1} ${writtenVertices + i + 2}\n`;
  275. }
  276. }
  277. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  278. }
  279. writtenVertices += vertices.count;
  280. } );
  281. result = `${header}${vertexList}${includeIndices ? `${faceList}\n` : '\n'}`;
  282. }
  283. if ( typeof onDone === 'function' ) requestAnimationFrame( () => onDone( result ) );
  284. return result;
  285. }
  286. }
  287. THREE.PLYExporter = PLYExporter;
  288. } )();