Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Created April 10, 2026 13:23
Show Gist options
  • Select an option

  • Save Xenakios/e39a4eace18140f8bc9813d116d96113 to your computer and use it in GitHub Desktop.

Select an option

Save Xenakios/e39a4eace18140f8bc9813d116d96113 to your computer and use it in GitHub Desktop.
// Public domain. No guarantees.
// Noise that tends to produce values as far as possible from the current value
// especially with higher depths
class BlueNoise
{
public:
BlueNoise(unsigned int seed = 0) : m_rng{seed, 1} { m_previous = m_rng.nextFloat(); }
float operator()() noexcept
{
float maxdist = 0.0f;
float z0 = 0.0f;
for (int i = 0; i < m_depth; ++i)
{
// nextFloat should produce a uniform random number in range 0.0 - 1.0
float z1 = m_rng.nextFloat();
float dist = std::abs(z1 - m_previous);
if (dist > maxdist)
{
maxdist = dist;
z0 = z1;
}
}
m_previous = z0;
return m_previous;
}
// too low depth isn't that different from regular random numbers
// too high depth starts pretty much oscillating between very high and very low numbers
void setDepth(int d) noexcept { m_depth = std::clamp(d, 1, 32); }
int getDepth() const noexcept { return m_depth; }
private:
xenakios::Xoroshiro128Plus m_rng;
float m_previous = 0.0f;
int m_depth = 4;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment