您的位置:首页 > 其它

快速8-bit通道的伪HDR的实现

2015-07-18 12:33 239 查看
作者Nhsoft

看了Hugo老师的文章,试验了一下.

写了一个Fragment shader,用ShaderDesigner写的.

fakehdr.frag

//http://freespace.virgin.net/hugo.elias/graphics/x_posure.htm

//Hugo老师指出,曝光度是根据进入镜头的光线来决定的。

//在我们渲染完一个图象后,应该采用float16格式来保存。

//但是如果对于一个不是float16格式的Rendertarget,我们可以取

//周围的颜色平均后作为决定曝光度的因子,

//以下的shader代码应该说是非常偷工减料的。但是效果看上去还可以。

uniform sampler2D texture;

const float blurfactor = 12.5;//blur度,就是把多远处的像素取过来

const float expfactor = 0.8;//曝光度

const float cdelp = 0.0009765625;//一个像素对应的float大小。我用的纹理是1024,自己去算

const float sharpness = 0.8;//看代码。。。。

vec4 xposure(vec4 cl,float e)

{

return ( exp(expfactor) - exp(expfactor-e)) * cl;

}

void main()

{

float delp = blurfactor* cdelp;

vec4 texColor = texture2D(texture,vec2(gl_TexCoord[0].s ,gl_TexCoord[0].t ));

//把当前位置的颜色和周围的做blur,算曝光度时候和算最后颜色结合的时候,权重是不同,

//最终的颜色是。。。。。

vec4 color = texColor*2.0 / 22.0;

color += texture2D(texture,vec2(gl_TexCoord[0].s+delp,gl_TexCoord[0].t+delp))*5.0 /22.0;

color += texture2D(texture,vec2(gl_TexCoord[0].s+delp,gl_TexCoord[0].t-delp))*5.0 /22.0;

color += texture2D(texture,vec2(gl_TexCoord[0].s-delp,gl_TexCoord[0].t-delp))*5.0 /22.0;

color += texture2D(texture,vec2(gl_TexCoord[0].s-delp,gl_TexCoord[0].t+delp))*5.0 /22.0;

float e = color.r * 0.3 + color.g * 0.59 + color.b * 11;

//算出了亮度信息,

gl_FragColor = xposure(texColor*sharpness + color*(1-sharpness),e);

}

以上是个GLSL代码:

翻译成CG也很简单地

made by Azure

//http://freespace.virgin.net/hugo.elias/graphics/x_posure.htm

//Hugo老师指出,曝光度是根据进入镜头的光线来决定的。

//在我们渲染完一个图象后,应该采用float16格式来保存。

//但是如果对于一个不是float16格式的Rendertarget,我们可以取

//周围的颜色平均后作为决定曝光度的因子,

//以下的shader代码应该说是非常偷工减料的。但是效果看上去还可以。

uniform sampler2D texture;

const float blurfactor = 12.5;//blur度,就是把多远处的像素取过来

const float expfactor = 0.8;//曝光度

const float cdelp = 0.0009765625;//一个像素对应的float大小。我用的纹理是1024,自己去算

const float sharpness = 0.8;//看代码。。。。

float4 xposure(float4 cl,float e)

{

return ( exp(expfactor) - exp(expfactor-e)) * cl;

}

void main( float2 texcoord : TEXCOORD0

out float4 finalColor : COLOR)

{

float delp = blurfactor* cdelp;

float4 texColor = tex2D(texture, texcoord.x, texcoord.y);

//把当前位置的颜色和周围的做blur,算曝光度时候和算最后颜色结合的时候,权重是不同,

//最终的颜色是。。。。。

float4 color = texColor*2.0 / 22.0;

color += tex2D(texture,float2(texcoord.x+delp, texcoord.y+delp))*5.0 /22.0;

color += tex2D(texture,float2(texcoord.x+delp, texcoord.y- delp))*5.0 /22.0;

color += tex2D(texture,float2(texcoord.x- delp, texcoord.y- delp))*5.0 /22.0;

color += tex2D(texture,float2(texcoord.x- delp, texcoord.y+delp))*5.0 /22.0;

float e = color.r * 0.3 + color.g * 0.59 + color.b * 11;

//算出了亮度信息,

finalColor = xposure(texColor*sharpness + color*(1-sharpness),e);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: