PlaneHelper.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * @author WestLangley / http://github.com/WestLangley
  3. */
  4. import { Line } from '../objects/Line.js';
  5. import { Mesh } from '../objects/Mesh.js';
  6. import { LineBasicMaterial } from '../materials/LineBasicMaterial.js';
  7. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
  8. import { Float32BufferAttribute } from '../core/BufferAttribute.js';
  9. import { BufferGeometry } from '../core/BufferGeometry.js';
  10. import { Object3D } from '../core/Object3D.js';
  11. function PlaneHelper( plane, size, hex ) {
  12. this.type = 'PlaneHelper';
  13. this.plane = plane;
  14. this.size = ( size === undefined ) ? 1 : size;
  15. var color = ( hex !== undefined ) ? hex : 0xffff00;
  16. var 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 ];
  17. var geometry = new BufferGeometry();
  18. geometry.addAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  19. geometry.computeBoundingSphere();
  20. Line.call( this, geometry, new LineBasicMaterial( { color: color } ) );
  21. //
  22. var positions2 = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, - 1, 1, 1, - 1, 1 ];
  23. var geometry2 = new BufferGeometry();
  24. geometry2.addAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  25. geometry2.computeBoundingSphere();
  26. this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false } ) ) );
  27. }
  28. PlaneHelper.prototype = Object.create( Line.prototype );
  29. PlaneHelper.prototype.constructor = PlaneHelper;
  30. PlaneHelper.prototype.updateMatrixWorld = function ( force ) {
  31. var scale = - this.plane.constant;
  32. if ( Math.abs( scale ) < 1e-8 ) scale = 1e-8; // sign does not matter
  33. this.scale.set( 0.5 * this.size, 0.5 * this.size, scale );
  34. this.lookAt( this.plane.normal );
  35. Object3D.prototype.updateMatrixWorld.call( this, force );
  36. };
  37. export { PlaneHelper };