PLYExporter.js 12 KB

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