-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcomputeLight_fp.glsl
More file actions
296 lines (253 loc) · 11.3 KB
/
computeLight_fp.glsl
File metadata and controls
296 lines (253 loc) · 11.3 KB
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
/*
===========================================================================
Copyright (C) 2009-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
This file is part of XreaL source code.
XreaL source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
XreaL source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with XreaL source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// computeLight_fp.glsl - Light computing helper functions
#define COMPUTELIGHT_GLSL
#if !defined(USE_BSP_SURFACE)
#define USE_MODEL_SURFACE
#endif
#if !defined(USE_GRID_LIGHTING)
#define USE_LIGHT_MAPPING
#endif
#if defined(USE_REFLECTIVE_SPECULAR)
uniform samplerCube u_EnvironmentMap0;
uniform samplerCube u_EnvironmentMap1;
uniform float u_EnvironmentInterpolation;
#endif // USE_REFLECTIVE_SPECULAR
#if defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1
#if defined(HAVE_ARB_uniform_buffer_object)
struct light {
vec4 center_radius;
vec4 color_type;
vec4 direction_angle;
};
layout(std140) uniform u_Lights {
light lights[ MAX_REF_LIGHTS ];
};
#define GetLight(idx, component) lights[idx].component
#else // !HAVE_ARB_uniform_buffer_object
uniform sampler2D u_LightsTexture;
#define idxToTC( idx, w, h ) vec2( floor( ( idx * ( 1.0 / w ) ) + 0.5 ) * ( 1.0 / h ), \
fract( ( idx + 0.5 ) * (1.0 / w ) ) )
const struct GetLightOffsets {
int center_radius;
int color_type;
int direction_angle;
} getLightOffsets = GetLightOffsets(0, 1, 2);
#define GetLight(idx, component) texture2D( u_LightsTexture, idxToTC(3 * idx + getLightOffsets.component, 64.0, float( 3 * MAX_REF_LIGHTS / 64 ) ) )
#endif // HAVE_ARB_uniform_buffer_object
uniform int u_numLights;
#endif // defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1
// lighting helper functions
#if defined(USE_GRID_LIGHTING) || defined(USE_GRID_DELUXE_MAPPING)
void ReadLightGrid(in vec4 texel, out vec3 ambientColor, out vec3 lightColor) {
float ambientScale = 2.0 * texel.a;
float directedScale = 2.0 - ambientScale;
ambientColor = ambientScale * texel.rgb;
lightColor = directedScale * texel.rgb;
}
#endif
#if defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING) || (defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1)
uniform vec2 u_SpecularExponent;
#if defined(USE_REFLECTIVE_SPECULAR)
void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
vec4 diffuseColor, vec4 materialColor,
inout vec4 color, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
#else // !USE_REFLECTIVE_SPECULAR
void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
vec4 diffuseColor, vec4 materialColor,
inout vec4 color )
#endif // !USE_REFLECTIVE_SPECULAR
{
vec3 H = normalize( lightDir + viewDir );
#if defined(USE_PHYSICAL_MAPPING) || defined(r_specularMapping)
float NdotH = clamp( dot( normal, H ), 0.0, 1.0 );
#endif // USE_PHYSICAL_MAPPING || r_specularMapping
// clamp( NdotL, 0.0, 1.0 ) is done below
float NdotL = dot( normal, lightDir );
#if !defined(USE_BSP_SURFACE) && defined(r_halfLambertLighting)
// http://developer.valvesoftware.com/wiki/Half_Lambert
NdotL = NdotL * 0.5 + 0.5;
NdotL *= NdotL;
#endif
NdotL = clamp( NdotL, 0.0, 1.0 );
#if defined(USE_PHYSICAL_MAPPING)
// Daemon PBR packing defaults to ORM like glTF 2.0 defines
// https://www.khronos.org/blog/art-pipeline-for-gltf
// > ORM texture for Occlusion, Roughness, and Metallic
// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/material.pbrMetallicRoughness.schema.json
// > The metalness values are sampled from the B channel. The roughness values are sampled from the G channel.
// > These values are linear. If other channels are present (R or A), they are ignored for metallic-roughness calculations.
// https://docs.blender.org/manual/en/2.80/addons/io_scene_gltf2.html
// > glTF stores occlusion in the red (R) channel, allowing it to optionally share the same image
// > with the roughness and metallic channels.
float roughness = materialColor.g;
float metalness = materialColor.b;
float NdotV = clamp( dot( normal, viewDir ), 0.0, 1.0);
float VdotH = clamp( dot( viewDir, H ), 0.0, 1.0);
float alpha = roughness * roughness;
float k = 0.125 * (roughness + 1.0) * (roughness + 1.0);
float D = alpha / ((NdotH * NdotH) * (alpha * alpha - 1.0) + 1.0);
D *= D;
float FexpNH = pow(1.0 - NdotH, 5.0);
float FexpNV = pow(1.0 - NdotV, 5.0);
vec3 F = mix(vec3(0.04), diffuseColor.rgb, metalness);
F = F + (1.0 - F) * FexpNH;
float G = NdotL / (NdotL * (1.0 - k) + k);
G *= NdotV / (NdotV * (1.0 - k) + k);
vec3 diffuseBRDF = NdotL * diffuseColor.rgb * (1.0 - metalness);
vec3 specularBRDF = vec3((D * F * G) / max(4.0 * NdotL * NdotV, 0.0001f));
color.rgb += (diffuseBRDF + specularBRDF) * lightColor.rgb * NdotL;
color.a = mix(diffuseColor.a, 1.0, FexpNV);
#else // !USE_PHYSICAL_MAPPING
#if defined(USE_REFLECTIVE_SPECULAR)
// not implemented for PBR yet
vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect(-viewDir, normal));
vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect(-viewDir, normal));
materialColor.rgb *= mix(envColor0, envColor1, u_EnvironmentInterpolation).rgb;
#endif // USE_REFLECTIVE_SPECULAR
color.rgb += lightColor.rgb * NdotL * diffuseColor.rgb;
#if defined(r_specularMapping)
// The minimal specular exponent should preferably be nonzero to avoid the undefined pow(0, 0)
color.rgb += lightColor.rgb * materialColor.rgb * pow( NdotH, u_SpecularExponent.x * materialColor.a + u_SpecularExponent.y) * r_SpecularScale;
#endif // r_specularMapping
#endif // !USE_PHYSICAL_MAPPING
}
#endif // defined(USE_DELUXE_MAPPING) || defined(USE_GRID_DELUXE_MAPPING) || (defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1)
#if !defined(USE_DELUXE_MAPPING) && !defined(USE_GRID_DELUXE_MAPPING)
void computeLight(in vec3 lightColor, vec4 diffuseColor, inout vec4 color) {
color.rgb += lightColor.rgb * diffuseColor.rgb;
}
#endif // !defined(USE_DELUXE_MAPPING) && !defined(USE_GRID_DELUXE_MAPPING)
#if defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1
#if defined(HAVE_EXT_texture_integer) && defined(r_highPrecisionRendering)
const int lightsPerLayer = 16;
#define lightTilesSampler_t usampler3D
#define lightTilesUniform u_LightTilesInt
#define idxs_t uvec4
idxs_t fetchIdxs( in vec3 coords, in lightTilesSampler_t lightTilesUniform ) {
return texture3D( lightTilesUniform, coords );
}
int nextIdx( inout idxs_t idxs ) {
uvec4 tmp = ( idxs & uvec4( 3 ) ) * uvec4( 0x40, 0x10, 0x04, 0x01 );
idxs = idxs >> 2;
return int( tmp.x + tmp.y + tmp.z + tmp.w );
}
#else // !HAVE_EXT_texture_integer || !r_highPrecisionRendering
const int lightsPerLayer = 4;
#define lightTilesSampler_t sampler3D
#define lightTilesUniform u_LightTiles
#define idxs_t vec4
idxs_t fetchIdxs( in vec3 coords, in lightTilesSampler_t lightTilesUniform ) {
return texture3D( lightTilesUniform, coords ) * 255.0;
}
int nextIdx( inout idxs_t idxs ) {
vec4 tmp = idxs;
idxs = floor(idxs * 0.25);
tmp -= 4.0 * idxs;
return int( dot( tmp, vec4( 64.0, 16.0, 4.0, 1.0 ) ) );
}
#endif // !HAVE_EXT_texture_integer || !r_highPrecisionRendering
uniform lightTilesSampler_t lightTilesUniform;
const int numLayers = MAX_REF_LIGHTS / 256;
#if defined(USE_REFLECTIVE_SPECULAR)
void computeDynamicLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
vec4 material, inout vec4 color, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
#else // !USE_REFLECTIVE_SPECULAR
void computeDynamicLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
vec4 material, inout vec4 color )
#endif // !USE_REFLECTIVE_SPECULAR
{
vec4 center_radius = GetLight( idx, center_radius );
vec4 color_type = GetLight( idx, color_type );
vec3 L;
float attenuation;
if( color_type.w == 0.0 ) {
// point light
L = center_radius.xyz - P;
// 2.57 ~= 8.0 ^ ( 1.0 / 2.2 ), adjusted after overbright changes
float t = 1.0 + 2.57 * length(L) / center_radius.w;
// Quadratic attenuation function instead of linear because of overbright changes
attenuation = 1.0 / ( t * t );
L = normalize(L);
} else if( color_type.w == 1.0 ) {
// spot light
vec4 direction_angle = GetLight( idx, direction_angle );
L = center_radius.xyz - P;
// 2.57 ~= 8.0 ^ ( 1.0 / 2.2 ), adjusted after overbright changes
float t = 1.0 + 2.57 * length(L) / center_radius.w;
// Quadratic attenuation function instead of linear because of overbright changes
attenuation = 1.0 / ( t * t );
L = normalize( L );
if( dot( L, direction_angle.xyz ) <= direction_angle.w ) {
attenuation = 0.0;
}
} else if( color_type.w == 2.0 ) {
// sun (directional) light
L = GetLight( idx, direction_angle ).xyz;
attenuation = 1.0;
}
#if defined(USE_REFLECTIVE_SPECULAR)
computeDeluxeLight( L, normal, viewDir,
attenuation * attenuation * color_type.xyz,
diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1 );
#else // !USE_REFLECTIVE_SPECULAR
computeDeluxeLight( L, normal, viewDir,
attenuation * attenuation * color_type.xyz,
diffuse, material, color );
#endif // !USE_REFLECTIVE_SPECULAR
}
#if defined(USE_REFLECTIVE_SPECULAR)
void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
inout vec4 color, in lightTilesSampler_t lightTilesUniform,
in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
#else // !USE_REFLECTIVE_SPECULAR
void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
inout vec4 color, in lightTilesSampler_t lightTilesUniform )
#endif // !USE_REFLECTIVE_SPECULAR
{
vec2 tile = floor( gl_FragCoord.xy * (1.0 / float( TILE_SIZE ) ) ) + 0.5;
vec3 tileScale = vec3( r_tileStep, 1.0/numLayers );
#if defined(r_showLightTiles)
float numLights = 0.0;
#endif
for( int layer = 0; layer < numLayers; layer++ ) {
idxs_t idxs = fetchIdxs( tileScale * vec3( tile, float( layer ) + 0.5 ), lightTilesUniform );
for( int i = 0; i < lightsPerLayer; i++ ) {
int idx = numLayers * nextIdx( idxs ) + layer;
if( idx >= u_numLights )
{
break;
}
#if defined(USE_REFLECTIVE_SPECULAR)
computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1 );
#else // !USE_REFLECTIVE_SPECULAR
computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color );
#endif // !USE_REFLECTIVE_SPECULAR
#if defined(r_showLightTiles)
numLights++;
#endif
}
}
#if defined(r_showLightTiles)
if (numLights > 0.0)
{
color = vec4(numLights/(lightsPerLayer*numLayers), numLights/(lightsPerLayer*numLayers), numLights/(lightsPerLayer*numLayers), 1.0);
}
#endif
}
#endif // defined(r_realtimeLighting) && r_realtimeLightingRenderer == 1