Increase shader compatability + guide
Optimization
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
Here are guides on writing shaders for flixel and openfl.
|
||||
|
||||
And to make sure that the shaders works on as many devices as possible.
|
||||
|
||||
-------
|
||||
|
||||
Always have numbers be 0.0 not 0. or .0
|
||||
|
||||
Don't use Switch cases, use if statements instead
|
||||
|
||||
Use floats where its expected, such as vec2,3,4
|
||||
|
||||
Don't use "texture" use "flixel_texture2D" or "texture2D"
|
||||
|
||||
(These might work, but its not recommended)
|
||||
Don't use ivec2,3,4
|
||||
Don't use bvec2,3,4
|
||||
Don't use uvec2,3,4
|
||||
Don't use uint
|
||||
|
||||
Don't name variables starting with "gl_"
|
||||
|
||||
Don't have default values for uniforms, set it in the constructor if a class, or after loading the shader
|
||||
|
||||
Don't use <number>u, like 8u
|
||||
Don't use the variable name "input"
|
||||
Don't use the variable name "sample"
|
||||
|
||||
-------
|
||||
|
||||
Common Practice:
|
||||
|
||||
Use texture2D for camera shaders, and FlxGame shaders, and flixel_texture2D for everything else
|
||||
flixel_texture2D applies the .colorTransform.
|
||||
texture2D doesn't apply it.
|
||||
|
||||
-------
|
||||
|
||||
Extra:
|
||||
|
||||
Use tabs for indentation
|
||||
@@ -13,5 +13,5 @@ void main()
|
||||
vec2 pixel = gl_FragCoord.xy;
|
||||
|
||||
if (boxCollision(pixel, clipRect)) gl_FragColor = flixel_texture2D(bitmap, openfl_TextureCoordv.xy);
|
||||
else gl_FragColor = vec4(0.);
|
||||
else gl_FragColor = vec4(0.0);
|
||||
}
|
||||
@@ -7,12 +7,9 @@ int steps = 16;
|
||||
int stepsInside = 2;
|
||||
float strength = 0.0075;
|
||||
vec4 getColor(vec2 pos) {
|
||||
if (pos.x < 0.0) pos.x = 0.0;
|
||||
else if (pos.x > 1.0 - (1.0 / openfl_TextureSize.x)) pos.x = 1.0 - (1.0 / openfl_TextureSize.x);
|
||||
if (pos.y < 0.0) pos.y = 0.0;
|
||||
else if (pos.y > 1.0 - (1.0 / openfl_TextureSize.y)) pos.y = 1.0 - (1.0 / openfl_TextureSize.y);
|
||||
// return textureCam(bitmap, pos);
|
||||
return flixel_texture2D(bitmap, pos);
|
||||
vec2 ps = (pos);
|
||||
ps = clamp(ps, vec2(0.0), 1.0 - (1.0 / openfl_TextureSize.xy));
|
||||
return flixel_texture2D(bitmap, (ps));
|
||||
}
|
||||
void main() {
|
||||
// vec2 camPos = getCamPos(openfl_TextureCoordv);
|
||||
|
||||
@@ -14,7 +14,7 @@ uniform float pixelOffset;
|
||||
uniform float playerPosition;
|
||||
|
||||
uniform sampler2D waveformTexture;
|
||||
uniform ivec2 waveformSize;
|
||||
uniform vec2 waveformSize;
|
||||
|
||||
uniform bool lowDetail;
|
||||
|
||||
@@ -22,7 +22,7 @@ float getAmplitude(vec2 pixel) {
|
||||
float pixelID = floor((pixel.y+pixelOffset)/3.0);
|
||||
|
||||
// TODO: INVESTIGATE THE 1.+ AND WHY IT WORKS (SRSLY I GOT NO CLUE) -lunar
|
||||
vec2 wavePixel = vec2(mod(pixelID,waveformSize.x), 1.0+floor(pixelID/waveformSize.x));
|
||||
vec2 wavePixel = vec2(mod(pixelID, waveformSize.x), 1.0+floor(pixelID/waveformSize.x));
|
||||
vec4 waveData = texture2D(waveformTexture, wavePixel / waveformSize);
|
||||
|
||||
int id = int(mod(wavePixel.x, 3.0));
|
||||
|
||||
@@ -457,14 +457,7 @@ vec4 flixel_texture2D(sampler2D bitmap, vec2 coord)
|
||||
}
|
||||
|
||||
color = vec4(color.rgb / color.a, color.a);
|
||||
|
||||
mat4 colorMultiplier = mat4(0);
|
||||
colorMultiplier[0][0] = openfl_ColorMultiplierv.x;
|
||||
colorMultiplier[1][1] = openfl_ColorMultiplierv.y;
|
||||
colorMultiplier[2][2] = openfl_ColorMultiplierv.z;
|
||||
colorMultiplier[3][3] = openfl_ColorMultiplierv.w;
|
||||
|
||||
color = clamp(openfl_ColorOffsetv + (color * colorMultiplier), 0.0, 1.0);
|
||||
color = clamp(openfl_ColorOffsetv + (color * openfl_ColorMultiplierv), 0.0, 1.0);
|
||||
|
||||
if (color.a > 0.0)
|
||||
{
|
||||
@@ -491,39 +484,9 @@ vec4 textureCam(sampler2D bitmap, vec2 pos) {
|
||||
return flixel_texture2D(bitmap, camToOg(pos));
|
||||
}";
|
||||
|
||||
public static final fragBody:String = "vec4 color = texture2D (bitmap, openfl_TextureCoordv);
|
||||
|
||||
if (color.a == 0.0) {
|
||||
|
||||
gl_FragColor = vec4 (0.0, 0.0, 0.0, 0.0);
|
||||
|
||||
} else if (openfl_HasColorTransform) {
|
||||
|
||||
color = vec4 (color.rgb / color.a, color.a);
|
||||
|
||||
mat4 colorMultiplier = mat4 (0);
|
||||
colorMultiplier[0][0] = openfl_ColorMultiplierv.x;
|
||||
colorMultiplier[1][1] = openfl_ColorMultiplierv.y;
|
||||
colorMultiplier[2][2] = openfl_ColorMultiplierv.z;
|
||||
colorMultiplier[3][3] = 1.0; // openfl_ColorMultiplierv.w;
|
||||
|
||||
color = clamp (openfl_ColorOffsetv + (color * colorMultiplier), 0.0, 1.0);
|
||||
|
||||
if (color.a > 0.0) {
|
||||
|
||||
gl_FragColor = vec4 (color.rgb * color.a * openfl_Alphav, color.a * openfl_Alphav);
|
||||
|
||||
} else {
|
||||
|
||||
gl_FragColor = vec4 (0.0, 0.0, 0.0, 0.0);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
gl_FragColor = color * openfl_Alphav;
|
||||
|
||||
}";
|
||||
public static final fragBody:String = "vec4 color = flixel_texture2D (bitmap, openfl_TextureCoordv);
|
||||
gl_FragColor = color;
|
||||
";
|
||||
public static final vertHeader:String = "attribute float openfl_Alpha;
|
||||
attribute vec4 openfl_ColorMultiplier;
|
||||
attribute vec4 openfl_ColorOffset;
|
||||
|
||||
@@ -75,18 +75,18 @@ const int EFFECT_TYPE_FLAG = 4;
|
||||
uniform int effectType;
|
||||
|
||||
/**
|
||||
* How fast the waves move over time
|
||||
*/
|
||||
* How fast the waves move over time
|
||||
*/
|
||||
uniform float uSpeed;
|
||||
|
||||
/**
|
||||
* Number of waves over time
|
||||
*/
|
||||
* Number of waves over time
|
||||
*/
|
||||
uniform float uFrequency;
|
||||
|
||||
/**
|
||||
* How much the pixels are going to stretch over the waves
|
||||
*/
|
||||
* How much the pixels are going to stretch over the waves
|
||||
*/
|
||||
uniform float uWaveAmplitude;
|
||||
|
||||
vec2 sineWave(vec2 pt)
|
||||
|
||||
Reference in New Issue
Block a user