2
0

GridHelper.js 1.8 KB

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