CircleBufferGeometry.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @author benaadams / https://twitter.com/ben_a_adams
  3. */
  4. THREE.CircleBufferGeometry = function ( radius, segments, thetaStart, thetaLength ) {
  5. THREE.BufferGeometry.call( this );
  6. this.type = 'CircleBufferGeometry';
  7. this.parameters = {
  8. radius: radius,
  9. segments: segments,
  10. thetaStart: thetaStart,
  11. thetaLength: thetaLength
  12. };
  13. radius = radius || 50;
  14. segments = segments !== undefined ? Math.max( 3, segments ) : 8;
  15. thetaStart = thetaStart !== undefined ? thetaStart : 0;
  16. thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
  17. var vertices = segments + 2;
  18. var positions = new Float32Array( vertices * 3 );
  19. var normals = new Float32Array( vertices * 3 );
  20. var uvs = new Float32Array( vertices * 2 );
  21. // center data is already zero, but need to set a few extras
  22. normals[ 2 ] = 1.0;
  23. uvs[ 0 ] = 0.5;
  24. uvs[ 1 ] = 0.5;
  25. for ( var s = 0, i = 3, ii = 2 ; s <= segments; s ++, i += 3, ii += 2 ) {
  26. var segment = thetaStart + s / segments * thetaLength;
  27. positions[ i ] = radius * Math.cos( segment );
  28. positions[ i + 1 ] = radius * Math.sin( segment );
  29. normals[ i + 2 ] = 1; // normal z
  30. uvs[ ii ] = ( positions[ i ] / radius + 1 ) / 2;
  31. uvs[ ii + 1 ] = ( positions[ i + 1 ] / radius + 1 ) / 2;
  32. }
  33. var indices = [];
  34. for ( var i = 1; i <= segments; i ++ ) {
  35. indices.push( i );
  36. indices.push( i + 1 );
  37. indices.push( 0 );
  38. }
  39. this.setIndex( new THREE.BufferAttribute( new Uint16Array( indices ), 1 ) );
  40. this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  41. this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  42. this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  43. this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );
  44. };
  45. THREE.CircleBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  46. THREE.CircleBufferGeometry.prototype.constructor = THREE.CircleBufferGeometry;
  47. THREE.CircleBufferGeometry.prototype.clone = function () {
  48. var geometry = new THREE.CircleBufferGeometry(
  49. this.parameters.radius,
  50. this.parameters.segments,
  51. this.parameters.thetaStart,
  52. this.parameters.thetaLength
  53. );
  54. geometry.copy( this );
  55. return geometry;
  56. };