Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Created April 27, 2018 08:45
Show Gist options
  • Save PrimaryFeather/ead378cc659dffc0fb710e492734ffae to your computer and use it in GitHub Desktop.
Save PrimaryFeather/ead378cc659dffc0fb710e492734ffae to your computer and use it in GitHub Desktop.
Chromatic Aberration Filter for Starling
package starling.extensions
{
import starling.filters.FragmentFilter;
import starling.rendering.FilterEffect;
/**
* Chromatic Aberration
* USAGE:
* var filter:CAFilter = new CAFilter();
* filter.angle = 20;
* filter.intensity = 20;
* layer1.filter = filter;
*/
public class CAFilter extends FragmentFilter
{
public function CAFilter():void
{ }
override protected function createEffect():FilterEffect
{
return new CAEffect();
}
private function get caEffect():CAEffect
{
return effect as CAEffect;
}
public function get intensity():Number { return caEffect.intensity; }
public function set intensity(value:Number):void
{
caEffect.intensity = value;
setRequiresRedraw();
}
public function get angle():Number { return caEffect.angle; }
public function set angle(value:Number):void
{
caEffect.angle = value;
setRequiresRedraw();
}
}
}
// ################################################################################################
import flash.display3D.Context3D;
import flash.display3D.Context3DProgramType;
import starling.rendering.FilterEffect;
import starling.rendering.Program;
class CAEffect extends FilterEffect
{
private var _constants:Vector.<Number>;
private var _intensity:Number;
private var _angle:Number;
public function CAEffect()
{
_constants = new Vector.<Number>(4, true);
_intensity = 0;
_angle = 0;
}
override protected function createProgram():Program
{
var vertexShader:String = STD_VERTEX_SHADER;
var fragmentShader:String = [
"tex ft0, v0, fs0 <2d, clamp, linear, mipnone>",
"add ft3.xyzw, fc0.xyzw, v0.xyxy",
"tex ft1, ft3.xy, fs0 <2d, clamp, linear, mipnone>",
"tex ft2, ft3.zw, fs0 <2d, clamp, linear, mipnone>",
"mov ft0.x, ft1.x",
"mov ft0.z, ft2.z",
"mov oc, ft0"
].join("\n");
return Program.fromSource(vertexShader, fragmentShader);
}
override protected function beforeDraw(context:Context3D):void
{
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, _constants, int(_constants.length / 4));
super.beforeDraw(context);
}
private function doMagic():void
{
var x:Number = _intensity * Math.cos(_angle);
var y:Number = _intensity * Math.sin(_angle);
_constants[0] = x;
_constants[1] = y;
_constants[2] = -x;
_constants[3] = -y;
}
public function get intensity():Number { return _intensity * 1000; }
public function set intensity(value:Number):void
{
_intensity = value / 1000;
doMagic();
}
public function get angle():Number { return _angle; }
public function set angle(value:Number):void
{
_angle = value;
doMagic();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment