我试图用置换贴图实现2D失真效果,我将在运行时通过组合这样的图像来创建。 Link to Displacementmap
但是由于(255,255,x)等于无位移的事实,我无法将这些图像与现有的BlendModes结合起来。
所以我想将信息存储在RGBA(对于x +,y +,x-,y-)的颜色中,但渲染到RenderTarget2D会将每个颜色设置为Alpha = 0到Black,Alpha = 0。 是否有更好的方法来组合位移图或防止这种行为的方法?
答案 0 :(得分:0)
您可以像这样使用自定义着色器
/* 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();
}
}
在LoadContent方法中:
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");
在Draw方法中:
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();