PLYExporter.js 12 KB

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