使用HLSL旋转效果,从Microsoft SpriteEffects示例开始

时间:2014-02-06 20:16:59

标签: c# xna xna-4.0 hlsl

The sample

如果您观看代码,我对refraction.fxvoid DrawRefractGlacier(GameTime gameTime)函数感兴趣。在这里,您可以注意到该函数使用纹理在图像上呈现水失真(waterfall.jpg为“扭曲图像”,glacier.jpg为扭曲图像。

如果您在refraction.fx内阅读,则在开头说:

// Effect uses a scrolling displacement texture to offset the position of the main
// texture. Depending on the contents of the displacement texture, this can give a
// wide range of refraction, rippling, warping, and swirling type effects.

通过更改图像似乎很容易实现其他效果。我尝试了这样的图像:

My image

我希望通过旋转旋转或螺旋来实现扭曲周围一切的效果。我怎么能这样做?

一些关于纹理外观的简单顺序屏幕:

Screen1 Screen2 enter image description here

折射着色器:

// Effect uses a scrolling displacement texture to offset the position of the main
// texture. Depending on the contents of the displacement texture, this can give a
// wide range of refraction, rippling, warping, and swirling type effects.

float2 DisplacementScroll;
float2 angle;

sampler TextureSampler : register(s0);
sampler DisplacementSampler : register(s1);

float2x2 RotationMatrix(float rotation)   
{   
    float c = cos(rotation);   
    float s = sin(rotation);   

    return float2x2(c, -s, s ,c);   
}  

float4 main(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
   float2 rotated_texcoord = texCoord;   
        rotated_texcoord -= float2(0.25, 0.25);   
        rotated_texcoord = mul(rotated_texcoord, RotationMatrix(angle));   
        rotated_texcoord += float2(0.25, 0.25);   

    float2 DispScroll = DisplacementScroll;

    // Look up the displacement amount.
    float2 displacement = tex2D(DisplacementSampler, DispScroll+ texCoord / 3);

    // Offset the main texture coordinates.
    texCoord += displacement * 0.2 - 0.15;

    // Look up into the main texture.
    return tex2D(TextureSampler, texCoord) * color;
}


technique Refraction
{
    pass Pass1
    {
        PixelShader = compile ps_2_0 main();
    }
}

它的绘制电话:

void DrawRefractGlacier(GameTime gameTime)
{
    // Set an effect parameter to make the
    // displacement texture scroll in a giant circle.
    refractionEffect.Parameters["DisplacementScroll"].SetValue(
                                                MoveInCircle(gameTime, 0.2f));

    // Set the displacement texture.
    graphics.GraphicsDevice.Textures[1] = waterfallTexture;

    // Begin the sprite batch.
    spriteBatch.Begin(0, null, null, null, null, refractionEffect);

    // Because the effect will displace the texture coordinates before
    // sampling the main texture, the coordinates could sometimes go right
    // off the edges of the texture, which looks ugly. To prevent this, we
    // adjust our sprite source region to leave a little border around the
    // edge of the texture. The displacement effect will then just move the
    // texture coordinates into this border region, without ever hitting
    // the edge of the texture.

    Rectangle croppedGlacier = new Rectangle(32, 32,
                                             glacierTexture.Width - 64,
                                             glacierTexture.Height - 64);

    spriteBatch.Draw(glacierTexture,
                     GraphicsDevice.Viewport.Bounds,
                     croppedGlacier,
                     Color.White);

    // End the sprite batch.
    spriteBatch.End();
}

0 个答案:

没有答案