PLYExporter.js 12 KB

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