Skip to content

Commit

Permalink
Add shaders
Browse files Browse the repository at this point in the history
Shaders from #22
  • Loading branch information
yavko authored Dec 11, 2023
1 parent 7011145 commit 9978013
Show file tree
Hide file tree
Showing 7 changed files with 749 additions and 0 deletions.
45 changes: 45 additions & 0 deletions bloom.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// blooms entire screen (https://github.com/hyprland-community/awesome-hyprland/issues/22#issuecomment-1682012576) credit to https://github.com/mekb-turtle
#version 330 core

precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D tex;

uniform int bloomRadius = 10;
uniform float bloomIntensity = 0.7f;
uniform float bloomThreshold = 0.4f;

void main() {
vec4 color = texture(tex, v_texcoord);

vec4 bloomThreshold4 = vec4(bloomThreshold);
bloomThreshold4.w = 0.0f;

vec2 invTextureSize = 1.0f / textureSize(tex, 0);
float invBloomRadius = bloomRadius == 0 ? 1.0f : 1.0f / float(bloomRadius);
float invBloomThreshold = 1.0f / (1.0f - bloomThreshold);

float samples = 0.0f;
vec4 colorSum = vec4(0.0f);

for (int x = -bloomRadius; x <= bloomRadius; ++x) {
for (int y = -bloomRadius; y <= bloomRadius; ++y) {
vec2 offset = vec2(x, y) * invTextureSize;

vec2 coords = v_texcoord + offset;
vec4 color = texture(tex, coords);
color = max(color - bloomThreshold, vec4(0.0f));

float strength = 1 - (length(offset) * invBloomRadius);
samples += strength;

strength *= max(max(color.x, color.y), color.z) * invBloomThreshold;

strength *= bloomIntensity;
colorSum += color * strength;
}
}

colorSum /= samples;
gl_FragColor = min(color + colorSum, 1.0f);
}
47 changes: 47 additions & 0 deletions bluelight.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// bluelight filter for hyprland (https://github.com/hyprwm/Hyprland/issues/1140#issuecomment-1335128437) credit to https://github.com/MahouShoujoMivutilde

precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D tex;

const float temperature = 2600.0;
const float temperatureStrength = 1.0;

#define WithQuickAndDirtyLuminancePreservation
const float LuminancePreservationFactor = 1.0;

// function from https://www.shadertoy.com/view/4sc3D7
// valid from 1000 to 40000 K (and additionally 0 for pure full white)
vec3 colorTemperatureToRGB(const in float temperature){
// values from: http://blenderartists.org/forum/showthread.php?270332-OSL-Goodness&p=2268693&viewfull=1#post2268693
mat3 m = (temperature <= 6500.0) ? mat3(vec3(0.0, -2902.1955373783176, -8257.7997278925690),
vec3(0.0, 1669.5803561666639, 2575.2827530017594),
vec3(1.0, 1.3302673723350029, 1.8993753891711275)) :
mat3(vec3(1745.0425298314172, 1216.6168361476490, -8257.7997278925690),
vec3(-2666.3474220535695, -2173.1012343082230, 2575.2827530017594),
vec3(0.55995389139931482, 0.70381203140554553, 1.8993753891711275));
return mix(
clamp(vec3(m[0] / (vec3(clamp(temperature, 1000.0, 40000.0)) + m[1]) + m[2]), vec3(0.0), vec3(1.0)),
vec3(1.0),
smoothstep(1000.0, 0.0, temperature)
);
}

void main() {
vec4 pixColor = texture2D(tex, v_texcoord);

// RGB
vec3 color = vec3(pixColor[0], pixColor[1], pixColor[2]);

#ifdef WithQuickAndDirtyLuminancePreservation
color *= mix(1.0,
dot(color, vec3(0.2126, 0.7152, 0.0722)) / max(dot(color, vec3(0.2126, 0.7152, 0.0722)), 1e-5),
LuminancePreservationFactor);
#endif

color = mix(color, color * colorTemperatureToRGB(temperature), temperatureStrength);

vec4 outCol = vec4(color, pixColor[3]);

gl_FragColor = outCol;
}
36 changes: 36 additions & 0 deletions blur.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// blurs whole screen (https://github.com/hyprland-community/awesome-hyprland/issues/22#issuecomment-1681505196) credit to https://github.com/mekb-turtle
#version 330 core

precision mediump float;
varying vec2 v_texcoord;
uniform sampler2D tex;

uniform int blurRadius = 1;

void main() {
if (blurRadius <= 0) {
gl_FragColor = texture(tex, v_texcoord);
return;
}

vec2 invTextureSize = 1.0f / textureSize(tex, 0);
float invBlurRadius = 1.0f / float(blurRadius);

float samples = 0.0f;
vec4 colorSum = vec4(0.0f);

for (int x = -blurRadius; x <= blurRadius; ++x) {
for (int y = -blurRadius; y <= blurRadius; ++y) {
vec2 offset = vec2(x, y) * invTextureSize;

float strength = 1 - (length(offset) * invBlurRadius);
samples += strength;

vec2 coords = v_texcoord + offset;
colorSum += texture(tex, coords) * strength;
}
}

colorSum /= samples;
gl_FragColor = colorSum;
}
Loading

0 comments on commit 9978013

Please sign in to comment.