Last active
June 2, 2017 13:44
-
-
Save IbeVanmeenen/d4f5225ad7d2fa54fabcc38d740ba30e to your computer and use it in GitHub Desktop.
Chroma Filter Shader - Pixi.js
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
function ChromaFilter() { | |
const vertexShader = null; | |
const fragmentShader = [ | |
"varying vec2 vTextureCoord;", | |
"uniform float thresholdSensitivity;", | |
"uniform float smoothing;", | |
"uniform vec3 colorToReplace;", | |
"uniform sampler2D uSampler;", | |
"void main() {", | |
"vec4 textureColor = texture2D(uSampler, vTextureCoord);", | |
"float maskY = 0.2989 * colorToReplace.r + 0.5866 * colorToReplace.g + 0.1145 * colorToReplace.b;", | |
"float maskCr = 0.7132 * (colorToReplace.r - maskY);", | |
"float maskCb = 0.5647 * (colorToReplace.b - maskY);", | |
"float Y = 0.2989 * textureColor.r + 0.5866 * textureColor.g + 0.1145 * textureColor.b;", | |
"float Cr = 0.7132 * (textureColor.r - Y);", | |
"float Cb = 0.5647 * (textureColor.b - Y);", | |
"float blendValue = smoothstep(thresholdSensitivity, thresholdSensitivity + smoothing, distance(vec2(Cr, Cb), vec2(maskCr, maskCb)));", | |
"gl_FragColor = vec4(textureColor.rgb, textureColor.a * blendValue);", | |
"}" | |
].join('\n'); | |
let uniforms = {}; | |
PIXI.Filter.call(this, vertexShader, fragmentShader); | |
this.uniforms.thresholdSensitivity = 0.4; | |
this.uniforms.smoothing = 0.1; | |
this.uniforms.colorToReplace = [(37 / 255), (255 / 255), (10 / 255)]; | |
} | |
ChromaFilter.prototype = Object.create(PIXI.Filter.prototype); | |
ChromaFilter.prototype.constructor = ChromaFilter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment