我的“perlin”噪音效果着色器可以产生全白或全黑

时间:2011-03-13 21:30:24

标签: graphics pixel-shader perlin-noise

我正在尝试在NVidia FX Composer中编写“perlin”噪声着色器。但是,无论我如何调整噪音功能,它都会返回100%白色或100%黑色。我不知道如何解决这个问题,甚至问题是什么。

编辑:如果您看过This page,您可能知道我在哪里获得了代码。想想我会从一个已经开始使用CPU的方法开始。

请帮助。

float Noise1(int x, int y)
{
    int n = x + y * 57;
// int n = x + y * 1376312627;
// n = n * n;
// n = (int)pow(n * pow(2, 13), n);
// return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) + 0x7fffffff) / 1073741824.0);    
// return abs( ( (n * (n * n * 15731 + 789221) + 1376312589) + 0x7fffffff) / 2147483647.0);
// return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) + 0x7fffffff) / 2147483647.0);    
// return ( n / 2147483647.0);    
return ( ((float)n) / 500.0 );
// return n = 2147483647.0;
}

float SmoothNoise_1(int x, int y)
{
    float corners = ( Noise1(x-1, y-1) + Noise1(x+1, y-1) + Noise1(x-1, y+1) + Noise1(x+1, y+1) ) / 16.0;
    float sides   = ( Noise1(x-1, y)   + Noise1(x+1, y)   + Noise1(x,   y-1) + Noise1(x, y+1) ) /  8.0;
    float center  =  Noise1(x, y) / 4.0;
    return corners + sides + center;
}

float Cosine_Interpolate(float a, float b, float x)
{
float ft = x * 3.1415927;
float f = (1 - cos(ft)) * 0.5;

return  a*(1-f) + b*f;
}

float InterpolatedNoise_1(float x, float y)
{
int integer_X    = (int)x;
float fractional_X = x - integer_X;

int integer_Y    = (int)y;
float fractional_Y = y - integer_Y;

float v1 = SmoothNoise_1(integer_X,     integer_Y);
float v2 = SmoothNoise_1(integer_X + 1, integer_Y);
float v3 = SmoothNoise_1(integer_X,     integer_Y + 1);
float v4 = SmoothNoise_1(integer_X + 1, integer_Y + 1);

float i1 = Cosine_Interpolate(v1 , v2 , fractional_X);
float i2 = Cosine_Interpolate(v3 , v4 , fractional_X);

return Cosine_Interpolate(i1 , i2 , fractional_Y);
}


int width = 512;
int height = 512;


float4 PerlinNoise_2D(float2 xy : TEXCOORD0) : COLOR0
{
float4 total = 0;
// int p = persistence;
float p = 1.0;
// int n = Number_Of_Octaves - 1;
int n = 2;

for(int i = 0; i < n; ++i)
{
    float frequency = pow(2, i);
    float amplitude = pow(p, i);

    /* total.a = InterpolatedNoise_1(xy.x * width * frequency, xy.y * height * frequency) * amplitude;
    total.r = InterpolatedNoise_1(xy.x * width * frequency, xy.y * height * frequency) * amplitude;
    total.g = InterpolatedNoise_1(xy.x * width * frequency, xy.y * height * frequency) * amplitude;
    total.b = InterpolatedNoise_1(xy.x * width * frequency, xy.y * height * frequency) * amplitude; */
    /* total.a = InterpolatedNoise_1(xy.x * frequency, xy.y * frequency) * amplitude;
    total.r = InterpolatedNoise_1(xy.x * frequency, xy.y * frequency) * amplitude;
    total.g = InterpolatedNoise_1(xy.x * frequency, xy.y * frequency) * amplitude;
    total.b = InterpolatedNoise_1(xy.x * frequency, xy.y * frequency) * amplitude; */
    total.a = InterpolatedNoise_1(xy.x * width, xy.y * height);
    total.r = InterpolatedNoise_1(xy.x * width, xy.y * height);
    total.g = InterpolatedNoise_1(xy.x * width, xy.y * height);
    total.b = InterpolatedNoise_1(xy.x * width, xy.y * height);
}

return clamp(total, 0.0, 1.0);
// return (float)(int)(2147483647 + 2147483647 + 2147483647 / 2) / 2147483647.0;
}

technique Perlin
{
pass p0
{
        VertexShader = null;
        PixelShader = compile ps_3_0 PerlinNoise_2D();
}
}

感谢。

1 个答案:

答案 0 :(得分:0)

简而言之,因为GeForce 8800GT不执行Bitwise而pow()返回浮点数。所以没有旋转和摆动整数位。 (非常技术性的解释,那)。