forked from unlimitedbacon/stl-thumb
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added macos comptible shaders from unlimitedbacon#14
Co-authored-by: Vitor Vasconcellos <[email protected]>
- Loading branch information
1 parent
6bc9057
commit 3b42bde
Showing
6 changed files
with
231 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#version 150 | ||
|
||
//precision mediump float; | ||
|
||
uniform vec2 resolution; | ||
uniform sampler2D tex; | ||
uniform int enabled; | ||
|
||
in vec2 v_tex_coords; | ||
|
||
out vec4 color; | ||
|
||
#define FXAA_REDUCE_MIN (1.0/ 128.0) | ||
#define FXAA_REDUCE_MUL (1.0 / 8.0) | ||
#define FXAA_SPAN_MAX 8.0 | ||
|
||
vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution, | ||
vec2 v_rgbNW, vec2 v_rgbNE, | ||
vec2 v_rgbSW, vec2 v_rgbSE, | ||
vec2 v_rgbM) { | ||
vec4 color; | ||
|
||
// Sample adjascent pixels | ||
vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y); | ||
vec3 rgbNW = texture(tex, v_rgbNW).xyz; | ||
vec3 rgbNE = texture(tex, v_rgbNE).xyz; | ||
vec3 rgbSW = texture(tex, v_rgbSW).xyz; | ||
vec3 rgbSE = texture(tex, v_rgbSE).xyz; | ||
vec4 texColor = texture(tex, v_rgbM); | ||
vec3 rgbM = texColor.xyz; | ||
|
||
// Calculate luminance | ||
vec3 luma = vec3(0.299, 0.587, 0.114); | ||
vec4 luma4 = vec4(0.299, 0.587, 0.114, 0.0); | ||
|
||
float lumaNW = dot(rgbNW, luma); | ||
float lumaNE = dot(rgbNE, luma); | ||
float lumaSW = dot(rgbSW, luma); | ||
float lumaSE = dot(rgbSE, luma); | ||
float lumaM = dot(rgbM, luma); | ||
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE))); | ||
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE))); | ||
|
||
// Determining blend direction | ||
vec2 dir; | ||
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE)); | ||
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE)); | ||
|
||
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * | ||
(0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN); | ||
|
||
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce); | ||
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX), | ||
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX), | ||
dir * rcpDirMin)) * inverseVP; | ||
|
||
// Blending | ||
// A lot of stuff here is my attempt to incorporate the alpha channel into the blending. I don't think I did it right. ¯\_(ツ)_/¯ | ||
// A bit of the background color still bleeds through around the edges of the image. It's hardly noticable, though. | ||
vec2 coordAA = fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5); | ||
vec2 coordAB = fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5); | ||
float alphaAA = texture(tex, coordAA).a; | ||
float alphaAB = texture(tex, coordAB).a; | ||
float alphaATotal = alphaAA + alphaAB; | ||
float weightAA = alphaAA / alphaATotal; | ||
float weightAB = alphaAB / alphaATotal; | ||
vec4 rgbA = vec4( // Multiply by 2 | ||
texture(tex, coordAA).rgb*weightAA + texture(tex, coordAB).rgb*weightAB, | ||
0.5 * (alphaAA + alphaAB) | ||
); | ||
|
||
vec2 coordBC = fragCoord * inverseVP + dir * -0.5; | ||
vec2 coordBD = fragCoord * inverseVP + dir * 0.5; | ||
float alphaBC = texture(tex, coordBC).a; | ||
float alphaBD = texture(tex, coordBD).a; | ||
float alphaBTotal = alphaAA + alphaAB + alphaBC + alphaBD; | ||
float weightBA = alphaAA / alphaBTotal; | ||
float weightBB = alphaAB / alphaBTotal; | ||
float weightBC = alphaBC / alphaBTotal; | ||
float weightBD = alphaBD / alphaBTotal; | ||
vec4 rgbB = vec4( | ||
texture(tex, coordAA).rgb*weightBA + texture(tex, coordAB).rgb*weightBB + texture(tex, coordBC).rgb*weightBC + texture(tex, coordBD).rgb*weightBD, | ||
0.25 * (alphaAA + alphaAB + alphaBC + alphaBD) | ||
); | ||
|
||
float lumaB = dot(rgbB, luma4); | ||
if ((lumaB < lumaMin) || (lumaB > lumaMax)) | ||
color = rgbA; | ||
else | ||
color = rgbB; | ||
return color; | ||
} | ||
|
||
void main() { | ||
vec2 fragCoord = v_tex_coords * resolution; | ||
if (enabled != 0) { | ||
vec2 inverseVP = 1.0 / resolution.xy; | ||
vec2 v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP; | ||
vec2 v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP; | ||
vec2 v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP; | ||
vec2 v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP; | ||
vec2 v_rgbM = vec2(fragCoord * inverseVP); | ||
color = fxaa(tex, fragCoord, resolution, v_rgbNW, v_rgbNE, v_rgbSW, | ||
v_rgbSE, v_rgbM); | ||
} else { | ||
color = texture(tex, v_tex_coords); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#version 150 | ||
|
||
in vec2 position; | ||
in vec2 i_tex_coords; | ||
|
||
out vec2 v_tex_coords; | ||
|
||
void main() { | ||
gl_Position = vec4(position, 0.0, 1.0); | ||
v_tex_coords = i_tex_coords; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#version 150 | ||
|
||
in vec3 v_normal; | ||
in vec3 v_position; | ||
|
||
out vec4 color; | ||
|
||
uniform vec3 u_light; | ||
|
||
uniform vec3 ambient_color; | ||
uniform vec3 diffuse_color; | ||
uniform vec3 specular_color; | ||
|
||
void main() { | ||
float diffuse = max(dot(normalize(v_normal), normalize(u_light)), 0.0); | ||
|
||
vec3 camera_dir = normalize(-v_position); | ||
vec3 half_direction = normalize(normalize(u_light) + camera_dir); | ||
float specular = pow(max(dot(half_direction, normalize(v_normal)), 0.0), 16.0); | ||
|
||
// Alternative specular method | ||
// vec3 R = reflect( normalize(-u_light), normalize(v_normal) ); | ||
// float cosAlpha = clamp( dot(camera_dir,R), 0, 1 ); | ||
// float specular = pow( cosAlpha, 4.0 ); | ||
|
||
color = vec4(ambient_color + diffuse * diffuse_color + specular * specular_color, 1.0); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#version 150 | ||
|
||
in vec3 position; | ||
in vec3 normal; | ||
|
||
out vec3 v_normal; | ||
out vec3 v_position; | ||
|
||
uniform mat4 perspective; | ||
//uniform mat4 view; | ||
//uniform mat4 model; | ||
uniform mat4 modelview; | ||
|
||
void main() { | ||
// These never change, so they can be computed CPU side. | ||
//mat4 modelview = view * model; | ||
|
||
gl_Position = perspective * modelview * vec4(position, 1.0); | ||
|
||
vec4 p = modelview * vec4(position, 1.0); | ||
v_position = p.xyz / p.w; | ||
|
||
v_normal = mat3(modelview) * normal; | ||
} | ||
|