4

More than 3 years have passed since last update.

posted at

# 概要

glslでフィボナッチ数列を作る

フィボナッチ数列のアルゴリズムは山ほどありますが、
https://qiita.com/rana_kualu/items/49a60a4076e63ac6e399

``````const float SQRT_5 = sqrt(5.);
const float PHY    = (1.+SQRT_5)*.5;

float fib(float n){return sign(n)*floor(pow(PHY,abs(n))/SQRT_5+.5);}
``````

※ wikipediaの負数対応は微妙だったので、最適化してます。

# フィボナッチ数列で円形グラデーション

``````precision mediump float;
uniform vec2  m;       // mouse
uniform float t;       // time
uniform vec2  r;       // resolution
uniform sampler2D smp; // prev scene

const float SQRT_5 = sqrt(5.);
const float PHY    = (1.+SQRT_5)*.5;

float fib(float n){return sign(n)*floor(pow(PHY,abs(n))/SQRT_5+.5);}

void main(void){
vec2 p = (gl_FragCoord.xy * 2.0 - r) / min(r.x, r.y);
float c = fib(length(p)*15.)/600.;
gl_FragColor = vec4(vec3(c), 1.0);
}
``````

グロウしてもいいですね

``````precision mediump float;
uniform vec2  m;       // mouse
uniform float t;       // time
uniform vec2  r;       // resolution
uniform sampler2D smp; // prev scene

const float SQRT_5 = sqrt(5.);
const float PHY    = (1.+SQRT_5)*.5;

float fib(float n){return sign(n)*floor(pow(PHY,abs(n))/SQRT_5+.5);}

void main(void){
vec2 p = (gl_FragCoord.xy * 2.0 - r) / min(r.x, r.y);
float c = fib(length(p)*15.)/600.;
gl_FragColor = vec4(vec3(.1/c), 1.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
What you can do with signing up
4