-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
194450d
commit bc22ce1
Showing
5 changed files
with
150 additions
and
91 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,58 @@ | ||
struct HslUniforms { | ||
uHsl:vec3<f32>, | ||
uColorize:f32, | ||
uAlpha:f32, | ||
}; | ||
|
||
@group(0) @binding(1) var uSampler: texture_2d<f32>; | ||
@group(1) @binding(0) var<uniform> hslUniforms : HslUniforms; | ||
|
||
@fragment | ||
fn mainFragment( | ||
@location(0) uv: vec2<f32>, | ||
@builtin(position) position: vec4<f32> | ||
) -> @location(0) vec4<f32> { | ||
let color: vec4<f32> = textureSample(uSampler, uSampler, uv); | ||
var resultRGB: vec3<f32> = color.rgb; | ||
|
||
let hue: f32 = hslUniforms.uHsl[0]; | ||
let saturation: f32 = hslUniforms.uHsl[1]; | ||
let lightness: f32 = hslUniforms.uHsl[2]; | ||
|
||
// colorize | ||
if (hslUniforms.uColorize > 0.5) { | ||
resultRGB = vec3<f32>(dot(color.rgb, vec3<f32>(0.299, 0.587, 0.114)), 0., 0.); | ||
} | ||
|
||
// hue | ||
resultRGB = hueShift(resultRGB, hue); | ||
|
||
// saturation | ||
// https://github.com/evanw/glfx.js/blob/master/src/filters/adjust/huesaturation.js | ||
let average: f32 = (resultRGB.r + resultRGB.g + resultRGB.b) / 3.0; | ||
|
||
if (saturation > 0.) { | ||
resultRGB += (average - resultRGB) * (1. - 1. / (1.001 - saturation)); | ||
} else { | ||
resultRGB -= (average - resultRGB) * saturation; | ||
} | ||
|
||
// lightness | ||
resultRGB = mix(resultRGB, vec3<f32>(ceil(lightness)) * color.a, abs(lightness)); | ||
|
||
// alpha | ||
return mix(color, vec4<f32>(resultRGB, color.a), hslUniforms.uAlpha); | ||
} | ||
|
||
// https://gist.github.com/mairod/a75e7b44f68110e1576d77419d608786?permalink_comment_id=3195243#gistcomment-3195243 | ||
const k: vec3<f32> = vec3(0.57735, 0.57735, 0.57735); | ||
|
||
fn hueShift(color: vec3<f32>, angle: f32) -> vec3<f32> | ||
{ | ||
let cosAngle: f32 = cos(angle); | ||
return vec3<f32>( | ||
color * cosAngle + | ||
cross(k, color) * sin(angle) + | ||
k * dot(k, color) * (1.0 - cosAngle) | ||
); | ||
} |
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