AxesHelper.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 AxesHelper extends LineSegments {
  7. constructor( size = 1 ) {
  8. const vertices = [
  9. 0, 0, 0, size, 0, 0,
  10. 0, 0, 0, 0, size, 0,
  11. 0, 0, 0, 0, 0, size
  12. ];
  13. const colors = [
  14. 1, 0, 0, 1, 0.6, 0,
  15. 0, 1, 0, 0.6, 1, 0,
  16. 0, 0, 1, 0, 0.6, 1
  17. ];
  18. const geometry = new BufferGeometry();
  19. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  20. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  21. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  22. super( geometry, material );
  23. this.type = 'AxesHelper';
  24. }
  25. setColors( xAxisColor, yAxisColor, zAxisColor ) {
  26. const color = new Color();
  27. const array = this.geometry.attributes.color.array;
  28. color.set( xAxisColor );
  29. color.toArray( array, 0 );
  30. color.toArray( array, 3 );
  31. color.set( yAxisColor );
  32. color.toArray( array, 6 );
  33. color.toArray( array, 9 );
  34. color.set( zAxisColor );
  35. color.toArray( array, 12 );
  36. color.toArray( array, 15 );
  37. this.geometry.attributes.color.needsUpdate = true;
  38. return this;
  39. }
  40. dispose() {
  41. this.geometry.dispose();
  42. this.material.dispose();
  43. }
  44. }
  45. export { AxesHelper };
粤ICP备19079148号