0

我正在尝试使用置换贴图实现 2D 失真效果,我将在运行时通过组合这样的图像来创建它。置换贴图的链接

但由于 (255,255,x) 等于没有位移,我无法将这些图像与现有的 BlendModes 结合起来。

所以我想将信息存储在颜色的 RGBA(对于 x+、y+、x-、y-)中,但是渲染到 RenderTarget2D 会将 Alpha = 0 的每种颜色设置为 Alpha = 0 的黑色。有没有更好的方法来结合置换贴图或防止这种行为的方法?

4

1 回答 1

0

You can use custom shader like this

/* Variables */

float OffsetPower;

texture TextureMap; // texture 0
sampler2D textureMapSampler = sampler_state
{
    Texture = (TextureMap);
    MagFilter = Linear;
    MinFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

texture DisplacementMap; // texture 1
sampler2D displacementMapSampler = sampler_state
{
    Texture = (DisplacementMap);
    MagFilter = Linear;
    MinFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

/* Vertex shader output structures */

struct VertexShaderOutput
{
    float4 Position : position0;
    float2 TexCoord : texcoord0;
};

/* Pixel shaders */

float4 PixelShader1(VertexShaderOutput pVertexOutput) : color0
{
    float4 displacementColor = tex2D(displacementMapSampler, pVertexOutput.TexCoord);
    float offset = (displacementColor.g - displacementColor.r) * OffsetPower;

    float2 newTexCoord = float2(pVertexOutput.TexCoord.x + offset, pVertexOutput.TexCoord.y + offset);
    float4 texColor = tex2D(textureMapSampler, newTexCoord);

    return texColor;
}

/* Techniques */

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

In LoadContent method:

this.textureMap = this.Content.Load<Texture2D>(@"Textures/creeper");
this.displacementMap = this.Content.Load<Texture2D>(@"Textures/displacement_map");
this.displacementEffect = this.Content.Load<Effect>(@"Effects/displacement");

In Draw method:

Rectangle destinationRectangle = new Rectangle(0, 0, this.GraphicsDevice.Viewport.Width, this.GraphicsDevice.Viewport.Height);

this.displacementEffect.Parameters["OffsetPower"].SetValue(0.5f);
this.displacementEffect.Parameters["DisplacementMap"].SetValue(this.displacementMap);

this.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, this.displacementEffect);
this.spriteBatch.Draw(this.textureMap, destinationRectangle, Color.White);
this.spriteBatch.End();

Result.

于 2014-07-09T23:11:42.797 回答