Skip to content

Commit e8ba9a8

Browse files
added some more dope ass filters
1 parent 6b948bb commit e8ba9a8

File tree

14 files changed

+338
-43
lines changed

14 files changed

+338
-43
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"NUMPADTHREE",
3232
"NUMPADTWO",
3333
"NUMPADZERO",
34-
"openeditors"
34+
"openeditors",
35+
"Tiltshift"
3536
]
3637
}

assets/shaders/grain.frag

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#pragma header
2+
3+
/*
4+
Film Grain post-process shader v1.1
5+
Martins Upitis (martinsh) devlog-martinsh.blogspot.com
6+
2013
7+
8+
--------------------------
9+
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
10+
So you are free to share, modify and adapt it for your needs, and even use it for commercial use.
11+
I would also love to hear about a project you are using it.
12+
13+
Have fun,
14+
Martins
15+
--------------------------
16+
17+
Perlin noise shader by toneburst:
18+
http://machinesdontcare.wordpress.com/2009/06/25/3d-perlin-noise-sphere-vertex-shader-sourcecode/
19+
*/
20+
uniform float uTime;
21+
22+
const float permTexUnit = 1.0/256.0; // Perm texture texel-size
23+
const float permTexUnitHalf = 0.5/256.0; // Half perm texture texel-size
24+
25+
float width = openfl_TextureSize.x;
26+
float height = openfl_TextureSize.y;
27+
28+
const float grainamount = 0.1; //grain amount
29+
bool colored = false; //colored noise?
30+
float coloramount = 0.6;
31+
float grainsize = 2.0; //grain particle size (1.5 - 2.5)
32+
33+
//a random texture generator, but you can also use a pre-computed perturbation texture
34+
vec4 rnm(in vec2 tc)
35+
{
36+
float noise = sin(dot(tc + vec2(uTime,uTime),vec2(12.9898,78.233))) * 43758.5453;
37+
38+
float noiseR = fract(noise)*2.0-1.0;
39+
float noiseG = fract(noise*1.2154)*2.0-1.0;
40+
float noiseB = fract(noise*1.3453)*2.0-1.0;
41+
float noiseA = fract(noise*1.3647)*2.0-1.0;
42+
43+
return vec4(noiseR,noiseG,noiseB,noiseA);
44+
}
45+
46+
float fade(in float t) {
47+
return t*t*t*(t*(t*6.0-15.0)+10.0);
48+
}
49+
50+
float pnoise3D(in vec3 p)
51+
{
52+
vec3 pi = permTexUnit*floor(p)+permTexUnitHalf; // Integer part, scaled so +1 moves permTexUnit texel
53+
// and offset 1/2 texel to sample texel centers
54+
vec3 pf = fract(p); // Fractional part for interpolation
55+
56+
// Noise contributions from (x=0, y=0), z=0 and z=1
57+
float perm00 = rnm(pi.xy).a ;
58+
vec3 grad000 = rnm(vec2(perm00, pi.z)).rgb * 4.0 - 1.0;
59+
float n000 = dot(grad000, pf);
60+
vec3 grad001 = rnm(vec2(perm00, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
61+
float n001 = dot(grad001, pf - vec3(0.0, 0.0, 1.0));
62+
63+
// Noise contributions from (x=0, y=1), z=0 and z=1
64+
float perm01 = rnm(pi.xy + vec2(0.0, permTexUnit)).a ;
65+
vec3 grad010 = rnm(vec2(perm01, pi.z)).rgb * 4.0 - 1.0;
66+
float n010 = dot(grad010, pf - vec3(0.0, 1.0, 0.0));
67+
vec3 grad011 = rnm(vec2(perm01, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
68+
float n011 = dot(grad011, pf - vec3(0.0, 1.0, 1.0));
69+
70+
// Noise contributions from (x=1, y=0), z=0 and z=1
71+
float perm10 = rnm(pi.xy + vec2(permTexUnit, 0.0)).a ;
72+
vec3 grad100 = rnm(vec2(perm10, pi.z)).rgb * 4.0 - 1.0;
73+
float n100 = dot(grad100, pf - vec3(1.0, 0.0, 0.0));
74+
vec3 grad101 = rnm(vec2(perm10, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
75+
float n101 = dot(grad101, pf - vec3(1.0, 0.0, 1.0));
76+
77+
// Noise contributions from (x=1, y=1), z=0 and z=1
78+
float perm11 = rnm(pi.xy + vec2(permTexUnit, permTexUnit)).a ;
79+
vec3 grad110 = rnm(vec2(perm11, pi.z)).rgb * 4.0 - 1.0;
80+
float n110 = dot(grad110, pf - vec3(1.0, 1.0, 0.0));
81+
vec3 grad111 = rnm(vec2(perm11, pi.z + permTexUnit)).rgb * 4.0 - 1.0;
82+
float n111 = dot(grad111, pf - vec3(1.0, 1.0, 1.0));
83+
84+
// Blend contributions along x
85+
vec4 n_x = mix(vec4(n000, n001, n010, n011), vec4(n100, n101, n110, n111), fade(pf.x));
86+
87+
// Blend contributions along y
88+
vec2 n_xy = mix(n_x.xy, n_x.zw, fade(pf.y));
89+
90+
// Blend contributions along z
91+
float n_xyz = mix(n_xy.x, n_xy.y, fade(pf.z));
92+
93+
// We are done, return the final noise value.
94+
return n_xyz;
95+
}
96+
97+
//2d coordinate orientation thing
98+
vec2 coordRot(in vec2 tc, in float angle)
99+
{
100+
float aspect = width/height;
101+
float rotX = ((tc.x*2.0-1.0)*aspect*cos(angle)) - ((tc.y*2.0-1.0)*sin(angle));
102+
float rotY = ((tc.y*2.0-1.0)*cos(angle)) + ((tc.x*2.0-1.0)*aspect*sin(angle));
103+
rotX = ((rotX/aspect)*0.5+0.5);
104+
rotY = rotY*0.5+0.5;
105+
return vec2(rotX,rotY);
106+
}
107+
108+
void main()
109+
{
110+
vec2 texCoord = openfl_TextureCoordv.st;
111+
112+
vec3 rotOffset = vec3(1.425,3.892,5.835); //rotation offset values
113+
vec2 rotCoordsR = coordRot(texCoord, uTime + rotOffset.x);
114+
vec3 noise = vec3(pnoise3D(vec3(rotCoordsR*vec2(width/grainsize,height/grainsize),0.0)));
115+
116+
if (colored)
117+
{
118+
vec2 rotCoordsG = coordRot(texCoord, uTime + rotOffset.y);
119+
vec2 rotCoordsB = coordRot(texCoord, uTime + rotOffset.z);
120+
noise.g = mix(noise.r,pnoise3D(vec3(rotCoordsG*vec2(width/grainsize,height/grainsize),1.0)),coloramount);
121+
noise.b = mix(noise.r,pnoise3D(vec3(rotCoordsB*vec2(width/grainsize,height/grainsize),2.0)),coloramount);
122+
}
123+
124+
vec3 col = texture2D(bitmap, openfl_TextureCoordv).rgb;
125+
col = col+noise*grainamount;
126+
127+
gl_FragColor = vec4(col,1.0);
128+
}

assets/shaders/hq2x.frag

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma header
2+
3+
void main()
4+
{
5+
float x = 1.0 / openfl_TextureSize.x;
6+
float y = 1.0 / openfl_TextureSize.y;
7+
8+
vec4 color1 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(-x, -y));
9+
vec4 color2 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(0.0, -y));
10+
vec4 color3 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(x, -y));
11+
12+
vec4 color4 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(-x, 0.0));
13+
vec4 color5 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(0.0, 0.0));
14+
vec4 color6 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(x, 0.0));
15+
16+
vec4 color7 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(-x, y));
17+
vec4 color8 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(0.0, y));
18+
vec4 color9 = texture2D(bitmap, openfl_TextureCoordv.st + vec2(x, y));
19+
vec4 avg = color1 + color2 + color3 + color4 + color5 + color6 + color7 + color8 + color9;
20+
21+
gl_FragColor = avg / 9.0;
22+
}

assets/shaders/tiltshift.frag

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#pragma header
2+
3+
// Modified version of a tilt shift shader from Martin Jonasson (http://grapefrukt.com/)
4+
// Read http://notes.underscorediscovery.com/ for context on shaders and this file
5+
// License : MIT
6+
7+
/*
8+
* Take note that blurring in a single pass (the two for loops below) is more expensive than separating
9+
* the x and the y blur into different passes. This was used where bleeding edge performance
10+
* was not crucial and is to illustrate a point.
11+
*
12+
* The reason two passes is cheaper? "texture2D" is a fairly high cost call, sampling a texture.
13+
*
14+
* So, in a single pass, like below, there are 3 steps, per x and y.
15+
*
16+
* That means a total of 9 "taps", it touches the texture to sample 9 times.
17+
*
18+
* Now imagine we apply this to some geometry, that is equal to 16 pixels on screen (tiny)
19+
* (16 * 16) * 9 = 2304 samples taken, for width * height number of pixels, * 9 taps
20+
* Now, if you split them up, it becomes 3 for x, and 3 for y, a total of 6 taps
21+
* (16 * 16) * 6 = 1536 samples
22+
*
23+
* That\'s on a *tiny* sprite, let\'s scale that up to 128x128 sprite...
24+
* (128 * 128) * 9 = 147,456
25+
* (128 * 128) * 6 = 98,304
26+
*
27+
* That\'s 33.33..% cheaper for splitting them up.
28+
* That\'s with 3 steps, with higher steps (more taps per pass...)
29+
*
30+
* A really smooth, 6 steps, 6*6 = 36 taps for one pass, 12 taps for two pass
31+
* You will notice, the curve is not linear, at 12 steps it\'s 144 vs 24 taps
32+
* It becomes orders of magnitude slower to do single pass!
33+
* Therefore, you split them up into two passes, one for x, one for y.
34+
*/
35+
36+
// I am hardcoding the constants like a jerk
37+
38+
const float bluramount = 1.0;
39+
const float center = 1.0;
40+
const float stepSize = 0.004;
41+
const float steps = 3.0;
42+
43+
const float minOffs = (float(steps-1.0)) / -2.0;
44+
const float maxOffs = (float(steps-1.0)) / +2.0;
45+
46+
void main() {
47+
float amount;
48+
vec4 blurred;
49+
50+
// Work out how much to blur based on the mid point
51+
amount = pow((openfl_TextureCoordv.y * center) * 2.0 - 1.0, 2.0) * bluramount;
52+
53+
// This is the accumulation of color from the surrounding pixels in the texture
54+
blurred = vec4(0.0, 0.0, 0.0, 1.0);
55+
56+
// From minimum offset to maximum offset
57+
for (float offsX = minOffs; offsX <= maxOffs; ++offsX) {
58+
for (float offsY = minOffs; offsY <= maxOffs; ++offsY) {
59+
60+
// copy the coord so we can mess with it
61+
vec2 temp_tcoord = openfl_TextureCoordv.xy;
62+
63+
//work out which uv we want to sample now
64+
temp_tcoord.x += offsX * amount * stepSize;
65+
temp_tcoord.y += offsY * amount * stepSize;
66+
67+
// accumulate the sample
68+
blurred += texture2D(bitmap, temp_tcoord);
69+
}
70+
}
71+
72+
// because we are doing an average, we divide by the amount (x AND y, hence steps * steps)
73+
blurred /= float(steps * steps);
74+
75+
// return the final blurred color
76+
gl_FragColor = blurred;
77+
}

source/InitState.hx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package;
22

3-
import flixel.input.keyboard.FlxKey;
4-
import starcore.backend.util.SaveUtil;
5-
import openfl.events.KeyboardEvent;
63
import flixel.FlxG;
74
import flixel.FlxState;
5+
import flixel.input.keyboard.FlxKey;
86
import flixel.math.FlxMath;
97
import flixel.system.FlxAssets;
108
import flixel.tweens.FlxTween;
119
import lime.app.Application;
1210
import openfl.Lib;
1311
import openfl.display.StageQuality;
1412
import openfl.display.StageScaleMode;
13+
import openfl.events.KeyboardEvent;
14+
import starcore.backend.api.DiscordClient;
1515
import starcore.backend.data.ClientPrefs;
1616
import starcore.backend.util.CacheUtil;
1717
import starcore.backend.util.FlixelUtil;
1818
import starcore.backend.util.LoggerUtil;
1919
import starcore.backend.util.PathUtil;
20+
import starcore.backend.util.SaveUtil;
2021
import starcore.menus.MainMenuState;
21-
import starcore.backend.api.DiscordClient;
22-
#if ADVANCED_SHADERS_ALLOWED
2322
import starcore.shaders.*;
24-
#end
2523
#if web
2624
import js.Browser;
2725
#end
@@ -101,9 +99,10 @@ class InitState extends FlxState
10199

