C# Class SparrowSharp.Filters.FragmentFilter

The FragmentFilter class is the base class for all filter effects in Sparrow.

All other filters of this package extend this class. You can attach them to any display object through the 'filter' property.

A fragment filter works in the following way:

* The object that is filtered is rendered into a texture (in stage coordinates).

* That texture is passed to the first filter pass.

* Each pass processes the texture using a fragment shader (and optionally a vertex shader) to achieve a certain effect.

* The output of each pass is used as the input for the next pass; if it's the final pass, it will be rendered directly to the back buffer.

All of this is set up by the abstract FragmentFilter class. Concrete subclasses just need to override the protected methods 'createPrograms', 'activateWithPass' and (optionally) 'deactivateWithPass' to create and execute its custom shader code. Each filter can be configured to either replace the original object, or be drawn below or above it. This can be done through the 'mode' property, which accepts one of the enums defined in the 'SPFragmentFilterMode' enum.

Beware that each filter should be used only on one object at a time. Otherwise, it will get slower and require more resources; and caching will lead to undefined results.
Datei anzeigen Open project: fmotagarcia/sparrow-sharp Class Usage Examples

Public Properties

Property Type Description
MarginX float
MarginY float
Mode FragmentFilterMode
OffsetX float
OffsetY float
Resolution float

Protected Properties

Property Type Description
NumPasses int
TexCoordsID int
VertexPosID int

Public Methods

Method Description
Cache ( ) : void

Caches the filter output into a Texture. An uncached filter is rendered in every frame; a cached filter only once. However, if the filtered object or the filter settings change, it has to be updated manually; to do that, call "cache" again.

ClearCache ( ) : void

Clears the cached output of the filter. After calling this method, the filter will be executed once per frame again.

RenderObject ( DisplayObject obj, RenderSupport support ) : void

Applies the filter on a certain display object, rendering the output into the current render target. This method is called automatically by Sparrow's rendering system for the object the filter is attached to.

Protected Methods

Method Description
ActivateWithPass ( int pass, Texture texture, Matrix mvpMatrix ) : void

Subclasses must override this method and use it to activate their shader program. The 'ActivateWithPass' call directly precedes the call to 'GL.DrawElements'.

CreatePrograms ( ) : void

Subclasses must override this method and use it to create their fragment and vertex shaders.

DeactivateWithPass ( int pass, Texture texture ) : void

This method is called directly after 'GL.DrawElements'. If you need to clean up any resources, you can do so in this method

FragmentFilter ( int numPasses = 1, float resolution = 1.0f ) : System

Initializes a fragment filter with the specified number of passes and resolution.

StandardFragmentShader ( ) : String

The standard fragment shader code. It just forwards the texture color to the output.

StandardVertexShader ( ) : String

The standard vertex shader code.

Private Methods

Method Description
CalcBounds ( DisplayObject obj, Stage stage, float scale, bool intersectWithStage, Rectangle &bounds, Rectangle &boundsPOT ) : void
CreateTexture ( int width, int height, float scale ) : Texture
DisposeCache ( ) : void
DisposePassTextures ( ) : void
PassTextureForPass ( int pass ) : Texture
RenderPasses ( DisplayObject obj, RenderSupport support, bool intoCache ) : QuadBatch
UpdateBuffers ( Rectangle bounds ) : void
UpdatePassTextures ( int width, int height, float scale ) : void

Method Details

ActivateWithPass() protected abstract method

Subclasses must override this method and use it to activate their shader program. The 'ActivateWithPass' call directly precedes the call to 'GL.DrawElements'.
protected abstract ActivateWithPass ( int pass, Texture texture, Matrix mvpMatrix ) : void
pass int
texture Sparrow.Textures.Texture
mvpMatrix Matrix
return void

Cache() public method

Caches the filter output into a Texture. An uncached filter is rendered in every frame; a cached filter only once. However, if the filtered object or the filter settings change, it has to be updated manually; to do that, call "cache" again.
public Cache ( ) : void
return void

ClearCache() public method

Clears the cached output of the filter. After calling this method, the filter will be executed once per frame again.
public ClearCache ( ) : void
return void

CreatePrograms() protected abstract method

Subclasses must override this method and use it to create their fragment and vertex shaders.
protected abstract CreatePrograms ( ) : void
return void

DeactivateWithPass() protected method

This method is called directly after 'GL.DrawElements'. If you need to clean up any resources, you can do so in this method
protected DeactivateWithPass ( int pass, Texture texture ) : void
pass int
texture Sparrow.Textures.Texture
return void

FragmentFilter() protected method

Initializes a fragment filter with the specified number of passes and resolution.
protected FragmentFilter ( int numPasses = 1, float resolution = 1.0f ) : System
numPasses int
resolution float
return System

RenderObject() public method

Applies the filter on a certain display object, rendering the output into the current render target. This method is called automatically by Sparrow's rendering system for the object the filter is attached to.
public RenderObject ( DisplayObject obj, RenderSupport support ) : void
obj Sparrow.Display.DisplayObject
support Sparrow.Core.RenderSupport
return void

StandardFragmentShader() protected static method

The standard fragment shader code. It just forwards the texture color to the output.
protected static StandardFragmentShader ( ) : String
return String

StandardVertexShader() protected static method

The standard vertex shader code.
protected static StandardVertexShader ( ) : String
return String

Property Details

MarginX public_oe property

The x-margin will extend the size of the filter texture along the x-axis. Useful when the filter will "grow" the rendered object.
public float MarginX
return float

MarginY public_oe property

The y-margin will extend the size of the filter texture along the y-axis. Useful when the filter will "grow" the rendered object.
public float MarginY
return float

Mode public_oe property

The filter mode, which is one of the constants defined in the 'FragmentFilterMode' enum. (default: FragmentFilterMode.Replace)
public FragmentFilterMode Mode
return FragmentFilterMode

NumPasses protected_oe property

The number of passes the filter is applied. The "activate" and "deactivate" methods will be called that often.
protected int NumPasses
return int

OffsetX public_oe property

Use the x-offset to move the filter output to the right or left.
public float OffsetX
return float

OffsetY public_oe property

Use the y-offset to move the filter output to the top or bottom.
public float OffsetY
return float

Resolution public_oe property

The resolution of the filter texture. "1" means stage resolution, "0.5" half the stage resolution. A lower resolution saves memory and execution time(depending on the GPU), but results in a lower output quality. Values greater than 1 are allowed; such values might make sense for a cached filter when it is scaled up. default 1
public float Resolution
return float

TexCoordsID protected_oe property

The ID of the vertex buffer attribute that stores the Texture coordinates.
protected int TexCoordsID
return int

VertexPosID protected_oe property

The ID of the vertex buffer attribute that stores the Vertex position.
protected int VertexPosID
return int