mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-10 22:01:52 +09:00
colour grading proof-of-concept on postprocessing
This commit is contained in:
75
src/shaders/postproc_dither.frag
Normal file
75
src/shaders/postproc_dither.frag
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Blue Noise texture created by Christoph Peters, released under CC0
|
||||
* http://momentsingraphics.de/BlueNoise.html
|
||||
*/
|
||||
|
||||
#version 130
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
|
||||
vec4 gammaIn(vec4 col) {
|
||||
return pow(col, vec4(2.2));
|
||||
}
|
||||
|
||||
vec4 gammaOut(vec4 col) {
|
||||
return pow(col, vec4(1.0 / 2.2));
|
||||
}
|
||||
|
||||
varying vec4 v_color; // unused!
|
||||
varying vec2 v_texCoords;
|
||||
uniform sampler2D u_texture;
|
||||
uniform sampler2D u_pattern;
|
||||
uniform ivec2 rnd = ivec2(0,0);
|
||||
|
||||
uniform float quant = 255.0; // 64 steps -> 63.0; 256 steps -> 255.0
|
||||
|
||||
vec2 boolean = vec2(0.0, 1.0);
|
||||
vec4 halfvec = vec4(0.5);
|
||||
|
||||
vec2 patternsize = vec2(1.0/512.0, 1.0/512.0);
|
||||
|
||||
mat4 rgb_to_ycocg = mat4(
|
||||
0.25, 1.0, -0.5, 0.0,
|
||||
0.5, 0.0, 1.0, 0.0,
|
||||
0.25, -1.0, -0.5, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
);
|
||||
|
||||
mat4 ycocg_to_rgb = mat4(
|
||||
1.0, 1.0, 1.0, 0.0,
|
||||
0.5, 0.0, -0.5, 0.0,
|
||||
-0.5, 0.5, -0.5, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
);
|
||||
|
||||
|
||||
vec4 nearestColour(vec4 inColor) {
|
||||
return floor(vec4(quant) * inColor + halfvec) * vec4(1.0 / quant);
|
||||
}
|
||||
|
||||
vec4 getDitherredDot(vec4 inColor) {
|
||||
vec4 bayerThreshold = vec4(texture2D(u_pattern, (gl_FragCoord.xy + rnd) * patternsize) - 0.5);
|
||||
return nearestColour(bayerThreshold * vec4(1.0 / quant) + inColor);
|
||||
}
|
||||
|
||||
|
||||
uniform vec4 vibrancy = vec4(1.0);//vec4(1.0, 1.4, 1.2, 1.0);
|
||||
|
||||
void main(void) {
|
||||
// convert input RGB into YCoCg
|
||||
vec4 incolour = texture2D(u_texture, v_texCoords);
|
||||
vec4 yog = rgb_to_ycocg * incolour; // vec4(Y, Co, Cg, A) where Y,A=[0,1]; Co,Cg=[-1,1]
|
||||
|
||||
// Do colour-grading magic
|
||||
vec4 sgn = sign(yog);
|
||||
vec4 absval = abs(yog);
|
||||
vec4 raised = pow(absval, boolean.yyyy / vibrancy);
|
||||
vec4 newColour = sgn * raised;
|
||||
|
||||
// Dither the output
|
||||
vec4 graded = ycocg_to_rgb * newColour;
|
||||
vec4 selvec = getDitherredDot(graded);
|
||||
gl_FragColor = selvec * boolean.yyyx + boolean.xxxy; // use quantised RGB but not the A
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#version 130
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
precision mediump float;
|
||||
#endif
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ vec4 gammaOut(vec4 col) {
|
||||
return pow(col, vec4(1.0 / 2.2));
|
||||
}
|
||||
|
||||
varying vec4 v_color;
|
||||
varying vec4 v_color; // unused!
|
||||
varying vec2 v_texCoords;
|
||||
uniform sampler2D u_texture;
|
||||
uniform sampler2D u_pattern;
|
||||
@@ -30,6 +30,21 @@ vec4 halfvec = vec4(0.5);
|
||||
|
||||
vec2 patternsize = vec2(1.0/512.0, 1.0/512.0);
|
||||
|
||||
mat4 rgb_to_ycocg = mat4(
|
||||
0.25, 1.0, -0.5, 0.0,
|
||||
0.5, 0.0, 1.0, 0.0,
|
||||
0.25, -1.0, -0.5, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
);
|
||||
|
||||
mat4 ycocg_to_rgb = mat4(
|
||||
1.0, 1.0, 1.0, 0.0,
|
||||
0.5, 0.0, -0.5, 0.0,
|
||||
-0.5, 0.5, -0.5, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
);
|
||||
|
||||
|
||||
vec4 nearestColour(vec4 inColor) {
|
||||
return floor(vec4(quant) * inColor + halfvec) * vec4(1.0 / quant);
|
||||
}
|
||||
@@ -40,11 +55,20 @@ vec4 getDitherredDot(vec4 inColor) {
|
||||
}
|
||||
|
||||
|
||||
void main(void) {
|
||||
// create texture coordinates based on pixelSize //
|
||||
vec4 inColor = v_color * texture2D(u_texture, v_texCoords);
|
||||
vec4 selvec = getDitherredDot(inColor);
|
||||
uniform vec4 vibrancy = vec4(1.0);//vec4(1.0, 1.4, 1.2, 1.0);
|
||||
|
||||
// gl_FragColor = inColor * boolean.yyyx + boolean.xxxy;
|
||||
gl_FragColor = selvec * boolean.yyyx + inColor * boolean.xxxy; // use quantised RGB but not the A
|
||||
void main(void) {
|
||||
// convert input RGB into YCoCg
|
||||
vec4 incolour = texture2D(u_texture, v_texCoords);
|
||||
vec4 yog = rgb_to_ycocg * incolour; // vec4(Y, Co, Cg, A) where Y,A=[0,1]; Co,Cg=[-1,1]
|
||||
|
||||
// Do colour-grading magic
|
||||
vec4 sgn = sign(yog);
|
||||
vec4 absval = abs(yog);
|
||||
vec4 raised = pow(absval, boolean.yyyy / vibrancy);
|
||||
vec4 newColour = sgn * raised;
|
||||
|
||||
// Dither the output
|
||||
vec4 graded = ycocg_to_rgb * newColour;
|
||||
gl_FragColor = graded * boolean.yyyx + boolean.xxxy; // use quantised RGB but not the A
|
||||
}
|
||||
@@ -8,17 +8,17 @@ varying vec2 v_texCoords;
|
||||
uniform sampler2D u_texture;
|
||||
|
||||
mat4 rgb_to_ycocg = mat4(
|
||||
0.25, 0.5, 0.25, 1.0,
|
||||
1.0, 0.0, -1.0, 1.0,
|
||||
-0.5, 1.0, -0.5, 1.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
0.25, 1.0, -0.5, 0.0,
|
||||
0.5, 0.0, 1.0, 0.0,
|
||||
0.25, -1.0, -0.5, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
);
|
||||
|
||||
mat4 ycocg_to_rgb = mat4(
|
||||
1.0, 0.5, -0.5, 1.0,
|
||||
1.0, 0.0, 0.5, 1.0,
|
||||
1.0, -0.5, -0.5, 1.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
1.0, 1.0, 1.0, 0.0,
|
||||
0.5, 0.0, -0.5, 0.0,
|
||||
-0.5, 0.5, -0.5, 0.0,
|
||||
0.0, 0.0, 0.0, 1.0
|
||||
);
|
||||
|
||||
vec2 boolean = vec2(0.0, 1.0);
|
||||
@@ -27,5 +27,7 @@ void main() {
|
||||
vec4 incolour = texture2D(u_texture, v_texCoords);
|
||||
vec4 yog = rgb_to_ycocg * incolour; // vec4(Y, Co, Cg, A) where Y,A=[0,1]; Co,Cg=[-1,1]
|
||||
|
||||
gl_FragColor = ycocg_to_rgb * yog;
|
||||
vec4 scalar = vec4(1.0, 2.0, 2.0, 1.0);
|
||||
|
||||
gl_FragColor = ycocg_to_rgb * (yog * scalar);
|
||||
}
|
||||
Reference in New Issue
Block a user