PolarGridHelper.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { LineSegments } from '../../objects/LineSegments';
  2. import { VertexColors } from '../../constants';
  3. import { LineBasicMaterial } from '../../materials/LineBasicMaterial';
  4. import { Float32BufferAttribute } from '../../core/BufferAttribute';
  5. import { BufferGeometry } from '../../core/BufferGeometry';
  6. import { Color } from '../../math/Color';
  7. /**
  8. * @author mrdoob / http://mrdoob.com/
  9. * @author Mugen87 / http://github.com/Mugen87
  10. * @author Hectate / http://www.github.com/Hectate
  11. */
  12. function PolarGridHelper( radius, radials, circles, divisions, color1, color2 ) {
  13. radius = radius || 10;
  14. radials = radials || 16;
  15. circles = circles || 8;
  16. divisions = divisions || 64;
  17. color1 = new Color( color1 !== undefined ? color1 : 0x444444 );
  18. color2 = new Color( color2 !== undefined ? color2 : 0x888888 );
  19. var vertices = [];
  20. var colors = [];
  21. var x, z;
  22. var v, i, j, r, color;
  23. // create the radials
  24. for ( i = 0; i <= radials; i++ ) {
  25. v = ( i / radials ) * ( Math.PI * 2 );
  26. x = Math.sin( v ) * radius;
  27. z = Math.cos( v ) * radius;
  28. vertices.push( 0, 0, 0 );
  29. vertices.push( x, 0, z );
  30. color = ( i & 1 ) ? color1 : color2;
  31. colors.push( color.r, color.g, color.b );
  32. colors.push( color.r, color.g, color.b );
  33. }
  34. // create the circles
  35. for ( i = 0; i <= circles; i++ ) {
  36. color = ( i & 1 ) ? color1 : color2;
  37. r = radius - ( radius / circles * i )
  38. for ( j = 0; j < divisions; j ++ ) {
  39. // first vertex
  40. v = ( j / divisions ) * ( Math.PI * 2 );
  41. x = Math.sin( v ) * r;
  42. z = Math.cos( v ) * r;
  43. vertices.push( x, 0, z );
  44. colors.push( color.r, color.g, color.b );
  45. // second vertex
  46. v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
  47. x = Math.sin( v ) * r;
  48. z = Math.cos( v ) * r;
  49. vertices.push( x, 0, z );
  50. colors.push( color.r, color.g, color.b );
  51. }
  52. }
  53. var geometry = new BufferGeometry();
  54. geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  55. geometry.addAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  56. var material = new LineBasicMaterial( { vertexColors: VertexColors } );
  57. LineSegments.call( this, geometry, material );
  58. }
  59. PolarGridHelper.prototype = Object.create( LineSegments.prototype );
  60. PolarGridHelper.prototype.constructor = PolarGridHelper;
  61. export { PolarGridHelper };