【问题标题】:c# XNA 2D Distortion Effectc# XNA 2D 失真效果
【发布时间】:2014-07-09 21:34:11
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# xna 2d spritebatch


    【解决方案1】:

    你可以像这样使用自定义着色器

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

    Result.

    【讨论】:

    • 我将失真与位移混淆了,我想分别存储 x 和 y 位移以激活像i.stack.imgur.com/m8IoW.jpg 这样的红色,我的着色器正在工作。问题是将两张地图添加到一张地图中,两张图像重叠。由于没有位移是 0.5r 0.5g ?b 所以将 0.25r 0.5g (x+= -0.25, y+=0) 添加到 0.25r 0.5g 将导致 0r 0.5g (x+= -0.5, y+=0
    • @AntiHeadshot,对不起,你很困惑 :) 据我了解,你以某种方式计算失真:偏移 = ((r-0.5)*power, (g-0.5)*power)。我现在无法检查这个,但认为如果你这样做,你会得到正确的失真。
    • 是的,没错,但我的问题不是着色器,而是创建置换贴图,但我认为我有一个解决方案。今晚我将测试实现它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    相关资源
    最近更新 更多