hashedAlphaNoise function

double hashedAlphaNoise(
  1. double x,
  2. double y,
  3. double z,
  4. double cutoff,
)

HashedAlphaNoise in surface.glsl and depth_predraw.frag, operation for operation: the noise a hashed alpha is compared with at the scene-space point (x, y, z), counted in sixteenths of a metre from where the world starts. cutoff is the hashed sentinel, -2 less the scene's origin in those cells, each below 128, which the renderer packs.

The cells are wrapped to [0, 128) before the hash sees them, so every number it takes is small; the hash is Hoskins' hash13, with no sin.

Implementation

double hashedAlphaNoise(double x, double y, double z, double cutoff) {
  double fract(double v) => v - v.floorToDouble();
  double wrap(double v) => v - 128.0 * (v / 128.0).floorToDouble();
  final key = -2.0 - cutoff;
  final cx = wrap((x * 16.0).floorToDouble() + (key / 16384.0).floorToDouble());
  final cy = wrap(
    (y * 16.0).floorToDouble() + wrap((key / 128.0).floorToDouble()),
  );
  final cz = wrap((z * 16.0).floorToDouble() + wrap(key));
  var px = fract(cx * 0.1031);
  var py = fract(cy * 0.1031);
  var pz = fract(cz * 0.1031);
  // `p3 += dot(p3, p3.zyx + 31.32)`.
  final d = px * (pz + 31.32) + py * (py + 31.32) + pz * (px + 31.32);
  px += d;
  py += d;
  pz += d;
  return fract((px + py) * pz);
}