-
Notifications
You must be signed in to change notification settings - Fork 0
/
velocity.frag
executable file
·94 lines (79 loc) · 2.45 KB
/
velocity.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
precision highp float;
uniform int FRAMEINDEX;
uniform sampler2D velocityTexture;
uniform sampler2D positionTexture;
uniform sampler2D densityTexture;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 mouseButtons;
uniform float time;
#define PI 3.141593
#define SDIST 0.006
#define SENSOR_ROT 20.
#define TURN_ROT 30.
vec2 rot(vec2 st, float t) {
float tt = t / 180. * PI;
float c = cos(tt), s = sin(tt);
return mat2(c, -s, s, c) * st;
}
float atan2(in float y, in float x){
return x == 0.0 ? sign(y) * PI / 2. : atan(y, x);
}
void main(){
if (FRAMEINDEX == 0|| mouseButtons.x > 0.) {
float id = gl_FragCoord.x * 3. + gl_FragCoord.y;
gl_FragColor = vec4(
cos(id),
sin(id),
0,
1
);
// gl_FragColor = vec4(0, 1, 0, 1);
return;
}
vec2 uv = gl_FragCoord.xy / resolution;
vec2 position = texture2D(positionTexture, uv).rg; // (0, 0) to (1, 1)
vec2 velocity = texture2D(velocityTexture, uv).rg;
vec2 newVelocity = velocity;
bool move = mouseButtons.x > 0.;
if (move || true) {
// Get sensor positions
vec2 scpos = position + velocity * SDIST;
vec2 slpos = position + rot(velocity, SENSOR_ROT) * SDIST;
vec2 srpos = position + rot(velocity, -SENSOR_ROT) * SDIST;
// wrap
scpos = fract(scpos);
slpos = fract(slpos);
srpos = fract(srpos);
// どのセンサーが一番濃いか判定する
float sc = texture2D(densityTexture, scpos).r;
float sl = texture2D(densityTexture, slpos).r;
float sr = texture2D(densityTexture, srpos).r;
if (sc > sl && sc > sr) {
// no turn
}
else if (sl > sc && sl > sr) {
// turn left
newVelocity = rot(velocity, TURN_ROT);
}
else if (sr > sc && sr > sl) {
// turn right
newVelocity = rot(velocity, -TURN_ROT);
}
else {
// random
if (sin(time * 3208. + uv.x * 842. + uv.y * 472.) > .0) {
newVelocity = rot(velocity, TURN_ROT);
}
else {
newVelocity = rot(velocity, -TURN_ROT);
}
}
}
// float a = atan2(newVelocity.x, newVelocity.y) / PI * .5 + .5;
// a = fract(a);
// a = floor(a * 6.) / 3. * PI;
// newVelocity = rot(vec2(0, 1), a);
// velocity =
gl_FragColor = vec4(newVelocity, 0, 1);
}