Glow.hx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package h2d.filter;
  2. /**
  3. Adds a glow backdrop to the filtered Object.
  4. **/
  5. class Glow extends Blur {
  6. /**
  7. The color of the glow.
  8. **/
  9. public var color : Int;
  10. /**
  11. Transparency value of the glow.
  12. **/
  13. public var alpha : Float;
  14. /**
  15. Subtracts the original image from the glow output when enabled.
  16. **/
  17. public var knockout : Bool;
  18. /**
  19. Produce gradient glow when enabled, otherwise creates hard glow without smoothing.
  20. **/
  21. public var smoothColor : Bool;
  22. /**
  23. Create new Glow filter.
  24. @param color The color of the glow.
  25. @param alpha Transparency value of the glow.
  26. @param radius The glow distance in pixels.
  27. @param gain The glow color intensity.
  28. @param quality The sample count on each pixel as a tradeoff of speed/quality.
  29. @param smoothColor Produce gradient glow when enabled, otherwise creates hard glow without smoothing.
  30. **/
  31. public function new( color : Int = 0xFFFFFF, alpha = 1., radius = 1., gain = 1., quality = 1., smoothColor = false ) {
  32. super(radius, gain, quality);
  33. this.color = color;
  34. this.alpha = alpha;
  35. this.smoothColor = smoothColor;
  36. pass.shader.hasFixedColor = true;
  37. }
  38. function setParams() {
  39. pass.shader.fixedColor.setColor(color);
  40. pass.shader.fixedColor.w = smoothColor ? alpha * 1.5 /* more accurate ramp */ : alpha;
  41. pass.shader.smoothFixedColor = smoothColor;
  42. }
  43. override function draw( ctx : RenderContext, t : h2d.Tile ) {
  44. setParams();
  45. var tex = t.getTexture();
  46. var old = tex.filter;
  47. var save = ctx.textures.allocTileTarget("glowSave", t);
  48. h3d.pass.Copy.run(tex, save, None);
  49. tex.filter = Linear;
  50. pass.apply(ctx, tex);
  51. tex.filter = old;
  52. if( knockout )
  53. h3d.pass.Copy.run(save, tex, Erase);
  54. else
  55. h3d.pass.Copy.run(save, tex, Alpha);
  56. return t;
  57. }
  58. }