102100
// Assign the shaders AFTER all assets have been loaded!
103101
#if ADVANCED_SHADERS_ALLOWED
104-
CacheUtil.vcrBorderFilter = new VCRBorderShader();
105-
CacheUtil.vcrMario85Filter = new VCRMario85Shader();
102+
CacheUtil.vcrBorderShader = new VCRBorderShader();
103+
CacheUtil.vcrMario85Shader = new VCRMario85Shader();
106104
#end
105+
CacheUtil.grainShader = new GrainShader();
107106
}
108107

109108
function addBackgroundProcesses():Void
@@ -112,12 +111,13 @@ class InitState extends FlxState
112111
LoggerUtil.log('Adding background processes');
113112
// Update the shaders that need to
114113
// constantly be reset
115-
#if ADVANCED_SHADERS_ALLOWED
116114
FlxG.signals.postUpdate.add(() ->
117115
{
118-
CacheUtil.vcrMario85Filter.update(FlxG.elapsed);
116+
#if ADVANCED_SHADERS_ALLOWED
117+
CacheUtil.vcrMario85Shader.update(FlxG.elapsed);
118+
#end
119+
CacheUtil.grainShader.update(FlxG.elapsed);
119120
});
120-
#end
121121
}
122122

123123
function addEventListeners():Void

source/starcore/backend/data/ClientPrefs.hx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import haxe.Exception;
1414
*/
1515
enum ShaderModeType
1616
{
17-
DEFAULT; // All shaders applied
18-
FAST; // Only the VCRMario85 and Scanline shaders enabled
19-
MINIMAL; // Only scanline shader enabled
17+
DEFAULT; // All shaders applied (excluding Scanline)
18+
FAST; // Grain, Scanline, Hq2x and Tiltshift shaders are applied
19+
MINIMAL; // Grain, Scanline and Hq2x shaders are applied
2020
NONE; // No shaders at all
2121
}
2222

source/starcore/backend/data/Constants.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final class Constants
4949
#if !web
5050
'shaderMode' => ShaderModeType.DEFAULT,
5151
#else
52-
'shaderMode' => ShaderModeType.MINIMAL,
52+
'shaderMode' => ShaderModeType.FAST,
5353
#end
5454
// Misc.
5555
'discordRPC' => true,

source/starcore/backend/util/CacheUtil.hx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,22 @@ final class CacheUtil
4343
/**
4444
* VCR border shader for Starcore.
4545
*/
46-
public static var vcrBorderFilter:VCRBorderShader;
46+
public static var vcrBorderShader:VCRBorderShader;
4747

4848
/**
4949
* VCR Mario 85 shader for Starcore.
5050
*/
51-
public static var vcrMario85Filter:VCRMario85Shader;
51+
public static var vcrMario85Shader:VCRMario85Shader;
52+
53+
/**
54+
* Grain shader for Starcore.
55+
*/
56+
public static var grainShader:GrainShader;
5257

5358
/**
5459
* Is caps lock enabled?
5560
*/
56-
public static var capsLockEnabled:Bool = false; // TODO: Find a better way to do this.
61+
public static var capsLockEnabled:Bool = false; // TODO: Find a better way to do this...
5762

5863
function new() {}
5964
}

0 commit comments

Comments
 (0)