我的“柏林”噪声效果着色器产生全白或全黑

发布于 2024-10-21 21:37:07 字数 3386 浏览 2 评论 0原文

我正在尝试在 NVidia FX Composer 中编写“perlin”噪声着色器。然而,无论我如何调整噪声函数,它都会返回 100% 白色或 100% 黑色。我不知道如何解决这个问题,甚至不知道问题出在哪里。

编辑:如果您看过此页面,您可能知道我在哪里得到了代码。我想我应该从我已经在 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();
}
}

谢谢。

I'm trying to code a "perlin" noise shader in NVidia FX Composer. However, no matter how I tweak the noise function, it returns either 100% white or 100% black. I have no clue how to solve this or even where the problem is.

Edit: If you've seen This page, you probably know where I got the code. Figured I'd start with a method I'd already gotten working on a CPU.

Help, please.

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-10-28 21:37:07

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

In short form, because a GeForce 8800GT doesn't do Bitwise and pow() returns a float. So no spinning and wobbling integer bits. (Very technical explanation, that).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文