forked from mrdoob/three.js
/
SSRShader.js
329 lines (249 loc) · 7.74 KB
/
SSRShader.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
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
import {
Matrix4,
Vector2
} from "../../../build/three.module.js";
/**
* References:
* https://lettier.github.io/3d-game-shaders-for-beginners/screen-space-reflection.html
*/
var SSRShader = {
defines: {
MAX_STEP: 0,
isPerspectiveCamera: true,
isDistanceAttenuation: true,
isInfiniteThick: false,
isNoise: false,
noiseOversample: 1,
isSelective: false,
},
uniforms: {
"tDiffuse": { value: null },
"tNormal": { value: null },
"tMetalness": { value: null },
"tDepth": { value: null },
"cameraNear": { value: null },
"cameraFar": { value: null },
"resolution": { value: new Vector2() },
"cameraProjectionMatrix": { value: new Matrix4() },
"cameraInverseProjectionMatrix": { value: new Matrix4() },
"opacity": { value: .5 },
"maxDistance": { value: 200 },
"cameraRange": { value: 0 },
"surfDist": { value: .007 },
"thickTolerance": { value: .03 },
"noiseIntensity": { value: .1 },
},
vertexShader: [
"varying vec2 vUv;",
"void main() {",
" vUv = uv;",
" gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
"}"
].join("\n"),
fragmentShader: `
// precision highp float;
precision highp sampler2D;
varying vec2 vUv;
uniform sampler2D tDepth;
uniform sampler2D tNormal;
uniform sampler2D tMetalness;
uniform sampler2D tDiffuse;
uniform float cameraRange;
uniform vec2 resolution;
uniform float opacity;
uniform float cameraNear;
uniform float cameraFar;
uniform float maxDistance;
uniform float surfDist;
uniform mat4 cameraProjectionMatrix;
uniform mat4 cameraInverseProjectionMatrix;
uniform float thickTolerance;
uniform float noiseIntensity;
#include <packing>
float getDepth( const in vec2 uv ) {
return texture2D( tDepth, uv ).x;
}
float getViewZ( const in float depth ) {
#ifdef isPerspectiveCamera
return perspectiveDepthToViewZ( depth, cameraNear, cameraFar );
#else
return orthographicDepthToViewZ( depth, cameraNear, cameraFar );
#endif
}
vec3 getViewPosition( const in vec2 uv, const in float depth/*clip space*/, const in float clipW ) {
vec4 clipPosition = vec4( ( vec3( uv, depth ) - 0.5 ) * 2.0, 1.0 );//ndc
clipPosition *= clipW; //clip
return ( cameraInverseProjectionMatrix * clipPosition ).xyz;//view
}
vec3 getViewNormal( const in vec2 uv ) {
return unpackRGBToNormal( texture2D( tNormal, uv ).xyz );
}
vec2 viewPositionToXY(vec3 viewPosition){
vec2 xy;
vec4 clip=cameraProjectionMatrix*vec4(viewPosition,1);
xy=clip.xy;//clip
float clipW=clip.w;
xy/=clipW;//NDC
xy=(xy+1.)/2.;//uv
xy*=resolution;//screen
return xy;
}
vec3 hash3( float n ){
// http://glslsandbox.com/e#61476.1
return fract(sin(vec3(n,n+1.0,n+2.0))*vec3(43758.5453123,22578.1459123,19642.3490423));
}
void main(){
#ifdef isSelective
float metalness=texture2D(tMetalness,vUv).r;
if(metalness==0.) return;
#endif
float depth = getDepth( vUv );
float viewZ = getViewZ( depth );
if(-viewZ>=cameraFar) return;
float clipW = cameraProjectionMatrix[2][3] * viewZ+cameraProjectionMatrix[3][3];
vec3 viewPosition=getViewPosition( vUv, depth, clipW );
vec2 d0=gl_FragCoord.xy;
vec2 d1;
vec4 allResult;
for(int ni=0;ni<noiseOversample;ni++){
vec4 result;
vec3 viewNormal=getViewNormal( vUv );
#ifdef isNoise
viewNormal+=(hash3(viewPosition.x+viewPosition.y+viewPosition.z+float(ni))-.5)*noiseIntensity;
viewNormal=normalize(viewNormal);
#endif
vec3 viewReflectDir;
#ifdef isPerspectiveCamera
viewReflectDir=reflect(normalize(viewPosition),viewNormal);
#else
viewReflectDir=reflect(vec3(0,0,-1),viewNormal);
#endif
vec3 d1viewPosition=viewPosition+viewReflectDir*maxDistance;
#ifdef isPerspectiveCamera
if(d1viewPosition.z>-cameraNear){
//https://tutorial.math.lamar.edu/Classes/CalcIII/EqnsOfLines.aspx
vec3 n=normalize(d1viewPosition-viewPosition);
float t=(-cameraNear-viewPosition.z)/n.z;
d1viewPosition=viewPosition+n*t;
}
#endif
d1=viewPositionToXY(d1viewPosition);
float totalLen=length(d1-d0);
float xLen=d1.x-d0.x;
float yLen=d1.y-d0.y;
float totalStep=max(abs(xLen),abs(yLen));
float xSpan=xLen/totalStep;
float ySpan=yLen/totalStep;
for(float i=0.;i<MAX_STEP;i++){
if(i>=totalStep) break;
vec2 xy=vec2(d0.x+i*xSpan,d0.y+i*ySpan);
if(xy.x<0.||xy.x>resolution.x||xy.y<0.||xy.y>resolution.y) break;
float s=length(xy-d0)/totalLen;
vec2 uv=xy/resolution;
float d = getDepth(uv);
float vZ = getViewZ( d );
if(-vZ>=cameraFar) continue;
float cW = cameraProjectionMatrix[2][3] * vZ+cameraProjectionMatrix[3][3];
vec3 vP=getViewPosition( uv, d, cW );
#ifdef isPerspectiveCamera
// https://www.comp.nus.edu.sg/~lowkl/publications/lowk_persp_interp_techrep.pdf
float recipVPZ=1./viewPosition.z;
float viewReflectRayZ=1./(recipVPZ+s*(1./d1viewPosition.z-recipVPZ));
float sD=surfDist*cW;
#else
float viewReflectRayZ=viewPosition.z+s*(d1viewPosition.z-viewPosition.z);
float sD=surfDist;
#endif
#ifdef isInfiniteThick
if(viewReflectRayZ+thickTolerance*clipW<vP.z) break;
#endif
float away=abs(viewReflectRayZ-vZ);
float op=opacity;
#ifdef isDistanceAttenuation
float one_minus_s=1.-s;
float attenuation=one_minus_s*one_minus_s;
op=opacity*attenuation;
#endif
if(away<sD){
vec3 vN=getViewNormal( uv );
if(dot(viewReflectDir,vN)>=0.) continue;
vec4 reflectColor=texture2D(tDiffuse,uv);
result.xyz=reflectColor.xyz;
result.a=op;
allResult+=result;
break;
}
}
}
gl_FragColor=allResult/float(noiseOversample);
}
`
};
var SSRDepthShader = {
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 uv ) {",
" #if PERSPECTIVE_CAMERA == 1",
" float fragCoordZ = texture2D( tDepth, uv ).x;",
" float viewZ = perspectiveDepthToViewZ( fragCoordZ, cameraNear, cameraFar );",
" return viewZToOrthographicDepth( viewZ, cameraNear, cameraFar );",
" #else",
" return texture2D( tDepth, uv ).x;",
" #endif",
"}",
"void main() {",
" float depth = getLinearDepth( vUv );",
" gl_FragColor = vec4( vec3( 1.0 - depth ), 1.0 );",
"}"
].join("\n")
};
var SSRBlurShader = {
uniforms: {
"tDiffuse": { value: null },
"resolution": { value: new Vector2() },
"opacity": { value: .5 },
},
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 );",
" vec4 result = vec4(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 );",
" }",
" }",
" gl_FragColor = vec4( result / ( 25.0 ) ); // 25.0 = 5.0 * 5.0",
"}"
].join("\n")
};
export { SSRShader, SSRDepthShader, SSRBlurShader };