GLSL 片段着色器中纹理到对象的简单映射
我在片段着色器中映射纹理时遇到问题。我有一个与窗口大小相同的纹理(我使用在上一个通道中渲染的场景的一部分,但我在下面的示例中使用砖块纹理),并且我需要将其映射为 2D。这有可能吗?我尝试了不同的方法,但没有任何效果。
您能告诉我片段着色器应该是什么样子吗?请任何建议都会有所帮助。谢谢你!
I have a problem with mapping of texture in fragment shader. I have a texture that has same size as window (I use part of scene rendered in previous pass, but I use brick texture in the example below) and I need to map it as in 2D. Is it somehow possible ? I tried different ways, but nothing worked.
Could you please advice me how should the fragment shader looks like ? Please any advice will be helpfull. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,如果我理解正确,那么您想要使用之前生成的纹理,就像它是某种“图层”一样,其中纹理像素以 1:1 映射到视口像素,并且纹理与视口具有非常相同的大小?
如果是这样,那么这很容易(从 GLSL 版本 1.30 开始)。有一个函数 texelFetch,它采用像素索引作为坐标(与采用 [0; 1] 范围内的值的
texture
不同)。内置变量gl_FragCoord给出当前处理的片段的窗口坐标。因此,通过将gl_FragCoord
中的坐标用于texelFetch
,您可以获得所需的结果。So if I understand you correctly, then you want to use the previously generated texture as if it was some kind of "layer", where texture pixels map 1:1 to viewport pixels, and the texture has the very same size like the viewport?
If so then this is very easy (as of GLSL version 1.30). There is the function
texelFetch
, which takes a pixel index as coordinate (unliketexture
which takes a value in the range [0; 1]). The built in variablegl_FragCoord
gives the window coordinates of the currently processed fragment. So by using the coordinates ingl_FragCoord
fortexelFetch
you get the desired result.