4 ms·
Could someone explain this? How is it used, why does it take a vector as input, where do the magic numbers come from, etc.
by makeworld 6y ago
Could someone explain this? How is it used, why does it take a vector as input, where do the magic numbers come from, etc.
- tiborsaas 6y agoIt's written in GLSL which is a C like language for shaders designed to be executed on the GPU. The 2D vector used for input is to represent pixel coordinates mapped from 0 to 1 or -1 to 1 on both axes. The magic numbers are nothing special, they are just large numbers to make the result unpredictable for the given input. The top level function is a fract which takes the fractional part of a number. So if the result of the inner computation is twisted enough it will be hard to trace it back to the original values. There are lots of variations for these one liners, most of the do a great job to produce noise.
- hyperman1 6y agorandom typically works by storing/modifying some state, so every call with the same argument results in different numbers. In shaders, the same code is executed in parallel for potentially every pixel. Storing state would mean pixels could only be calculated serially, slowing things down. Hence you need a random-ish function that depends only on its input. Very low RNG quality is not a blocker, as long as things look good. So in shaders you see a lot of random generators which simply take the pixel coordinate or something else that distinguishes 2 pixels and do some nonsense operations on them.