Jump to content

  • Log In with Google      Sign In   
  • Create Account


Perlin Noise tiling


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 montify   Members   -  Reputation: 361

Like
0Likes
Like

Posted 25 December 2012 - 10:36 PM

Hello

I implement Libnoise for the Terrain, it works perfect, i have a Quadtree with 33x33 Chunks and i tile this with
perlin.GetValue() <-- Libnoise.

Now for the performance i implement PerlinNoise on the GPU, but anyone can tell me how i can tile the "Heightmap"?


I need for every Chunk that get splitted a new Heightmap with the right coordinates.

Here's the code:

 

 

 

float4x4 World;
float3 coord;
 
struct VertexShaderInput
{
    float4 Position : POSITION0;
    float2 texCoord : TEXCOORD0;
};
 
struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 texCoord : TEXCOORD0;
    float4 wPosition: TEXCOORD1;
};
 
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;
 
    output.Position = input.Position;
    output.wPosition = input.Position;
    output.texCoord = input.Position;
 
    return output;
}
 
texture permTexture2d;
texture permGradTexture;
 
sampler permSampler2d = sampler_state 
{
    texture = <permTexture2d>;
    AddressU  = Wrap;       
    AddressV  = Wrap;
    MAGFILTER = POINT;
    MINFILTER = POINT;
    MIPFILTER = NONE;   
};
 
sampler permGradSampler = sampler_state 
{
    texture = <permGradTexture>;
    AddressU  = Wrap;       
    AddressV  = Clamp;
    MAGFILTER = POINT;
    MINFILTER = POINT;
    MIPFILTER = NONE;
};
 
float3 fade(float3 t)
{
    return t * t * t * (t * (t * 6 - 15) + 10); // new curve
}
float4 perm2d(float2 p)
{
    return tex2D(permSampler2d, p);
}
float gradperm(float x, float3 p)
{
    float3 sample = tex1D(permGradSampler, x);
    return dot(sample, p);
}
 
float gradPerm(float x, float3 p)
{
return dot(tex1D(permGradSampler, x), p);
}
float inoise(float3 p)
{
 
  float3 P = fmod(floor(p), 256);
  p -= floor(p);                     
float3 f = fade(p); 
 
P = P /256;
const float one = -11.0 /256;
 
float4 AA = perm2d(P.xy) + P.z;
 
  return lerp(lerp( lerp( gradPerm(AA.x, p ),  
                             gradPerm(AA.z, p + float3(-1, 0, 0) ), f.x),
                       lerp( gradPerm(AA.y, p + float3(0, -1, 0) ),
                             gradPerm(AA.w, p + float3(-1, -1, 0) ), f.x), f.y),
                             
                 lerp( lerp( gradPerm(AA.x+one, p + float3(0, 0, -1) ),
                             gradPerm(AA.z+one, p + float3(-1, 0, -1) ), f.x),
                       lerp( gradPerm(AA.y+one, p + float3(0, -1, -1) ),
                             gradPerm(AA.w+one, p + float3(-1, -1, -1) ), f.x), f.y), f.z);
}
 
float turbulence(float3 p, int octaves, float lacunarity = 2.0, float gain = 1.5)
{
float sum = 0;
float freq = 1.0, amp = 1.0;
for(int i=0; i < octaves; i++) {
sum += abs(inoise(p*freq))*amp;
freq *= lacunarity;
amp *= gain;
}
return sum;
}
 
float ridge(float h, float offset)
{
    h = abs(h);
    h = offset - h;
    h = h * h;
    return h;
}
 
float ridgedmf(float3 p, int octaves, float lacunarity, float gain, float offset = 1.0)
{
    float sum = 0;
    float freq = 1.3f;
    float amp = 0.5;
    float prev = 1.0;
    for(int i=0; i < octaves; i++)
    {
        float n = ridge(inoise(p*freq), offset);
        sum += n*amp*prev;
        prev = n;
        freq *= lacunarity;
        amp *= gain;
    }
    return sum;
}
 
float fBm(float3 p, int octaves, float lacunarity, float gain = 0.7) 
    float freq = 1.3f, 
        amp  = 0.5f; 
    float sum  = 0.6f; 
    for(int i=0; i < octaves; i++) { 
        sum += inoise(p*freq)*amp; 
        freq *= lacunarity; 
        amp *= gain; 
    } 
    return sum; 
 
 
 
float Interpolate(float x, float y, float a)
{
    float val = (1 - cos(a * 3.1415926535f)) * .5;
    return  x * (1 - val) + y * val;
}
 
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
    float3 p = input.wPosition;
    float inz = inoise(p)*0.7+0.7;
 
 
/*inz += ridgedmf(p,8,2.0,0.5);
inz *= turbulence(p,8,2.0,0.5);*/
inz *= fBm(p,8,2.0,0.8);
inz *= Interpolate(0.7,0.7,0.5);
 
 
    return float4(inz,inz,inz,1);
}
 
technique PerlinNoise
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}

 

 


Edited by montify, 25 December 2012 - 11:04 PM.


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS