-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
157 lines (142 loc) · 4.3 KB
/
index.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const h = require('h');
const fs = require('fs');
const fft = require('../');
const css = require('insert-css');
const path = require('path');
const resl = require('resl');
const regl = require('regl');
const glsl = require('glslify');
const mobile = require('is-mobile');
const radiusSlider = h('input', {type: 'range', min: 0, max: 50, step: 0.1, id: 'radius', value: 10});
const angleSlider = h('input', {type: 'range', min: 0, max: 180, step: 1, id: 'angle', value: 0});
const radiusReadout = h('span', {class: 'readout'});
const angleReadout = h('span', {class: 'readout'});
const controls = h('div', [
h('div', [h('label', 'Radius:', {for: 'radius'}), radiusSlider, radiusReadout]),
h('div', [h('label', 'Angle:', {for: 'angle'}), angleSlider, angleReadout]),
]);
const root = h('div', {id: 'root'});
document.body.appendChild(root);
document.body.appendChild(controls);
css(fs.readFileSync(path.join(__dirname, 'index.css'), 'utf8'));
resl({
manifest: {mist: {type: 'image', src: 'mist.jpg'}},
onDone: ({mist}) => {
regl({
pixelRatio: 1,
container: root,
attributes: {antialias: false},
onDone: require('fail-nicely')(regl => start(regl, mist)),
optionalExtensions: ['oes_texture_float'],
extensions: ['oes_texture_half_float']
})
}
})
function start (regl, mist) {
const width = regl._gl.canvas.width;
const height = regl._gl.canvas.height;
const img = regl.texture({data: mist, flipY: true});
const type = (regl.hasExtension('oes_texture_float') && !mobile) ? 'float' : 'half float';
const fbos = [0, 1, 2].map(() => regl.framebuffer({colorType: type, width: width, height: height}));
const apply = regl({
vert: `
precision mediump float;
attribute vec2 xy;
void main () {
gl_Position = vec4(xy, 0, 1);
}
`,
frag: glsl(`
precision highp float;
#pragma glslify: fft = require(../)
uniform sampler2D src;
uniform vec2 resolution;
uniform float subtransformSize, normalization;
uniform bool horizontal, forward;
void main () {
gl_FragColor = fft(src, resolution, subtransformSize, horizontal, forward, normalization);
}
`),
uniforms: {
resolution: regl.prop('resolution'),
forward: regl.prop('forward'),
subtransformSize: regl.prop('subtransformSize'),
horizontal: regl.prop('horizontal'),
normalization: regl.prop('normalization'),
src: regl.prop('input'),
},
attributes: {xy: [-4, -4, 4, -4, 0, 4]},
framebuffer: regl.prop('output'),
depth: {enable: false},
count: 3
});
const filter = regl({
vert: `
precision mediump float;
varying vec2 uv;
attribute vec2 xy;
void main () {
uv = 0.5 * xy + 0.5;
gl_Position = vec4(xy, 0, 1);
}
`,
frag: glsl(`
precision highp float;
#pragma glslify: wavenumber = require(../wavenumber)
varying vec2 uv;
uniform sampler2D src;
uniform vec2 resolution, e1;
uniform float radius;
void main () {
vec4 col = texture2D(src, uv);
vec2 kxy = wavenumber(resolution);
float k1 = dot(e1, kxy) * radius;
gl_FragColor = col * exp(-k1 * k1 * 0.5);
}
`),
uniforms: {
resolution: regl.prop('resolution'),
radius: regl.prop('radius'),
src: regl.prop('input'),
e1: regl.prop('e1'),
},
attributes: {xy: [-4, -4, 4, -4, 0, 4]},
framebuffer: regl.prop('output'),
depth: {enable: false},
count: 3
});
const forward = fft({
width: width,
height: height,
input: img,
ping: fbos[0],
pong: fbos[1],
output: fbos[0],
forward: true
});
const inverse = fft({
width: width,
height: height,
input: fbos[1],
ping: fbos[1],
pong: fbos[2],
forward: false
});
apply(forward);
function draw () {
radiusReadout.textContent = radiusSlider.value;
angleReadout.textContent = angleSlider.value;
var theta = parseFloat(angleSlider.value) * Math.PI / 180.0;
filter({
input: fbos[0],
output: fbos[1],
resolution: [1 / width, 1 / height],
radius: parseFloat(radiusSlider.value),
e1: [Math.cos(theta), Math.sin(theta)]
});
apply(inverse);
}
radiusSlider.addEventListener('input', draw);
angleSlider.addEventListener('input', draw);
draw();
}