/
process.glsl
158 lines (102 loc) · 3.68 KB
/
process.glsl
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
#include "Uniforms.glsl"
#include "Samplers.glsl"
#include "Transform.glsl"
#include "ScreenPos.glsl"
#ifdef GL_ES
#extension GL_OES_standard_derivatives : enable
precision highp float;
#endif
//varying vec4 vScreenPos;
varying vec2 vScreenPos;
varying vec3 vFarRay;
#ifdef COMPILEVS
vec3 GetViewFarRay(vec4 clipPos)
{
return vec3(cFrustumSize.x * clipPos.x,
cFrustumSize.y * clipPos.y,
cFrustumSize.z);
}
void VS()
{
mat4 modelMatrix = iModelMatrix;
vec3 worldPos = GetWorldPos(modelMatrix);
gl_Position = GetClipPos(worldPos);
//vScreenPos = GetScreenPos(gl_Position);
vScreenPos = GetScreenPosPreDiv(gl_Position);
vFarRay = GetViewFarRay(gl_Position);
}
#endif
#ifdef COMPILEPS
#ifdef GL_ES
uniform sampler2D sDepthBuffer;
#endif
//#define NUM_SAMPLES 9.0
//#define NUM_SPIRAL_TURNS 7.0
#define NUM_SAMPLES 4.0
#define NUM_SPIRAL_TURNS 4.0
uniform vec3 cFrustumSize;
uniform vec4 cGBufferOffsets;
uniform vec4 cProjInfo;
uniform mat3 cView;
uniform float cNoiseMult;
uniform float cProjScale;
uniform float cProjScale2;
uniform float cRadius;
uniform float cBias;
uniform float cIntensityDivR6;
vec3 GetViewPosition(vec2 ssPos, float depth)
{
// eye_z = depth(0=camera .. 1=far) * far
float eye_z = depth * cFrustumSize.z;
return vec3(ssPos * cProjInfo.xy + cProjInfo.zw, 1.0) * eye_z;
}
float rand(vec2 co)
{
return fract(sin(dot(co, vec2(12.9898,78.233))) * 43758.5453);
}
void PS(){
vec4 color = vec4(0.0,0.0,0.0,0.0);
//vec3 edC = texture2D(sDiffMap, vScreenPos.xy / vScreenPos.w).rgb;
vec3 edC = texture2D(sDiffMap, vScreenPos).rgb;
//vec3 edC = texture2D(sDepthBuffer, vScreenPos.xy / vScreenPos.w).rgb;
float depthC = DecodeDepth(edC);
//vec3 vsC = GetViewPosition(vScreenPos.xy / vScreenPos.w, depthC);
vec3 vsC = GetViewPosition(vScreenPos, depthC);
//normals
vec3 vsN = normalize( cross(dFdy(vsC), dFdx(vsC)) );
//test
float intensity = cIntensityDivR6;
float randomAngle = fract( pow(vScreenPos.x, vScreenPos.y) * 43758.5453 ) / cGBufferInvSize.x;
//vec3 normal = DecodeNormal(texture2D(sNormalMap, vScreenPos.xy / vScreenPos.w));
//vec3 normal = DecodeNormal(texture2D(sNormalMap, vScreenPos));
//vec3 vsN = cView * normal;
float ssDiskRadius = cProjScale2 * cProjScale * cRadius / vsC.z;
float sum = 0.0;
float radius2 = cRadius * cRadius;
const float epsilon = 0.01;
for (float i = 0.0; i < NUM_SAMPLES; ++i)
{
float alpha = (i + 0.5) * (1.0 / NUM_SAMPLES);
float angle = randomAngle + alpha * (NUM_SPIRAL_TURNS * 6.28);
vec2 ssOffset = floor( alpha * ssDiskRadius * vec2(cos(angle), sin(angle)) );
//vec2 ssP = (vScreenPos.xy / vScreenPos.w) + ssOffset * cGBufferInvSize;
vec2 ssP = vScreenPos + ssOffset * cGBufferInvSize;
float depthP = DecodeDepth(texture2D(sDiffMap, ssP).rgb);
vec3 vsP = GetViewPosition(ssP, depthP);
//vec3 vsP = GetViewPosition(ssP, depthC);
vec3 v = vsP - vsC;
float vv = dot(v, v);
float vn = dot(v, vsN);
float f = max(radius2 - vv, 0.0);
sum += f * f * f * max((vn - cBias) / (epsilon + vv), 0.0);
}
float occlusion = max(0.0, 1.0 - sum * intensity * (5.0 / NUM_SAMPLES));
//test end
//vec4 diffuse = texture2D(sDiffMap,vScreenPos.xy / vScreenPos.w);
//vec4 diffuse = texture2D(sDepthBuffer,vScreenPos.xy / vScreenPos.w);
//gl_FragColor = vec4(vsN,1.0) * occlusion;
gl_FragColor = vec4(1,1,1,1.0) * occlusion;
//gl_FragColor = vec4(depthC,0,0,1);
//gl_FragColor = texture2D(sNormalMap, vScreenPos.xy / vScreenPos.w);
}
#endif