Advertisement Jump to content
Sign in to follow this  
MrGao

How to use the perlin noise in FX composer

This topic is 1730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone?

I try to apply the perlin noise  on a sphere in FX composer But unfortunately?The output value is always 0. the two texture produced correctly. so I think the inoise() which comes from GPU Gems2 CD is no problem.I think the only thing that might be wrong is the parameter I gives to inoise() has a problem.Could someone tell me where my code was wrong?

 

// permutation table
static int permutation[] = { 151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
};
 
// gradients for 3d noise
static float3 g[] = {
    1,1,0,
    -1,1,0,
    1,-1,0,
    -1,-1,0,
    1,0,1,
    -1,0,1,
    1,0,-1,
    -1,0,-1, 
    0,1,1,
    0,-1,1,
    0,1,-1,
    0,-1,-1,
    1,1,0,
    0,-1,1,
    -1,1,0,
    0,-1,-1
};
 
texture permTexture2d
<
    string texturetype = "2D";
    string format = "a8r8g8b8";
string function = "GeneratePermTexture2d";
int width = 256, height = 256;
>;
 
texture permGradTexture
<
    string texturetype = "2D";
string format = "q8w8v8u8";
string function = "GeneratePermGradTexture";
int width = 256, height = 1;
>;
 
// 2d permutation texture for optimized version
int perm(int i)
{
return permutation[i % 256];
}
 
float4 GeneratePermTexture2d(float2 p : POSITION) : COLOR
{
p *= 256;
int A = perm(p.x) + p.y;
int AA = perm(A);
int AB = perm(A + 1);
  int B =  perm(p.x + 1) + p.y;
  int BA = perm(B);
  int BB = perm(B + 1);
return float4(AA, AB, BA, BB) / 255.0;
}
 
// permuted gradient texture for optimized version
float3 GeneratePermGradTexture(float p : POSITION) : COLOR
{
return g[ permutation[p*256] % 16 ];
}
 
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
// return t * t * (3 - 2 * t); // old curve
}
 
float4 perm2d(float2 p)
{
return tex2D(permSampler2d, p);
}
 
float gradperm(float x, float3 p)
{
return dot(tex1D(permGradSampler, x), p);
}
 
// optimized version
float inoise(float3 p)
{
float3 P = fmod(floor(p), 256.0); // FIND UNIT CUBE THAT CONTAINS POINT
  p -= floor(p);                      // FIND RELATIVE X,Y,Z OF POINT IN CUBE.
float3 f = fade(p);                 // COMPUTE FADE CURVES FOR EACH OF X,Y,Z.
 
P = P / 256.0;
const float one = 1.0 / 256.0;
 
    // HASH COORDINATES OF THE 8 CUBE CORNERS
float4 AA = perm2d(P.xy) + P.z;
 
// AND ADD BLENDED RESULTS FROM 8 CORNERS OF CUBE
  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);
}
 
float4x4 WorldViewProj : WorldViewProjection< string UIWidget = "None"; >;;
float4x4 World : World< string UIWidget = "None"; >;;
 
struct outInfor
{
float4 HPosition:POSITION;
float4 WPosition:TEXCOORD0;
};
 
outInfor mainVS(float3 pos : POSITION) 
{
outInfor OUT;
OUT.HPosition = mul(float4(pos.xyz, 1.0), WorldViewProj);
OUT.WPosition = mul(float4(pos.xyz, 1.0), World);
return OUT;
}
 
float4 mainPS(outInfor IN) : COLOR {
return inoise(IN.WPosition.xyz);
}
 
technique technique0
{
pass p0
{
CullMode = None;
VertexShader = compile vs_3_0 mainVS();
PixelShader = compile ps_3_0 mainPS();
}
}

 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!