14
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GLSLでよく使う自作関数、マクロ

Last updated at Posted at 2022-04-22

GLSLを書くときによく自作する関数やマクロのメモ。随時更新予定。

PI, TAU

PIが円周率、TAUが円周率の2倍。

#define PI 3.14159265359
#define TAU 6.28318530718

palette

三角関数を使っていい感じのカラーパレットを作成できる関数。

vec3 palette(float t, vec3 offset, vec3 amplitude, vec3 frequency, vec3 phase) {
    return offset + amplitude * cos(6.28318530718 * (t * frequency + phase));
}

マクロ関数版。

#define palette(t,offset,amplitude,frequency,phase) ((offset)+(amplitude)*cos(6.28318530718*((t)*(frequency)+(phase))))

使い方(マクロ関数版)。

vec3 c = palette(t, 0.5, 0.5, 1.0, vec3(0.0, 0.1, 0.2));

palette.png

詳しくは以下の記事を参照。

remap

ある範囲の値を別の範囲に変換する関数。[in0, in1]の範囲のt[out0, out1]の範囲に変換する。

float remap(float x, float in0, float in1, float out0, float out1) {
    return out0 + (out1 - out0) * (x - in0) / (in1 - in0);
}

マクロ関数版。

#define remap(x,in0,in1,out0,out1) ((out0)+((out1)-(out0))*((x)-(in0))/((in1)-(in0)))

linearstep

smoothstepっぽい感じで2つの閾値の間を線形に補間する関数。

float linearstep(float edge0, float edge1, float x) {
    return clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
}

マクロ関数版。

#define linearstep(edge0,edge1,x) (min(max(((x)-(edge0))/((edge1)-(edge0)),0.0),1.0))

使い方。

float v = linearstep(0.3, 0.7, x);

linearstep.png

詳しくは前に書いた記事を参照。

bumpstep

step関数で凸状に二値化する関数。

float bumpstep(float edge0, float edge1, float x) {
    return step(edge0, x) * (1.0 - step(edge1, x));
}

マクロ関数版。

#define bumpstep(edge0,edge1,x) (step((edge0),(x))*(1.0-step((edge1),(x))))

使い方。

float v = bumpstep(0.3, 0.7, x);

bumpstep.png

bumpsmoothstep

bumpstepsmoothstep版。

float bumpsmoothstep(float edge0, float edge1, float edge2, float edge3, float x) {
    return smoothstep(edge0, edge1, x) * (1.0 - smoothstep(edge2, edge3, x));
}

マクロ関数版。

#define bumpsmoothstep(edge0,edge1,edge2,edge3,x) (smoothstep((edge0),(edge1),(x))*(1.0-smoothstep((edge2),(edge3),(x))))

使い方。

float v = bumpsmoothstep(0.2, 0.4, 0.6, 0.8, x);

bumpsmoothstep.png

decay

x-==0のとき1、xが大きくなるほど0に近い値を返し、いい感じの減衰を作れる関数。基本的には、x>=0の前提で使う。実際に使うときはわざわざ関数を定義しないけど。

float decay(float x) {
    return exp(-x);
}

decay.png

rotate

回転行列を生成する関数。

mat2 rotate(float r) {
    float c = cos(r);
    float s = sin(r);
    return mat2(c, s, -s, c);
}

blendScreen

Screen合成をする関数。

vec3 blendScreen(vec3 a, vec3 b) {
    return 1.0 - (1.0 - a) * (1.0 - b);
}

マクロ関数版。

#define blendScreen(a,b) (1.0-(1.0-(a))*(1.0-(b)))

random

乱数を返す関数。

float random(vec2 x){
    return fract(sin(dot(x ,vec2(12.9898, 78.233))) * 43758.5453);
}
14
16
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
14
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?