GridHelper.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { LineSegments } from '../objects/LineSegments.js';
  5. import { VertexColors } from '../constants.js';
  6. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  7. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  8. import { BufferGeometry } from '../core/BufferGeometry.js';
  9. import { Color } from '../math/Color.js';
  10. function GridHelper( size, divisions, color1, color2 ) {
  11. this.parameters = {
  12. size: size,
  13. divisions: divisions,
  14. color1: color1,
  15. color2: color2
  16. };
  17. size = size || 10;
  18. divisions = divisions || 10;
  19. color1 = new Color( color1 !== undefined ? color1 : 0x444444 );
  20. color2 = new Color( color2 !== undefined ? color2 : 0x888888 );
  21. var center = divisions / 2;
  22. var step = size / divisions;
  23. var halfSize = size / 2;
  24. var vertices = [], colors = [];
  25. for ( var i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
  26. vertices.push( - halfSize, 0, k, halfSize, 0, k );
  27. vertices.push( k, 0, - halfSize, k, 0, halfSize );
  28. var color = i === center ? color1 : color2;
  29. color.toArray( colors, j ); j += 3;
  30. color.toArray( colors, j ); j += 3;
  31. color.toArray( colors, j ); j += 3;
  32. color.toArray( colors, j ); j += 3;
  33. }
  34. var geometry = new BufferGeometry();
  35. geometry.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  36. geometry.addAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  37. var material = new LineBasicMaterial( { vertexColors: VertexColors } );
  38. LineSegments.call( this, geometry, material );
  39. }
  40. GridHelper.prototype = Object.assign( Object.create( LineSegments.prototype ), {
  41. constructor: GridHelper,
  42. copy: function ( source ) {
  43. LineSegments.prototype.copy.call( this, source );
  44. Object.assign( this.parameters, source.parameters );
  45. this.geometry.copy( source.geometry );
  46. this.material.copy( source.material );
  47. return this;
  48. },
  49. clone: function () {
  50. var parameters = this.parameters;
  51. return new this.constructor( parameters.size, parameters.divisions, parameters.color1, parameters.color2 );
  52. }
  53. } );
  54. export { GridHelper };