Webgl - 使用一个程序来掩盖以前的程序

时间:2017-12-14 13:20:59

标签: javascript glsl webgl

我正在运行一个着色器程序,它渲染出一些顶点,然后当然会在片段着色器中对它们进行着色。

然后我运行了另一个程序,我想用它来屏蔽前一个渲染的形状。

到目前为止,第一个呈现的内容如下:

animate_() {
    requestAnimationFrame(() => this.animate_());

    this.gl_.clearColor(0, 0, 0, 0);
    this.gl_.clear(webgl.COLOR_BUFFER_BIT);

    this.gl_.useProgram(this.program_);

    // Runs enableVertexAttribArray, bindBuffer, vertexAttribPointer
    renderBufferAttribute(this.gl_, this.position_);

    // Pass through all my uniforms here
    // ...

    // Draw triangles
    this.gl_.drawArrays(webgl.TRIANGLES, 0,
        this.particleCount_.total * PARTICLE_ARRAY_COUNT / 2);
}

然后我有第二个程序呈现如下:

animate_() {
    requestAnimationFrame(() => this.animate_());

    // this.gl_.clearColor(0, 0, 0, 0);
    // this.gl_.clear(webgl.COLOR_BUFFER_BIT);

    this.gl_.useProgram(this.program_);

    renderBufferAttribute(this.gl_, this.position_);

    this.gl_.drawArrays(webgl.TRIANGLES, 0, 6);
}

我尝试在片段着色器中设置所有方向的不透明度,然后使用上面的gl.clearColor和gl.clear,但它要么清除整个事物,要么将其设置为颜色。

然后我还尝试仅在第二个程序的顶点着色器中渲染超过一半的画布,但仍无法找到解决方案。

在两个程序中也使用这些:

this.gl_.blendFunc(webgl.SRC_ALPHA, webgl.ONE_MINUS_SRC_ALPHA);
this.gl_.enable(webgl.BLEND);

这甚至可能吗?

修改

我还需要淡化面具的边缘。

谢谢!

1 个答案:

答案 0 :(得分:0)

从片段着色器中删除像素是否更好? - >它根本没有称之为fragementshader。

您可能会阅读更多关于stencilFunc的信息,其工作方式类似于模板并丢弃片段着色器的调用。因此,显示'

下面的颜色

here或某些opengl变体。