CircleGeometry.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @author hughes
  3. */
  4. import { Geometry } from '../core/Geometry';
  5. function CircleGeometry( radius, segments, thetaStart, thetaLength ) {
  6. Geometry.call( this );
  7. this.type = 'CircleGeometry';
  8. this.parameters = {
  9. radius: radius,
  10. segments: segments,
  11. thetaStart: thetaStart,
  12. thetaLength: thetaLength
  13. };
  14. this.fromBufferGeometry( new CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) );
  15. }
  16. CircleGeometry.prototype = Object.create( Geometry.prototype );
  17. CircleGeometry.prototype.constructor = CircleGeometry;
  18. /**
  19. * @author benaadams / https://twitter.com/ben_a_adams
  20. * @author Mugen87 / https://github.com/Mugen87
  21. */
  22. import { Float32BufferAttribute } from '../core/BufferAttribute';
  23. import { BufferGeometry } from '../core/BufferGeometry';
  24. import { Vector3 } from '../math/Vector3';
  25. import { Vector2 } from '../math/Vector2';
  26. function CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) {
  27. BufferGeometry.call( this );
  28. this.type = 'CircleBufferGeometry';
  29. this.parameters = {
  30. radius: radius,
  31. segments: segments,
  32. thetaStart: thetaStart,
  33. thetaLength: thetaLength
  34. };
  35. radius = radius || 50;
  36. segments = segments !== undefined ? Math.max( 3, segments ) : 8;
  37. thetaStart = thetaStart !== undefined ? thetaStart : 0;
  38. thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
  39. // buffers
  40. var indices = [];
  41. var vertices = [];
  42. var normals = [];
  43. var uvs = [];
  44. // helper variables
  45. var i, s;
  46. var vertex = new Vector3();
  47. var uv = new Vector2();
  48. // center point
  49. vertices.push( 0, 0, 0 );
  50. normals.push( 0, 0, 1 );
  51. uvs.push( 0.5, 0.5 );
  52. for ( s = 0, i = 3; s <= segments; s ++, i += 3 ) {
  53. var segment = thetaStart + s / segments * thetaLength;
  54. // vertex
  55. vertex.x = radius * Math.cos( segment );
  56. vertex.y = radius * Math.sin( segment );
  57. vertices.push( vertex.x, vertex.y, vertex.z );
  58. // normal
  59. normals.push( 0, 0, 1 );
  60. // uvs
  61. uv.x = ( vertices[ i ] / radius + 1 ) / 2;
  62. uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2;
  63. uvs.push( uv.x, uv.y );
  64. }
  65. // indices
  66. for ( i = 1; i <= segments; i ++ ) {
  67. indices.push( i, i + 1, 0 );
  68. }
  69. // build geometry
  70. this.setIndex( indices );
  71. this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  72. this.addAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  73. this.addAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  74. }
  75. CircleBufferGeometry.prototype = Object.create( BufferGeometry.prototype );
  76. CircleBufferGeometry.prototype.constructor = CircleBufferGeometry;
  77. export { CircleGeometry, CircleBufferGeometry };
粤ICP备19079148号