-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
341 lines (224 loc) · 9.68 KB
/
index.html
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<html>
<head>
<link rel=stylesheet href="main.css" />
<script src="/three.js/build/three.min.js"></script>
<script src="/three.js/examples/js/shaders/CopyShader.js"></script>
<script>
/**
* @author alteredq / http://alteredqualia.com/
*
* Screen-space ambient occlusion shader
* - ported from
* SSAO GLSL shader v1.2
* assembled by Martins Upitis (martinsh) (http://devlog-martinsh.blogspot.com)
* original technique is made by ArKano22 (http://www.gamedev.net/topic/550699-ssao-no-halo-artifacts/)
* - modifications
* - modified to use RGBA packed depth texture (use clear color 1,1,1,1 for depth pass)
* - refactoring and optimizations
*/
THREE.SSAOShader = {
uniforms: {
"tDiffuse": { type: "t", value: null },
"tDepth": { type: "t", value: null },
"size": { type: "v2", value: new THREE.Vector2( 512, 512 ) },
"cameraNear": { type: "f", value: 1 },
"cameraFar": { type: "f", value: 1000 },
"onlyAO": { type: "i", value: 0 },
"aoClamp": { type: "f", value: 0.5 },
"lumInfluence": { type: "f", value: 0.5 }
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
"vUv = uv;",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform float cameraNear;",
"uniform float cameraFar;",
"uniform bool onlyAO;", // use only ambient occlusion pass?
"uniform vec2 size;", // texture width, height
"uniform float aoClamp;", // depth clamp - reduces haloing at screen edges
"uniform float lumInfluence;", // how much luminance affects occlusion
"uniform sampler2D tDiffuse;",
"uniform sampler2D tDepth;",
"varying vec2 vUv;",
// "#define PI 3.14159265",
"#define DL 2.399963229728653", // PI * ( 3.0 - sqrt( 5.0 ) )
"#define EULER 2.718281828459045",
// helpers
"float width = size.x;", // texture width
"float height = size.y;", // texture height
"float cameraFarPlusNear = cameraFar + cameraNear;",
"float cameraFarMinusNear = cameraFar - cameraNear;",
"float cameraCoef = 2.0 * cameraNear;",
// user variables
"const int samples = 8;", // ao sample count
"const float radius = 4.0;", // ao radius
"const bool useNoise = false;", // use noise instead of pattern for sample dithering
"const float noiseAmount = 0.0005;", // dithering amount
"const float diffArea = 0.55;", // self-shadowing reduction
"const float gDisplace = 0.5;", // gauss bell center
// RGBA depth
"float unpackDepth( const in vec4 rgba_depth ) {",
"const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
"float depth = dot( rgba_depth, bit_shift );",
"return depth;",
"}",
// generating noise / pattern texture for dithering
"vec2 rand( const vec2 coord ) {",
"vec2 noise;",
"if ( useNoise ) {",
"float nx = dot ( coord, vec2( 12.9898, 78.233 ) );",
"float ny = dot ( coord, vec2( 12.9898, 78.233 ) * 2.0 );",
"noise = clamp( fract ( 43758.5453 * sin( vec2( nx, ny ) ) ), 0.0, 1.0 );",
"} else {",
"float ff = fract( 1.0 - coord.s * ( width / 2.0 ) );",
"float gg = fract( coord.t * ( height / 2.0 ) );",
"noise = vec2( 0.25, 0.75 ) * vec2( ff ) + vec2( 0.75, 0.25 ) * gg;",
"}",
"return ( noise * 2.0 - 1.0 ) * noiseAmount;",
"}",
"float readDepth( const in vec2 coord ) {",
// "return ( 2.0 * cameraNear ) / ( cameraFar + cameraNear - unpackDepth( texture2D( tDepth, coord ) ) * ( cameraFar - cameraNear ) );",
"return cameraCoef / ( cameraFarPlusNear - unpackDepth( texture2D( tDepth, coord ) ) * cameraFarMinusNear );",
"}",
"float compareDepths( const in float depth1, const in float depth2, inout int far ) {",
"float garea = 4.0;", // gauss bell width
"float diff = ( depth1 - depth2 ) * 100.0;", // depth difference (0-100)
// reduce left bell width to avoid self-shadowing
"if ( diff < gDisplace ) {",
"garea = diffArea;",
"} else {",
"far = 1;",
"}",
"float dd = diff - gDisplace;",
"float gauss = pow( EULER, -2.0 * dd * dd / ( garea * garea ) );",
"return gauss;",
"}",
"float calcAO( float depth, float dw, float dh ) {",
"float dd = radius - depth * radius;",
"vec2 vv = vec2( dw, dh );",
"vec2 coord1 = vUv + dd * vv;",
"vec2 coord2 = vUv - dd * vv;",
"float temp1 = 0.0;",
"float temp2 = 0.0;",
"int far = 0;",
"temp1 = compareDepths( depth, readDepth( coord1 ), far );",
// DEPTH EXTRAPOLATION
"if ( far > 0 ) {",
"temp2 = compareDepths( readDepth( coord2 ), depth, far );",
"temp1 += ( 1.0 - temp1 ) * temp2;",
"}",
"return temp1;",
"}",
"void main() {",
"vec2 noise = rand( vUv );",
"float depth = readDepth( vUv );",
"float tt = clamp( depth, aoClamp, 1.0 );",
"float w = ( 1.0 / width ) / tt + ( noise.x * ( 1.0 - noise.x ) );",
"float h = ( 1.0 / height ) / tt + ( noise.y * ( 1.0 - noise.y ) );",
"float ao = 0.0;",
"float dz = 1.0 / float( samples );",
"float z = 1.0 - dz / 2.0;",
"float l = 0.0;",
"for ( int i = 0; i <= samples; i ++ ) {",
"float r = sqrt( 1.0 - z );",
"float pw = cos( l ) * r;",
"float ph = sin( l ) * r;",
"ao += calcAO( depth, pw * w, ph * h );",
"z = z - dz;",
"l = l + DL;",
"}",
"ao /= float( samples );",
"ao = 1.0 - ao;",
"vec3 color = texture2D( tDiffuse, vUv ).rgb;",
"vec3 lumcoeff = vec3( 0.299, 0.587, 0.114 );",
"float lum = dot( color.rgb, lumcoeff );",
"vec3 luminance = vec3( lum );",
"vec3 final = vec3( color * mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // mix( color * ao, white, luminance )
"if ( onlyAO ) {",
"final = vec3( mix( vec3( ao ), vec3( 1.0 ), luminance * lumInfluence ) );", // ambient occlusion only
"}",
"gl_FragColor = vec4( final, 1.0 );",
"}"
].join("\n")
};
</script>
<script>
//------------------------------------
/**
* @author alteredq / http://alteredqualia.com/
* @author davidedc / http://www.sketchpatch.net/
*
* NVIDIA FXAA by Timothy Lottes
* http://timothylottes.blogspot.com/2011/06/fxaa3-source-released.html
* - WebGL port by @supereggbert
* http://www.glge.org/demos/fxaa/
*/
THREE.FXAAShader = {
uniforms: {
"tDiffuse": { type: "t", value: null },
"resolution": { type: "v2", value: new THREE.Vector2( 1 / 1024, 1 / 512 ) }
},
vertexShader: [
"void main() {",
"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: [
"uniform sampler2D tDiffuse;",
"uniform vec2 resolution;",
"#define FXAA_REDUCE_MIN (1.0/128.0)",
"#define FXAA_REDUCE_MUL (1.0/8.0)",
"#define FXAA_SPAN_MAX 8.0",
"void main() {",
"vec3 rgbNW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, -1.0 ) ) * resolution ).xyz;",
"vec3 rgbNE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, -1.0 ) ) * resolution ).xyz;",
"vec3 rgbSW = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( -1.0, 1.0 ) ) * resolution ).xyz;",
"vec3 rgbSE = texture2D( tDiffuse, ( gl_FragCoord.xy + vec2( 1.0, 1.0 ) ) * resolution ).xyz;",
"vec4 rgbaM = texture2D( tDiffuse, gl_FragCoord.xy * resolution );",
"vec3 rgbM = rgbaM.xyz;",
"vec3 luma = vec3( 0.299, 0.587, 0.114 );",
"float lumaNW = dot( rgbNW, luma );",
"float lumaNE = dot( rgbNE, luma );",
"float lumaSW = dot( rgbSW, luma );",
"float lumaSE = dot( rgbSE, luma );",
"float lumaM = dot( rgbM, luma );",
"float lumaMin = min( lumaM, min( min( lumaNW, lumaNE ), min( lumaSW, lumaSE ) ) );",
"float lumaMax = max( lumaM, max( max( lumaNW, lumaNE) , max( lumaSW, lumaSE ) ) );",
"vec2 dir;",
"dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));",
"dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));",
"float dirReduce = max( ( lumaNW + lumaNE + lumaSW + lumaSE ) * ( 0.25 * FXAA_REDUCE_MUL ), FXAA_REDUCE_MIN );",
"float rcpDirMin = 1.0 / ( min( abs( dir.x ), abs( dir.y ) ) + dirReduce );",
"dir = min( vec2( FXAA_SPAN_MAX, FXAA_SPAN_MAX),",
"max( vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),",
"dir * rcpDirMin)) * resolution;",
"vec4 rgbA = (1.0/2.0) * (",
"texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (1.0/3.0 - 0.5)) +",
"texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (2.0/3.0 - 0.5)));",
"vec4 rgbB = rgbA * (1.0/2.0) + (1.0/4.0) * (",
"texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (0.0/3.0 - 0.5)) +",
"texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (3.0/3.0 - 0.5)));",
"float lumaB = dot(rgbB, vec4(luma, 0.0));",
"if ( ( lumaB < lumaMin ) || ( lumaB > lumaMax ) ) {",
"gl_FragColor = rgbA;",
"} else {",
"gl_FragColor = rgbB;",
"}",
"}"
].join("\n")
};
</script>
<script src="/three.js/examples/js/postprocessing/EffectComposer.js"></script>
<script src="/three.js/examples/js/postprocessing/RenderPass.js"></script>
<script src="/three.js/examples/js/postprocessing/MaskPass.js"></script>
<script src="/three.js/examples/js/postprocessing/ShaderPass.js"></script>
<script src="/main.js"></script>
</head>
<body onload="run()">
<div id="output"></div>
</body>
</html>