What's the origin of this GLSL rand() one-liner?

前端 未结 4 811
忘了有多久
忘了有多久 2020-12-22 19:17

I\'ve seen this pseudo-random number generator for use in shaders referred to here and there around the web:

float rand(vec2 co){
  return fract(sin(dot(co.x         


        
4条回答
  •  失恋的感觉
    2020-12-22 19:48

    the constant values are arbitrary, especially that they are very large, and a couple of decimals away from prime numbers.

    a modulus over 1 of a hi amplitude sinus multiplied by 4000 is a periodic function. it's like a window blind or a corrugated metal made very small because it's multiplied by 4000, and turned at an angle by the dot product.

    as the function is 2-D, the dot product has the effect of turning the periodic function at an oblique relative to X and Y axis. By 13/79 ratio approximately. It is inefficient, you can actually achieve the same by doing sinus of (13x + 79y) this will also achieve the same thing I think with less maths..

    If you find the period of the function in both X and Y, you can sample it so that it will look like a simple sine wave again.

    Here is a picture of it zoomed in graph

    I don't know the origin but it is similar to many others, if you used it in graphics at regular intervals it would tend to produce moire patterns and you could see it's eventually goes around again.

提交回复
热议问题