LoftGeometry.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute,
  4. ShapeUtils,
  5. Vector2,
  6. Vector3
  7. } from 'three';
  8. const _vector = /*@__PURE__*/ new Vector3();
  9. /**
  10. * This class can be used to generate a geometry by lofting (skinning) a surface
  11. * through a series of cross sections. Each section is an array of points in 3D
  12. * space and all sections must have the same number of points.
  13. *
  14. * `LoftGeometry` is the general case of geometries like {@link LatheGeometry}
  15. * (which revolves a fixed profile around an axis) or {@link TubeGeometry}
  16. * (which sweeps a circular section along a path): the sections can have any
  17. * shape, and can change shape, size, position and orientation from one
  18. * section to the next.
  19. *
  20. * Sections wind around the loft so the resulting face normals point outwards
  21. * when each section is ordered counterclockwise as seen from the end of the
  22. * loft, looking back towards the start. If the surface appears inside out,
  23. * reverse the point order of each section.
  24. *
  25. * ```js
  26. * const sections = [];
  27. *
  28. * for ( let i = 0; i <= 10; i ++ ) {
  29. *
  30. * const points = [];
  31. * const radius = 2 + Math.sin( i * 0.8 );
  32. *
  33. * for ( let j = 0; j < 32; j ++ ) {
  34. *
  35. * const angle = j / 32 * Math.PI * 2;
  36. * points.push( new THREE.Vector3( Math.sin( angle ) * radius, i, Math.cos( angle ) * radius ) );
  37. *
  38. * }
  39. *
  40. * sections.push( points );
  41. *
  42. * }
  43. *
  44. * const geometry = new LoftGeometry( sections, { capStart: true, capEnd: true } );
  45. * const material = new THREE.MeshStandardMaterial( { color: 0x00ff00 } );
  46. * const mesh = new THREE.Mesh( geometry, material );
  47. * scene.add( mesh );
  48. * ```
  49. *
  50. * @augments BufferGeometry
  51. * @three_import import { LoftGeometry } from 'three/addons/geometries/LoftGeometry.js';
  52. */
  53. class LoftGeometry extends BufferGeometry {
  54. /**
  55. * Constructs a new loft geometry.
  56. *
  57. * @param {Array<Array<Vector3>>} sections - The cross sections to skin. At least
  58. * two sections are required and all sections must have the same number of points.
  59. * @param {Object} [options={}] - The loft options.
  60. * @param {boolean} [options.closed=true] - Whether each section is treated as a
  61. * closed ring (e.g. a fuselage) or an open strip (e.g. a ribbon).
  62. * @param {boolean} [options.capStart=false] - Whether the first section is closed
  63. * with a cap or not.
  64. * @param {boolean} [options.capEnd=false] - Whether the last section is closed
  65. * with a cap or not.
  66. */
  67. constructor( sections = [], options = {} ) {
  68. super();
  69. this.type = 'LoftGeometry';
  70. const { closed = true, capStart = false, capEnd = false } = options;
  71. /**
  72. * Holds the constructor parameters that have been
  73. * used to generate the geometry. Any modification
  74. * after instantiation does not change the geometry.
  75. *
  76. * @type {Object}
  77. */
  78. this.parameters = {
  79. sections: sections,
  80. closed: closed,
  81. capStart: capStart,
  82. capEnd: capEnd
  83. };
  84. const rows = sections.length;
  85. if ( rows < 2 ) {
  86. console.error( 'THREE.LoftGeometry: At least two sections are required.' );
  87. return;
  88. }
  89. const columns = sections[ 0 ].length;
  90. for ( let i = 1; i < rows; i ++ ) {
  91. if ( sections[ i ].length !== columns ) {
  92. console.error( 'THREE.LoftGeometry: All sections must have the same number of points.' );
  93. return;
  94. }
  95. }
  96. // closed sections repeat their first point so the surface can wrap
  97. // around with continuous uvs
  98. const pointsPerRow = closed ? columns + 1 : columns;
  99. // buffers
  100. const indices = [];
  101. const vertices = [];
  102. const uvs = [];
  103. // generate vertices and uvs
  104. for ( let i = 0; i < rows; i ++ ) {
  105. const section = sections[ i ];
  106. for ( let j = 0; j < pointsPerRow; j ++ ) {
  107. const point = section[ j % columns ];
  108. vertices.push( point.x, point.y, point.z );
  109. uvs.push( i / ( rows - 1 ), j / ( pointsPerRow - 1 ) );
  110. }
  111. }
  112. // generate indices
  113. for ( let i = 0; i < rows - 1; i ++ ) {
  114. for ( let j = 0; j < pointsPerRow - 1; j ++ ) {
  115. const a = i * pointsPerRow + j;
  116. const b = i * pointsPerRow + j + 1;
  117. const c = ( i + 1 ) * pointsPerRow + j + 1;
  118. const d = ( i + 1 ) * pointsPerRow + j;
  119. // faces one and two
  120. indices.push( a, b, d );
  121. indices.push( b, c, d );
  122. }
  123. }
  124. // generate caps
  125. if ( capStart === true ) generateCap( 0 );
  126. if ( capEnd === true ) generateCap( rows - 1 );
  127. // build geometry
  128. this.setIndex( indices );
  129. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  130. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  131. this.computeVertexNormals();
  132. // the seam vertices of closed sections are duplicated so their computed
  133. // normals must be averaged to achieve smooth shading across the seam
  134. if ( closed === true ) {
  135. const normals = this.getAttribute( 'normal' );
  136. for ( let i = 0; i < rows; i ++ ) {
  137. const a = i * pointsPerRow;
  138. const b = i * pointsPerRow + ( pointsPerRow - 1 );
  139. _vector.set(
  140. normals.getX( a ) + normals.getX( b ),
  141. normals.getY( a ) + normals.getY( b ),
  142. normals.getZ( a ) + normals.getZ( b )
  143. ).normalize();
  144. normals.setXYZ( a, _vector.x, _vector.y, _vector.z );
  145. normals.setXYZ( b, _vector.x, _vector.y, _vector.z );
  146. }
  147. }
  148. function generateCap( sectionIndex ) {
  149. const section = sections[ sectionIndex ];
  150. // compute the centroid of the section and the normal of its plane
  151. // via Newell's method
  152. const centroid = new Vector3();
  153. const normal = new Vector3();
  154. for ( let i = 0; i < columns; i ++ ) {
  155. const p = section[ i ];
  156. const q = section[ ( i + 1 ) % columns ];
  157. centroid.add( p );
  158. normal.x += ( p.y - q.y ) * ( p.z + q.z );
  159. normal.y += ( p.z - q.z ) * ( p.x + q.x );
  160. normal.z += ( p.x - q.x ) * ( p.y + q.y );
  161. }
  162. centroid.divideScalar( columns );
  163. normal.normalize();
  164. // make sure the cap faces away from the rest of the surface
  165. const neighbor = sections[ sectionIndex === 0 ? 1 : rows - 2 ];
  166. _vector.set( 0, 0, 0 );
  167. for ( let i = 0; i < columns; i ++ ) _vector.add( neighbor[ i ] );
  168. _vector.divideScalar( columns ).sub( centroid );
  169. if ( normal.dot( _vector ) > 0 ) normal.negate();
  170. // project the section onto the cap plane
  171. const tangent = new Vector3( 1, 0, 0 );
  172. if ( Math.abs( normal.x ) > 0.9 ) tangent.set( 0, 1, 0 );
  173. const bitangent = new Vector3().crossVectors( normal, tangent ).normalize();
  174. tangent.crossVectors( bitangent, normal );
  175. const contour = [];
  176. const points = section.slice();
  177. for ( let i = 0; i < columns; i ++ ) {
  178. _vector.subVectors( points[ i ], centroid );
  179. contour.push( new Vector2( _vector.dot( tangent ), _vector.dot( bitangent ) ) );
  180. }
  181. // triangulateShape() expects contours in counterclockwise order
  182. if ( ShapeUtils.isClockWise( contour ) === true ) {
  183. contour.reverse();
  184. points.reverse();
  185. }
  186. const faces = ShapeUtils.triangulateShape( contour, [] );
  187. // compute the bounding box of the contour for uv generation
  188. const min = new Vector2( Infinity, Infinity );
  189. const max = new Vector2( - Infinity, - Infinity );
  190. for ( let i = 0; i < columns; i ++ ) {
  191. min.min( contour[ i ] );
  192. max.max( contour[ i ] );
  193. }
  194. const width = Math.max( max.x - min.x, Number.EPSILON );
  195. const height = Math.max( max.y - min.y, Number.EPSILON );
  196. // generate vertices, uvs and indices; cap vertices are not shared
  197. // with the wall so the cap is flat shaded with a hard edge
  198. const indexOffset = vertices.length / 3;
  199. for ( let i = 0; i < columns; i ++ ) {
  200. const point = points[ i ];
  201. vertices.push( point.x, point.y, point.z );
  202. uvs.push( ( contour[ i ].x - min.x ) / width, ( contour[ i ].y - min.y ) / height );
  203. }
  204. for ( let i = 0; i < faces.length; i ++ ) {
  205. const face = faces[ i ];
  206. indices.push( indexOffset + face[ 0 ], indexOffset + face[ 1 ], indexOffset + face[ 2 ] );
  207. }
  208. }
  209. }
  210. copy( source ) {
  211. super.copy( source );
  212. this.parameters = Object.assign( {}, source.parameters );
  213. return this;
  214. }
  215. }
  216. export { LoftGeometry };
粤ICP备19079148号