3 ms·
Meanwhile in graphics programming land: float rand(vec2 co){ return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453); }
by tiborsaas 6y ago
Meanwhile in graphics programming land:
float rand(vec2 co){
return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453);
}
- LolWolf 6y agoHonestly, the one thing that got me into graphics (from physics and math) was just the incredible amount of: "you can literally do anything so long as you make it pretty in the end." I took that as a life philosophy and it's been pretty great so far.
- bobbylarrybobby 6y agoI assume you’ve seen John Carmack’s square root hack? What a beaut
- makeworld 6y agoCould 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.
- jl2718 6y agoIs this actually faster than seeding the PRNG with co.xy? Or xxhash({co.xy,time})?