【问题标题】:HLSL Perlin Noise and XNAHLSL Perlin 噪声和 XNA
【发布时间】:2014-03-23 15:41:26
【问题描述】:

我需要为我正在实施的 2d 游戏实时生成 Perlin Noise。 CPU 噪音运行良好,但我的一半屏幕尺寸 (1920 x 1080) 需要约 1 秒。我可以在没有太多伪像的情况下将其放大大约 4 倍,但假设我的 cpu 相当快,这仍然有点慢。所以我想在GPU上实现它。问题是我没有使用 HLSL 的经验,也没有太多关于它的教程...... 我试图实现this(我几乎完全复制了它)并添加了这个:

void SpriteVertexShader(inout float4 color    : COLOR0,
                        inout float2 texCoord : TEXCOORD0,
                        inout float4 position : SV_Position)
{
    position = mul(position, MatrixTransform);
}

technique Technique1
{
    pass Pass1
    {
        // TODO: set renderstates here.
        VertexShader = compile vs_3_0 SpriteVertexShader();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}

最后。我这样称呼它:

Matrix projection = Matrix.CreateOrthographicOffCenter(0,
  GraphicsDevice.Viewport.Width,
  GraphicsDevice.Viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
perlin.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);
perlin.CurrentTechnique = perlin.Techniques["Technique1"];

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
perlin.CurrentTechnique.Passes[0].Apply();
spriteBatch.Draw(new Texture2D(GraphicsDevice,1000,1000),
                               new Rectangle(0, 0, 500, 500), Color.White);
spriteBatch.End();

我终于得到了一个黑色的纹理。我究竟做错了什么? 我不明白顶点着色器部分...但是here 他们说我必须更改它,这样我就不会收到错误:“这对于目标着色器模型来说太复杂了”...

【问题讨论】:

    标签: c# xna hlsl perlin-noise


    【解决方案1】:

    似乎 XNA 不支持这样创建的纹理:

    texture permTexture
    <
      string texturetype = "2D";
      string format = "l8";
      string function = "GeneratePermTexture";
      int width = 256, height = 1;
    >;
    
    float4 GeneratePermTexture(float p : POSITION) : COLOR
    {
      return permutation[p * 256] / 255.0;
    }
    
    sampler permSampler = sampler_state
    {
      texture = <permTexture>;
      AddressU  = Wrap;
      AddressV  = Clamp;
      MAGFILTER = POINT;
      MINFILTER = POINT;
      MIPFILTER = NONE;
    };
    

    您需要在着色器之外创建烫发纹理和烫发渐变,然后将它们作为参数传递给着色器。

    我开始写代码,但已经在这里找到了一个例子:XNA 4.0 perlin noise using the gpu

    【讨论】:

      猜你喜欢
      • 2014-11-20
      • 2014-02-15
      • 2020-06-06
      • 2011-09-20
      • 2021-06-30
      • 2013-07-23
      • 2020-06-22
      • 2011-03-19
      • 1970-01-01
      相关资源
      最近更新 更多