Created
April 23, 2021 19:03
-
-
Save manthrax/5a50556b77795742cea38db3d532ec98 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// **** This is for testing each number in the 32 bit uint random state individually. **** | |
//#define BIT_BY_BIT_DEBUG | |
#define saturate(a) clamp(a, 0.0, 1.0) | |
// ---- Random functions use one 32 bit state var to change things up ---- | |
// This is the single state variable for the random number generator. | |
uint randomState = 4056649889u; | |
// 0xffffff is biggest 2^n-1 that 32 bit float does exactly. | |
// Check with Math.fround(0xffffff) in javascript. | |
const float invMax24Bit = 1.0 / float(0xffffff); | |
// This is the main hash function that should produce a non-repeating | |
// pseudo-random sequence for 2^31 iterations. | |
uint SmallHashA(in uint seed) { | |
return (seed ^ 1057926937u) * 3812423987u ^ | |
((seed*seed) * 4000000007u); | |
} | |
// This is an extra hash function to clean things up a little. | |
uint SmallHashB(in uint seed) { | |
return (seed ^ 2156034509u) * 3699529241u; | |
} | |
// Hash the random state to get a random float ranged [0..1] | |
float RandFloat() { | |
randomState = SmallHashA(randomState); | |
// Add these 2 lines for extra randomness. And change last line to tempState. | |
//uint tempState = (randomState << 13) | (randomState >> 19); | |
//tempState = SmallHashB(tempState); | |
return float((randomState>>8) & 0xffffffu) * invMax24Bit; | |
} | |
// Hash the random state to get 2 random floats ranged [0..1] | |
// Reduced precision to 16 bits per component. | |
vec2 RandVec2() { | |
randomState = SmallHashA(randomState); | |
uint tempState = (randomState << 13) | (randomState >> 19); | |
tempState = SmallHashB(tempState); | |
return vec2(tempState & 0xffffu, | |
(tempState >> 16) & 0xffffu) / float(0xffff); | |
} | |
// Hash the random state to get 3 random floats ranged [0..1] | |
// Reduced precision to 10 bits per component. | |
vec3 RandVec3() { | |
randomState = SmallHashA(randomState); | |
uint tempState = (randomState << 13) | (randomState >> 19); | |
tempState = SmallHashB(tempState); | |
return vec3((tempState >> 2) & 0x3ffu, | |
(tempState >> 12) & 0x3ffu, | |
(tempState >> 22) & 0x3ffu) / float(0x3ffu); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment