PLYExporter.js 12 KB

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