Opengl es 光晕着色器似乎放错了位置 我想得到一个中间有发光的渐变圆。使用我的代码中使用的方法。但有些地方出了问题。中间的发光部分不能精确地居中。辉光下的底部比顶部大。检查了油漆上的像素,只是为了确定这不是一种视觉错觉 如图所示:

Opengl es 光晕着色器似乎放错了位置 我想得到一个中间有发光的渐变圆。使用我的代码中使用的方法。但有些地方出了问题。中间的发光部分不能精确地居中。辉光下的底部比顶部大。检查了油漆上的像素,只是为了确定这不是一种视觉错觉 如图所示:,opengl-es,glsl,shader,opengl-es-2.0,Opengl Es,Glsl,Shader,Opengl Es 2.0,为什么会这样 代码: // Author @patriciogv - 2015 // http://patriciogonzalezvivo.com #ifdef GL_ES precision mediump float; #endif #define PI 3.14159265359 #define TWO_PI 6.28318530718 uniform vec2 u_resolution; uniform vec2 u_mou

为什么会这样

代码:

   // Author @patriciogv - 2015
   // http://patriciogonzalezvivo.com

   #ifdef GL_ES
   precision mediump float;
   #endif

   #define PI 3.14159265359
   #define TWO_PI 6.28318530718


   uniform vec2 u_resolution;
   uniform vec2 u_mouse;
   uniform float u_time;



   void main(){
       vec2 c = gl_FragCoord.xy/u_resolution.xy;
       c.x *= u_resolution.x/u_resolution.y;
       c.y =  c.y;
       c = vec2(0.5, 0.5) - c;

       float d = smoothstep(0.0, 1.572, 0.336 - length(c.xy));
       float glowsize = 30.712;
       gl_FragColor = vec4(0., .0, d, 1.) * glowsize ;

   }

必须首先进行平移,以便原点位于视口的中心。之后,必须应用纵横比:

precision mediump float;

varying vec2 vertPos;
varying vec4 vertColor;
uniform vec2 u_resolution;

void main()
{
    vec2 c = gl_FragCoord.xy/u_resolution.xy;
    c = vec2(0.5, 0.5) - c;
    c.x *= u_resolution.x/u_resolution.y;

    float d = smoothstep(0.0, 1.572, 0.336 - length(c.xy));
    float glowsize = 30.712;
    gl_FragColor = vec4( vec3(/*0., .0,*/ d), 1.) * glowsize ;
}

注意,如果纵横比为1/2且
vec2 c=vec2(0.5,0.5)
(这是位于视口中心的片段),则结果为:

c = (0.5, 0.5)

c' = (0.5, 0.5) - c * (1.0/2.0, 1.0) 
c' = (0.25, 0.0)
c = (0.5, 0.5)

c' = [(0.5, 0.5) - c] * (1.0/2.0, 1.0) 
c' = (0.0, 0.0)
如果先平移,然后再乘以纵横比,则结果为:

c = (0.5, 0.5)

c' = (0.5, 0.5) - c * (1.0/2.0, 1.0) 
c' = (0.25, 0.0)
c = (0.5, 0.5)

c' = [(0.5, 0.5) - c] * (1.0/2.0, 1.0) 
c' = (0.0, 0.0)

更新

使用以下着色器,您可以看到结果完全居中:

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main() {
    vec2 c = gl_FragCoord.xy/u_resolution.xy;
    c = vec2(0.5, 0.5) - c;
    c.x *= u_resolution.x/u_resolution.y;

    float d = smoothstep(0.0, 1.572, 0.336 - length(c.xy));
    float glowsize = 30.712;

    float dia = step(abs(abs(c.x)-abs(c.y)),0.005);
    gl_FragColor = vec4( mix( vec3(0.0, 0.0, d) * glowsize, vec3(1.0,1.0,0.0), dia), 1.0);
}
预览:


谢谢您的回复!。然而问题仍然是一样的,最下面的部分是bigger@RomeoTheWizard视口是否在画布上,是否在视口上绘制?我的意思是,如果你使用
gl_FragColor=vec4(1.0,0.0,0.0,1.0),所有的颜色都是红色的?是,所有部件均涂有油漆red@RomeoTheWizard,当我将答案的代码复制到[,则发光完全居中,但底部发光部分似乎比上部大。