GridHelper.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 GridHelper extends LineSegments {
  7. constructor( size = 10, divisions = 10, color1 = 0x444444, color2 = 0x888888 ) {
  8. color1 = new Color( color1 );
  9. color2 = new Color( color2 );
  10. const center = divisions / 2;
  11. const step = size / divisions;
  12. const halfSize = size / 2;
  13. const vertices = [], colors = [];
  14. for ( let i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
  15. vertices.push( - halfSize, 0, k, halfSize, 0, k );
  16. vertices.push( k, 0, - halfSize, k, 0, halfSize );
  17. const color = i === center ? color1 : color2;
  18. color.toArray( colors, j ); j += 3;
  19. color.toArray( colors, j ); j += 3;
  20. color.toArray( colors, j ); j += 3;
  21. color.toArray( colors, j ); j += 3;
  22. }
  23. const geometry = new BufferGeometry();
  24. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  25. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  26. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  27. super( geometry, material );
  28. this.type = 'GridHelper';
  29. }
  30. }
  31. export { GridHelper };