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
|
[require]
GLSL >= 1.20
[vertex shader]
#version 120
#extension GL_ARB_texture_rectangle : enable
#define flat
#define texture2DRectOffset(sampler, coords, offset) texture2DRect(sampler, coords + vec2(offset))
#define shadow2DRectOffset(sampler, coords, offset) shadow2DRect(sampler, coords + vec2(offset))
attribute vec4 vvertex;
uniform mat4 lightmatrix;
void main(void)
{
gl_Position = lightmatrix * vvertex;
}
[fragment shader]
#version 120
#extension GL_ARB_texture_rectangle : enable
#define flat
#define texture2DRectOffset(sampler, coords, offset) texture2DRect(sampler, coords + vec2(offset))
#define shadow2DRectOffset(sampler, coords, offset) shadow2DRect(sampler, coords + vec2(offset))
#define fragdata(loc, name, type)
#define fragcolor gl_FragData[0]
uniform sampler2DRect tex0, tex1, tex3 , tex2;
uniform sampler2DRectShadow tex4;
uniform vec3 splitcenter[3];
uniform vec3 splitbounds[3];
uniform vec3 splitscale[3];
uniform vec3 splitoffset[3];
uniform mat3 csmmatrix;
uniform vec3 sunlightdir;
uniform vec3 sunlightcolor;
uniform vec3 skylightcolor;
uniform float giscale, rhnudge;
uniform vec4 rhbb[2];
uniform vec3 rhscale[2];
uniform vec3 rhoffset[2];
uniform sampler3D tex6, tex7, tex8, tex9;
uniform vec3 camera;
uniform mat4 worldmatrix;
uniform vec4 fogdir;
uniform vec3 fogcolor;
uniform vec2 fogdensity;
uniform vec4 radialfogscale;
uniform vec2 shadowatlasscale;
uniform vec4 lightscale;
uniform sampler2DRect tex5; uniform vec2 aoscale; uniform vec4 aoparams;
uniform vec3 gdepthscale;
uniform vec3 gdepthunpackparams;
fragdata(0, fragcolor, vec4)
#define shadowval(center, xoff, yoff) float(shadow2DRect(tex4, center + vec3(xoff, yoff, 0.0)))
float filtershadow(vec3 shadowtc)
{
vec2 offset = fract(shadowtc.xy - 0.5);
vec3 center = shadowtc;
center.xy -= offset*0.5;
vec4 size = vec4(offset + 1.0, 2.0 - offset);
return (1.0/9.0)*dot(size.zxzx*size.wwyy,
vec4(shadowval(center, -0.5, -0.5),
shadowval(center, 1.0, -0.5),
shadowval(center, -0.5, 1.0),
shadowval(center, 1.0, 1.0)));
}
vec3 getcsmtc(vec3 pos, float distbias)
{
pos = csmmatrix * pos;
pos.z -= distbias;
if(all(lessThan(abs(pos - splitcenter[0]), splitbounds[0])))
pos = pos*splitscale[0] + splitoffset[0];
else
if(all(lessThan(abs(pos - splitcenter[1]), splitbounds[1])))
pos = pos*splitscale[1] + splitoffset[1];
else
if(all(lessThan(abs(pos.xy - splitcenter[2].xy), splitbounds[2].xy)))
pos = pos*splitscale[2] + splitoffset[2];
else pos = vec3(-1.0);
return pos;
}
vec4 getrhlight(vec3 pos, vec3 norm)
{
vec3 tc;
pos += norm*rhnudge;
if(all(lessThan(abs(pos - rhbb[0].xyz), vec3(rhbb[0].w))))
tc = pos*rhscale[0] + rhoffset[0];
else
if(all(lessThan(abs(pos - rhbb[1].xyz), vec3(rhbb[1].w))))
tc = pos*rhscale[1] + rhoffset[1];
else
tc = vec3(-1.0);
vec4 shr = texture3D(tex6, tc), shg = texture3D(tex7, tc), shb = texture3D(tex8, tc), sha = texture3D(tex9, tc);
shr.rgb -= 0.5;
shg.rgb -= 0.5;
shb.rgb -= 0.5;
sha.rgb -= 0.5;
vec4 basis = vec4(norm*-(1.023326*0.488603/3.14159*2.0), (0.886226*0.282095/3.14159));
return clamp(vec4(dot(basis, shr), dot(basis, shg), dot(basis, shb), min(dot(basis, sha), norm.z + 1.0)), 0.0, 1.0);
}
void main(void)
{
#define gfetch(sampler, coords) texture2DRect(sampler, coords)
#define accumlight(light) fragcolor.rgb = light
#define accumalpha(alpha) fragcolor.a = alpha
vec4 normal = gfetch(tex1, gl_FragCoord.xy);
normal.xyz = normal.xyz*2.0 - 1.0;
#define alpha normal.a
vec4 diffuse = gfetch(tex0, gl_FragCoord.xy);
vec3 glow = gfetch(tex2, gl_FragCoord.xy).rgb;
#define depthvals gfetch(tex3, gl_FragCoord.xy)
float depth = depthvals.r;
vec4 pos = worldmatrix * vec4(gl_FragCoord.xy, depth, 1.0);
pos.xyz /= pos.w;
float fogcoord = length(camera - pos.xyz);
vec3 light = lightscale.rgb;
vec4 rhlight = getrhlight(pos.xyz, normal.xyz);
light += rhlight.a * skylightcolor;
light *= diffuse.rgb;
float ao = texture2DRect(tex5, gl_FragCoord.xy*aoscale).r;
light *= aoparams.x + ao*aoparams.y;
light += glow * lightscale.a;
vec3 sunlight = rhlight.rgb * giscale * diffuse.rgb;
float sunfacing = dot(sunlightdir, normal.xyz);
if(sunfacing > 0.0)
{
#define distbias 0.0
vec3 csmtc = getcsmtc(pos.xyz, distbias);
float sunoccluded = sunfacing * filtershadow(csmtc);
vec3 camdir = normalize(camera - pos.xyz);
float facing = 2.0*dot(normal.xyz, camdir);
float sunspec = pow(clamp(sunfacing*facing - dot(camdir, sunlightdir), 0.0, 1.0), 16.0) * diffuse.a;
sunlight += (diffuse.rgb + sunspec) * sunoccluded;
}
sunlight *= aoparams.z + ao*aoparams.w;
light += sunlight * sunlightcolor;
float foglerp = clamp(exp2(fogcoord*fogdensity.x)*fogdensity.y, 0.0, 1.0);
accumlight(mix(fogcolor*alpha, light, foglerp));
accumalpha(alpha);
}
|