Created
April 28, 2023 21:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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