PLYExporter.js 12 KB

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