LensFlare.js 2.4 KB

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