PlaneHelper.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. import { FrontSide, BackSide } from '../constants.js';
  12. function PlaneHelper( plane, size, hex ) {
  13. this.type = 'PlaneHelper';
  14. this.plane = plane;
  15. this.size = ( size === undefined ) ? 1 : size;
  16. var color = ( hex !== undefined ) ? hex : 0xffff00;
  17. 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 ];
  18. var geometry = new BufferGeometry();
  19. geometry.addAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  20. geometry.computeBoundingSphere();
  21. Line.call( this, geometry, new LineBasicMaterial( { color: color } ) );
  22. //
  23. var positions2 = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, - 1, 1, 1, - 1, 1 ];
  24. var geometry2 = new BufferGeometry();
  25. geometry2.addAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  26. geometry2.computeBoundingSphere();
  27. this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false } ) ) );
  28. }
  29. PlaneHelper.prototype = Object.create( Line.prototype );
  30. PlaneHelper.prototype.constructor = PlaneHelper;
  31. PlaneHelper.prototype.updateMatrixWorld = function ( force ) {
  32. var scale = - this.plane.constant;
  33. if ( Math.abs( scale ) < 1e-8 ) scale = 1e-8; // sign does not matter
  34. this.scale.set( 0.5 * this.size, 0.5 * this.size, scale );
  35. this.children[ 0 ].material.side = ( scale < 0 ) ? BackSide : FrontSide; // renderer flips side when determinant < 0; flipping not wanted here
  36. this.lookAt( this.plane.normal );
  37. Object3D.prototype.updateMatrixWorld.call( this, force );
  38. };
  39. export { PlaneHelper };
粤ICP备19079148号