PlaneHelper.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { Line } from '../objects/Line.js';
  2. import { Mesh } from '../objects/Mesh.js';
  3. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  4. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
  5. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  6. import { BufferGeometry } from '../core/BufferGeometry.js';
  7. import { FrontSide, BackSide } from '../constants.js';
  8. class PlaneHelper extends Line {
  9. constructor( plane, size, hex ) {
  10. const color = ( hex !== undefined ) ? hex : 0xffff00;
  11. const positions = [ 1, - 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0 ];
  12. const geometry = new BufferGeometry();
  13. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  14. geometry.computeBoundingSphere();
  15. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  16. this.type = 'PlaneHelper';
  17. this.plane = plane;
  18. this.size = ( size === undefined ) ? 1 : size;
  19. const positions2 = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, - 1, 1, 1, - 1, 1 ];
  20. const geometry2 = new BufferGeometry();
  21. geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  22. geometry2.computeBoundingSphere();
  23. this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) );
  24. }
  25. updateMatrixWorld( force ) {
  26. let scale = - this.plane.constant;
  27. if ( Math.abs( scale ) < 1e-8 ) scale = 1e-8; // sign does not matter
  28. this.scale.set( 0.5 * this.size, 0.5 * this.size, scale );
  29. this.children[ 0 ].material.side = ( scale < 0 ) ? BackSide : FrontSide; // renderer flips side when determinant < 0; flipping not wanted here
  30. this.lookAt( this.plane.normal );
  31. super.updateMatrixWorld( force );
  32. }
  33. }
  34. export { PlaneHelper };