使用片段着色器失真后如何使屏幕居中?

How to center the screen after using a distortion with fragment shader?

我正在尝试使用片段着色器添加一些醉酒效果。

但是我在右上角的坐标上仍然遇到了一些问题,正如你在图片上看到的那样...

这是片段着色器:

/* Render of the screen */
uniform sampler2D uSampler;
/* Texture of the distortion*/
uniform sampler2D uDeformation;
/* Texture for the intensity */
uniform sampler2D uIntensity;
/* The time (u_time) */
uniform float uTime;
/* Scale of Deformation */
uniform float uScale;
/* Coords of fragment */
varying vec2 vTextureCoord;

void main (void) {
    vec4 intensityVec = texture2D(uIntensity, vTextureCoord)*(uTime,0.5); // Intensité qu'on met aux corrdonnées (uTime,0.5)
    vec2 mod_texcoord = vec2(0,vTextureCoord.y*sin(uTime))+intensityVec.xy; // Calcul des coordonnées de la deformation à appliquer en fonction du sinus
    gl_FragColor = texture2D(uSampler, vTextureCoord+uScale*texture2D(uDeformation, mod_texcoord).rg); // On assemble l'intensité, la deformation et le rendu
}

有人知道我应该做什么吗?我试图重新缩放它,但它似乎什么也没做,而且我没有想法...

如何根据与屏幕边缘的距离将原始纹理坐标与变形坐标混合,以便变形向边缘淡出?

像这样:

float tc_mix_fact = 1.0 - distance(vTextureCoord, vec2(0.5)));
vec2 final_tc = mix(vTextureCoord, uScale*texture2D(uDeformation, mod_texcoord).rg, tc_mix_fact);
gl_FragColor = texture2D(uSampler, final_tc);

您很可能想调整函数以混合纹理坐标,但这应该是一个不错的起点。

注意:tc_mix_fact 的距离函数会很慢,因此您也可以创建一个查找 gradient/vignetting 纹理来加快速度。

注 2:除了将混合因子应用到纹理坐标,将它应用到 uScale 因子可能看起来更好,而不是淡出屏幕边缘的失真。

我终于得到了几乎我想要的结果(我只是缩放效果有点问题)我只是移动了 uScalecenter 来调整 final_coords 和成功了!

vec2 final_coords = mix(vTextureCoord, texture2D(uDeformation, mod_texcoord).rg, center*uScale);