【发布时间】:2019-09-14 02:47:53
【问题描述】:
我想在一个片段着色器中在 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 的实际作用。它是否像字典或哈希表一样,因为它在两个值之间进行映射,如果是,我该如何选择这两个值是什么?任何提示或更正都会很棒。
提前感谢
【问题讨论】:
-
sampler2D是对纹理的引用。纹理是一个二维数据数组,您可以使用texture2D函数提取数据。您将 sampler2D 制服和标准化纹理坐标传递给它。 WebGL 中的输出是通过一个特殊的变量gl_FragColor。您可能需要read some tutorials on webgl。这是一个specifically about textures,但如果您不熟悉 WebGL 的其余部分,您可能需要阅读前面的文章。