PLYBinaryExporter.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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.PLYBinaryExporter();
  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.PLYBinaryExporter = function () {};
  17. THREE.PLYBinaryExporter.prototype = {
  18. constructor: THREE.PLYBinaryExporter,
  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. // count the number of vertices
  29. var vertexCount = 0;
  30. var faceCount = 0;
  31. object.traverse( function ( child ) {
  32. if ( child instanceof THREE.Mesh ) {
  33. var mesh = child;
  34. var geometry = mesh.geometry;
  35. if ( geometry instanceof THREE.Geometry ) {
  36. var bufferGeometry = geomToBufferGeom.get( geometry ) || new THREE.BufferGeometry().setFromObject( mesh );
  37. geomToBufferGeom.set( geometry, bufferGeometry );
  38. geometry = bufferGeometry;
  39. }
  40. if ( geometry instanceof THREE.BufferGeometry ) {
  41. var vertices = geometry.getAttribute( 'position' );
  42. var normals = geometry.getAttribute( 'normal' );
  43. var uvs = geometry.getAttribute( 'uv' );
  44. var colors = geometry.getAttribute( 'color' );
  45. var indices = geometry.getIndex();
  46. if ( vertices == null ) {
  47. return;
  48. }
  49. vertexCount += vertices.count;
  50. faceCount += indices ? indices.count / 3 : vertices.count / 3;
  51. if ( normals != null ) includeNormals = true;
  52. if ( uvs != null ) includeUVs = true;
  53. if ( colors != null ) includeColors = true;
  54. }
  55. }
  56. } );
  57. includeNormals = includeNormals && excludeProperties.indexOf( 'normal' ) === - 1;
  58. includeColors = includeColors && excludeProperties.indexOf( 'color' ) === - 1;
  59. includeUVs = includeUVs && excludeProperties.indexOf( 'uv' ) === - 1;
  60. includeIndices = includeIndices && excludeProperties.indexOf( 'index' ) === - 1;
  61. if ( includeIndices && faceCount !== Math.floor( faceCount ) ) {
  62. // point cloud meshes will not have an index array and may not have a
  63. // number of vertices that is divisble by 3 (and therefore representable
  64. // as triangles)
  65. console.error(
  66. 'PLYBinaryExporter: Failed to generate a valid PLY file with triangle indices because the ' +
  67. 'number of indices is not divisible by 3.'
  68. );
  69. return null;
  70. }
  71. // get how many bytes will be needed to save out the faces
  72. // so we can use a minimal amount of memory / data
  73. var indexByteCount = 1;
  74. if ( vertexCount > 256 ) { // 2^8 bits
  75. indexByteCount = 2;
  76. }
  77. if ( vertexCount > 65536 ) { // 2^16 bits
  78. indexByteCount = 4;
  79. }
  80. // Form the header
  81. var header =
  82. 'ply\n' +
  83. 'format binary_big_endian 1.0\n' +
  84. `element vertex ${vertexCount}\n` +
  85. // position
  86. 'property float x\n' +
  87. 'property float y\n' +
  88. 'property float z\n';
  89. if ( includeNormals === true ) {
  90. // normal
  91. header +=
  92. 'property float nx\n' +
  93. 'property float ny\n' +
  94. 'property float nz\n';
  95. }
  96. if ( includeUVs === true ) {
  97. // uvs
  98. header +=
  99. 'property float s\n' +
  100. 'property float t\n';
  101. }
  102. if ( includeColors === true ) {
  103. // colors
  104. header +=
  105. 'property uchar red\n' +
  106. 'property uchar green\n' +
  107. 'property uchar blue\n';
  108. }
  109. if ( includeIndices === true ) {
  110. // faces
  111. header +=
  112. `element face ${faceCount}\n` +
  113. `property list uchar uint${ indexByteCount * 8 } vertex_index\n`;
  114. }
  115. header += 'end_header\n';
  116. var headerBin = new TextEncoder().encode( header );
  117. // 3 position values at 4 bytes
  118. // 3 normal values at 4 bytes
  119. // 3 color channels with 1 byte
  120. // 2 uv values at 4 bytes
  121. var vertexListLength = vertexCount * ( 4 * 3 + ( includeNormals ? 4 * 3 : 0 ) + ( includeColors ? 3 : 0 ) + ( includeUVs ? 4 * 2 : 0 ) );
  122. // 1 byte shape desciptor
  123. // 3 vertex indices at ${indexByteCount} bytes
  124. var faceListLength = includeIndices ? faceCount * ( indexByteCount * 3 + 1 ) : 0;
  125. var output = new DataView( new ArrayBuffer( headerBin.length + vertexListLength + faceListLength ) );
  126. new Uint8Array( output.buffer ).set( headerBin, 0 );
  127. var vOffset = headerBin.length;
  128. var fOffset = headerBin.length + vertexListLength;
  129. var writtenVertices = 0;
  130. var vertex = new THREE.Vector3();
  131. var normalMatrixWorld = new THREE.Matrix3();
  132. object.traverse( function ( child ) {
  133. if ( child instanceof THREE.Mesh ) {
  134. var mesh = child;
  135. var geometry = mesh.geometry;
  136. if ( geometry instanceof THREE.Geometry ) {
  137. geometry = geomToBufferGeom.get( geometry );
  138. }
  139. if ( geometry instanceof THREE.BufferGeometry ) {
  140. var vertices = geometry.getAttribute( 'position' );
  141. var normals = geometry.getAttribute( 'normal' );
  142. var uvs = geometry.getAttribute( 'uv' );
  143. var colors = geometry.getAttribute( 'color' );
  144. var indices = geometry.getIndex();
  145. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  146. if ( vertices == null ) {
  147. return;
  148. }
  149. // form each line
  150. for ( var i = 0, l = vertices.count; i < l; i ++ ) {
  151. vertex.x = vertices.getX( i );
  152. vertex.y = vertices.getY( i );
  153. vertex.z = vertices.getZ( i );
  154. vertex.applyMatrix4( mesh.matrixWorld );
  155. // Position information
  156. output.setFloat32( vOffset, vertex.x );
  157. vOffset += 4;
  158. output.setFloat32( vOffset, vertex.y );
  159. vOffset += 4;
  160. output.setFloat32( vOffset, vertex.z );
  161. vOffset += 4;
  162. // Normal information
  163. if ( includeNormals === true ) {
  164. if ( normals != null ) {
  165. vertex.x = normals.getX( i );
  166. vertex.y = normals.getY( i );
  167. vertex.z = normals.getZ( i );
  168. vertex.applyMatrix3( normalMatrixWorld );
  169. output.setFloat32( vOffset, vertex.x );
  170. vOffset += 4;
  171. output.setFloat32( vOffset, vertex.y );
  172. vOffset += 4;
  173. output.setFloat32( vOffset, vertex.z );
  174. vOffset += 4;
  175. } else {
  176. output.setFloat32( vOffset, 0 );
  177. vOffset += 4;
  178. output.setFloat32( vOffset, 0 );
  179. vOffset += 4;
  180. output.setFloat32( vOffset, 0 );
  181. vOffset += 4;
  182. }
  183. }
  184. // UV information
  185. if ( includeUVs === true ) {
  186. if ( uvs != null ) {
  187. output.setFloat32( vOffset, uvs.getX( i ) );
  188. vOffset += 4;
  189. output.setFloat32( vOffset, uvs.getY( i ) );
  190. vOffset += 4;
  191. } else if ( includeUVs !== false ) {
  192. output.setFloat32( vOffset, 0 );
  193. vOffset += 4;
  194. output.setFloat32( vOffset, 0 );
  195. vOffset += 4;
  196. }
  197. }
  198. // Color information
  199. if ( includeColors === true ) {
  200. if ( colors != null ) {
  201. output.setUint8( vOffset, Math.floor( colors.getX( i ) * 255 ) );
  202. vOffset += 1;
  203. output.setUint8( vOffset, Math.floor( colors.getY( i ) * 255 ) );
  204. vOffset += 1;
  205. output.setUint8( vOffset, Math.floor( colors.getZ( i ) * 255 ) );
  206. vOffset += 1;
  207. } else {
  208. output.setUint8( vOffset, 255 );
  209. vOffset += 1;
  210. output.setUint8( vOffset, 255 );
  211. vOffset += 1;
  212. output.setUint8( vOffset, 255 );
  213. vOffset += 1;
  214. }
  215. }
  216. }
  217. if ( includeIndices === true ) {
  218. // Create the face list
  219. var faceIndexFunc = `setUint${indexByteCount * 8}`;
  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[ faceIndexFunc ]( fOffset, indices.getX( i + 0 ) + writtenVertices );
  225. fOffset += indexByteCount;
  226. output[ faceIndexFunc ]( fOffset, indices.getX( i + 1 ) + writtenVertices );
  227. fOffset += indexByteCount;
  228. output[ faceIndexFunc ]( fOffset, indices.getX( i + 2 ) + writtenVertices );
  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[ faceIndexFunc ]( fOffset, writtenVertices + i );
  236. fOffset += indexByteCount;
  237. output[ faceIndexFunc ]( fOffset, writtenVertices + i + 1 );
  238. fOffset += indexByteCount;
  239. output[ faceIndexFunc ]( fOffset, writtenVertices + i + 2 );
  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. }
  249. } );
  250. return output;
  251. }
  252. };