PLYExporter.js 12 KB

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