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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM