Question

I am making a 3d perlin noise generator. It is important that the coordinate is the seed so that it gives me the same random value every time (the noise is never stored). This is my function for generating random values based on a seed which is the coordinate:

__forceinline float getRandomField(const vec3& Position) const
{
    uint32_t seed = uint32_t(Position.x);
    seed <<= 8;
    seed ^= 0xE56FAA12;
    seed |= uint32_t(Position.y);
    seed <<= 8;
    seed ^= 0x69628a2d;
    seed |= uint32_t(Position.z);
    seed <<= 8;
    seed ^= 0xa7b2c49a;

    srand(seed);

    return (float(int(rand()%2001) -1000)/1000.0f);
}

Result (a slice in the x,y plane):

Perlin noise with undesired pattern

Weirdly enough, this gives me a pattern. Obviously, I have no idea what I'm doing. I though that apply some weird xor values and bit shifting into a random seed would give me a random number. Apparently I was wrong.

What is the best way to create a random value from a coordinate without it resulting in a pattern?

Était-ce utile?

La solution

Just to give a answer to other people who might have the same problem. I found one that produced a pretty good result. I didn't even need the rand() function.

__forceinline float getRandomField(const vec3& Position) const
{
    uint32_t seed = uint32_t(Position.x)*1087;
    seed ^= 0xE56FAA12;
    seed += uint32_t(Position.y)*2749;
    seed ^= 0x69628a2d;
    seed += uint32_t(Position.z)*3433;
    seed ^= 0xa7b2c49a;

    return (float(int(seed%2001) -1000)/1000.0f);
}

The key is to multiply each axis with a prime number. I found these on wikipedia: 1087,2749,3433. The hex values xor'ed with is just randomly hammered down by me. They are not primes, as far as I know. Check out the result:

Not bad huh?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top