ParametricGeometry.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @author zz85 / https://github.com/zz85
  3. *
  4. * Parametric Surfaces Geometry
  5. * based on the brilliant article by @prideout http://prideout.net/blog/?p=44
  6. */
  7. import { Geometry } from '../core/Geometry';
  8. function ParametricGeometry( func, slices, stacks ) {
  9. Geometry.call( this );
  10. this.type = 'ParametricGeometry';
  11. this.parameters = {
  12. func: func,
  13. slices: slices,
  14. stacks: stacks
  15. };
  16. this.fromBufferGeometry( new ParametricBufferGeometry( func, slices, stacks ) );
  17. this.mergeVertices();
  18. }
  19. ParametricGeometry.prototype = Object.create( Geometry.prototype );
  20. ParametricGeometry.prototype.constructor = ParametricGeometry;
  21. /**
  22. * @author Mugen87 / https://github.com/Mugen87
  23. *
  24. * Parametric Surfaces Geometry
  25. * based on the brilliant article by @prideout http://prideout.net/blog/?p=44
  26. */
  27. import { BufferGeometry } from '../core/BufferGeometry';
  28. import { Float32BufferAttribute, Uint16BufferAttribute, Uint32BufferAttribute } from '../core/BufferAttribute';
  29. function ParametricBufferGeometry( func, slices, stacks ) {
  30. BufferGeometry.call( this );
  31. this.type = 'ParametricBufferGeometry';
  32. this.parameters = {
  33. func: func,
  34. slices: slices,
  35. stacks: stacks
  36. };
  37. // buffers
  38. var vertices = [];
  39. var uvs = [];
  40. var i, j, p;
  41. var u, v;
  42. // generate vertices and uvs
  43. var sliceCount = slices + 1;
  44. for ( i = 0; i <= stacks; i ++ ) {
  45. v = i / stacks;
  46. for ( j = 0; j <= slices; j ++ ) {
  47. u = j / slices;
  48. p = func( u, v );
  49. vertices.push( p.x, p.y, p.z );
  50. uvs.push( u, v );
  51. }
  52. }
  53. // generate indices
  54. var indices = [];
  55. var a, b, c, d;
  56. for ( i = 0; i < stacks; i ++ ) {
  57. for ( j = 0; j < slices; j ++ ) {
  58. a = i * sliceCount + j;
  59. b = i * sliceCount + j + 1;
  60. c = ( i + 1 ) * sliceCount + j + 1;
  61. d = ( i + 1 ) * sliceCount + j;
  62. // faces one and two
  63. indices.push( a, b, d );
  64. indices.push( b, c, d );
  65. }
  66. }
  67. // build geometry
  68. this.setIndex( new ( indices.length > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 ) );
  69. this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  70. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  71. // generate normals
  72. this.computeVertexNormals();
  73. }
  74. ParametricBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  75. ParametricBufferGeometry.prototype.constructor = ParametricBufferGeometry;
  76. export { ParametricGeometry, ParametricBufferGeometry };
粤ICP备19079148号