Last active
December 15, 2015 17:19
-
-
Save quidmonkey/5295797 to your computer and use it in GitHub Desktop.
WebGL Shaders with Alpha
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
// vertex shader | |
attribute float a_alpha; | |
attribute vec2 a_position; | |
attribute vec2 a_texCoord; | |
uniform vec2 u_resolution; | |
varying vec2 v_texCoord; | |
void main () { | |
// normalize and convert to -1 to 1 (clip space) | |
vec2 clipSpace = a_position / u_resolution * 2.0 - 1.0; | |
// invert y-axis | |
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1); | |
v_alpha = a_alpha; | |
v_texCoord = a_texCoord; | |
} | |
// fragment | |
precision mediump float; | |
uniform sampler2D u_image; | |
varying float v_alpha; | |
varying vec2 v_texCoord; | |
void main () { | |
vec4 texture = texture2D(u_image, v_texCoord); | |
gl_FragColor = vec4(texture.rgb, texture.a * v_alpha); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment