GridHelper.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. dispose() {
  31. this.geometry.dispose();
  32. this.material.dispose();
  33. }
  34. }
  35. export { GridHelper };
粤ICP备19079148号