2
0

LensFlare.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Object3D } from '../core/Object3D';
  2. import { NormalBlending } from '../constants';
  3. import { Color } from '../math/Color';
  4. import { Vector3 } from '../math/Vector3';
  5. /**
  6. * @author mikael emtinger / http://gomo.se/
  7. * @author alteredq / http://alteredqualia.com/
  8. */
  9. function LensFlare( texture, size, distance, blending, color ) {
  10. Object3D.call( this );
  11. this.lensFlares = [];
  12. this.positionScreen = new Vector3();
  13. this.customUpdateCallback = undefined;
  14. if ( texture !== undefined ) {
  15. this.add( texture, size, distance, blending, color );
  16. }
  17. }
  18. LensFlare.prototype = Object.assign( Object.create( Object3D.prototype ), {
  19. constructor: LensFlare,
  20. isLensFlare: true,
  21. copy: function ( source ) {
  22. Object3D.prototype.copy.call( this, source );
  23. this.positionScreen.copy( source.positionScreen );
  24. this.customUpdateCallback = source.customUpdateCallback;
  25. for ( var i = 0, l = source.lensFlares.length; i < l; i ++ ) {
  26. this.lensFlares.push( source.lensFlares[ i ] );
  27. }
  28. return this;
  29. },
  30. add: function ( texture, size, distance, blending, color, opacity ) {
  31. if ( size === undefined ) size = - 1;
  32. if ( distance === undefined ) distance = 0;
  33. if ( opacity === undefined ) opacity = 1;
  34. if ( color === undefined ) color = new Color( 0xffffff );
  35. if ( blending === undefined ) blending = NormalBlending;
  36. distance = Math.min( distance, Math.max( 0, distance ) );
  37. this.lensFlares.push( {
  38. texture: texture, // THREE.Texture
  39. size: size, // size in pixels (-1 = use texture.width)
  40. distance: distance, // distance (0-1) from light source (0=at light source)
  41. x: 0, y: 0, z: 0, // screen position (-1 => 1) z = 0 is in front z = 1 is back
  42. scale: 1, // scale
  43. rotation: 0, // rotation
  44. opacity: opacity, // opacity
  45. color: color, // color
  46. blending: blending // blending
  47. } );
  48. },
  49. /*
  50. * Update lens flares update positions on all flares based on the screen position
  51. * Set myLensFlare.customUpdateCallback to alter the flares in your project specific way.
  52. */
  53. updateLensFlares: function () {
  54. var f, fl = this.lensFlares.length;
  55. var flare;
  56. var vecX = - this.positionScreen.x * 2;
  57. var vecY = - this.positionScreen.y * 2;
  58. for ( f = 0; f < fl; f ++ ) {
  59. flare = this.lensFlares[ f ];
  60. flare.x = this.positionScreen.x + vecX * flare.distance;
  61. flare.y = this.positionScreen.y + vecY * flare.distance;
  62. flare.wantedRotation = flare.x * Math.PI * 0.25;
  63. flare.rotation += ( flare.wantedRotation - flare.rotation ) * 0.25;
  64. }
  65. }
  66. } );
  67. export { LensFlare };