2016-04-14 8 views
0

OpenGLフラグメントシェーダを使用してPerlinノイズを計算する際に問題が発生しました。 結果はブロック的で、連続していません。 enter image description herePerlinノイズブロックグリッド

私は、実装のこの種を使用しようとしている

enter image description here

を私はここで、問題を把握することはできません私のフラグメントシェーダのコードは次のとおりです。

#version 330 core 
out vec3 color; 
in vec4 p; 
in vec2 uv; 

// random value for x gradiant coordinate 
float randx(vec2 co){ 
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453); 
} 


// random value for y gradaint coordiante 
float randy(vec2 co){ 
    return fract(cos(dot(co.xy ,vec2(4.9898,78.233))) * 68758.5453); 
} 


// smooth interpolation funtion 
float smoothInter(float x){ 
    return 6*x*x*x*x*x -15*x*x*x*x + 10*x*x*x; 
} 


float grid_dim = 10.0f; 


void main() { 
    // Get coloumn and row of the bottom left 
    //point of the square in wich the point is in the grid 
    int col = int(uv.x * grid_dim); 
    int row = int(uv.y * grid_dim); 



// Get the 4 corner coordinate of the square, 
//divided by the grid_dim to have value between [0,1] 
vec2 bl = vec2(col, row)/10.0f; 
vec2 br = vec2(col+1, row)/10.0f; 
vec2 tl = vec2(col, row+1)/10.0f; 
vec2 tr = vec2(col+1, row+1)/10.0f; 

// Get vectors that goes from the corner to the point 
vec2 a = normalize(uv - bl); 
vec2 b = normalize(uv - br); 
vec2 c = normalize(uv - tl); 
vec2 d = normalize(uv - tr); 

// Compute the dot products 
float q = dot(vec2(randx(tl),randy(tl)), c); 
float r = dot(vec2(randx(tr),randy(tr)), d); 
float s = dot(vec2(randx(bl),randy(bl)), a); 
float t = dot(vec2(randx(br),randy(br)), b); 

// interpolate using mix and our smooth interpolation function 
float st = mix(s, t, smoothInter(uv.x)); 
float qr = mix(q, r, smoothInter(uv.x)); 
float noise = mix(st, qr, smoothInter(uv.y)); 

// Output the color 
color = vec3(noise, noise, noise); 

}

答えて

1

最後の数行では、グローバルのx座標とy座標のsmoothInter()をlocで呼び出す必要がある場合アル座標。

float st = mix(s, t, smoothInter((uv.x - col) * grid_dim)); 
float qr = mix(q, r, smoothInter((uv.x - col) * grid_dim)); 
float noise = mix(st, qr, smoothInter((uv.y - row) * grid_dim)); 

グリッドセルは単位幅ではないため、ここではgrid_dimで乗算します。 smoothInter()は0と1の間の値をとる必要があります。

また、normalize()呼び出しを削除し、結果を[0,1]の範囲に正規化する必要がありました。これは難解でした。グリッド頂点にランダム勾配ベクトルを生成する方法があるためです。それがそのままで、あなたのコードはおよそ-2500と+2500の間の値を出力するようです。これを適切な範囲にスケールすると、私はいくつかの望ましくない規則性が現れました。私はもう一度これをprngの選択に置いた。

関連する問題