-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
CompositeShader.js
290 lines (201 loc) · 7.37 KB
/
CompositeShader.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import { Vector2, Color } from '//cdn.skypack.dev/three@0.130.1/build/three.module.js';
export const CompositeShader = {
defines: {
BLUR_ITERATIONS: 5,
BLUR_MODE: 0,
AO_ONLY: 0,
COLOR_ONLY: 0,
DEPTH_THRESHOLD: '5e-1',
},
uniforms: {
fullSize: { value: new Vector2() },
aoSize: { value: new Vector2() },
normalBuffer: { value: null },
depthBuffer: { value: null },
colorBuffer: { value: null },
gtaoBuffer: { value: null },
intensity: { value: 1.0 },
blurStride: { value: 1.0 },
ambientColor: { value: new Color() },
ambientIntensity: { value: 0 },
},
vertexShader:
/* glsl */`
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
`,
fragmentShader:
/* glsl */`
varying vec2 vUv;
uniform vec3 ambientColor;
uniform float ambientIntensity;
uniform vec2 aoSize;
uniform vec2 fullSize;
uniform sampler2D colorBuffer;
uniform sampler2D depthBuffer;
uniform sampler2D normalBuffer;
uniform sampler2D gtaoBuffer;
uniform float intensity;
uniform int blurStride;
vec3 UnpackNormal( vec4 d ) {
return d.xyz * 2.0 - 1.0;
}
vec3 MultiBounce( float ao, vec3 albedo ) {
vec3 x = vec3( ao );
vec3 a = 2.0404 * albedo - vec3( 0.3324 );
vec3 b = -4.7951 * albedo + vec3( 0.6417 );
vec3 c = 2.7552 * albedo + vec3( 0.6903 );
return max( x, ( ( x * a + b ) * x + c ) * x );
}
void main() {
vec4 color = texture2D( colorBuffer, vUv );
// NO_BLUR
#if BLUR_MODE == 0
vec4 accumSample = texture2D( gtaoBuffer, vUv );
#else
vec2 currTexel = vUv * fullSize;
vec2 currAoTexel = vUv * aoSize;
// aoPixels per full size ones. Should be 1/2 at
vec2 texelRatio = aoSize / fullSize;
vec3 currNormal = UnpackNormal( texture2D( normalBuffer, vUv ) );
float currDepth = texture2D( depthBuffer, vUv ).r;
// TODO: pull this sampling out into a function
vec4 accumSample = vec4( 0.0 );
float totalWeight = 1e-10;
float pixelOffset = - float( BLUR_ITERATIONS ) / 2.0;
pixelOffset += mod( float( BLUR_ITERATIONS ), 2.0 ) == 0.0 ? 0.0 : 0.5;
pixelOffset *= float( blurStride );
// BOX_BLUR
#if BLUR_MODE == 1
#pragma unroll_loop_start
for ( int x = 0; x < BLUR_ITERATIONS; x ++ ) {
#pragma unroll_loop_start
for ( int y = 0; y < BLUR_ITERATIONS; y ++ ) {
vec2 step = vec2( float( x ), float( y ) ) * float( blurStride );
// iterate over full res pixels
vec2 offsetUv = currTexel + ( pixelOffset + step ) / texelRatio;
offsetUv /= fullSize;
// get the associated pixel in the AO buffer
vec2 aoUv = currAoTexel + pixelOffset + step;
aoUv /= aoSize;
// if the pixels are close enough in space then blur them together
float offsetDepth = texture2D( depthBuffer, offsetUv ).r;
if ( abs( offsetDepth - currDepth ) <= DEPTH_THRESHOLD ) {
// Weigh the sample based on normal similarity
vec3 offsetNormal = UnpackNormal( texture2D( normalBuffer, offsetUv ) );
float weight = max( 0.0, dot( offsetNormal, currNormal ) );
// square the weight to give pixels with a closer normal even higher priority
weight *= weight;
// accumulate
vec4 val = texture2D( gtaoBuffer, aoUv );
accumSample += val * weight;
totalWeight += weight;
}
}
#pragma unroll_loop_end
}
#pragma unroll_loop_end
// CROSS_BLUR
#elif BLUR_MODE == 2
#pragma unroll_loop_start
for ( int i = 0; i < BLUR_ITERATIONS; i ++ ) {
vec2 offsetUv, aoUv;
float offsetDepth;
// X sample
// iterate over full res pixels
offsetUv = currTexel + vec2( pixelOffset + float( i * blurStride ), 0.0 ) / texelRatio;
offsetUv /= fullSize;
aoUv = currAoTexel + vec2( pixelOffset + float( i * blurStride ), 0.0 );
aoUv /= aoSize;
// further more negative
offsetDepth = texture2D( depthBuffer, offsetUv ).r;
if ( abs(offsetDepth - currDepth) <= DEPTH_THRESHOLD ) {
vec3 offsetNormal = UnpackNormal( texture2D( normalBuffer, offsetUv ) );
float weight = max(0.0, dot( offsetNormal, currNormal ) );
weight *= weight;
vec4 val = texture2D( gtaoBuffer, aoUv );
accumSample += val * weight;
totalWeight += weight;
}
// TODO: this should not be here if on the center pixel
// Y sample
// iterate over full res pixels
offsetUv = currTexel + vec2( 0.0, pixelOffset + float( i * blurStride ) ) / texelRatio;
offsetUv /= fullSize;
aoUv = currAoTexel + vec2( 0.0, pixelOffset + float( i * blurStride ) );
aoUv /= aoSize;
// further more negative
offsetDepth = texture2D( depthBuffer, offsetUv ).r;
if ( abs(offsetDepth - currDepth) <= DEPTH_THRESHOLD ) {
vec3 offsetNormal = UnpackNormal( texture2D( normalBuffer, offsetUv ) );
float weight = max(0.0, dot( offsetNormal, currNormal ) );
weight *= weight;
vec4 val = texture2D( gtaoBuffer, aoUv );
accumSample += val * weight;
totalWeight += weight;
}
}
#pragma unroll_loop_end
// DIAGONAL_BLUR
#elif BLUR_MODE == 3
#pragma unroll_loop_start
for ( int i = 0; i < BLUR_ITERATIONS; i ++ ) {
vec2 offsetUv, aoUv;
float offsetDepth;
// X sample
// iterate over full res pixels
offsetUv = currTexel + vec2( pixelOffset + float( i * blurStride ), pixelOffset + float( i * blurStride ) ) / texelRatio;
offsetUv /= fullSize;
aoUv = currAoTexel + vec2( pixelOffset + float( i * blurStride ), pixelOffset + float( i * blurStride ) );
aoUv /= aoSize;
// further more negative
offsetDepth = texture2D( depthBuffer, offsetUv ).r;
if ( abs(offsetDepth - currDepth) <= DEPTH_THRESHOLD ) {
vec3 offsetNormal = UnpackNormal( texture2D( normalBuffer, offsetUv ) );
float weight = max(0.0, dot( offsetNormal, currNormal ) );
weight *= weight;
vec4 val = texture2D( gtaoBuffer, aoUv );
accumSample += val * weight;
totalWeight += weight;
}
// TODO: this should not be here if on the center pixel
// Y sample
// iterate over full res pixels
offsetUv = currTexel + vec2( - pixelOffset - float( i * blurStride ), pixelOffset + float( i * blurStride ) ) / texelRatio;
offsetUv /= fullSize;
aoUv = currAoTexel + vec2( - pixelOffset - float( i * blurStride ), pixelOffset + float( i * blurStride ) );
aoUv /= aoSize;
// further more negative
offsetDepth = texture2D( depthBuffer, offsetUv ).r;
if ( abs(offsetDepth - currDepth) <= DEPTH_THRESHOLD ) {
vec3 offsetNormal = UnpackNormal( texture2D( normalBuffer, offsetUv ) );
float weight = max(0.0, dot( offsetNormal, currNormal ) );
weight *= weight;
vec4 val = texture2D( gtaoBuffer, aoUv );
accumSample += val * weight;
totalWeight += weight;
}
}
#pragma unroll_loop_end
#endif
accumSample /= totalWeight;
#endif
float gtao = accumSample.a;
#if COLOR_ONLY
gl_FragColor = vec4( accumSample.rgb, 1.0 );
#elif AO_ONLY
vec3 rgb = mix( vec3( 1.0 ), vec3( accumSample.a ), intensity );
gl_FragColor = vec4( rgb, 1.0 );
#else
vec3 rgb = mix( color.rgb, color.rgb * MultiBounce( gtao, color.rgb ), intensity );
vec3 delta = color.rgb - rgb;
vec3 ambient = ambientColor * delta * ambientIntensity;
float colorFade = ( 1.0 - pow( 1.0 - gtao, 2.0 ) );
gl_FragColor = vec4( rgb + ambient + accumSample.rgb * ( 0.75 + colorFade * 0.25 ), color.a );
#endif
}
`
};