Filters

Filters apply to the display tree, so can be used to create screen effects. You can assign a filter to a h2d.Sprite.

  1. mySprite.filter = new Glow();

Build-in filters

Heaps provides several filters like Ambient, Bloom, Blur, ColorMatrix, Displacement, DropShadow, Glob, Mask. They can be found in the h2d.filter package.

Creating custom screen shader filters

To create a shader that works as filter, you need a screen shader.

As you can see in h3d.shader.ScreenShader, screen shaders only provides you input.position and input.uv, and you can alter the output.position or output.color.

  1. @input var input : {
  2. position : Vec2,
  3. uv : Vec2,
  4. };
  5. var output : {
  6. position : Vec4,
  7. color : Vec4,
  8. };

Let’s create a simple shader that changes the red channel based on a parameter:

  1. class MyFilterShader extends h3d.shader.ScreenShader {
  2. static var SRC = {
  3. @param var texture : Sampler2D;
  4. @param var red : Float;
  5. function fragment() {
  6. pixelColor = texture.get(input.uv);
  7. pixelColor.r = red; // change red channel
  8. }
  9. }
  10. }

Usage:

  1. var myShader = new MyFilterShader();
  2. myShader.red = 1; // use 0-1 range
  3. var customFilter = new h2d.filter.Shader(myShader);
  4. mySprite.filter = customFilter;

Multiple filters on one sprite

To apply multiple filters, use h2d.filter.Group.

  1. var group = new Group([filter1, filter2]);
  2. mySprite.filter = group;