forked from lettier/3d-game-shaders-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 1
/
screen-space-refraction.frag
137 lines (103 loc) · 3.41 KB
/
screen-space-refraction.frag
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
/*
(C) 2019 David Lettier
lettier.com
*/
#version 150
uniform mat4 lensProjection;
uniform sampler2D positionFromTexture;
uniform sampler2D positionToTexture;
uniform sampler2D normalFromTexture;
uniform vec2 rior;
uniform vec2 enabled;
out vec4 fragColor;
void main() {
float maxDistance = 5;
float resolution = 0.3;
int steps = 5;
float thickness = 0.5;
vec2 texSize = textureSize(positionFromTexture, 0).xy;
vec2 texCoord = gl_FragCoord.xy / texSize;
vec4 uv = vec4(texCoord.xy, 1, 1);
vec4 positionFrom = texture(positionFromTexture, texCoord);
if (positionFrom.w <= 0 || enabled.x != 1) { fragColor = uv; return; }
vec3 unitPositionFrom = normalize(positionFrom.xyz);
vec3 normalFrom = normalize(texture(normalFromTexture, texCoord).xyz);
vec3 pivot = normalize(refract(unitPositionFrom, normalFrom, rior.x));
vec4 positionTo = positionFrom;
vec4 startView = vec4(positionFrom.xyz + (pivot * 0), 1);
vec4 endView = vec4(positionFrom.xyz + (pivot * maxDistance), 1);
vec4 startFrag = startView;
startFrag = lensProjection * startFrag;
startFrag.xyz /= startFrag.w;
startFrag.xy = startFrag.xy * 0.5 + 0.5;
startFrag.xy *= texSize;
vec4 endFrag = endView;
endFrag = lensProjection * endFrag;
endFrag.xyz /= endFrag.w;
endFrag.xy = endFrag.xy * 0.5 + 0.5;
endFrag.xy *= texSize;
vec2 frag = startFrag.xy;
uv.xy = frag / texSize;
float deltaX = endFrag.x - startFrag.x;
float deltaY = endFrag.y - startFrag.y;
float useX = abs(deltaX) >= abs(deltaY) ? 1 : 0;
float delta = mix(abs(deltaY), abs(deltaX), useX) * clamp(resolution, 0, 1);
vec2 increment = vec2(deltaX, deltaY) / max(delta, 0.001);
float search0 = 0;
float search1 = 0;
int hit0 = 0;
int hit1 = 0;
float viewDistance = startView.y;
float depth = thickness;
float i = 0;
for (i = 0; i < int(delta); ++i) {
frag += increment;
uv.xy = frag / texSize;
positionTo = texture(positionToTexture, uv.xy);
search1 =
mix
( (frag.y - startFrag.y) / deltaY
, (frag.x - startFrag.x) / deltaX
, useX
);
search1 = clamp(search1, 0, 1);
viewDistance = (startView.y * endView.y) / mix(endView.y, startView.y, search1);
depth = viewDistance - positionTo.y;
if (depth > 0 && depth < thickness) {
hit0 = 1;
break;
} else {
search0 = search1;
}
}
search1 = search0 + ((search1 - search0) / 2);
steps *= hit0;
for (i = 0; i < steps; ++i) {
frag = mix(startFrag.xy, endFrag.xy, search1);
uv.xy = frag / texSize;
positionTo = texture(positionToTexture, uv.xy);
viewDistance = (startView.y * endView.y) / mix(endView.y, startView.y, search1);
depth = viewDistance - positionTo.y;
if (depth > 0 && depth < thickness) {
hit1 = 1;
search1 = search0 + ((search1 - search0) / 2);
} else {
float temp = search1;
search1 = search1 + ((search1 - search0) / 2);
search0 = temp;
}
}
float visibility =
hit1
* positionTo.w
* ( 1
- max
( dot(-unitPositionFrom, pivot)
, 0
)
)
* (uv.x < 0 || uv.x > 1 ? 0 : 1)
* (uv.y < 0 || uv.y > 1 ? 0 : 1);
visibility = clamp(visibility, 0, 1);
fragColor = vec4(mix(texCoord.xy, uv.xy, visibility), 1, 1);
}