首页 > 解决方案 > pass texture map to another fragment shader

问题描述

I want to generate a texture map in WebGl in one fragment shader and then pass that texture map to another fragment shader for processing but the syntax escapes me. I believe if I understood it correctly, an example I found online said I could do something like this:

(1)
// setup frame buffer and depth buffer for the two fragment shaders.


(2)
// texture map generating frag shader:

uniform sampler2D texturemap;

void main(){
    // generate texture map
    vec4 coorindate_value = ...;
    output_texture = texture( texturemap , coorindate_value );
    // my understanding is that that sampler2d will generate some kind of a mapping. how can I map coorindate_value to some other vec4, like another vec4 coordinate???
}


(3)
// second fragment shader:

uniform sampler2D same_texturemap;

void main(){
    vec4 coorindate_value = ...;
    vec4 value = texture2D( same_texturemap , coorindate_value );
    // the returned value should be one of the color values from the first fragment shader, correct??

}

I'm not looking for anyone to provide code to help me here necessarily, but just to get some confirmation that I have an understanding of how this could work. I suppose my main confusion is over what sampler2D actually does. Is it like a dictionary or hashtable in that it maps between two values, and if so, how do I choose what those two values are? Any tips or corrections would be great.

thanks much in advance

标签: webgltexture2d

解决方案


Asampler2D是对纹理单元的引用。纹理单元保存对纹理的引用。texture2D纹理是一个二维数据数组,您可以使用该函数提取数据。您将 sampler2D 制服和归一化纹理坐标传递给它。它从纹理返回一个采样值。我说采样值是因为该值的生成方式取决于纹理的过滤器设置。

WebGL 中的输出是通过一个特殊的变量gl_FragColor。如果没有绑定帧缓冲区,则输出将转到画布的当前帧缓冲区。

您可能需要阅读一些关于 webgl 的教程。这是一篇专门关于纹理渲染到纹理的文章,但如果您不熟悉 WebGL 的其余部分,您可能需要阅读前面的文章。


推荐阅读