Filter.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package h2d.filter;
  2. /**
  3. The base filter class, you can extend it in order to define your own filters, although ShaderFilter will be the most straightforward way to define simple custom filter.
  4. Keep in mind that filters use internal Object resolution to render its content, hence scaling of the filtered object would not increase the rendering resolution.
  5. For example, 20x20px `Bitmap` with `scale = 2` will render onto 20x20 filter texture if filter is attached to it directly,
  6. but if filter is attached to the parent of that bitmap, filter will render 40x40 texture.
  7. Another thing to be aware of, is that `Scene.scaleMode` does not affect filter resolution either,
  8. and upscaling contents with `scaleMode` would not upscale the resolution of filtered content.
  9. Filters limit their render area dictated by bound object boundaries, `Filter.autoBounds` and `Filter.boundsExtend` variables and `Filter.getBounds` method.
  10. See their respective docs for details.
  11. For optimization purposes, rendering boundaries are clipped by scene viewport and nothing will be rendered offscreen.
  12. **/
  13. class Filter {
  14. /**
  15. When enabled, rendering bounds of the filter will be expanded by `Filter.boundsExtend` in all directions.
  16. Otherwise filter should provide custom bounds through `Filter.getBounds` call.
  17. Default : true.
  18. **/
  19. public var autoBounds = true;
  20. /**
  21. Rendering texture boundaries extent. Increases the rendering area by twice the `Filter.boundsExtend` value.
  22. Automatically applied to object bounds when `autoBounds = true` or `Filter.getBounds` is not overridden.
  23. Does not affect boundaries when `autoBounds = true` and `boundsExtend` is less than 0.
  24. **/
  25. public var boundsExtend : Float = 0.;
  26. /**
  27. When enabled, some filters will use bilinear filtering on temporary textures.
  28. Does not affect the majority of filters.
  29. @see `AbstractMask`
  30. **/
  31. public var smooth = false;
  32. /**
  33. When filter is disabled, attached object will render as usual.
  34. **/
  35. @:isVar public var enable(get,set) = true;
  36. /**
  37. Custom rendering resolution scaling of the filter.
  38. Stacks with additional scaling from `Filter.useResolutionScaling` if enabled.
  39. **/
  40. public var resolutionScale(default, set):Float = 1;
  41. /**
  42. Use the screen resolution to upscale/downscale the filter rendering resolution.
  43. Stacks with additional scaling from `Filter.resolutionScale` if enabled.
  44. **/
  45. public var useScreenResolution(default, set):Bool = defaultUseScreenResolution;
  46. /**
  47. Defines default value for `Filter.useResolutionScaling`.
  48. **/
  49. public static var defaultUseScreenResolution:Bool = false;
  50. function new() {
  51. }
  52. function get_enable() return enable;
  53. function set_enable(v) return enable = v;
  54. function set_resolutionScale(v) return resolutionScale = v;
  55. function set_useScreenResolution(v) return useScreenResolution = v;
  56. /**
  57. Used to sync data for rendering.
  58. **/
  59. public function sync( ctx : RenderContext, s : Object ) {
  60. }
  61. /**
  62. Sent when filter is bound to an Object `s`.
  63. If Object was not yet allocated, method will be called when it's added to allocated Scene.
  64. **/
  65. public function bind( s : Object ) {
  66. }
  67. /**
  68. Sent when filter was unbound from an Object `s`.
  69. Method won't be called if Object was not yet allocated.
  70. **/
  71. public function unbind( s : Object ) {
  72. }
  73. /**
  74. Method should populate `bounds` with rendering boundaries of the Filter for Object `s`.
  75. Initial `bounds` contents are undefined and it's recommended to either clear them or call `s.getBounds(s, bounds)`.
  76. Only used when `Filter.autoBounds` is `false`.
  77. By default uses given Object bounds and extends them with `Filter.boundsExtend`.
  78. Compared to `autoBounds = true`, negative `boundsExtend` are still applied, causing rendering area to shrink.
  79. @param s The Object instance to which the filter is applied.
  80. @param bounds The Bounds instance which should be populated by the filter boundaries.
  81. @param scale Contains the desired rendering resolution scaling which should be accounted when constructing the bounds.
  82. Can be edited to override provided scale values.
  83. **/
  84. public function getBounds( s : Object, bounds : h2d.col.Bounds, scale : h2d.col.Point ) {
  85. s.getBounds(s, bounds);
  86. bounds.xMin = bounds.xMin * scale.x - boundsExtend;
  87. bounds.xMax = bounds.xMax * scale.x + boundsExtend;
  88. bounds.yMin = bounds.yMin * scale.y - boundsExtend;
  89. bounds.yMax = bounds.yMax * scale.y + boundsExtend;
  90. }
  91. /**
  92. Renders the filter onto Texture in `input` Tile.
  93. **/
  94. public function draw( ctx : RenderContext, input : h2d.Tile ) {
  95. return input;
  96. }
  97. }