这种GLSL rand()单衬板的起源是什么? [英] What's the origin of this GLSL rand() one-liner?

查看:137
本文介绍了这种GLSL rand()单衬板的起源是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到此伪随机数生成器用于着色器,该着色器称为

此功能的起源是什么?常数值看起来像是任意的吗,还是选择某些艺术?是否有关于此功能优点的讨论?

我遇到的对此功能的最早参考是解决方案

一个非常有趣的问题!

我试图在输入答案时弄清楚这一点:) 首先是一种简单的玩法:

函数要做的第一件事是将2d变为1d.这本身并不有趣,但是选择了数字以使它们通常不会重复.另外,我们在那里还有一个浮点数. y或x还会有更多的位,但是可能只是正确选择了数字,所以进行了混合.

然后我们对一个黑盒sin()函数进行采样.这将在很大程度上取决于实现!

最后,它通过乘并取小数来放大sin()实现中的错误.

在一般情况下,我认为这不是一个很好的哈希函数. sin()在GPU上是一个数字上的黑匣子.通过采用几乎所有哈希函数并将其转换,应该可以构建出更好的代码.困难的部分是将cpu哈希中使用的典型整数运算转换为float(半位或32bit)或定点运算,但这应该是可能的.

同样,将其作为哈希函数的真正问题是sin()是一个黑匣子.

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.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

It's variously called "canonical", or "a one-liner I found on the web somewhere".

What's the origin of this function? Are the constant values as arbitrary as they seem or is there some art to their selection? Is there any discussion of the merits of this function?

EDIT: The oldest reference to this function that I've come across is this archive from Feb '08, the original page now being gone from the web. But there's no more discussion of it there than anywhere else.

解决方案

Very interesting question!

I am trying to figure this out while typing the answer :) First an easy way to play with it: http://www.wolframalpha.com/input/?i=plot%28+mod%28+sin%28x*12.9898+%2B+y*78.233%29+*+43758.5453%2C1%29x%3D0..2%2C+y%3D0..2%29

Then let's think about what we are trying to do here: For two input coordinates x,y we return a "random number". Now this is not a random number though. It's the same every time we input the same x,y. It's a hash function!

The first thing the function does is to go from 2d to 1d. That is not interesting in itself, but the numbers are chosen so they do not repeat typically. Also we have a floating point addition there. There will be a few more bits from y or x, but the numbers might just be chosen right so it does a mix.

Then we sample a black box sin() function. This will depend a lot on the implementation!

Lastly it amplifies the error in the sin() implementation by multiplying and taking the fraction.

I don't think this is a good hash function in the general case. The sin() is a black box, on the GPU, numerically. It should be possible to construct a much better one by taking almost any hash function and converting it. The hard part is to turn the typical integer operation used in cpu hashing into float (half or 32bit) or fixed point operations, but it should be possible.

Again, the real problem with this as a hash function is that sin() is a black box.

这篇关于这种GLSL rand()单衬板的起源是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