OBJExporter.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import {
  2. Color,
  3. ColorManagement,
  4. Matrix3,
  5. SRGBColorSpace,
  6. Vector2,
  7. Vector3
  8. } from 'three';
  9. class OBJExporter {
  10. parse( object ) {
  11. let output = '';
  12. let indexVertex = 0;
  13. let indexVertexUvs = 0;
  14. let indexNormals = 0;
  15. const vertex = new Vector3();
  16. const color = new Color();
  17. const normal = new Vector3();
  18. const uv = new Vector2();
  19. const face = [];
  20. function parseMesh( mesh ) {
  21. let nbVertex = 0;
  22. let nbNormals = 0;
  23. let nbVertexUvs = 0;
  24. const geometry = mesh.geometry;
  25. const normalMatrixWorld = new Matrix3();
  26. // shortcuts
  27. const vertices = geometry.getAttribute( 'position' );
  28. const normals = geometry.getAttribute( 'normal' );
  29. const uvs = geometry.getAttribute( 'uv' );
  30. const indices = geometry.getIndex();
  31. // name of the mesh object
  32. output += 'o ' + mesh.name + '\n';
  33. // name of the mesh material
  34. if ( mesh.material && mesh.material.name ) {
  35. output += 'usemtl ' + mesh.material.name + '\n';
  36. }
  37. // vertices
  38. if ( vertices !== undefined ) {
  39. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  40. vertex.fromBufferAttribute( vertices, i );
  41. // transform the vertex to world space
  42. vertex.applyMatrix4( mesh.matrixWorld );
  43. // transform the vertex to export format
  44. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  45. }
  46. }
  47. // uvs
  48. if ( uvs !== undefined ) {
  49. for ( let i = 0, l = uvs.count; i < l; i ++, nbVertexUvs ++ ) {
  50. uv.fromBufferAttribute( uvs, i );
  51. // transform the uv to export format
  52. output += 'vt ' + uv.x + ' ' + uv.y + '\n';
  53. }
  54. }
  55. // normals
  56. if ( normals !== undefined ) {
  57. normalMatrixWorld.getNormalMatrix( mesh.matrixWorld );
  58. for ( let i = 0, l = normals.count; i < l; i ++, nbNormals ++ ) {
  59. normal.fromBufferAttribute( normals, i );
  60. // transform the normal to world space
  61. normal.applyMatrix3( normalMatrixWorld ).normalize();
  62. // transform the normal to export format
  63. output += 'vn ' + normal.x + ' ' + normal.y + ' ' + normal.z + '\n';
  64. }
  65. }
  66. // faces
  67. if ( indices !== null ) {
  68. for ( let i = 0, l = indices.count; i < l; i += 3 ) {
  69. for ( let m = 0; m < 3; m ++ ) {
  70. const j = indices.getX( i + m ) + 1;
  71. face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
  72. }
  73. // transform the face to export format
  74. output += 'f ' + face.join( ' ' ) + '\n';
  75. }
  76. } else {
  77. for ( let i = 0, l = vertices.count; i < l; i += 3 ) {
  78. for ( let m = 0; m < 3; m ++ ) {
  79. const j = i + m + 1;
  80. face[ m ] = ( indexVertex + j ) + ( normals || uvs ? '/' + ( uvs ? ( indexVertexUvs + j ) : '' ) + ( normals ? '/' + ( indexNormals + j ) : '' ) : '' );
  81. }
  82. // transform the face to export format
  83. output += 'f ' + face.join( ' ' ) + '\n';
  84. }
  85. }
  86. // update index
  87. indexVertex += nbVertex;
  88. indexVertexUvs += nbVertexUvs;
  89. indexNormals += nbNormals;
  90. }
  91. function parseLine( line ) {
  92. let nbVertex = 0;
  93. const geometry = line.geometry;
  94. const type = line.type;
  95. // shortcuts
  96. const vertices = geometry.getAttribute( 'position' );
  97. // name of the line object
  98. output += 'o ' + line.name + '\n';
  99. if ( vertices !== undefined ) {
  100. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  101. vertex.fromBufferAttribute( vertices, i );
  102. // transform the vertex to world space
  103. vertex.applyMatrix4( line.matrixWorld );
  104. // transform the vertex to export format
  105. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z + '\n';
  106. }
  107. }
  108. if ( type === 'Line' ) {
  109. output += 'l ';
  110. for ( let j = 1, l = vertices.count; j <= l; j ++ ) {
  111. output += ( indexVertex + j ) + ' ';
  112. }
  113. output += '\n';
  114. }
  115. if ( type === 'LineSegments' ) {
  116. for ( let j = 1, k = j + 1, l = vertices.count; j < l; j += 2, k = j + 1 ) {
  117. output += 'l ' + ( indexVertex + j ) + ' ' + ( indexVertex + k ) + '\n';
  118. }
  119. }
  120. // update index
  121. indexVertex += nbVertex;
  122. }
  123. function parsePoints( points ) {
  124. let nbVertex = 0;
  125. const geometry = points.geometry;
  126. const vertices = geometry.getAttribute( 'position' );
  127. const colors = geometry.getAttribute( 'color' );
  128. output += 'o ' + points.name + '\n';
  129. if ( vertices !== undefined ) {
  130. for ( let i = 0, l = vertices.count; i < l; i ++, nbVertex ++ ) {
  131. vertex.fromBufferAttribute( vertices, i );
  132. vertex.applyMatrix4( points.matrixWorld );
  133. output += 'v ' + vertex.x + ' ' + vertex.y + ' ' + vertex.z;
  134. if ( colors !== undefined ) {
  135. color.fromBufferAttribute( colors, i );
  136. ColorManagement.fromWorkingColorSpace( color, SRGBColorSpace );
  137. output += ' ' + color.r + ' ' + color.g + ' ' + color.b;
  138. }
  139. output += '\n';
  140. }
  141. output += 'p ';
  142. for ( let j = 1, l = vertices.count; j <= l; j ++ ) {
  143. output += ( indexVertex + j ) + ' ';
  144. }
  145. output += '\n';
  146. }
  147. // update index
  148. indexVertex += nbVertex;
  149. }
  150. object.traverse( function ( child ) {
  151. if ( child.isMesh === true ) {
  152. parseMesh( child );
  153. }
  154. if ( child.isLine === true ) {
  155. parseLine( child );
  156. }
  157. if ( child.isPoints === true ) {
  158. parsePoints( child );
  159. }
  160. } );
  161. return output;
  162. }
  163. }
  164. export { OBJExporter };
粤ICP备19079148号