forked from N8python/2dgi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHorizontalBlurShader.js
83 lines (74 loc) · 2.57 KB
/
HorizontalBlurShader.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
import * as THREE from 'https://cdn.skypack.dev/pin/three@v0.137.0-X5O2PK3x44y1WRry67Kr/mode=imports/optimized/three.js';
const HorizontalBlurShader = {
uniforms: {
'tDiffuse': { value: null },
'h': { value: 1.0 / 512.0 },
'resolution': { value: new THREE.Vector2() },
'metaTex': { value: null },
'wallAware': { value: true },
'sigma': { value: 0.1 },
'bilateralBlur': { value: true }
},
vertexShader: /* glsl */ `
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: /* glsl */ `
varying vec2 vUv;
uniform sampler2D tDiffuse;
uniform sampler2D metaTex;
uniform float h;
uniform vec2 resolution;
uniform bool wallAware;
uniform float sigma;
uniform bool bilateralBlur;
float normpdf3(in vec3 v, in float sigma)
{
return 0.39894*exp(-0.5*dot(v,v)/(sigma*sigma))/sigma;
}
void main() {
if (texture2D(metaTex, vUv).x == 1.0) {
gl_FragColor = texture2D(tDiffuse, vUv);
return;
}
vec4 sum = vec4( 0.0 );
float weightSum = 0.0;
vec4 ref = texture2D(tDiffuse, vUv);
float[9] weights = float[9](0.051, 0.0918, 0.12245, 0.1531, 0.1633, 0.1531, 0.12245, 0.0918, 0.051);
float radius = h / resolution.x; //max(h * (1.0 - d) * (-blurSharp * pow(b - 0.5, 2.0) + 1.0), blurThreshold / resolution.x);
for(float i = -1.0; i >= -4.0; i--) {
vec2 sampleUv = vec2( vUv.x + i * radius, vUv.y );
float metaVal = texture2D(metaTex, sampleUv).x;
if (metaVal == 1.0 && wallAware) {
break;
}
vec4 sam = texture2D( tDiffuse, sampleUv);
float w = weights[int(i + 4.0)] * (1.0 - metaVal) * (bilateralBlur ? normpdf3((sam - ref).rgb, sigma) : 1.0);
sum += sam * w;
weightSum += w;
}
for(float i = 0.0; i <= 0.0; i++) {
vec2 sampleUv = vec2( vUv.x + i * radius, vUv.y );
float w = weights[int(i + 4.0)] * (1.0 - texture2D(metaTex, sampleUv).x);
sum += texture2D( tDiffuse, sampleUv) * w;
weightSum += w;
}
for(float i = 1.0; i <= 4.0; i++) {
vec2 sampleUv = vec2( vUv.x + i * radius, vUv.y );
float metaVal = texture2D(metaTex, sampleUv).x;
if (metaVal == 1.0 && wallAware) {
break;
}
vec4 sam = texture2D( tDiffuse, sampleUv);
float w = weights[int(i + 4.0)] * (1.0 - metaVal) * (bilateralBlur ? normpdf3((sam - ref).rgb, sigma) : 1.0);
sum += sam * w;
weightSum += w;
}
sum /= weightSum;
gl_FragColor = sum;
gl_FragColor = clamp(gl_FragColor, vec4(0.0), vec4(1.0));
}`
};
export { HorizontalBlurShader };