worldHash function

int worldHash(
  1. int seed,
  2. int x,
  3. int y,
  4. int z,
)

A 32-bit hash of a world position under seed: the same cell gives the same number on every chunk, isolate and run, which is what lets two chunks agree on a tree or a structure that crosses their border. Features salt y (or scale x and z by primes) to draw independent rolls from one position.

Implementation

int worldHash(int seed, int x, int y, int z) {
  var h = _u32(x * 73856093) ^ _u32(y * 19349663) ^ _u32(z * 83492791) ^ _u32(_u32(seed) * 2654435761);
  h = _u32(h);
  h ^= h >> 13;
  h = _u32(h * 0x5bd1e995);
  h ^= h >> 15;
  return h;
}