Shader特效——实现“HDR”效果【GLSL】


参考自:http://tech.it168.com/n/2007-03-29/200703291522292_6.shtml


原图:

Shader特效——实现“HDR”效果【GLSL】_第1张图片



效果图:

Shader特效——实现“HDR”效果【GLSL】_第2张图片


第一个Pass的片元着色器:

uniform sampler2D Texture0;

const vec2 texSize = vec2(256., 256.);

varying vec2 vUV;

vec4 blur_filter(mat3 filter, vec2 filter_pos_delta[9], 
                sampler2D image, vec2 xy, vec2 texSize)
{
   vec4 final_color = vec4(0., 0., 0., 0.);
   
   for(int i=0; i<3; i++)
   {
      for(int j=0; j<3; j++)
      {
         vec2 new_xy = vec2(xy.x + filter_pos_delta[3*i+j].x, 
            xy.y + filter_pos_delta[3*i+j].y);
         vec2 new_uv = vec2(new_xy.x / texSize.x, new_xy.y / texSize.y);
         final_color += texture2D(Texture0, new_uv) * filter[i][j];
      }
   }
   
   
   return final_color;
}

void main(void)
{            
   vec2 filter_pos_delta[9];
   //vec2(-1., -1.), vec2(0., -1.), vec2(1., -1.),
   //vec2(-1., 0.), vec2(0., 0.), vec2(1., 0.),
   //vec2(-1., 1.), vec2(0., 1.), vec2(1., 1.)  
   filter_pos_delta[0] = vec2(-1., -1.);
   filter_pos_delta[1] = vec2(0., -1.);
   filter_pos_delta[2] = vec2(1., -1.);
   filter_pos_delta[3] = vec2(-1., 0.);
   filter_pos_delta[4] = vec2(0., 0.);
   filter_pos_delta[5] = vec2(1., 0.);
   filter_pos_delta[6] = vec2(-1., 1.);
   filter_pos_delta[7] = vec2(0., 1.);
   filter_pos_delta[8] = vec2(1., 1.);
   
   mat3 filter = mat3(1./16., 1./8.,1./16.,
                     1./8.,1./4.,1./8.,
                     1./16.,1./8.,1./16.);
                     
   vec2 xy = vec2(vUV.x * texSize.x, vUV.y * texSize.y);
   
   vec4 color = blur_filter(filter, filter_pos_delta, 
                Texture0, xy, texSize);


   gl_FragColor = color;
}


第二个Pass的片元着色器:

uniform sampler2D Texture0;
uniform sampler2D renderTexture;

varying vec2 vUV;

const float k = 1.5;   // .5 - 2.

vec4 xposure(vec4 color, float gray, float ex)
{
   float b = (4. * ex - 1.);   
   float a = 1. - b;        
   float f = gray * (a * gray + b);   
   
   return f * color;
}

void main(void)
{
   vec4 color = texture2D(renderTexture, vUV);
   float lum = .3 * color.x + .59 * color.y + .11 * color.z;
   vec4 fColor = texture2D(Texture0, vUV);
   
   gl_FragColor = xposure(fColor, lum, k);
}

当 k = 1.5 时, f = -4*gray^2+5*gray ,  gray ∈[0, 1],函数如图所示

Shader特效——实现“HDR”效果【GLSL】_第3张图片





你可能感兴趣的:(Shader,Shader实例解析)