PolarGridHelper.js 1.9 KB

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