我想在一个片段着色器中的WebGl中生成一个纹理贴图,然后将该纹理贴图传递给另一个片段着色器进行处理,但是语法使我无所适从。我相信,如果我对它的理解正确,那么我在网上发现的一个例子说我可以做这样的事情:
(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??
}
我并不是在寻找任何人来提供必要的代码来帮助我,但只是为了获得一些确认,我已经了解这是如何工作的。 我想我的主要困惑在于sampler2D实际做什么。它像一个字典或哈希表,它在两个值之间映射,如果是的话,我该如何选择这两个值呢?任何技巧或更正都很好。
非常感谢
答案 0 :(得分:0)
sampler2D
是对纹理单元的引用。纹理单元保存对纹理的引用。纹理是2D数据数组,您可以使用texture2D
函数将数据提取出来。您将其传递给sampler2D制服和标准化的纹理坐标。它从纹理返回一个 sampled 值。我说 sampled 值是因为该值的生成方式取决于纹理的滤镜设置。
WebGL中的输出通过特殊变量gl_FragColor
来实现。如果未绑定任何帧缓冲区,则输出将转到画布的当前帧缓冲区。
您可能需要read some tutorials on webgl。这里是specifically about textures和rendering to texture之一,但是如果您不熟悉其余WebGL,则可能需要阅读前面的文章。