2d中水波的原理与实现

不废话,直接上代码

 

【xna 中调用】

 

GraphicsDevice.Textures[1] = wa;

// TODO: Add your drawing code here
times += (float)gameTime.ElapsedGameTime.Milliseconds;
eff.CurrentTechnique = eff.Techniques[0];
eff.Parameters["times"].SetValue(times);
//eff.Parameters["point"].SetValue(point);

spriteBatch.Begin(SpriteSortMode.Deferred,
BlendState.AlphaBlend,
SamplerState.LinearWrap,
DepthStencilState.Default,
RasterizerState.CullNone,
eff);
spriteBatch.Draw(tex, Vector2.Zero, Color.White);
spriteBatch.End();

 

【shader】

float times;
sampler tex_sampler:register(s0);
sampler tex_ripple:register(s1);

struct Input
{
float2 uv:TEXCOORD0;
};

float4 PShader(Input input) : COLOR0
{
//==================================================================================
//波动效果
float4 color = float4(0,0,0,0);
float2 backgroundUv = input.uv;

//mark #1
//当前像素点显示的纹理上的UV坐标
//是根据原始显示的纹理UV计偏移
//其实就是纹理上的点做园周运动
//原理:像素点x ,uv为(x,y)
//修改像素点显示的纹理
//uv => 新的uv为(x*(1+ sin(x * Increment)),y*(1+cos(y* Increment)));

backgroundUv.x += sin(times / 500.f + backgroundUv.x * 15) * 0.005;
backgroundUv.y += cos(times / 500.f + backgroundUv.y * 15) * 0.005;
color = tex2D(tex_sampler,backgroundUv);

//==================================================================================
//原理同上
//水底的光效果
float4 shading = float4(0,0,0,0);
float2 lightUv = input.uv;

//同上 mark #1
input.uv.x += cos(times / 100.f + input.uv.x * 50 ) * 0.01 ;
input.uv.y += sin(times / 100.f + input.uv.y * 50) * 0.01 ;
shading = tex2D(tex_ripple,input.uv);

//让一张白图呈沅的颜色
shading.r *= 0.7f;
shading.g *= 0.59f;
shading.b *= 0.11f;
//==================================================================================

return color + shading * 0.3 ;
}

technique Technique1
{
pass Pass1
{
PixelShader = compile ps_2_0 PShader();
}
}

由于系统只支持2M之下的附件,索要源代码请联系QQ:371741579

posted @ 2012-02-02 16:06  czjone  阅读(2873)  评论(7编辑  收藏  举报