PLYExporter.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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' ] });
  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. };
  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. var includeIndices = true;
  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. includeNormals = includeNormals && excludeAttributes.indexOf( 'normal' ) === - 1;
  83. includeColors = includeColors && excludeAttributes.indexOf( 'color' ) === - 1;
  84. includeUVs = includeUVs && excludeAttributes.indexOf( 'uv' ) === - 1;
  85. includeIndices = includeIndices && excludeAttributes.indexOf( 'index' ) === - 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. // get how many bytes will be needed to save out the faces
  97. // so we can use a minimal amount of memory / data
  98. var indexByteCount = 1;
  99. if ( vertexCount > 256 ) { // 2^8 bits
  100. indexByteCount = 2;
  101. }
  102. if ( vertexCount > 65536 ) { // 2^16 bits
  103. indexByteCount = 4;
  104. }
  105. var header =
  106. 'ply\n' +
  107. `format ${ options.binary ? 'binary_big_endian' : 'ascii' } 1.0\n` +
  108. `element vertex ${vertexCount}\n` +
  109. // position
  110. 'property float x\n' +
  111. 'property float y\n' +
  112. 'property float z\n';
  113. if ( includeNormals === true ) {
  114. // normal
  115. header +=
  116. 'property float nx\n' +
  117. 'property float ny\n' +
  118. 'property float nz\n';
  119. }
  120. if ( includeUVs === true ) {
  121. // uvs
  122. header +=
  123. 'property float s\n' +
  124. 'property float t\n';
  125. }
  126. if ( includeColors === true ) {
  127. // colors
  128. header +=
  129. 'property uchar red\n' +
  130. 'property uchar green\n' +
  131. 'property uchar blue\n';
  132. }
  133. if ( includeIndices === true ) {
  134. // faces
  135. header +=
  136. `element face ${faceCount}\n` +
  137. `property list uchar uint${ indexByteCount * 8 } vertex_index\n`;
  138. }
  139. header += 'end_header\n';
  140. // Generate attribute data
  141. var vertex = new THREE.Vector3();
  142. var normalMatrixWorld = new THREE.Matrix3();
  143. var result = null;
  144. if ( options.binary === true ) {
  145. // Binary File Generation
  146. var headerBin = new TextEncoder().encode( header );
  147. // 3 position values at 4 bytes
  148. // 3 normal values at 4 bytes
  149. // 3 color channels with 1 byte
  150. // 2 uv values at 4 bytes
  151. var vertexListLength = vertexCount * ( 4 * 3 + ( includeNormals ? 4 * 3 : 0 ) + ( includeColors ? 3 : 0 ) + ( includeUVs ? 4 * 2 : 0 ) );
  152. // 1 byte shape desciptor
  153. // 3 vertex indices at ${indexByteCount} bytes
  154. var faceListLength = includeIndices ? faceCount * ( indexByteCount * 3 + 1 ) : 0;
  155. var output = new DataView( new ArrayBuffer( headerBin.length + vertexListLength + faceListLength ) );
  156. new Uint8Array( output.buffer ).set( headerBin, 0 );
  157. var vOffset = headerBin.length;
  158. var fOffset = headerBin.length + vertexListLength;
  159. var writtenVertices = 0;
  160. traverseMeshes( function ( mesh, geometry ) {
  161. var vertices = geometry.getAttribute( 'position' );
  162. var normals = geometry.getAttribute( 'normal' );
  163. var uvs = geometry.getAttribute( 'uv' );
  164. var colors = geometry.getAttribute( 'color' );
  165. var indices = geometry.getIndex();
  166. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  167. for ( var i = 0, l = vertices.count; i < l; i ++ ) {
  168. vertex.x = vertices.getX( i );
  169. vertex.y = vertices.getY( i );
  170. vertex.z = vertices.getZ( i );
  171. vertex.applyMatrix4( mesh.matrixWorld );
  172. // Position information
  173. output.setFloat32( vOffset, vertex.x );
  174. vOffset += 4;
  175. output.setFloat32( vOffset, vertex.y );
  176. vOffset += 4;
  177. output.setFloat32( vOffset, vertex.z );
  178. vOffset += 4;
  179. // Normal information
  180. if ( includeNormals === true ) {
  181. if ( normals != null ) {
  182. vertex.x = normals.getX( i );
  183. vertex.y = normals.getY( i );
  184. vertex.z = normals.getZ( i );
  185. vertex.applyMatrix3( normalMatrixWorld );
  186. output.setFloat32( vOffset, vertex.x );
  187. vOffset += 4;
  188. output.setFloat32( vOffset, vertex.y );
  189. vOffset += 4;
  190. output.setFloat32( vOffset, vertex.z );
  191. vOffset += 4;
  192. } else {
  193. output.setFloat32( vOffset, 0 );
  194. vOffset += 4;
  195. output.setFloat32( vOffset, 0 );
  196. vOffset += 4;
  197. output.setFloat32( vOffset, 0 );
  198. vOffset += 4;
  199. }
  200. }
  201. // UV information
  202. if ( includeUVs === true ) {
  203. if ( uvs != null ) {
  204. output.setFloat32( vOffset, uvs.getX( i ) );
  205. vOffset += 4;
  206. output.setFloat32( vOffset, uvs.getY( i ) );
  207. vOffset += 4;
  208. } else if ( includeUVs !== false ) {
  209. output.setFloat32( vOffset, 0 );
  210. vOffset += 4;
  211. output.setFloat32( vOffset, 0 );
  212. vOffset += 4;
  213. }
  214. }
  215. // Color information
  216. if ( includeColors === true ) {
  217. if ( colors != null ) {
  218. output.setUint8( vOffset, Math.floor( colors.getX( i ) * 255 ) );
  219. vOffset += 1;
  220. output.setUint8( vOffset, Math.floor( colors.getY( i ) * 255 ) );
  221. vOffset += 1;
  222. output.setUint8( vOffset, Math.floor( colors.getZ( i ) * 255 ) );
  223. vOffset += 1;
  224. } else {
  225. output.setUint8( vOffset, 255 );
  226. vOffset += 1;
  227. output.setUint8( vOffset, 255 );
  228. vOffset += 1;
  229. output.setUint8( vOffset, 255 );
  230. vOffset += 1;
  231. }
  232. }
  233. }
  234. if ( includeIndices === true ) {
  235. // Create the face list
  236. var faceIndexFunc = `setUint${indexByteCount * 8}`;
  237. if ( indices !== null ) {
  238. for ( var i = 0, l = indices.count; i < l; i += 3 ) {
  239. output.setUint8( fOffset, 3 );
  240. fOffset += 1;
  241. output[ faceIndexFunc ]( fOffset, indices.getX( i + 0 ) + writtenVertices );
  242. fOffset += indexByteCount;
  243. output[ faceIndexFunc ]( fOffset, indices.getX( i + 1 ) + writtenVertices );
  244. fOffset += indexByteCount;
  245. output[ faceIndexFunc ]( fOffset, indices.getX( i + 2 ) + writtenVertices );
  246. fOffset += indexByteCount;
  247. }
  248. } else {
  249. for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
  250. output.setUint8( fOffset, 3 );
  251. fOffset += 1;
  252. output[ faceIndexFunc ]( fOffset, writtenVertices + i );
  253. fOffset += indexByteCount;
  254. output[ faceIndexFunc ]( fOffset, writtenVertices + i + 1 );
  255. fOffset += indexByteCount;
  256. output[ faceIndexFunc ]( fOffset, writtenVertices + i + 2 );
  257. fOffset += indexByteCount;
  258. }
  259. }
  260. }
  261. // Save the amount of verts we've already written so we can offset
  262. // the face index on the next mesh
  263. writtenVertices += vertices.count;
  264. } );
  265. result = output.buffer;
  266. } else {
  267. // Ascii File Generation
  268. // count the number of vertices
  269. var writtenVertices = 0;
  270. var vertexList = '';
  271. var faceList = '';
  272. traverseMeshes( function ( mesh, geometry ) {
  273. var vertices = geometry.getAttribute( 'position' );
  274. var normals = geometry.getAttribute( 'normal' );
  275. var uvs = geometry.getAttribute( 'uv' );
  276. var colors = geometry.getAttribute( 'color' );
  277. var indices = geometry.getIndex();
  278. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  279. // form each line
  280. for ( var i = 0, l = vertices.count; i < l; i ++ ) {
  281. vertex.x = vertices.getX( i );
  282. vertex.y = vertices.getY( i );
  283. vertex.z = vertices.getZ( i );
  284. vertex.applyMatrix4( mesh.matrixWorld );
  285. // Position information
  286. var line =
  287. vertex.x + ' ' +
  288. vertex.y + ' ' +
  289. vertex.z;
  290. // Normal information
  291. if ( includeNormals === true ) {
  292. if ( normals != null ) {
  293. vertex.x = normals.getX( i );
  294. vertex.y = normals.getY( i );
  295. vertex.z = normals.getZ( i );
  296. vertex.applyMatrix3( normalMatrixWorld );
  297. line += ' ' +
  298. vertex.x + ' ' +
  299. vertex.y + ' ' +
  300. vertex.z;
  301. } else {
  302. line += ' 0 0 0';
  303. }
  304. }
  305. // UV information
  306. if ( includeUVs === true ) {
  307. if ( uvs != null ) {
  308. line += ' ' +
  309. uvs.getX( i ) + ' ' +
  310. uvs.getY( i );
  311. } else if ( includeUVs !== false ) {
  312. line += ' 0 0';
  313. }
  314. }
  315. // Color information
  316. if ( includeColors === true ) {
  317. if ( colors != null ) {
  318. line += ' ' +
  319. Math.floor( colors.getX( i ) * 255 ) + ' ' +
  320. Math.floor( colors.getY( i ) * 255 ) + ' ' +
  321. Math.floor( colors.getZ( i ) * 255 );
  322. } else {
  323. line += ' 255 255 255';
  324. }
  325. }
  326. vertexList += line + '\n';
  327. }
  328. // Create the face list
  329. if ( includeIndices === true ) {
  330. if ( indices !== null ) {
  331. for ( var i = 0, l = indices.count; i < l; i += 3 ) {
  332. faceList += `3 ${ indices.getX( i + 0 ) + writtenVertices }`;
  333. faceList += ` ${ indices.getX( i + 1 ) + writtenVertices }`;
  334. faceList += ` ${ indices.getX( i + 2 ) + writtenVertices }\n`;
  335. }
  336. } else {
  337. for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
  338. faceList += `3 ${ writtenVertices + i } ${ writtenVertices + i + 1 } ${ writtenVertices + i + 2 }\n`;
  339. }
  340. }
  341. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  342. }
  343. writtenVertices += vertices.count;
  344. } );
  345. result = `${ header }${vertexList}\n${ includeIndices ? `${faceList}\n` : '' }`;
  346. }
  347. if ( typeof onDone === 'function' ) requestAnimationFrame( () => onDone( result ) );
  348. return result;
  349. }
  350. };