Created
May 10, 2021 07:34
-
-
Save CynicatPro/40f1a3ddb3997b178876800e666c5a02 to your computer and use it in GitHub Desktop.
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
// source: Math for Game Programmers: Noise-Based RNG | |
// https://www.youtube.com/watch?v=LWFzPP8ZbdU | |
public static class CynNoise { | |
const uint BIT_NOISE0 = 0xB5297A4D; | |
const uint BIT_NOISE1 = 0x68E31DA4; | |
const uint BIT_NOISE2 = 0x1B56C4E9; | |
const uint PRIME0 = 3175861097; | |
const uint PRIME1 = 198491317; | |
const uint PRIME2 = 6542989; | |
public static uint Noise(uint position, uint seed = 0) { | |
uint mangled = position; | |
mangled *= BIT_NOISE0; | |
mangled += seed; | |
mangled ^= mangled >> 8; | |
mangled += BIT_NOISE1; | |
mangled ^= mangled << 8; | |
mangled *= BIT_NOISE2; | |
mangled ^= mangled >> 8; | |
return mangled; | |
} | |
public static uint Noise2D(uint positionX, uint positionY, uint seed = 0) { | |
return Noise(positionX + PRIME0 * positionY, seed); | |
} | |
public static uint Noise3D(uint positionX, uint positionY, uint positionZ, uint seed = 0) { | |
return Noise(positionX + PRIME0 * positionY + PRIME1 * positionZ, seed); | |
} | |
public static uint Noise4D(uint positionX, uint positionY, uint positionZ, uint positionW, uint seed = 0) { | |
return Noise(positionX + PRIME0 * positionY + PRIME1 * positionZ + PRIME2 * positionW, seed); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment