ParametricGeometry.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import {
  2. BufferGeometry,
  3. Float32BufferAttribute,
  4. Vector3
  5. } from 'three';
  6. /**
  7. * This class can be used to generate a geometry based on a parametric surface.
  8. *
  9. * Reference: [Mesh Generation with Python]{@link https://prideout.net/blog/old/blog/index.html@p=44.html}
  10. *
  11. * ```js
  12. * const geometry = new THREE.ParametricGeometry( klein, 25, 25 );
  13. * const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
  14. * const klein = new THREE.Mesh( geometry, material );
  15. * scene.add( klein );
  16. * ```
  17. *
  18. * @augments BufferGeometry
  19. */
  20. class ParametricGeometry extends BufferGeometry {
  21. /**
  22. * Constructs a new parametric geometry.
  23. *
  24. * @param {ParametricGeometry~Func} func - The parametric function. Default is a function that generates a curved plane surface.
  25. * @param {number} [slices=8] - The number of slices to use for the parametric function.
  26. * @param {number} [stacks=8] - The stacks of slices to use for the parametric function.
  27. */
  28. constructor( func = ( u, v, target ) => target.set( u, v, Math.cos( u ) * Math.sin( v ) ), slices = 8, stacks = 8 ) {
  29. super();
  30. this.type = 'ParametricGeometry';
  31. /**
  32. * Holds the constructor parameters that have been
  33. * used to generate the geometry. Any modification
  34. * after instantiation does not change the geometry.
  35. *
  36. * @type {Object}
  37. */
  38. this.parameters = {
  39. func: func,
  40. slices: slices,
  41. stacks: stacks
  42. };
  43. // buffers
  44. const indices = [];
  45. const vertices = [];
  46. const normals = [];
  47. const uvs = [];
  48. const EPS = 0.00001;
  49. const normal = new Vector3();
  50. const p0 = new Vector3(), p1 = new Vector3();
  51. const pu = new Vector3(), pv = new Vector3();
  52. // generate vertices, normals and uvs
  53. const sliceCount = slices + 1;
  54. for ( let i = 0; i <= stacks; i ++ ) {
  55. const v = i / stacks;
  56. for ( let j = 0; j <= slices; j ++ ) {
  57. const u = j / slices;
  58. // vertex
  59. func( u, v, p0 );
  60. vertices.push( p0.x, p0.y, p0.z );
  61. // normal
  62. // approximate tangent vectors via finite differences
  63. if ( u - EPS >= 0 ) {
  64. func( u - EPS, v, p1 );
  65. pu.subVectors( p0, p1 );
  66. } else {
  67. func( u + EPS, v, p1 );
  68. pu.subVectors( p1, p0 );
  69. }
  70. if ( v - EPS >= 0 ) {
  71. func( u, v - EPS, p1 );
  72. pv.subVectors( p0, p1 );
  73. } else {
  74. func( u, v + EPS, p1 );
  75. pv.subVectors( p1, p0 );
  76. }
  77. // cross product of tangent vectors returns surface normal
  78. normal.crossVectors( pu, pv ).normalize();
  79. normals.push( normal.x, normal.y, normal.z );
  80. // uv
  81. uvs.push( u, v );
  82. }
  83. }
  84. // generate indices
  85. for ( let i = 0; i < stacks; i ++ ) {
  86. for ( let j = 0; j < slices; j ++ ) {
  87. const a = i * sliceCount + j;
  88. const b = i * sliceCount + j + 1;
  89. const c = ( i + 1 ) * sliceCount + j + 1;
  90. const d = ( i + 1 ) * sliceCount + j;
  91. // faces one and two
  92. indices.push( a, b, d );
  93. indices.push( b, c, d );
  94. }
  95. }
  96. // build geometry
  97. this.setIndex( indices );
  98. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  99. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  100. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  101. }
  102. copy( source ) {
  103. super.copy( source );
  104. this.parameters = Object.assign( {}, source.parameters );
  105. return this;
  106. }
  107. }
  108. /**
  109. * Parametric function definition of `ParametricGeometry`.
  110. *
  111. * @callback ParametricGeometry~Func
  112. * @param {number} u - The `u` coordinate on the surface in the range `[0,1]`.
  113. * @param {number} v - The `v` coordinate on the surface in the range `[0,1]`.
  114. * @param {Vector3} target - The target vector that is used to store the method's result.
  115. */
  116. export { ParametricGeometry };
粤ICP备19079148号