始终面对相机的着色器纹理 [英] Shader Texture Always Facing The Camera

查看:110
本文介绍了始终面对相机的着色器纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个基本的着色器,尽管我遇到了一些麻烦,但确实困扰我!

I have this basic shader, though I ran into some trouble which really bugs me!

虽然我使用片段着色器应用纹理,但是我移动或旋转了相机,但脸上的纹理将始终面向相机(我已经添加了GIF图像作为示例).

I'm applying the texture using the fragment shader though however I move or rotate the camera, the texture on the face will always be facing the camera (I've added a GIF image as an example).

因此,您可以在上面的图像上看到,纹理始终面对相机,如果我移近一点,它也会保持其缩放比例.

So as you can see on the image above the texture keeps facing the camera, also if I move away of closer it will keep its scale.

我试图达到可以四处移动的目的,并且纹理将保持其位置,旋转和缩放根据面部和顶点,而不是像现在这样根据相机.

I'm trying to achive that I can move around and the texture will keep it's position, rotation and scale according to the face and vertices, and not according to the camera as it is doing now.

这是所需的结果,尽管使用了不赞成使用的方法glVertex2fglTexCoord2f,这与我通过着色器获得的结果相同,但我猜我的问题是计算纹理上正确的UV坐标.

Here is the desired result, though using the deprecated methods glVertex2f and the glTexCoord2f which is the same result I want though through shaders, I guess my problem is calculating the correct UV coordinates on the texture.


(来源: gyazo.com )


(source: gyazo.com)

重要我想使用着色器实现此目的,以便可以对实际纹理的颜色进行更改/计算. (例如,将其设为黑白).

Important I want to achive this using shaders so I can make/calculate changes into the colors of the actual texture. (For instance make it a black and white).

这是我当前的Vertex&片段着色器代码.

Here is my current Vertex & Fragment Shader Code.

void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

片段着色器代码

uniform vec2 size;
uniform sampler2D texture;

void main() {
    gl_FragColor = texture2D(texture, gl_FragCoord.xy / size.xy);
}

片段着色器中的uniform vec2 size;是在其上应用纹理的面本身的宽度和高度. uniform sampler2D texture;当然是草的纹理本身.

The uniform vec2 size; within the fragment shader, is the width and height of the face itself where the texture is applied on. The uniform sampler2D texture; is of course the grass texture itself.

推荐答案

这样做是因为您已经编程了.您可以直接从片段的坐标导出texcoords.如果希望纹理坚持几何图形",则需要定义几何图形和纹理空间之间的映射.通常,这是通过每个顶点的2D纹理坐标属性完成的-就像固定功能管道对您提到的glTexCoord2f()所做的一样.

It does that because that is whay you've programmed. You derive the texcoords directly from the coordinates of the fragment. If you want the texture to "stick to the geometry", you need to define a mapping between your geometry and texture space. Usually, this is done through a 2D texture coordinate attribute per vertex - just as the fixed function pipeline did with the glTexCoord2f() you mentioned.

在您的特定情况下(假设主机程序仍然使用与gl_Vertex相同的默认位置指定texcoords),您可以让GL对每个片段插入texcoords并使用它们来访问纹理.片段着色器:

In your specific case, (assuming the host program still specifies texcoords the same deprectaed way you are using for vertex positions with gl_Vertex), you could let the GL interpolate the texcoords per fragment and use these to access the texture in the fragment shader:

顶点着色器代码

varying vec2 vTexCoord;

void main() {
    vTexCoord = gl_MultiTexCoord0.st;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

片段着色器代码

uniform sampler2D texture;
varying vec2 vTexCoord;

void main() {
    gl_FragColor = texture2D(texture, vTexCoord);
}

我建议改用通用顶点属性,以及更现代的GLSL版本.

I would suggest to switch to generic vertex attributen, and more modern GLSL versions, though.

这篇关于始终面对相机的着色器纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