-
Notifications
You must be signed in to change notification settings - Fork 183
/
buffers.html
141 lines (121 loc) · 3.71 KB
/
buffers.html
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>GlslCanvas</title>
<script type="text/javascript" src="dist/GlslCanvas.js"></script>
<style>
body {
background: #101515;
}
#glslCanvas {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<canvas id="glslCanvas" data-fragment="
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
uniform sampler2D u_tex0; // data/moon.jpg
uniform vec2 u_tex0Resolution;
uniform sampler2D u_buffer0;
uniform sampler2D u_buffer1;
vec2 scale(vec2 st, float s) {
return (st-.5)*s+.5;
}
vec2 ratio(in vec2 st, in vec2 s) {
return mix( vec2((st.x*s.x/s.y)-(s.x*.5-s.y*.5)/s.y,st.y),
vec2(st.x,st.y*(s.y/s.x)-(s.y*.5-s.x*.5)/s.x),
step(s.x,s.y));
}
float circleSDF(vec2 st) {
return length(st - 0.5) * 2.0;
}
vec2 sphereCoords(vec2 _st, float _scale){
float maxFactor = sin(1.570796327);
vec2 uv = vec2(0.0);
vec2 xy = 2.0 * _st.xy - 1.0;
float d = length(xy);
if (d < (2.0-maxFactor)){
d = length(xy * maxFactor);
float z = sqrt(1.0 - d * d);
float r = atan(d, z) / 3.1415926535 * _scale;
float phi = atan(xy.y, xy.x);
uv.x = r * cos(phi) + 0.5;
uv.y = r * sin(phi) + 0.5;
} else {
uv = _st.xy;
}
return uv;
}
vec4 sphereTexture(in sampler2D _tex, in vec2 _uv, float _time) {
vec2 st = sphereCoords(_uv, 1.0);
float aspect = u_tex0Resolution.y/u_tex0Resolution.x;
st.x = fract(st.x * aspect + _time);
return texture2D(_tex, st);
}
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution.xy;
vec3 diff = vec3( vec2(1.0) / u_resolution.xy, 0.0);
vec2 mouse_uv = u_mouse.xy / u_resolution.xy;
float mouse_pointer = smoothstep(1.5, 0.5, length((mouse_uv - uv) * u_resolution) );
#if defined( BUFFER_0 )
// Ping
vec4 center = texture2D(u_buffer1, uv);
float top = texture2D(u_buffer1, uv - diff.zy).r;
float left = texture2D(u_buffer1, uv - diff.xz).r;
float right = texture2D(u_buffer1, uv + diff.xz).r;
float bottom = texture2D(u_buffer1, uv + diff.zy).r;
float red = -(center.g - 0.5) * 2.0 + (top + left + right + bottom - 2.0);
red += mouse_pointer; // mouse
red *= 0.98; // damping
red *= step(0.1, u_time); // hacky way of clearing the buffer
red = 0.5 + red * 0.5;
red = clamp(red, 0., 1.);
gl_FragColor = vec4(red, center.r, 0.0, 0.0);
#elif defined( BUFFER_1 )
// Pong
// Note: in this example you can get away with only one buffer...
// still is good to show off how easy is to make another buffer
vec4 ping = texture2D(u_buffer0, uv, 0.0);
if (u_time < 1.) {
ping = vec4(vec3(0.5), 1.);
}
gl_FragColor = ping;
#else
// Main Buffer
vec4 ripples = texture2D(u_buffer1, uv);
float offset = ripples.r - 0.5;
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st = scale(st, 2.0);
st = ratio(st, u_resolution);
st += offset;
vec4 color = sphereTexture(u_tex0, st, u_time * 0.01);
float radius = 1.0 - circleSDF(st);
color.rgb *= smoothstep(0.001, 0.02, radius);
color.rgb += (ripples.r - .5) * 2.;
gl_FragColor = color;
#endif
}
" width="800" height="600"></canvas>
</body>
<script>
var canvas = document.getElementById("glslCanvas");
var sandbox = new GlslCanvas(canvas);
var texCounter = 0;
var sandbox_content = "";
var sandbox_title = "";
var sandbox_author = "";
var sandbox_thumbnail = "";
canvas.style.width = '100%';
canvas.style.height = '100%';
</script>
</html>