神光GodRay的简单实现

Vertex Shader:

attribute vec4 rm_Vertex;

varying vec2  vTexCoord;

void main(void)
{
   // Clean up inaccuracies
   vec2 Position;
   Position.xy = sign(rm_Vertex.xy);

   gl_Position = vec4(Position.xy, 0.0, 1.0);
   vTexCoord = Position.xy *.5 + .5;
}

Godray Fragment Shader:

#ifdef GL_FRAGMENT_PRECISION_HIGH
   // Default precision
   precision highp float;
#else
   precision mediump float;
#endif

uniform mat4 matViewProjection;

uniform sampler2D Accum;
uniform sampler2D Noise;
uniform vec3 sunPos;
uniform float decal;
uniform float decal0;
uniform int numSamples;
varying vec2 vTexCoord;

vec4 godray(vec2 tc)
{
   vec2 delta = (tc - vTexCoord) / float(numSamples);
   vec2 curTc;
   vec3 accumColor = vec3(0.0);
   int i;
   for(i = 0; i <= numSamples; ++i)
   {
      curTc = vTexCoord + delta * float(i);
      if(curTc.x < 0.0 || curTc.x > 1.0 || curTc.y < 0.0 || curTc.y > 1.0)
         break;
      
      vec4 texel = texture2D(Accum, curTc);
      if (texel.xyz == 0.0)
      {
         accumColor += clamp(vec3(1.0/length(tc - curTc) * decal), 0.0, 1.0);
      }
   }
   return vec4(accumColor / (float(i)*decal0+0.01), 0.0);
}

vec4 godray_noise(vec2 tc)
{
   vec2 delta = (tc - vTexCoord) / float(numSamples);
   vec2 curTc = vTexCoord;
   vec3 accumColor = vec3(0.0);
   int i;
   for(i = 0; i <= numSamples+1; ++i)
   {
      if(curTc.x < 0.0 || curTc.x > 1.0 || curTc.y < 0.0 || curTc.y > 1.0)
         break;
         
      vec4 texel = texture2D(Accum, curTc);
      if (texel.xyz == 0.0)
      {
         accumColor += clamp(vec3(1.0/length(tc - curTc) * decal), 0.0, 1.0);
      }
      
      float noise = texture2D(Noise, curTc).x;
      curTc += delta * noise;
   }
   return vec4(accumColor / (float(i)*decal0+0.01), 0.0);
}

void main(void)
{
   vec4 clipPos = matViewProjection * vec4(sunPos, 1.0);
   vec2 tc = clipPos.xy/clipPos.w * 0.5 + 0.5;
   
   vec4 c = godray(tc);
   
   gl_FragColor = c;
}

Radial Blur Fragment Shader:

#ifdef GL_FRAGMENT_PRECISION_HIGH
   // Default precision
   precision highp float;
#else
   precision mediump float;
#endif

uniform mat4 matViewProjection;

uniform sampler2D Accum;
uniform sampler2D GodRay;
uniform vec3 sunPos;
uniform int numBlurSamples;
uniform float scale;
varying vec2 vTexCoord;

void main(void)
{
   vec4 clipPos = matViewProjection * vec4(sunPos, 1.0);
   vec2 tc = clipPos.xy/clipPos.w * 0.5 + 0.5;

   vec2 delta = (tc - vTexCoord) * scale;
   vec2 curTc = vTexCoord;
   vec3 accumColor = vec3(0.0);
   int i;
   for(i = 0; i <= numBlurSamples; ++i)
   {
      if(curTc.x < 0.0 || curTc.x > 1.0 || curTc.y < 0.0 || curTc.y > 1.0)
         break;
         
      accumColor += texture2D(GodRay, curTc).xyz;
      
      curTc += delta;
   }
      
   gl_FragColor = texture2D(Accum, vTexCoord) + vec4(accumColor/float(i), 1.0);
}

运行结果:下面是没有采用Noise,10个采样点,无Blur的结果,可以看到有很多Aliasing。

下图是采用Noise,10个采样点,带Blur-10个采样点和不带Blur的结果。效果上提升了不少。

Shader中的一些参数设置:

numSamples: 10 numBlurSamples: 10 decal: 0.03794 decal0: 0.96000 scale: 0.01820

Noise纹理:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值