Skip to content

Instantly share code, notes, and snippets.

@epreston
Created April 28, 2023 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save epreston/068ee5e95826efca2d3ef0b01f8942ca to your computer and use it in GitHub Desktop.
Save epreston/068ee5e95826efca2d3ef0b01f8942ca to your computer and use it in GitHub Desktop.
hash2d - javascript version of the cheap 2D hash often used in GLSL shaders
// returns a number with the fractional part of the given number
function fract(num) {
return num - Math.floor(num);
}
// hash2d - not repeatable or stable between platforms
function hash2d(x, y) {
return fract(43758.5453 * Math.sin(x * 78.233 - 12.9898 * y));
}
// hash2d with integer return scaled to a range
function hash2dInt(x, y, low, high) {
return Math.floor(hash2d(x, y) * (high - low + 1)) + low;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment