LensFlare.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 ontop z = 1 is back
  31. scale: 1, // scale
  32. rotation: 1, // 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. };
粤ICP备19079148号