PLYExporter.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 an array of attributes to
  9. * // explicitly exclude from the file:
  10. * // 'color', 'uv', 'normal', 'index'
  11. * var data = exporter.parse(mesh, [ 'color' ]);
  12. *
  13. * Format Definition:
  14. * http://paulbourke.net/dataformats/ply/
  15. */
  16. THREE.PLYExporter = function () {};
  17. THREE.PLYExporter.prototype = {
  18. constructor: THREE.PLYExporter,
  19. parse: function ( object, excludeProperties ) {
  20. if ( Array.isArray( excludeProperties ) !== true ) {
  21. excludeProperties = [];
  22. }
  23. var geomToBufferGeom = new WeakMap();
  24. var includeNormals = false;
  25. var includeColors = false;
  26. var includeUVs = false;
  27. var includeIndices = true;
  28. object.traverse( function ( child ) {
  29. if ( child instanceof THREE.Mesh ) {
  30. var mesh = child;
  31. var geometry = mesh.geometry;
  32. if ( geometry instanceof THREE.Geometry ) {
  33. var bufferGeometry = geomToBufferGeom.get( geometry ) || new THREE.BufferGeometry().setFromObject( mesh );
  34. geomToBufferGeom.set( geometry, bufferGeometry );
  35. geometry = bufferGeometry;
  36. }
  37. if ( geometry instanceof THREE.BufferGeometry ) {
  38. var vertices = geometry.getAttribute( 'position' );
  39. var normals = geometry.getAttribute( 'normal' );
  40. var uvs = geometry.getAttribute( 'uv' );
  41. var colors = geometry.getAttribute( 'color' );
  42. var indices = geometry.getIndex();
  43. if ( vertices == null ) {
  44. return;
  45. }
  46. if ( normals != null ) includeNormals = true;
  47. if ( uvs != null ) includeUVs = true;
  48. if ( colors != null ) includeColors = true;
  49. }
  50. }
  51. } );
  52. includeNormals = includeNormals && excludeProperties.indexOf( 'normal' ) === - 1;
  53. includeColors = includeColors && excludeProperties.indexOf( 'color' ) === - 1;
  54. includeUVs = includeUVs && excludeProperties.indexOf( 'uv' ) === - 1;
  55. includeIndices = includeIndices && excludeProperties.indexOf( 'index' ) === - 1;
  56. // count the number of vertices
  57. var vertexCount = 0;
  58. var faceCount = 0;
  59. var vertexList = '';
  60. var faceList = '';
  61. var vertex = new THREE.Vector3();
  62. var normalMatrixWorld = new THREE.Matrix3();
  63. object.traverse( function ( child ) {
  64. if ( child instanceof THREE.Mesh ) {
  65. var mesh = child;
  66. var geometry = mesh.geometry;
  67. if ( geometry instanceof THREE.Geometry ) {
  68. geometry = geomToBufferGeom.get( geometry );
  69. }
  70. if ( geometry instanceof THREE.BufferGeometry ) {
  71. var vertices = geometry.getAttribute( 'position' );
  72. var normals = geometry.getAttribute( 'normal' );
  73. var uvs = geometry.getAttribute( 'uv' );
  74. var colors = geometry.getAttribute( 'color' );
  75. var indices = geometry.getIndex();
  76. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  77. if ( vertices == null ) {
  78. return;
  79. }
  80. // form each line
  81. for ( var i = 0, l = vertices.count; i < l; i ++ ) {
  82. vertex.x = vertices.getX( i );
  83. vertex.y = vertices.getY( i );
  84. vertex.z = vertices.getZ( i );
  85. vertex.applyMatrix4( mesh.matrixWorld );
  86. // Position information
  87. var line =
  88. vertex.x + ' ' +
  89. vertex.y + ' ' +
  90. vertex.z;
  91. // Normal information
  92. if ( includeNormals === true ) {
  93. if ( normals != null ) {
  94. vertex.x = normals.getX( i );
  95. vertex.y = normals.getY( i );
  96. vertex.z = normals.getZ( i );
  97. vertex.applyMatrix3( normalMatrixWorld );
  98. line += ' ' +
  99. vertex.x + ' ' +
  100. vertex.y + ' ' +
  101. vertex.z;
  102. } else {
  103. line += ' 0 0 0';
  104. }
  105. }
  106. // UV information
  107. if ( includeUVs === true ) {
  108. if ( uvs != null ) {
  109. line += ' ' +
  110. uvs.getX( i ) + ' ' +
  111. uvs.getY( i );
  112. } else if ( includeUVs !== false ) {
  113. line += ' 0 0';
  114. }
  115. }
  116. // Color information
  117. if ( includeColors === true ) {
  118. if ( colors != null ) {
  119. line += ' ' +
  120. Math.floor( colors.getX( i ) * 255 ) + ' ' +
  121. Math.floor( colors.getY( i ) * 255 ) + ' ' +
  122. Math.floor( colors.getZ( i ) * 255 );
  123. } else {
  124. line += ' 255 255 255';
  125. }
  126. }
  127. vertexList += line + '\n';
  128. }
  129. // Create the face list
  130. if ( includeIndices === true ) {
  131. if ( indices !== null ) {
  132. for ( var i = 0, l = indices.count; i < l; i += 3 ) {
  133. faceList += `3 ${ indices.getX( i + 0 ) + vertexCount }`;
  134. faceList += ` ${ indices.getX( i + 1 ) + vertexCount }`;
  135. faceList += ` ${ indices.getX( i + 2 ) + vertexCount }\n`;
  136. }
  137. } else {
  138. for ( var i = 0, l = vertices.count; i < l; i += 3 ) {
  139. faceList += `3 ${ vertexCount + i } ${ vertexCount + i + 1 } ${ vertexCount + i + 2 }\n`;
  140. }
  141. }
  142. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  143. }
  144. vertexCount += vertices.count;
  145. }
  146. }
  147. } );
  148. if ( includeIndices && faceCount !== Math.floor( faceCount ) ) {
  149. // point cloud meshes will not have an index array and may not have a
  150. // number of vertices that is divisble by 3 (and therefore representable
  151. // as triangles)
  152. console.error(
  153. 'PLYExporter: Failed to generate a valid PLY file with triangle indices because the ' +
  154. 'number of indices is not divisible by 3.'
  155. );
  156. return null;
  157. }
  158. var output =
  159. 'ply\n' +
  160. 'format ascii 1.0\n' +
  161. `element vertex ${vertexCount}\n` +
  162. // position
  163. 'property float x\n' +
  164. 'property float y\n' +
  165. 'property float z\n';
  166. if ( includeNormals === true ) {
  167. // normal
  168. output +=
  169. 'property float nx\n' +
  170. 'property float ny\n' +
  171. 'property float nz\n';
  172. }
  173. if ( includeUVs === true ) {
  174. // uvs
  175. output +=
  176. 'property float s\n' +
  177. 'property float t\n';
  178. }
  179. if ( includeColors === true ) {
  180. // colors
  181. output +=
  182. 'property uchar red\n' +
  183. 'property uchar green\n' +
  184. 'property uchar blue\n';
  185. }
  186. if ( includeIndices === true ) {
  187. // faces
  188. output +=
  189. `element face ${faceCount}\n` +
  190. 'property list uchar int vertex_index\n';
  191. }
  192. output +=
  193. 'end_header\n' +
  194. `${vertexList}\n` +
  195. ( includeIndices ? `${faceList}\n` : '' );
  196. return output;
  197. }
  198. };