PLYExporter.js 12 KB

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