-
Notifications
You must be signed in to change notification settings - Fork 4
/
warp.js
96 lines (71 loc) · 2.19 KB
/
warp.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
"use strict";
var program = null;
/**
*
* @param context
* @returns {WorkingWebGLProgram}
*/
function getProgram (context) {
if (program === null) {
program = context.createProgram(`#version 300 es
precision highp float;
precision highp int;
precision highp sampler2D;
layout(location = 0) out vec4 fragColor;
const float eps = 0.0000001;
uniform vec2 resolution;
uniform float seed;
uniform float intensity;
uniform sampler2D source;
uniform bool sourceSet;
uniform vec2 sourceSize;
uniform sampler2D intensityMap;
uniform bool intensityMapSet;
uniform vec2 intensityMapSize;
vec2 getDir (in vec2 uv, in vec2 p) {
vec3 ccc = texture(intensityMap, uv).rgb;
float fcc = (ccc.r + ccc.g + ccc.b) / 3.;
vec2 dir = vec2(0.);
for (float i = 0.; i < 20.; i++) {
float cangle = i / 20. * 6.283185307179586;
vec2 cdir = vec2(cos(cangle), sin(cangle));
vec3 crgb = texture(intensityMap, uv + p * cdir * 0.5).rgb;
float cweight = fcc - (crgb.r + crgb.g + crgb.b) / 3.;
vec3 crgb2 = texture(intensityMap, uv + p * cdir * 1.5).rgb;
float cweight2 = (fcc - (crgb.r + crgb.g + crgb.b) / 3.) / 2.;
dir -= cdir * (cweight + cweight2);
}
return dir;
}
vec4 process (in vec2 uv) {
vec2 p = 1. / resolution.xy;
vec2 dir = intensityMapSet == true ? getDir(uv, p) : vec2(0., 0.);
return vec4(texture(source, uv + dir * intensity * 0.2).rgb, 1.);
}
void main () {
vec2 uv = gl_FragCoord.xy / resolution;
if (sourceSet == true) {
fragColor = process(uv);
} else {
fragColor = vec4(0., 0., 0., 1.);
}
}
`, {
intensityMap: 't',
source: 't',
intensity: 'f'
});
}
return program;
}
function warpJob (context, inputs, outputs, parameters, done) {
var program = getProgram(context);
var uniforms = {
source: inputs.input,
intensityMap: inputs.intensity,
intensity: parameters.intensity
};
program.execute(uniforms, outputs.output);
done();
}
module.exports = warpJob;