LensFlare.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /*
  16. * Add: adds another flare
  17. */
  18. THREE.LensFlare.prototype.add = function ( texture, size, distance, blending, color, opacity ) {
  19. if ( size === undefined ) size = - 1;
  20. if ( distance === undefined ) distance = 0;
  21. if ( opacity === undefined ) opacity = 1;
  22. if ( color === undefined ) color = new THREE.Color( 0xffffff );
  23. if ( blending === undefined ) blending = THREE.NormalBlending;
  24. distance = Math.min( distance, Math.max( 0, distance ) );
  25. this.lensFlares.push( {
  26. texture: texture, // THREE.Texture
  27. size: size, // size in pixels (-1 = use texture.width)
  28. distance: distance, // distance (0-1) from light source (0=at light source)
  29. x: 0, y: 0, z: 0, // screen position (-1 => 1) z = 0 is ontop z = 1 is back
  30. scale: 1, // scale
  31. rotation: 1, // rotation
  32. opacity: opacity, // opacity
  33. color: color, // color
  34. blending: blending // blending
  35. } );
  36. };
  37. /*
  38. * Update lens flares update positions on all flares based on the screen position
  39. * Set myLensFlare.customUpdateCallback to alter the flares in your project specific way.
  40. */
  41. THREE.LensFlare.prototype.updateLensFlares = function () {
  42. var f, fl = this.lensFlares.length;
  43. var flare;
  44. var vecX = - this.positionScreen.x * 2;
  45. var vecY = - this.positionScreen.y * 2;
  46. for( f = 0; f < fl; f ++ ) {
  47. flare = this.lensFlares[ f ];
  48. flare.x = this.positionScreen.x + vecX * flare.distance;
  49. flare.y = this.positionScreen.y + vecY * flare.distance;
  50. flare.wantedRotation = flare.x * Math.PI * 0.25;
  51. flare.rotation += ( flare.wantedRotation - flare.rotation ) * 0.25;
  52. }
  53. };