简体   繁体   English

HLSL Perlin噪声和XNA

[英]HLSL Perlin Noise and XNA

i need a real time Perlin Noise generation for a 2d game i am implementing. 我需要为正在实施的2d游戏提供实时的Perlin噪声生成。 The CPU noise runs fine, but needs ~1 sec for half my screen size (1920 x 1080). CPU噪声运行良好,但需要一半屏幕尺寸(1920 x 1080)约1秒。 I can scale it up about 4 times without too many artifacts but still that is a little slow assuming, that my cpu is pretty fast. 我可以将其放大约4倍,而不会出现太多伪影,但是假设我的cpu相当快,那还是有点慢。 So i want to implement it on the GPU. 所以我想在GPU上实现它。 THe problem is i have no experience with HLSL and there are not too many tutorials on it out there... I have tried to implement this (i pretty much copied it all) and added this: 问题是我没有使用HLSL的经验,并且那里没有太多的教程……我试图实现这一点 (我几乎复制了所有内容)并添加了以下内容:

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

at the end. 在末尾。 I Call it like this: 我这样称呼它:

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

and i finally get a black texture. 我终于得到了黑色的质地。 What am i doing wrong? 我究竟做错了什么? I don't understand the vertex shader part... But here they say i have to change it so that i won't get the error: "that is too complex for the target shader model"... 我不理解顶点着色器部分...但是在这里他们说我必须更改它,这样我才不会收到错误:“对于目标着色器模型来说太复杂了” ...

Seems XNA doesn't support textures which were created like that: 似乎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;
};

You need to create perm texture and perm gradient outside of shader and then pass them as parameters into shader. 您需要在着色器外部创建烫发纹理和烫发渐变,然后将它们作为参数传递到着色器中。

I started to write code but already found an example here: XNA 4.0 perlin noise using the gpu . 我开始编写代码,但在这里已经找到一个示例: 使用gpu的XNA 4.0 perlin noise

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM