2011-03-13 66 views
0

我試圖在NVidia FX編寫器中編寫「perlin」噪聲着色器。但是,無論我如何調整噪音功能,它都會返回100%白色或100%黑色。我不知道如何解決這個問題,甚至是問題出在哪裏。我的「perlin」噪聲效果着色器產生全白或全黑

編輯:如果你已經看到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

你的源代碼是http://freespace.virgin.net/hugo.elias/models/m_perlin.htm?只是想知道,我認識到這些功能的命名。旁註說,該網站沒有提到整個八度的事情不是'Perlin'噪音的一部分,而是http://en.wikipedia.org/wiki/Fractional_Brownian_motion。 – Dykam 2011-03-13 21:36:58

+0

您必須在編輯之前發佈。 :)是的,那是我第一次遇到「perlin」噪音的地方。 – 2011-03-13 21:59:16

回答

0

簡而言之,因爲GeForce 8800GT不做Bitwise和pow()返回一個浮點數。所以沒有旋轉和擺動整數位。 (非常技術性的解釋,那)。