Last active
March 9, 2016 18:17
-
-
Save erodozer/947907fa1332f2bfa9c9 to your computer and use it in GitHub Desktop.
Simple fragment shader that allows you to use any RGB image as a fading mask
This file contains hidden or 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
const float RATE = 5.0; | |
/** | |
* Converts RGB value from our texture channel into an HSV value. | |
* By using the V we can perform a transition using any color image | |
*/ | |
vec3 rgb2hsv(vec3 c) | |
{ | |
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); | |
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); | |
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); | |
float d = q.x - min(q.w, q.y); | |
float e = 1.0e-10; | |
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); | |
} | |
void mainImage( out vec4 fragColor, in vec2 fragCoord ) | |
{ | |
vec2 uv = fragCoord.xy / iResolution.xy; | |
//get current clear value | |
float clear = clamp(iGlobalTime / RATE, 0.0, 1.0); | |
//find the normal value to render to the screen if it's not masked | |
fragColor = texture2D(iChannel0, uv).rgba; | |
//find the masking value at the pixel | |
vec4 clearColor = texture2D(iChannel1, uv); | |
float clearV = rgb2hsv(clearColor.rgb).z; | |
//if the masking value is less than our current clear time, erase the pixel | |
if (clearV < clear) { | |
fragColor = vec4(0,0,0,0); | |
} | |
} |
This file contains hidden or 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
Currently the shader uses variables that are suitable for ShaderToy only. I'll be looking into making it work for LibGDX |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment