首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chroma键段着色器找不到颜色

Chroma键段着色器找不到颜色
EN

Stack Overflow用户
提问于 2017-05-18 09:26:13
回答 1查看 1.2K关注 0票数 2

我正在尝试编写一个片段着色器,作为特定颜色的色度键过滤器(例如,使所有像素具有特定的绿色透明)。

我正在编写的着色器是用于WebGL槽PIXI.js。

JSFiddle:https://jsfiddle.net/IbeVanmeenen/hexec6eg/14/

到目前为止,我根据我找到的here着色器为着色器编写了这段代码。

代码语言:javascript
复制
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);
}

现在,当我定义和测试它时,什么都不会发生。问题在于着色器,因为其他过滤器我尝试了工作。

我用于测试的颜色是rgb(85, 249, 44)

使用PIXI的着色器的完整代码如下:

代码语言:javascript
复制
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,
        uniforms
    );

    this.uniforms.thresholdSensitivity = 0.4;
    this.uniforms.smoothing = 0.1;
    this.uniforms.colorToReplace = [0.33, 0.97, 0.17];

    this.glShaderKey = 'chromakey';
}

ChromaFilter.prototype = Object.create(PIXI.Filter.prototype);
ChromaFilter.prototype.constructor = ChromaFilter;

这适用于像这样的视频精灵:

代码语言:javascript
复制
videoBase = new PIXI.VideoBaseTexture(videoLoaderVid);
videoBase.on('loaded', () => {
    video = videoBase.source;
    video.volume = 0;
    video.pause();
    video.currentTime = 0;

    videoTexture = new PIXI.Texture(videoBase);
    videoSprite = new PIXI.Sprite(videoTexture);

    const filter = new ChromaFilter();
    videoSprite.filters = [filter];

    resolve();
});

而PIXI就是这样建立起来的:

代码语言:javascript
复制
stage = new PIXI.Container();

renderer = PIXI.autoDetectRenderer(720, 720, {
    preserveDrawingBuffer: true,
    clearBeforeRender: true
});

canvasContainer.appendChild(renderer.view);

视频精灵位于它自己的DisplayObjectContainer上,显示在另一个DisplayObjectContainer之上(因此需要一个色度过滤器)。

更新:

在这里可以找到固定的着色器:

https://gist.github.com/IbeVanmeenen/d4f5225ad7d2fa54fabcc38d740ba30e

在这里可以找到一个固定的演示:

https://jsfiddle.net/IbeVanmeenen/hexec6eg/17/

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-20 13:54:54

着色器很好,问题是制服(colorToReplacethresholdSensitivitysmoothing)没有通过,它们都设置为0。幸运的是,我发现要解决需要删除第三个参数的问题,需要传递给PIXI.Filter构造函数:

代码语言:javascript
复制
/* ... */
PIXI.Filter.call(this, vertexShader, fragmentShader) // no uniforms param here
/* ... */

PS。你没有在聊天中回答,所以我在这里发布我的发现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44043782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档