GridHelper.js 1.5 KB

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