I'm trying to implement Perlin noise within a rendering engine that have some limitations and I do not know how to generate the gradients for each corners of a given cell.
The main limitation is that I have no random number generator that could return the same result for a given input (meaning no seed available). Another limitation is that I cannot create any lookup table (as proposed here) as the rendering engine does not support arrays.
I understand that I have to create a function that should return a four pseudo random gradients for a given vector index. My first attempt was to use this function that I got here which gives in my pseudo RSL language I'm using:
float randomf (vector vec) {
float t = sin(vec . vector(12.9898, 78.233, 43.47865)) * 43758.5453123;
return t - floor(t);
}
I could call this function twice for each gradient with some variations to make a gradient vector that I could then normalize but I feel there is a better solution that would require less calculation.
I found a solution here but even Inigo Quilez states there's surely a better hash function. Here's the glsl function:
vec2 hash( vec2 x )
{
const vec2 k = vec2( 0.3183099, 0.3678794 );
x = x*k + k.yx;
return -1.0 + 2.0*fract( 16.0 * k*fract( x.x*x.y*(x.x+x.y)) );
}
In general, I would like to find some resources from any field that could give me some guidelines on how to create pseudo random hash functions. From what I understand these functions are usually built after many trials, tweeking parameters one by one until the result is satisfying. Even if I understand what the function should do, I do not know what is a good starting point.
Thank you for your help. Please let my know if I need to clarify anything.
Aucun commentaire:
Enregistrer un commentaire