PolarGridHelper.js 2.2 KB

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