File size: 6,700 Bytes
6cd9596
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @author Mugen87 / https://github.com/Mugen87
 *
 * References:
 * http://john-chapman-graphics.blogspot.com/2013/01/ssao-tutorial.html
 * https://learnopengl.com/Advanced-Lighting/SSAO
 * https://github.com/McNopper/OpenGL/blob/master/Example28/shader/ssao.frag.glsl
 */

THREE.SSAOShader = {

	defines: {
		"PERSPECTIVE_CAMERA": 1,
		"KERNEL_SIZE": 32
	},

	uniforms: {

		"tDiffuse": { value: null },
		"tNormal": { value: null },
		"tDepth": { value: null },
		"tNoise": { value: null },
		"kernel": { value: null },
		"cameraNear": { value: null },
		"cameraFar": { value: null },
		"resolution": { value: new THREE.Vector2() },
		"cameraProjectionMatrix": { value: new THREE.Matrix4() },
		"cameraInverseProjectionMatrix": { value: new THREE.Matrix4() },
		"kernelRadius": { value: 8 },
		"minDistance": { value: 0.005 },
		"maxDistance": { value: 0.05 },

	},

	vertexShader: [

		"varying vec2 vUv;",

		"void main() {",

		"	vUv = uv;",

		"	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

		"}"

	].join( "\n" ),

	fragmentShader: [

		"uniform sampler2D tDiffuse;",
		"uniform sampler2D tNormal;",
		"uniform sampler2D tDepth;",
		"uniform sampler2D tNoise;",

		"uniform vec3 kernel[ KERNEL_SIZE ];",

		"uniform vec2 resolution;",

		"uniform float cameraNear;",
		"uniform float cameraFar;",
		"uniform mat4 cameraProjectionMatrix;",
		"uniform mat4 cameraInverseProjectionMatrix;",

		"uniform float kernelRadius;",
		"uniform float minDistance;", // avoid artifacts caused by neighbour fragments with minimal depth difference
		"uniform float maxDistance;", // avoid the influence of fragments which are too far away

		"varying vec2 vUv;",

		"#include <packing>",

		"float getDepth( const in vec2 screenPosition ) {",

		"	return texture2D( tDepth, screenPosition ).x;",

		"}",

		"float getLinearDepth( const in vec2 screenPosition ) {",

		"	#if PERSPECTIVE_CAMERA == 1",

		"		float fragCoordZ = texture2D( tDepth, screenPosition ).x;",
		"		float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );",
		"		return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );",

		"	#else",

		"		return texture2D( depthSampler, coord ).x;",

		"	#endif",

		"}",

		"float getViewZ( const in float depth ) {",

		"	#if PERSPECTIVE_CAMERA == 1",

		"		return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );",

		"	#else",

		"		return orthographicDepthToViewZ( depth, cameraNear, cameraFar );",

		"	#endif",

		"}",

		"vec3 getViewPosition( const in vec2 screenPosition, const in float depth, const in float viewZ ) {",

		"	float clipW = cameraProjectionMatrix[2][3] * viewZ + cameraProjectionMatrix[3][3];",

		"	vec4 clipPosition = vec4( ( vec3( screenPosition, depth ) - 0.5 ) * 2.0, 1.0 );",

		"	clipPosition *= clipW; // unprojection.",

		"	return ( cameraInverseProjectionMatrix * clipPosition ).xyz;",

		"}",

		"vec3 getViewNormal( const in vec2 screenPosition ) {",

		"	return unpackRGBToNormal( texture2D( tNormal, screenPosition ).xyz );",

		"}",

		"void main() {",

		"	float depth = getDepth( vUv );",
		"	float viewZ = getViewZ( depth );",

		"	vec3 viewPosition = getViewPosition( vUv, depth, viewZ );",
		"	vec3 viewNormal = getViewNormal( vUv );",

		" vec2 noiseScale = vec2( resolution.x / 4.0, resolution.y / 4.0 );",
		"	vec3 random = texture2D( tNoise, vUv * noiseScale ).xyz;",

		// compute matrix used to reorient a kernel vector

		"	vec3 tangent = normalize( random - viewNormal * dot( random, viewNormal ) );",
		"	vec3 bitangent = cross( viewNormal, tangent );",
		"	mat3 kernelMatrix = mat3( tangent, bitangent, viewNormal );",

		" float occlusion = 0.0;",

		" for ( int i = 0; i < KERNEL_SIZE; i ++ ) {",

		"		vec3 sampleVector = kernelMatrix * kernel[ i ];", // reorient sample vector in view space
		"		vec3 samplePoint = viewPosition + ( sampleVector * kernelRadius );", // calculate sample point

		"		vec4 samplePointNDC = cameraProjectionMatrix * vec4( samplePoint, 1.0 );", // project point and calculate NDC
		"		samplePointNDC /= samplePointNDC.w;",

		"		vec2 samplePointUv = samplePointNDC.xy * 0.5 + 0.5;", // compute uv coordinates

		"		float realDepth = getLinearDepth( samplePointUv );", // get linear depth from depth texture
		"		float sampleDepth = viewZToOrthographicDepth( samplePoint.z, cameraNear, cameraFar );", // compute linear depth of the sample view Z value
		"		float delta = sampleDepth - realDepth;",

		"		if ( delta > minDistance && delta < maxDistance ) {", // if fragment is before sample point, increase occlusion

		"			occlusion += 1.0;",

		"		}",

		"	}",

		"	occlusion = clamp( occlusion / float( KERNEL_SIZE ), 0.0, 1.0 );",

		"	gl_FragColor = vec4( vec3( 1.0 - occlusion ), 1.0 );",

		"}"

	].join( "\n" )

};

