PLYExporter.js 12 KB

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