Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there noise functions in GLSL OpenGL ES 2.0 (iOS)?

Or any counterpart? How can I generate a cheap random number?

like image 746
Geri Borbás Avatar asked May 07 '12 09:05

Geri Borbás


2 Answers

GLSL ES doesn't come with noise functions, and the desktop GLSL noise functions are almost never implemented.

However, there are some freeware noise functions available. They're supposed to be pretty decent and fast. I've never used them myself, but they should work. It's MIT-licensed code, if you're worried about that.

like image 93
Nicol Bolas Avatar answered Nov 01 '22 14:11

Nicol Bolas


Define "cheap".

The way random numbers work in computers is, they're not really random. You start with a number (the seed), and for each random number you want you do some fancy looking calculations on that number to get another number which looks random, and you use that number as your random number and the seed for the next random number. See here for the gory details.

Problem is, that procedure is inherently sequential, which is no good for shaders.

You could theoretically write a function in a fragment shader that makes some hash out of, say, the fragment position and potentially some uniform int that is incremented every frame, but that is an awful lot of work for a fragment shader, just to produce something that looks like noise.

The conventional technique for producing noise effects in OpenGL is to create a noisy texture and have the shader(s) use it in various ways. You could simply apply the texture as a standard texture to your surface, or you could stretch it or clamp its color values. For time-varying effects you might want to use a 3D texture, or have a larger 2D texture and pass a random texture coordinate offset to the fragment shader stage each frame via a uniform.

Also have a look at perlin noise, which essentially uses a variation of the effect described above.

like image 21
Michael Slade Avatar answered Nov 01 '22 15:11

Michael Slade