Reputation: 371
I have seen many noise implementations for things like perlin
and simplex noise
to achieve procedural terrain generation
, and they all seem to use 256 permutation values. Why is this? and what would be the effect of using more than 256?
Upvotes: 0
Views: 335
Reputation: 683
You could use any number of permutation values. The reason powers of two are preferred is, because it is cheaper to compute the modulus of N^2.
The reason for that is:
value % (N^2)
is equivalent to
value & (N^2 - 1)
And it is much cheaper to compute a bitwise & instead of a %.
For example in your code you could write this:
int v = perm[(x + perm[y % 256]) % 256];
Or
int v = perm[(x + perm[y & 255]) & 255];
Both give you the same result but the second method is faster.
Of course any power of two can be used for this. I think the reason for choosing 256 is that it is a good balance between variety of pseudo random numbers and low memory consumption.
Upvotes: 2