水波紋特效


      具體算法見 http://www.cnblogs.com/worldreason/archive/2008/05/09/1189648.html

下面直接上代碼:

HLSL:

sampler2D Background : register(s0); //水波背景圖片

float dx; //水波網格每個格子的大小 =1/(water.Width-1)
float dy; // =1/(water.Height-1)

texture Height; //水波網格的高度數據
sampler HeightSampler = sampler_state
{
    Texture = (Height);
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

float4 PS(float2 uv : TEXCOORD0) : COLOR0
{
    float xoff = tex2D(HeightSampler, float2(saturate(uv.x + dx), uv.y)).x - tex2D(HeightSampler, float2(saturate(uv.x - dx), uv.y)).x;
    float yoff = tex2D(HeightSampler, float2(uv.x, saturate(uv.y + dy))).x - tex2D(HeightSampler, float2(uv.x, saturate(uv.y - dy))).x;
    uv.x = saturate(uv.x + xoff / 20);
    uv.y = saturate(uv.y + yoff / 20);
    float4 color = tex2D(Background, uv);
    color.rgb += (xoff + yoff) / 2;
    return color;
}

technique simple
{
    pass Water
    {
        PixelShader = compile ps_2_0 PS();
    }
}

如果在WPF里使用,對應的HLSL需要做下修改:

float dx : register(C0);
float dy : register(C1);

sampler2D Background : register(S0);
sampler2D Height : register(S1);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float xoff = tex2D(HeightSampler, float2(saturate(uv.x + dx), uv.y)).x - tex2D(HeightSampler, float2(saturate(uv.x - dx), uv.y)).x;
    float yoff = tex2D(HeightSampler, float2(uv.x, saturate(uv.y + dy))).x - tex2D(HeightSampler, float2(uv.x, saturate(uv.y - dy))).x;
    uv.x = saturate(uv.x + xoff / 20);
    uv.y = saturate(uv.y + yoff / 20);
    float4 color = tex2D(BackgroundSampler, uv);
    color.rgb += (xoff + yoff) / 2;
    return color;
}

 

源代碼下載  包含C++ & Win32、WinForm & SlimDX、WPF三個版本


免責聲明!

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



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