ParametricBufferGeometry.js 1.7 KB

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