PolarGridHelper.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, radials, circles, divisions, color1, color2 ) {
  8. radius = radius || 10;
  9. radials = radials || 16;
  10. circles = circles || 8;
  11. divisions = divisions || 64;
  12. color1 = new Color( color1 !== undefined ? color1 : 0x444444 );
  13. color2 = new Color( color2 !== undefined ? color2 : 0x888888 );
  14. const vertices = [];
  15. const colors = [];
  16. // create the radials
  17. for ( let i = 0; i <= radials; i ++ ) {
  18. const v = ( i / radials ) * ( Math.PI * 2 );
  19. const x = Math.sin( v ) * radius;
  20. const z = Math.cos( v ) * radius;
  21. vertices.push( 0, 0, 0 );
  22. vertices.push( x, 0, z );
  23. const color = ( i & 1 ) ? color1 : color2;
  24. colors.push( color.r, color.g, color.b );
  25. colors.push( color.r, color.g, color.b );
  26. }
  27. // create the circles
  28. for ( let i = 0; i <= circles; i ++ ) {
  29. const color = ( i & 1 ) ? color1 : color2;
  30. const r = radius - ( radius / circles * i );
  31. for ( let j = 0; j < divisions; j ++ ) {
  32. // first vertex
  33. let v = ( j / divisions ) * ( Math.PI * 2 );
  34. let x = Math.sin( v ) * r;
  35. let z = Math.cos( v ) * r;
  36. vertices.push( x, 0, z );
  37. colors.push( color.r, color.g, color.b );
  38. // second vertex
  39. v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
  40. x = Math.sin( v ) * r;
  41. z = Math.cos( v ) * r;
  42. vertices.push( x, 0, z );
  43. colors.push( color.r, color.g, color.b );
  44. }
  45. }
  46. const geometry = new BufferGeometry();
  47. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  48. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  49. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  50. super( geometry, material );
  51. this.type = 'PolarGridHelper';
  52. }
  53. }
  54. export { PolarGridHelper };