Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Beeblerox
Created July 23, 2017 09:34
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Beeblerox/54324d9d5aa19b27651b7cda9130f5dd to your computer and use it in GitHub Desktop.
Save Beeblerox/54324d9d5aa19b27651b7cda9130f5dd to your computer and use it in GitHub Desktop.
CRTFilter for OpenFl 5.0
package;
import openfl.Lib;
import openfl.display.Shader;
import openfl.filters.BitmapFilter;
import openfl._internal.renderer.RenderSession;
import openfl.geom.Point;
/**
* ...
* @author Zaphod
*/
class CRTFilter extends BitmapFilter
{
private static var crtShader = new CRTShader();
public function new()
{
super();
}
override public function clone():BitmapFilter
{
return new CRTFilter();
}
override private function __initShader(renderSession:RenderSession, pass:Int):Shader
{
return crtShader;
}
}
/**
* Adaptation of https://www.shadertoy.com/view/Ms23DR
*/
class CRTShader extends Shader
{
@:glVertexSource(
"attribute float aAlpha;
attribute vec4 aPosition;
attribute vec2 aTexCoord;
varying float vAlpha;
varying vec2 vTexCoord;
uniform mat4 uMatrix;
void main(void)
{
vAlpha = aAlpha;
vTexCoord = aTexCoord;
gl_Position = uMatrix * aPosition;
}"
)
@:glFragmentSource(
"varying float vAlpha;
varying vec2 vTexCoord;
uniform sampler2D uImage0;
uniform vec2 uResolution;
uniform float uTime;
vec2 curve(vec2 uv)
{
uv = (uv - 0.5) * 2.0;
uv *= 1.1;
uv.x *= 1.0 + pow((abs(uv.y) / 5.0), 2.0);
uv.y *= 1.0 + pow((abs(uv.x) / 4.0), 2.0);
uv = (uv / 2.0) + 0.5;
uv = uv * 0.92 + 0.04;
return uv;
}
void main()
{
vec2 q = gl_FragCoord.xy / uResolution.xy;
vec2 uv = q;
uv = curve(uv);
vec3 oricol = texture2D(uImage0, vec2(q.x, q.y)).xyz;
vec3 col;
float x = sin(0.3 * uTime + uv.y * 21.0) * sin(0.7 * uTime + uv.y * 29.0) * sin(0.3 + 0.33 * uTime + uv.y * 31.0) * 0.0017;
col.r = texture2D(uImage0, vec2(x + uv.x + 0.001, uv.y + 0.001)).x + 0.05;
col.g = texture2D(uImage0, vec2(x + uv.x + 0.000, uv.y - 0.002)).y + 0.05;
col.b = texture2D(uImage0, vec2(x + uv.x - 0.002, uv.y + 0.000)).z + 0.05;
col.r += 0.08 * texture2D(uImage0, 0.75 * vec2(x + 0.025, -0.027) + vec2(uv.x + 0.001, uv.y + 0.001)).x;
col.g += 0.05 * texture2D(uImage0, 0.75 * vec2(x - 0.022, -0.02) + vec2(uv.x + 0.000, uv.y - 0.002)).y;
col.b += 0.08 * texture2D(uImage0, 0.75 * vec2(x - 0.02, -0.018) + vec2(uv.x - 0.002, uv.y + 0.000)).z;
col = clamp(col * 0.6 + 0.4 * col * col * 1.0, 0.0, 1.0);
float vig = (0.0 + 1.0 * 16.0 * uv.x * uv.y * (1.0 - uv.x) * (1.0 - uv.y));
col *= vec3(pow(vig, 0.3));
col *= vec3(0.95, 1.05, 0.95);
col *= 2.8;
float scans = clamp( 0.35 + 0.35 * sin(3.5 * uTime + uv.y * uResolution.y * 1.5), 0.0, 1.0);
float s = pow(scans, 1.7);
col = col * vec3( 0.4 + 0.7 * s);
col *= 1.0 + 0.01 * sin(110.0 * uTime);
if (uv.x < 0.0 || uv.x > 1.0)
{
col *= 0.0;
}
if (uv.y < 0.0 || uv.y > 1.0)
{
col *= 0.0;
}
col *= 1.0 - 0.65 * vec3(clamp((mod(gl_FragCoord.x, 2.0) - 1.0) * 2.0, 0.0, 1.0));
float comp = smoothstep(0.1, 0.9, sin(uTime));
gl_FragColor = vec4(col, 1.0);
}"
)
public function new()
{
super();
#if !macro
data.uResolution.value = [1.0, 1.0];
data.uTime.value = [0.0];
#end
}
private override function __update():Void
{
data.uResolution.value[0] = Lib.current.stage.stageWidth;
data.uResolution.value[1] = Lib.current.stage.stageHeight;
data.uTime.value[0] = Lib.getTimer() / 1000;
super.__update();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment