All files / src/shaders/pixi frag-chroma-key.ts

0% Statements 0/1
100% Branches 0/0
100% Functions 0/0
0% Lines 0/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                                       
/**
 *  A chroma-key frag shader.  Takes a given color and makes it transparent.
 *
 *
 * Sample usage with Pixi.
 * ```ts
 *
 *   import {color} from '@blinkk/degu';
 *   import {fragChromaKey} from 'degu/lib/shaders/pixi/frag-chroma-key';
 *
 *   let uniforms = {
 *     // Threshold
 *     epsilon: {type: '1f', value: 0.45},
 *     // The color to replace.
 *     color: {type: 'v3', value: color.hexToRgbNormalized(0x50ff00)},
 *   };
 *   let chromaKeyShader = new PIXI.Filter(null, fragChromaKey, uniforms);
 *
 *
 * ```
 *
 *
 * @see https://github.com/pixijs/pixi-filters/blob/master/filters/color-replace/src/colorReplace.frag
 * @see https://developer.tizen.org/community/tip-tech/creating-pixi.js-filters-using-webgl
 */
export const fragChromaKey = `
    varying vec2 vTextureCoord;
    uniform sampler2D uSampler;
 
    uniform vec3 color;
    uniform float epsilon;
 
    void main() {
        vec4 currentColor = texture2D(uSampler, vTextureCoord);
        vec3 colorDiff = color - (currentColor.rgb / max(currentColor.a, 0.0000000001));
        float colorDistance = length(colorDiff);
        if(colorDistance < epsilon) {
          gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
        } else {
          gl_FragColor = texture2D(uSampler, vTextureCoord);
        };
    }
`;