OpenGL 2D褪色圆通过分辨率拉伸/压缩 [英] OpenGL 2D faded circle being stretched/compressed by the resolution

查看:64
本文介绍了OpenGL 2D褪色圆通过分辨率拉伸/压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论分辨率如何,我都希望我的淡化照明(基于到点的距离)是一个完美的圆.当前,如果窗口的高度和宽度相等,则光只有一个圆圈.

I'd like my faded lighting (based on distance from a point) to be a perfect circle no matter the resolution. Currently, the light is only a circle if the height and width of the window are equal.

这是现在的样子:

我的片段着色器看起来像这样:

My fragment shader looks like this:

precision mediump float;
#endif

#define MAX_LIGHTS  10

// varying input variables from our vertex shader
varying vec4 v_color;
varying vec2 v_texCoords;

// a special uniform for textures
uniform sampler2D u_texture;
uniform vec2 u_resolution;
uniform float u_time;

uniform vec2 lightsPos[MAX_LIGHTS];
uniform vec3 lightsColor[MAX_LIGHTS];
uniform float lightsSize[MAX_LIGHTS];

uniform vec2 cam;

uniform vec2 randPos;

uniform bool dark;

void main()
{
    vec4 lights = vec4(0.0,0.0,0.0,1.0);
    float ratio = u_resolution.x/u_resolution.y;
    vec2 st = gl_FragCoord.xy/u_resolution;
    vec2 loc = vec2(.5 + randPos.x, 0.5 + randPos.y);

    for(int i = 0; i < MAX_LIGHTS; i++)
    {
        if(lightsSize[i] != 0.0)
        {
            // trying to reshape the light
            // vec2 st2 = st;
            // st2.x *= ratio;

            float size = 2.0/lightsSize[i];
            float dist = max(0.0, distance(lightsPos[i], st)); // st here was replaced with st2 when experimenting

            lights = lights + vec4(max(0.0, lightsColor[i].x - size * dist), max(0.0, lightsColor[i].y - size * dist), max(0.0, lightsColor[i].z - size * dist), 0.0);
        }
    }

    if(dark)
    {
        lights.r = max(lights.r, 0.075);
        lights.g = max(lights.g, 0.075);
        lights.b = max(lights.b, 0.075);
    }
    else
    {
        lights.r += 1.0;
        lights.g += 1.0;
        lights.b += 1.0;
    }

    gl_FragColor = texture2D(u_texture, v_texCoords) * lights;
}

我尝试通过将像素的 x 值乘以屏幕宽度与屏幕高度的 ratio 来重塑灯光,但是这导致灯光不亮地方.我想不出什么办法可以在保持形状的同时将它们放回正确的位置.

I tried reshaping the light by multiplying the x value of the pixel by the ratio of the screen width to the screen height but that caused the lights to be out of place. I couldn't figure out anything that would put them back in their correct place while maintaining their shape.

位移由我的相机在libgdx场景中的位置确定.

the displacement is determined by my camera's position in my libgdx scene.

推荐答案

您需要的是重新调整灯光位置和片段位置之间的差异

what you need is to rescale the difference between light position and fragment position

vec2 dr = st-lightsPos[i];
dr.x*=ratio;
float dist = length(dr);

这篇关于OpenGL 2D褪色圆通过分辨率拉伸/压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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