LoginSignup
69
54

More than 5 years have passed since last update.

ランダムな値を返す関数 on GLSL

Posted at

実装

GLSL には noise[1234] が定義されているが、実装が GPU のドライバに強く依存するのでそれに依存しない擬似ランダム関数を GLSL だけで作れないかという話。http://stackoverflow.com/questions/4200224/random-noise-functions-for-glsl からそのまま引用。

float rand(vec2 co){
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

引数 co にはフラグメントシェーダの場合は gl_FragCoord.xy を渡す。引数は明らかに読み取り専用なので const vec2 co としたほうがよいかもしれない。使う場面として SSAO とか。

どこから出てきたの?

ちなみにこの関数についてどこがオリジナルなのかの質問もある。どうやらそのサイトはすでに消滅しており、2008年2月にアーカイブされてたものがおそらくのオリジナルだろうということのようだ。
http://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner

Mali-400 上だと動かない模様

ARM の GPU である Mali-400 ではこの関数をそのまま使うことが出来ないようで対策版が作られている模様。
https://gist.github.com/johansten/3633917

69
54
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
69
54