水下效果

来源:互联网 发布:powerbi连接数据库 编辑:程序博客网 时间:2024/04/28 01:21
 
这里说的水下效果, 是指在水底下时, 为了增强真实感, 做的一种水的波动效果, 算是post process的一种吧
 
原理很简单, 就是把渲染好的屏幕场景做为一张纹理输入, 然后对上面的像素按时间进行扰动, 这样感觉好像水在晃动一样, 增强代入感. (想想潜水时^_^)
 
代码很简单, 没啥好说的
struct VS_OUTPUT
{
   float4 pos       : POSITION0;
   float2 texCoord : TEXCOORD0;
};
float fTime0_1;
 
VS_OUTPUT vs_main( float4 inPos: POSITION )
{
   VS_OUTPUT o = (VS_OUTPUT) 0;
 
   inPos.xy = sign( inPos.xy);
   o.pos = float4( inPos.xy, 0.0f, 1.0f);
 
  // get into range [0,1]
   o.texCoord = (float2(o.pos.x, -o.pos.y) + 1.0f)/2.0f;
   return o;
}
sampler2D Texture0;//屏幕RenderTarget
sampler2D Texture1;//扰动纹理
float fTime0_1;
 
float4 ps_main( float2 texCoord : TEXCOORD0 ) : COLOR
{
      float2 bump = tex2D(Texture1, texCoord + (fTime0_1*20));
      float2 texel = texCoord + bump/500;
   return tex2D( Texture0, texel );
}
Texture1是一张bumpmap
最终效果:
可能不太明显, 毕竟是个动态效果, 把波动值加大:
 
关于扰动的时候屏幕边缘会显示另一边的像素的问题, 只要把texture0的寻址模式改成CLAMP就OK啦
 
原创粉丝点击