Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
The revised ColorOffsetFilter of the tutorial from the Starling manual; now with fixed PMA handling.
// =================================================================================================
//
// Starling Framework
// Copyright Gamua GmbH. All Rights Reserved.
//
// This program is free software. You can redistribute and/or modify it
// in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================
package starling.extensions
{
import starling.filters.FragmentFilter;
import starling.rendering.FilterEffect;
public class ColorOffsetFilter extends FragmentFilter
{
public function ColorOffsetFilter(redOffset:Number=0, greenOffset:Number=0,
blueOffset:Number=0, alphaOffset:Number=0):void
{
colorOffsetEffect.redOffset = redOffset;
colorOffsetEffect.greenOffset = greenOffset;
colorOffsetEffect.blueOffset = blueOffset;
colorOffsetEffect.alphaOffset = alphaOffset;
}
override protected function createEffect():FilterEffect
{
return new ColorOffsetEffect();
}
private function get colorOffsetEffect():ColorOffsetEffect
{
return effect as ColorOffsetEffect;
}
public function get redOffset():Number { return colorOffsetEffect.redOffset; }
public function set redOffset(value:Number):void
{
colorOffsetEffect.redOffset = value;
setRequiresRedraw();
}
public function get greenOffset():Number { return colorOffsetEffect.greenOffset; }
public function set greenOffset(value:Number):void
{
colorOffsetEffect.greenOffset = value;
setRequiresRedraw();
}
public function get blueOffset():Number { return colorOffsetEffect.blueOffset; }
public function set blueOffset(value:Number):void
{
colorOffsetEffect.blueOffset = value;
setRequiresRedraw();
}
public function get alphaOffset():Number { return colorOffsetEffect.alphaOffset; }
public function set alphaOffset(value:Number):void
{
colorOffsetEffect.alphaOffset = value;
setRequiresRedraw();
}
}
}
import flash.display3D.Context3D;
import flash.display3D.Context3DProgramType;
import starling.rendering.FilterEffect;
import starling.rendering.Program;
class ColorOffsetEffect extends FilterEffect
{
private var _offsets:Vector.<Number>;
private static const MIN_COLOR:Vector.<Number> = new <Number>[0, 0, 0, 0.001];
public function ColorOffsetEffect()
{
_offsets = new Vector.<Number>(4, true);
}
override protected function createProgram():Program
{
var vertexShader:String = STD_VERTEX_SHADER;
var fragmentShader:String = [
tex("ft0", "v0", 0, texture), // get color from texture
"mov ft1, fc0", // copy complete offset to ft1
"mul ft1.xyz, fc0.xyz, ft0.www", // multiply offset.rgb with alpha (pma!)
"add oc, ft0, ft1 " // add offset, copy to output
].join("\n");
return Program.fromSource(vertexShader, fragmentShader);
}
override protected function beforeDraw(context:Context3D):void
{
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, _offsets);
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 1, MIN_COLOR);
super.beforeDraw(context);
}
public function get redOffset():Number { return _offsets[0]; }
public function set redOffset(value:Number):void { _offsets[0] = value; }
public function get greenOffset():Number { return _offsets[1]; }
public function set greenOffset(value:Number):void { _offsets[1] = value; }
public function get blueOffset():Number { return _offsets[2]; }
public function set blueOffset(value:Number):void { _offsets[2] = value; }
public function get alphaOffset():Number { return _offsets[3]; }
public function set alphaOffset(value:Number):void { _offsets[3] = value; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment