inline float Rand1(float3 seed)
{
float combinedSeed = dot(seed, NonAlignmentMuls.xyz);
float remainder = fmod(combinedSeed, PI);
float rand = frac(sin(remainder)*BigNonAlignmentMul);
combinedSeed = dot(seed, rand.xxx);
remainder = fmod(combinedSeed, PI);
rand = frac(sin(remainder)*BigNonAlignmentMul);
newSeed.xyz = rand.xxx;
return rand;
}
Okay so here’s the function I’ve got so far. It’s just based on “that canonical one-liner I found on the Internet.”
float rand(vec2 co)
{
return fract(sin(dot(co.xy ,vec2(12.9898,78.233)))*43758.5453);
}
The so-called NonAlignmentMuls and BigNonAlignmentMul are just constants I’ve defined (though wasn’t entirely sure what to call them).
BigNonAlignmentMul = 43758.5453
NonAlignmentMuls = float4(12.9898, 46.4321, 78.233, 138.8765)
The purpose of the fmod in there is just to make sure it functions correctly on certain GPUs (in phones) that take shortcuts with their sin functions. I saw someone recommend doing it this way when I was researching how the original function worked. I admit it’s probably overkill in and of itself.
newSeed is just a static global I’m writing to so I can use it as the seed if I need to generate another random number.
The real thing I’m really uncertain of is the fact that I’m basically generating one random number to generate a 2nd random number, but… it gives good results.
When I don’t include those extra lines of code (8-10) I get a lot of artifacts showing up at certain angles (especially when viewing an object close-up, more prominently on objects that have a lot of curvature). I think I’m basically “doubling up” on the randomness, but I can’t help but feel there is probably a much simpler way of achieving the same results. However, I’ve been tinkering with it but no matter what I try I can’t find another way to get the same results.
Here’s a picture of what it looks like without lines 8-10 (in an extreme close up of a sphere):
Here is the exact same area of the sphere but with the extra lines included:



