Shader.hx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package h2d.filter;
  2. /**
  3. The base class for simple filters that don't need specialized render passes and rely completely on the shaders.
  4. Provides an easy interface to implement custom filters without going too deep into filter rendering process with render passes.
  5. Compatible shaders should extend from `h3d.shader.ScreenShader` and contain an input texture uniform, as well as assign `pixelColor` in fragment shader.
  6. Sample of a simple custom filter:
  7. ```haxe
  8. class InvertColorShader extends h3d.shader.ScreenShader {
  9. static var SRC = {
  10. @param var texture : Sampler2D;
  11. function fragment() {
  12. var pixel : Vec4 = texture.get(calculatedUV);
  13. // Premultiply alpha to ensure correct transparency.
  14. pixelColor = vec4((1. - pixel.rgb) * pixel.a, pixel.a);
  15. // Some other filters directly assign `output.color` and fetch from `input.uv`.
  16. // While it will work, it does not work well when multiple shaders in one filter are involved.
  17. // In this case use `calculatedUV` and `pixelColor`.
  18. }
  19. }
  20. }
  21. // When initializing
  22. // Second argument should point at Sampler2D that will take in the texture with contents filter should modify.
  23. myObj.filter = new h2d.filter.Shader<InvertColorShader>(new InvertColorShader(), "texture");
  24. ```
  25. **/
  26. class Shader< T:h3d.shader.ScreenShader > extends Filter {
  27. /**
  28. The assigned shader instance.
  29. Can be accessed to modify shader uniforms.
  30. **/
  31. public var shader(get, never) : T;
  32. /**
  33. ScreenFX pass that will render the filter.
  34. **/
  35. public var pass : h3d.pass.ScreenFx<T>;
  36. /**
  37. When enabled, sampling on the input texture will use nearest neighbor algorithm.
  38. **/
  39. public var nearest : Bool;
  40. var textureParam : String;
  41. /**
  42. Create new shader filter.
  43. @param shader The shader instance that will be used for rendering.
  44. @param textureParam The name of `Sampler2D` in the shader to which input texture will be assigned. Should be present in the shader.
  45. **/
  46. public function new( shader : T, textureParam = "texture" ) {
  47. super();
  48. var found = false;
  49. for( v in @:privateAccess shader.shader.data.vars ) {
  50. if( v.name == textureParam ) {
  51. found = true;
  52. break;
  53. }
  54. }
  55. if( !found ) throw "Shader does not have '" + textureParam + "' variable";
  56. this.textureParam = textureParam;
  57. this.pass = new h3d.pass.ScreenFx(shader);
  58. }
  59. function get_shader() return pass.shader;
  60. override function draw( ctx : RenderContext, t : h2d.Tile ) {
  61. var out = ctx.textures.allocTileTarget("shaderTmp", t);
  62. ctx.engine.pushTarget(out);
  63. Reflect.setField(shader, textureParam + "__", t.getTexture());
  64. if( nearest ) t.getTexture().filter = Nearest;
  65. pass.render();
  66. ctx.engine.popTarget();
  67. return h2d.Tile.fromTexture(out);
  68. }
  69. }