THREE.SSAODepthShader = {

	defines: {
		"PERSPECTIVE_CAMERA": 1
	},

	uniforms: {

		"tDepth": { value: null },
		"cameraNear": { value: null },
		"cameraFar": { value: null },

	},

	vertexShader: [

		"varying vec2 vUv;",

		"void main() {",

		"	vUv = uv;",
		"	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

		"}"

	].join( "\n" ),

	fragmentShader: [

		"uniform sampler2D tDepth;",

		"uniform float cameraNear;",
		"uniform float cameraFar;",

		"varying vec2 vUv;",

		"#include <packing>",

		"float getLinearDepth( const in vec2 screenPosition ) {",

		"	#if PERSPECTIVE_CAMERA == 1",

		"		float fragCoordZ = texture2D( tDepth, screenPosition ).x;",
		"		float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );",
		"		return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );",

		"	#else",

		"		return texture2D( depthSampler, coord ).x;",

		"	#endif",

		"}",

		"void main() {",

		"	float depth = getLinearDepth( vUv );",
		"	gl_FragColor = vec4( vec3( 1.0 - depth ), 1.0 );",

		"}"

	].join( "\n" )

};

THREE.SSAOBlurShader = {

	uniforms: {

		"tDiffuse": { value: null },
		"resolution": { value: new THREE.Vector2() }

	},

	vertexShader: [

		"varying vec2 vUv;",

		"void main() {",

		"	vUv = uv;",
		"	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",

		"}"

	].join( "\n" ),

	fragmentShader: [

		"uniform sampler2D tDiffuse;",

		"uniform vec2 resolution;",

		"varying vec2 vUv;",

		"void main() {",

		"	vec2 texelSize = ( 1.0 / resolution );",
		"	float result = 0.0;",

		"	for ( int i = - 2; i <= 2; i ++ ) {",

		"		for ( int j = - 2; j <= 2; j ++ ) {",

		"			vec2 offset = ( vec2( float( i ), float( j ) ) ) * texelSize;",
		"			result += texture2D( tDiffuse, vUv + offset ).r;",

		"		}",

		"	}",

		"	gl_FragColor = vec4( vec3( result / ( 5.0 * 5.0 ) ), 1.0 );",

		"}"

	].join( "\n" )

};