PLYExporter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /**
  2. * @author Garrett Johnson / http://gkjohnson.github.io/
  3. * https://github.com/gkjohnson/ply-exporter-js
  4. *
  5. * Usage:
  6. * var exporter = new PLYExporter();
  7. *
  8. * // second argument is a list of options
  9. * exporter.parse(mesh, data => console.log(data), { binary: true, excludeAttributes: [ 'color' ] });
  10. *
  11. * Format Definition:
  12. * http://paulbourke.net/dataformats/ply/
  13. */
  14. import {
  15. BufferGeometry,
  16. Matrix3,
  17. Vector3
  18. } from "../../../build/three.module.js";
  19. var PLYExporter = function () {};
  20. PLYExporter.prototype = {
  21. constructor: PLYExporter,
  22. parse: function ( object, onDone, options ) {
  23. if ( onDone && typeof onDone === 'object' ) {
  24. console.warn( 'THREE.PLYExporter: The options parameter is now the third argument to the "parse" function. See the documentation for the new API.' );
  25. options = onDone;
  26. onDone = undefined;
  27. }
  28. // Iterate over the valid meshes in the object
  29. function traverseMeshes( cb ) {
  30. object.traverse( function ( child ) {
  31. if ( child.isMesh === true ) {
  32. var mesh = child;
  33. var geometry = mesh.geometry;
  34. if ( geometry.isGeometry === true ) {
  35. geometry = geomToBufferGeom.get( geometry );
  36. }
  37. if ( geometry.isBufferGeometry === true ) {
  38. if ( geometry.getAttribute( 'position' ) !== undefined ) {
  39. cb( mesh, geometry );
  40. }
  41. }
  42. }
  43. } );
  44. }
  45. // Default options
  46. var defaultOptions = {
  47. binary: false,
  48. excludeAttributes: [] // normal, uv, color, index
  49. };
  50. options = Object.assign( defaultOptions, options );
  51. var excludeAttributes = options.excludeAttributes;
  52. var geomToBufferGeom = new WeakMap();
  53. var includeNormals = false;
  54. var includeColors = false;
  55. var includeUVs = false;
  56. // count the vertices, check which properties are used,
  57. // and cache the BufferGeometry
  58. var vertexCount = 0;
  59. var faceCount = 0;
  60. object.traverse( function ( child ) {
  61. if ( child.isMesh === true ) {
  62. var mesh = child;
  63. var geometry = mesh.geometry;
  64. if ( geometry.isGeometry === true ) {
  65. var bufferGeometry = geomToBufferGeom.get( geometry ) || new BufferGeometry().setFromObject( mesh );
  66. geomToBufferGeom.set( geometry, bufferGeometry );
  67. geometry = bufferGeometry;
  68. }
  69. if ( geometry.isBufferGeometry === true ) {
  70. var vertices = geometry.getAttribute( 'position' );
  71. var normals = geometry.getAttribute( 'normal' );
  72. var uvs = geometry.getAttribute( 'uv' );
  73. var colors = geometry.getAttribute( 'color' );
  74. var indices = geometry.getIndex();
  75. if ( vertices === undefined ) {
  76. return;
  77. }
  78. vertexCount += vertices.count;
  79. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  80. if ( normals !== undefined ) includeNormals = true;
  81. if ( uvs !== undefined ) includeUVs = true;
  82. if ( colors !== undefined ) includeColors = true;
  83. }
  84. }
  85. } );
  86. var includeIndices = excludeAttributes.indexOf( 'index' ) === - 1;
  87. includeNormals = includeNormals && excludeAttributes.indexOf( 'normal' ) === - 1;
  88. includeColors = includeColors && excludeAttributes.indexOf( 'color' ) === - 1;
  89. includeUVs = includeUVs && excludeAttributes.indexOf( 'uv' ) === - 1;
  90. if ( includeIndices && faceCount !== Math.floor( faceCount ) ) {
  91. // point cloud meshes will not have an index array and may not have a
  92. // number of vertices that is divisble by 3 (and therefore representable
  93. // as triangles)
  94. console.error(
  95. 'PLYExporter: Failed to generate a valid PLY file with triangle indices because the ' +
  96. 'number of indices is not divisible by 3.'
  97. );
  98. return null;
  99. }
  100. // get how many bytes will be needed to save out the faces
  101. // so we can use a minimal amount of memory / data
  102. var indexByteCount = 1;
  103. if ( vertexCount > 256 ) { // 2^8 bits
  104. indexByteCount = 2;
  105. }
  106. if ( vertexCount > 65536 ) { // 2^16 bits
  107. indexByteCount = 4;
  108. }
  109. var header =
  110. 'ply\n' +
  111. `format ${ options.binary ? 'binary_big_endian' : 'ascii' } 1.0\n` +
  112. `element vertex ${vertexCount}\n` +
  113. // position
  114. 'property float x\n' +
  115. 'property float y\n' +
  116. 'property float z\n';
  117. if ( includeNormals === true ) {
  118. // normal
  119. header +=
  120. 'property float nx\n' +
  121. 'property float ny\n' +
  122. 'property float nz\n';
  123. }
  124. if ( includeUVs === true ) {
  125. // uvs
  126. header +=
  127. 'property float s\n' +
  128. 'property float t\n';
  129. }
  130. if ( includeColors === true ) {
  131. // colors
  132. header +=
  133. 'property uchar red\n' +
  134. 'property uchar green\n' +
  135. 'property uchar blue\n';
  136. }
  137. if ( includeIndices === true ) {
  138. // faces
  139. header +=
  140. `element face ${faceCount}\n` +
  141. `property list uchar uint${ indexByteCount * 8 } vertex_index\n`;
  142. }
  143. header += 'end_header\n';
  144. // Generate attribute data
  145. var vertex = new Vector3();
  146. var normalMatrixWorld = new Matrix3();
  147. var result = null;
  148. if ( options.binary === true ) {
  149. // Binary File Generation
  150. var headerBin = new TextEncoder().encode( header );
  151. // 3 position values at 4 bytes
  152. // 3 normal values at 4 bytes
  153. // 3 color channels with 1 byte
  154. // 2 uv values at 4 bytes
  155. var vertexListLength = vertexCount * ( 4 * 3 + ( includeNormals ? 4 * 3 : 0 ) + ( includeColors ? 3 : 0 ) + ( includeUVs ? 4 * 2 : 0 ) );
  156. // 1 byte shape desciptor
  157. // 3 vertex indices at ${indexByteCount} bytes
  158. var faceListLength = includeIndices ? faceCount * ( indexByteCount * 3 + 1 ) : 0;
  159. var output = new DataView( new ArrayBuffer( headerBin.length + vertexListLength + faceListLength ) );
  160. new Uint8Array( output.buffer ).set( headerBin, 0 );
  161. var vOffset = headerBin.length;
  162. var fOffset = headerBin.length + vertexListLength;
  163. var writtenVertices = 0;
  164. traverseMeshes( function ( mesh, geometry ) {
  165. var vertices = geometry.getAttribute( 'position' );
  166. var normals = geometry.getAttribute( 'normal' );
  167. var uvs = geometry.getAttribute( 'uv' );
  168. var colors = geometry.getAttribute( 'color' );
  169. var indices = geometry.getIndex();
  170. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  171. for ( var i = 0, l = vertices.count; i < l; i ++ ) {
  172. vertex.x = vertices.getX( i );
  173. vertex.y = vertices.getY( i );
  174. vertex.z = vertices.getZ( i );
  175. vertex.applyMatrix4( mesh.matrixWorld );
  176. // Position information
  177. output.setFloat32( vOffset, vertex.x );
  178. vOffset += 4;
  179. output.setFloat32( vOffset, vertex.y );
  180. vOffset += 4;
  181. output.setFloat32( vOffset, vertex.z );
  182. vOffset += 4;
  183. // Normal information
  184. if ( includeNormals === true ) {
  185. if ( normals != null ) {
  186. vertex.x = normals.getX( i );
  187. vertex.y = normals.getY( i );
  188. vertex.z = normals.getZ( i );
  189. vertex.applyMatrix3( normalMatrixWorld );
  190. output.setFloat32( vOffset, vertex.x );
  191. vOffset += 4;
  192. output.setFloat32( vOffset, vertex.y );
  193. vOffset += 4;
  194. output.setFloat32( vOffset, vertex.z );
  195. vOffset += 4;
  196. } else {
  197. output.setFloat32( vOffset, 0 );
  198. vOffset += 4;
  199. output.setFloat32( vOffset, 0 );
  200. vOffset += 4;
  201. output.setFloat32( vOffset, 0 );
  202. vOffset += 4;
  203. }
  204. }
  205. // UV information
  206. if ( includeUVs === true ) {
  207. if ( uvs != null ) {
  208. output.setFloat32( vOffset, uvs.getX( i ) );
  209. vOffset += 4;
  210. output.setFloat32( vOffset, uvs.getY( i ) );
  211. vOffset += 4;
  212. } else if ( includeUVs !== false ) {
  213. output.setFloat32( vOffset, 0 );
  214. vOffset += 4;
  215. output.setFloat32( vOffset, 0 );
  216. vOffset += 4;
  217. }
  218. }
  219. // Color information
  220. if ( includeColors === true ) {
  221. if ( colors != null ) {
  222. output.setUint8( vOffset, Math.floor( colors.getX( i ) * 255 ) );
  223. vOffset += 1;
  224. output.setUint8( vOffset, Math.floor( colors.getY( i ) * 255 ) );
  225. vOffset += 1;
  226. output.setUint8( vOffset, Math.floor( colors.getZ( i ) * 255 ) );
  227. vOffset += 1;
  228. } else {
  229. output.setUint8( vOffset, 255 );
  230. vOffset += 1;
  231. output.setUint8( vOffset, 255 );
  232. vOffset += 1;
  233. output.setUint8( vOffset, 255 );
  234. vOffset += 1;
  235. }
  236. }
  237. }
  238. if ( includeIndices === true ) {
  239. // Create the face list
  240. var faceIndexFunc = `setUint${indexByteCount * 8}`;
  241. if ( indices !== null ) {
  242. for ( var i = 0, l = indices.count; i < l; i += 3 ) {
  243. output.setUint8( fOffset, 3 );
  244. fOffset += 1;
  245. output[ faceIndexFunc ]( fOffset, indices.getX( i + 0 ) + writtenVertices );
  246. fOffset += indexByteCount;
  247. output[ faceIndexFunc ]( fOffset, indices.getX( i + 1 ) + writtenVertices );
  248. fOffset += indexByteCount;
  249. output[ faceIndexFunc ]( fOffset, indices.getX( i + 2 ) + writtenVertices );
  250. fOffset += indexByteCount;
  251. }
  252. } else {
  253. for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
  254. output.setUint8( fOffset, 3 );
  255. fOffset += 1;
  256. output[ faceIndexFunc ]( fOffset, writtenVertices + i );
  257. fOffset += indexByteCount;
  258. output[ faceIndexFunc ]( fOffset, writtenVertices + i + 1 );
  259. fOffset += indexByteCount;
  260. output[ faceIndexFunc ]( fOffset, writtenVertices + i + 2 );
  261. fOffset += indexByteCount;
  262. }
  263. }
  264. }
  265. // Save the amount of verts we've already written so we can offset
  266. // the face index on the next mesh
  267. writtenVertices += vertices.count;
  268. } );
  269. result = output.buffer;
  270. } else {
  271. // Ascii File Generation
  272. // count the number of vertices
  273. var writtenVertices = 0;
  274. var vertexList = '';
  275. var faceList = '';
  276. traverseMeshes( function ( mesh, geometry ) {
  277. var vertices = geometry.getAttribute( 'position' );
  278. var normals = geometry.getAttribute( 'normal' );
  279. var uvs = geometry.getAttribute( 'uv' );
  280. var colors = geometry.getAttribute( 'color' );
  281. var indices = geometry.getIndex();
  282. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  283. // form each line
  284. for ( var i = 0, l = vertices.count; i < l; i ++ ) {
  285. vertex.x = vertices.getX( i );
  286. vertex.y = vertices.getY( i );
  287. vertex.z = vertices.getZ( i );
  288. vertex.applyMatrix4( mesh.matrixWorld );
  289. // Position information
  290. var line =
  291. vertex.x + ' ' +
  292. vertex.y + ' ' +
  293. vertex.z;
  294. // Normal information
  295. if ( includeNormals === true ) {
  296. if ( normals != null ) {
  297. vertex.x = normals.getX( i );
  298. vertex.y = normals.getY( i );
  299. vertex.z = normals.getZ( i );
  300. vertex.applyMatrix3( normalMatrixWorld );
  301. line += ' ' +
  302. vertex.x + ' ' +
  303. vertex.y + ' ' +
  304. vertex.z;
  305. } else {
  306. line += ' 0 0 0';
  307. }
  308. }
  309. // UV information
  310. if ( includeUVs === true ) {
  311. if ( uvs != null ) {
  312. line += ' ' +
  313. uvs.getX( i ) + ' ' +
  314. uvs.getY( i );
  315. } else if ( includeUVs !== false ) {
  316. line += ' 0 0';
  317. }
  318. }
  319. // Color information
  320. if ( includeColors === true ) {
  321. if ( colors != null ) {
  322. line += ' ' +
  323. Math.floor( colors.getX( i ) * 255 ) + ' ' +
  324. Math.floor( colors.getY( i ) * 255 ) + ' ' +
  325. Math.floor( colors.getZ( i ) * 255 );
  326. } else {
  327. line += ' 255 255 255';
  328. }
  329. }
  330. vertexList += line + '\n';
  331. }
  332. // Create the face list
  333. if ( includeIndices === true ) {
  334. if ( indices !== null ) {
  335. for ( var i = 0, l = indices.count; i < l; i += 3 ) {
  336. faceList += `3 ${ indices.getX( i + 0 ) + writtenVertices }`;
  337. faceList += ` ${ indices.getX( i + 1 ) + writtenVertices }`;
  338. faceList += ` ${ indices.getX( i + 2 ) + writtenVertices }\n`;
  339. }
  340. } else {
  341. for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
  342. faceList += `3 ${ writtenVertices + i } ${ writtenVertices + i + 1 } ${ writtenVertices + i + 2 }\n`;
  343. }
  344. }
  345. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  346. }
  347. writtenVertices += vertices.count;
  348. } );
  349. result = `${ header }${vertexList}\n${ includeIndices ? `${faceList}\n` : '' }`;
  350. }
  351. if ( typeof onDone === 'function' ) requestAnimationFrame( () => onDone( result ) );
  352. return result;
  353. }
  354. };
  355. export { PLYExporter };