forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedback.js
68 lines (57 loc) · 1.28 KB
/
feedback.js
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
/*
tags: basic
<p> This example shows how to use copyTexImage2D to implement feedback effects </p>
*/
const regl = require('../regl')()
const mouse = require('mouse-change')()
const pixels = regl.texture()
const drawFeedback = regl({
frag: `
precision mediump float;
uniform sampler2D texture;
uniform vec2 mouse;
uniform float t;
varying vec2 uv;
void main () {
float dist = length(gl_FragCoord.xy - mouse);
gl_FragColor = vec4(0.98 * texture2D(texture,
uv + cos(t) * vec2(0.5 - uv.y, uv.x - 0.5) - sin(2.0 * t) * (uv - 0.5)).rgb, 1) +
exp(-0.01 * dist) * vec4(
1.0 + cos(2.0 * t),
1.0 + cos(2.0 * t + 1.5),
1.0 + cos(2.0 * t + 3.0),
0.0);
}`,
vert: `
precision mediump float;
attribute vec2 position;
varying vec2 uv;
void main () {
uv = position;
gl_Position = vec4(2.0 * position - 1.0, 0, 1);
}`,
attributes: {
position: [
-2, 0,
0, -2,
2, 2]
},
uniforms: {
texture: pixels,
mouse: ({pixelRatio, viewportHeight}) => [
mouse.x * pixelRatio,
viewportHeight - mouse.y * pixelRatio
],
t: ({tick}) => 0.01 * tick
},
count: 3
})
regl.frame(function () {
regl.clear({
color: [0, 0, 0, 1]
})
drawFeedback()
pixels({
copy: true
})
})