PLYExporter.js 12 KB

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