PLYExporter.js 12 KB

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