PolarGridHelper.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { LineSegments } from '../objects/LineSegments.js';
  2. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  3. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  4. import { BufferGeometry } from '../core/BufferGeometry.js';
  5. import { Color } from '../math/Color.js';
  6. class PolarGridHelper extends LineSegments {
  7. constructor( radius = 10, sectors = 16, rings = 8, divisions = 64, color1 = 0x444444, color2 = 0x888888 ) {
  8. color1 = new Color( color1 );
  9. color2 = new Color( color2 );
  10. const vertices = [];
  11. const colors = [];
  12. // create the sectors
  13. if ( sectors > 1 ) {
  14. for ( let i = 0; i < sectors; i ++ ) {
  15. const v = ( i / sectors ) * ( Math.PI * 2 );
  16. const x = Math.sin( v ) * radius;
  17. const z = Math.cos( v ) * radius;
  18. vertices.push( 0, 0, 0 );
  19. vertices.push( x, 0, z );
  20. const color = ( i & 1 ) ? color1 : color2;
  21. colors.push( color.r, color.g, color.b );
  22. colors.push( color.r, color.g, color.b );
  23. }
  24. }
  25. // create the rings
  26. for ( let i = 0; i < rings; i ++ ) {
  27. const color = ( i & 1 ) ? color1 : color2;
  28. const r = radius - ( radius / rings * i );
  29. for ( let j = 0; j < divisions; j ++ ) {
  30. // first vertex
  31. let v = ( j / divisions ) * ( Math.PI * 2 );
  32. let x = Math.sin( v ) * r;
  33. let z = Math.cos( v ) * r;
  34. vertices.push( x, 0, z );
  35. colors.push( color.r, color.g, color.b );
  36. // second vertex
  37. v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
  38. x = Math.sin( v ) * r;
  39. z = Math.cos( v ) * r;
  40. vertices.push( x, 0, z );
  41. colors.push( color.r, color.g, color.b );
  42. }
  43. }
  44. const geometry = new BufferGeometry();
  45. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  46. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  47. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  48. super( geometry, material );
  49. this.type = 'PolarGridHelper';
  50. }
  51. dispose() {
  52. this.geometry.dispose();
  53. this.material.dispose();
  54. }
  55. }
  56. export { PolarGridHelper };