简体   繁体   English

WebGL渲染到纹理-如何将数据写入Alpha通道?

[英]WebGL render to texture - how do I write data to the alpha channel?

To preface this, I'm trying to replicate the water rendering algorithm describe in this article http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter19.html . 为此,我正在尝试复制本文http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter19.html中描述的水渲染算法。 Part of this algorithm requires rendering an alpha mask into a framebuffer in order to use later for a texture sampling from the originally rendered scene. 此算法的一部分要求将alpha蒙版渲染到帧缓冲区中,以便以后用于从原始渲染的场景进行纹理采样。 In short, the algorithm looks like this: 简而言之,该算法如下所示:

  1. Render scene geometry into a texture S , skipping refractive meshes and replacing it with an alpha mask 将场景几何渲染为纹理S ,跳过折射网格并将其替换为Alpha蒙版
  2. Render refractive meshes by sampling texture S with perturbation IF it's inside the alpha mask, otherwise just directly sample texture S 如果纹理S在alpha蒙版内部,则通过摄动纹理S来渲染折射网格,否则直接采样纹理S

Unfortunately, I'm still learning WebGL and don't really know enough to know how to approach this. 不幸的是,我仍在学习WebGL,但对所要解决的方法的了解并不多。 Additionally, that article uses HLSL, and the conversion is nontrivial for me. 此外,该文章使用HLSL,对于我而言,转换是不平凡的。 Obviously, attempting to do this in the fragment shader won't work: 显然,尝试在片段着色器中执行此操作将不起作用:

void main( void ) {
    gl_FragColor = vec4( 0.0 );
}

because it will just blend with the previously rendered geometry and the alpha value will still be 1.0. 因为它只会与先前渲染的几何体融合,并且alpha值仍为1.0。

Here is a brief synopsis of what I have: 这是我所拥有的简要简介:

function animate(){
    ... snip ...
    renderer.render( scene, camera, rtTexture, true );

    renderer.render( screenScene, screenCamera );
}

// water fragment shader
void main( void ){
    // black out the alpha channel
    gl_FragColor = vec4(0.0);
}

// screen fragment shader 
varying vec2 vUv;
uniform sampler2D screenTex;

void main( void ) {
    gl_FragColor = texture2D( screenTex, vUv );

    // just trying to see what the alpha mask would look like
    if( gl_FragColor.a < 0.1 ){
        gl_FragColor.b = 1.0;
    }
}

The entire code can be found at http://scottrabin.github.com/Terrain/ 完整的代码可以在http://scottrabin.github.com/Terrain/找到。

Obviously, attempting to do this in the fragment shader won't work: because it will just blend with the previously rendered geometry and the alpha value will still be 1.0. 显然,尝试在片段着色器中执行此操作将不起作用:因为它将仅与先前渲染的几何体融合,并且alpha值仍为1.0。

That's up to you. 随你(由你决定。 Just use the proper blend modes: 只需使用适当的混合模式:

glBlendFuncSeparate(..., ..., GL_ONE, GL_ZERO);

glBlendFuncSeparate sets up separate blending for the RGB and Alpha portions of the color. glBlendFuncSeparate为颜色的RGB和Alpha部分设置单独的混合。 In this case, it writes the source alpha directly to the destination. 在这种情况下,它将源Alpha直接写入目标。

Note that if you're drawing something opaque, you don't need blend modes. 请注意,如果要绘制不透明的东西,则不需要混合模式。 The output alpha will be written as is, just like the color. 输出的alpha将像颜色一样原样写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM