Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good algo for glsl lowp random number generation (for use in graphics)?

Tags:

random

glsl

I need a random number generator to create some graphical static. I'm not looking for noise algorithms- I just want white noise. All I need for this is a random number generator in glsl. Specifically, I'll be using it to create a random lightness offset per-fragment, over time.

Requirements:

  • generates number between 0.0 and 1.0 (don't care if inclusive/exclusive)
  • This needn't be that random. This won't be used for any security purposes, it just needs to be "random" to the naked eye.
  • It needs to be computable with lowp floats only! (for use on mobile)
  • Only has fragment_x, fragment_y, and time_mod_ten_pi as inputs (time_mod_ten_pi is exactly what it sounds like; the time (in seconds) since the game began, mod (10*3.1415) passed in as a float to allow for easy, continuous oscillations without worrying about precision issues. and 30 seconds is waaay more than enough time that a human won't notice repeating noise)
  • when displayed on a fragment_x * fragment_y grid, I don't want any visible patterns (statically, or in motion)
  • the simpler/faster, the better!

want to reiterate here- needs to function with only lowp floats. that one-liner going around the internet (fract(sin(dot(...)))) does not fulfill this condition! (at least, I assume the issue is with the lowp floats... could be really feeble sin implementation as well... so if you can avoid sin of high numbers too? bonus?)

like image 459
Phildo Avatar asked Sep 14 '25 00:09

Phildo


1 Answers

This is the best I've been able to come up with, but I'm still not 100% satisfied (you can still see a small kaleidoscope-like effect if you look hard enough...

float rand(float frag_x, float frag_y, float t)
{
  float r = t*7.924;
  return fract(sin(frag_y+frag_x)*r+sin(frag_y-frag_x)*r*.7);
}

(the 7.924 and the .7 is me mashing on the keyboard- nothing more)

like image 186
Phildo Avatar answered Sep 15 '25 23:09

Phildo