Sine–Fract Hash (canonical GLSL one-liner)
別名: canonical one-liner GLSL rand、fract-sin hash、one-liner hash
No first-appearance record exists: the one-liner has no known author or original publication and was already widely diffused across the web by 2013 (to be confirmed — likely earlier).
作者不詳 名称 — 真に作者不明。この関数はコピペで流通し、名前や作者ではなく魔法定数(12.9898, 78.233, 43758.5453)で同定される。2013年に実務のグラフィクス技術者が調べても、 ネット上の「何十もの場所」に現れるにもかかわらず最初に書いた人物を特定できなかった (references 参照)。公刊や引用ではなくコピペ拡散でフォークロア的正典の地位を得た。 ここで用いる記述的ハンドル「Sine–Fract Hash」は reference トラックのレコードに付す 索引用ラベルであって、確立した名前や既知の作者がある、という主張ではない(二トラック 原則 SPEC §7.4: 検証可能な手続きは記録し、作者は不明のまま開いておく)。
ひとことで シェーダコードにおける事実上の擬似乱数ハッシュ。2次元座標を fract(sin(dot(p,k))*m) で [0,1) の値に写す1行の関数。テクスチャも状態も不要で安価なため、フラグメントシェーダ内の 乱数源(ディザ・value/gradient ノイズ・点描・ジッタ)として多用される。実務では名前や 作者ではなく「魔法定数」で同定される。
コード
上のデモの実際のスケッチ(サンドボックスで再現可能)。これで全部です。
sine-fract-hash/index.html
// The de-facto GLSL random hash, shown as noise. Left half: the raw per-pixel
// hash (uniform static). Right half: value noise built by bilinearly
// interpolating that same hash on an integer lattice. Both use the *canonical
// constants* verbatim — the technique's real identifier. Seeded per load.
const SEED = Math.random();
const c = document.getElementById('c');
const dpr = Math.min(devicePixelRatio || 1, 2);
const W = Math.min(innerWidth, innerHeight * 16 / 9);
c.width = Math.floor(W * dpr);
c.height = Math.floor(W * 9 / 16 * dpr);
const gl = c.getContext('webgl');
if (!gl) { c.outerHTML = '<p style="color:#fff;font-family:system-ui">WebGL unavailable</p>'; }
const vs = `attribute vec2 a; void main(){ gl_Position = vec4(a, 0., 1.); }`;
// highp + the canonical one-liner. Note (see the record's how_it_works): this
// leans on the hardware sin(); on some mobile GPUs (e.g. Adreno 320) sin() is
// imprecise for large arguments and the noise degrades — highp mitigates it.
const fs = `precision highp float;
uniform vec2 u_res; uniform float u_seed;
// canonical GLSL rand — magic constants verbatim
float rand(vec2 co){ return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453); }
float valueNoise(vec2 p){
vec2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f); // smoothstep fade
float a = rand(i);
float b = rand(i + vec2(1.0, 0.0));
float d = rand(i + vec2(0.0, 1.0));
float e = rand(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(d, e, f.x), f.y);
}
void main(){
vec2 uv = gl_FragCoord.xy / u_res; // 0..1
vec2 s = vec2(u_seed * 91.7, u_seed * 47.3);
float v;
if (uv.x < 0.5) {
// raw hash: one value per pixel -> TV-static
v = rand(floor(gl_FragCoord.xy) + s);
} else {
// value noise: the same hash, bilinearly smoothed on a lattice
v = valueNoise(gl_FragCoord.xy / 22.0 + s);
}
// a thin divider down the middle
float div = smoothstep(0.0015, 0.0, abs(uv.x - 0.5));
vec3 col = mix(vec3(v), vec3(0.15, 0.55, 0.95), div);
gl_FragColor = vec4(col, 1.0);
}`;
function sh(t, src){ const o = gl.createShader(t); gl.shaderSource(o, src); gl.compileShader(o);
if(!gl.getShaderParameter(o, gl.COMPILE_STATUS)) console.error(gl.getShaderInfoLog(o)); return o; }
const prog = gl.createProgram();
gl.attachShader(prog, sh(gl.VERTEX_SHADER, vs));
gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, fs));
gl.linkProgram(prog); gl.useProgram(prog);
const buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 1,-1, -1,1, -1,1, 1,-1, 1,1]), gl.STATIC_DRAW);
const loc = gl.getAttribLocation(prog, 'a');
gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
gl.uniform2f(gl.getUniformLocation(prog, 'u_res'), c.width, c.height);
gl.uniform1f(gl.getUniformLocation(prog, 'u_seed'), SEED);
gl.viewport(0, 0, c.width, c.height);
gl.drawArrays(gl.TRIANGLES, 0, 6);仕組み
正準形は float rand(vec2 co){ return fract(sin(dot(co.xy, vec2(12.9898,78.233))) * 43758.5453); }。 手順は、入力座標と固定ベクトル(12.9898, 78.233)の内積で2次元を1次元に潰し、sin() で [-1,1] に高周波で折り畳み、大きな定数(43758.5453)を掛けて微小な差を増幅し、fract() で カオス的な小数部だけを残して [0,1) の擬似乱数を得る。「魔法定数」が事実上の同定子。 原理的なハッシュではなくハードウェアの sin() 実装に依存するため GPU 間で結果が異なり、 大きな引数で sin() が不正確な環境(一部モバイル GPU)では破綻する。そのため highp 指定や sin 引数の事前縮約が加えられる。
パラメータ
coord: array — The 2D input coordinate hashed to a scalar (often the fragment's uv or pixel position).magicConstants: array — The fixed vector and multiplier (canonically 12.9898, 78.233, 43758.5453) that define this particular hash — its de-facto identifier.
系譜
このアルゴリズムを使う作品 (0)
参考文献
最終更新 2026-07-16