C# HLSL的旋转效果,从Microsoft SpriteEffects示例开始

C# HLSL的旋转效果,从Microsoft SpriteEffects示例开始,c#,xna,xna-4.0,hlsl,C#,Xna,Xna 4.0,Hlsl,如果你看了代码,我对refraction.fx和void drawrefractier glacier(GameTime GameTime)功能感兴趣。在这里,您可以注意到该函数使用纹理在图像上渲染水失真(falter.jpg作为“扭曲图像”,而glacier.jpg作为扭曲图像) 如果您在refraction.fx中阅读,开头会显示: // Effect uses a scrolling displacement texture to offset the position of the ma

如果你看了代码,我对
refraction.fx
void drawrefractier glacier(GameTime GameTime)
功能感兴趣。在这里,您可以注意到该函数使用纹理在图像上渲染水失真(
falter.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.
似乎通过改变图像很容易达到另一种效果。我试着用这样的图像:

我想达到把周围的一切扭曲成旋转漩涡或螺旋的效果。我该怎么做

一些简单的顺序屏幕显示了我的纹理的外观:

折射着色器:

// 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();
}

第一个想法是:折射在屏幕上滚动,在你的情况下,你应该用旋转矩阵变换位移值,得到旋转的失真。你能展示一下你的图像被扭曲的结果吗?也许你必须生成一个螺旋,而不是手工绘制。这些屏幕够了吗?或者我应该试着制作一个视频吗?我认为它们足够了。看起来你的图像在整个屏幕上都被拉伸了,而不是在中间。你关闭了着色器中的滚动效果了吗?除了加载图像,我什么都没碰。。。是的,它似乎散布在整个屏幕上!