summaryrefslogtreecommitdiff
path: root/src/rend2/glsl/lightall_fp.glsl
blob: 186ea4b0e6b36f539a0ece6729ab2cc77f9251aa (plain)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
uniform sampler2D u_DiffuseMap;

#if defined(USE_LIGHTMAP)
uniform sampler2D u_LightMap;
#endif

#if defined(USE_NORMALMAP)
uniform sampler2D u_NormalMap;
#endif

#if defined(USE_DELUXEMAP)
uniform sampler2D u_DeluxeMap;
#endif

#if defined(USE_SPECULARMAP)
uniform sampler2D u_SpecularMap;
#endif

#if defined(USE_SHADOWMAP)
uniform sampler2D u_ShadowMap;
#endif

uniform vec3      u_ViewOrigin;

#if defined(USE_TCGEN)
uniform int    u_TCGen0;
#endif

#if defined(USE_LIGHT_VECTOR)
uniform vec3      u_DirectedLight;
uniform vec3      u_AmbientLight;
uniform float     u_LightRadius;
#endif

#if defined(USE_LIGHT)
uniform vec2      u_MaterialInfo;
#endif

varying vec2      var_DiffuseTex;
#if defined(USE_LIGHTMAP)
varying vec2      var_LightTex;
#endif
varying vec4      var_Color;

#if defined(USE_NORMALMAP) && !defined(USE_VERT_TANGENT_SPACE)
varying vec3      var_Position;
#endif

varying vec3      var_SampleToView;

#if !defined(USE_FAST_LIGHT)
varying vec3      var_Normal;
#endif

#if defined(USE_VERT_TANGENT_SPACE)
varying vec3      var_Tangent;
varying vec3      var_Bitangent;
#endif

varying vec3      var_VertLight;

#if defined(USE_LIGHT) && !defined(USE_DELUXEMAP)
varying vec3      var_WorldLight;
#endif

#define EPSILON 0.00000001

#if defined(USE_PARALLAXMAP)
float SampleHeight(sampler2D normalMap, vec2 t)
{
  #if defined(SWIZZLE_NORMALMAP)
	return texture2D(normalMap, t).r;
  #else
	return texture2D(normalMap, t).a;
  #endif
}

float RayIntersectDisplaceMap(vec2 dp, vec2 ds, sampler2D normalMap)
{
	const int linearSearchSteps = 16;
	const int binarySearchSteps = 6;

	float depthStep = 1.0 / float(linearSearchSteps);

	// current size of search window
	float size = depthStep;

	// current depth position
	float depth = 0.0;

	// best match found (starts with last position 1.0)
	float bestDepth = 1.0;

	// search front to back for first point inside object
	for(int i = 0; i < linearSearchSteps - 1; ++i)
	{
		depth += size;
		
		float t = 1.0 - SampleHeight(normalMap, dp + ds * depth);
		
		if(bestDepth > 0.996)		// if no depth found yet
			if(depth >= t)
				bestDepth = depth;	// store best depth
	}

	depth = bestDepth;
	
	// recurse around first point (depth) for closest match
	for(int i = 0; i < binarySearchSteps; ++i)
	{
		size *= 0.5;

		float t = 1.0 - SampleHeight(normalMap, dp + ds * depth);
		
		if(depth >= t)
		{
			bestDepth = depth;
			depth -= 2.0 * size;
		}

		depth += size;
	}

	return bestDepth;
}
#endif

float CalcDiffuse(vec3 N, vec3 L, vec3 E, float NE, float NL, float fzero, float shininess)
{
  #if defined(USE_OREN_NAYAR) || defined(USE_TRIACE_OREN_NAYAR)
	float gamma = dot(E, L) - NE * NL;
	float B = 2.22222 + 0.1 * shininess;
		
	#if defined(USE_OREN_NAYAR)
	float A = 1.0 - 1.0 / (2.0 + 0.33 * shininess);
	gamma = clamp(gamma, 0.0, 1.0);
	#endif
	
	#if defined(USE_TRIACE_OREN_NAYAR)
	float A = 1.0 - 1.0 / (2.0 + 0.65 * shininess);

	if (gamma >= 0.0)
	#endif
	{
		B *= max(max(NL, NE), EPSILON);
	}

	return (A + gamma / B) * (1.0 - fzero);
  #else
	return 1.0 - fzero;
  #endif
}

#if defined(USE_SPECULARMAP)
float CalcSpecular(float NH, float NL, float NE, float EH, float fzero, float shininess)
{
  #if defined(USE_BLINN) || defined(USE_TRIACE) || defined(USE_TORRANCE_SPARROW)
	float blinn = pow(NH, shininess);
  #endif

  #if defined(USE_BLINN)
	return blinn;
  #endif

  #if defined(USE_COOK_TORRANCE) || defined (USE_TRIACE) || defined (USE_TORRANCE_SPARROW)
	float fresnel = fzero + (1.0 - fzero) * pow(1.0 - EH, 5);
  #endif

  #if defined(USE_COOK_TORRANCE) || defined(USE_TORRANCE_SPARROW)
	float geo = 2.0 * NH * min(NE, NL);
	geo /= max(EH, geo);
  #endif  

  #if defined(USE_COOK_TORRANCE)
	float m_sq = 2.0 / max(shininess, EPSILON);
	float NH_sq = NH * NH;
	float m_NH_sq = m_sq * NH_sq;
	float beckmann = exp((NH_sq - 1.0) / max(m_NH_sq, EPSILON)) / max(4.0 * m_NH_sq * NH_sq, EPSILON);

	return fresnel * geo * beckmann / max(NE, EPSILON);
  #endif

  #if defined(USE_TRIACE)
	float scale = 0.1248582 * shininess + 0.2691817;

	return fresnel * scale * blinn / max(max(NL, NE), EPSILON);
  #endif
  
  #if defined(USE_TORRANCE_SPARROW)
	float scale = 0.125 * shininess + 1.0;

	return fresnel * geo * scale * blinn / max(NE, EPSILON);
  #endif
}
#endif

void main()
{
#if !defined(USE_FAST_LIGHT) && (defined(USE_LIGHT) || defined(USE_NORMALMAP))
	vec3 surfNormal = normalize(var_Normal);
#endif

#if defined(USE_DELUXEMAP)
	vec3 worldLight = 2.0 * texture2D(u_DeluxeMap, var_LightTex).xyz - vec3(1.0);
	//worldLight += var_WorldLight * 0.0001;
#elif defined(USE_LIGHT)
	vec3 worldLight = var_WorldLight;
#endif

#if defined(USE_LIGHTMAP)
	vec4 lightSample = texture2D(u_LightMap, var_LightTex).rgba;
  #if defined(RGBE_LIGHTMAP)
	lightSample.rgb *= exp2(lightSample.a * 255.0 - 128.0);
  #endif
	vec3 directedLight = lightSample.rgb;
#elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
  #if defined(USE_INVSQRLIGHT)
	float intensity = 1.0 / dot(worldLight, worldLight);
  #else
	float intensity = clamp((1.0 - dot(worldLight, worldLight) / (u_LightRadius * u_LightRadius)) * 1.07, 0.0, 1.0);
  #endif

	vec3 directedLight = u_DirectedLight * intensity;
	vec3 ambientLight  = u_AmbientLight;

  #if defined(USE_SHADOWMAP)
	vec2 shadowTex = gl_FragCoord.xy * r_FBufScale;
	directedLight *= texture2D(u_ShadowMap, shadowTex).r;
  #endif
#elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
	vec3 directedLight = var_VertLight;
#endif
	
#if defined(USE_TCGEN) || defined(USE_NORMALMAP) || (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT))
	vec3 SampleToView = normalize(var_SampleToView);
#endif
	vec2 tex = var_DiffuseTex;

	float ambientDiff = 1.0;

#if defined(USE_NORMALMAP)
  #if defined(USE_VERT_TANGENT_SPACE)
    vec3   tangent = var_Tangent;
	vec3 bitangent = var_Bitangent;
  #else
	vec3 q0  = dFdx(var_Position);
	vec3 q1  = dFdy(var_Position);
	vec2 st0 = dFdx(tex);
	vec2 st1 = dFdy(tex);
	float dir = sign(st1.t * st0.s - st0.t * st1.s);

	vec3   tangent = normalize( q0 * st1.t - q1 * st0.t) * dir;
	vec3 bitangent = -normalize( q0 * st1.s - q1 * st0.s) * dir;
  #endif

	mat3 tangentToWorld = mat3(tangent, bitangent, var_Normal);

  #if defined(USE_PARALLAXMAP)
	vec3 offsetDir = normalize(SampleToView * tangentToWorld);
    #if 0
    float height = SampleHeight(u_NormalMap, tex);
	float pdist = 0.05 * height - (0.05 / 2.0);
    #else
	offsetDir.xy *= -0.05 / offsetDir.z;
	float pdist = RayIntersectDisplaceMap(tex, offsetDir.xy, u_NormalMap);
    #endif	
	tex += offsetDir.xy * pdist;
  #endif
  #if defined(SWIZZLE_NORMALMAP)
	vec3 normal = 2.0 * texture2D(u_NormalMap, tex).agb - 1.0;
  #else
	vec3 normal = 2.0 * texture2D(u_NormalMap, tex).rgb - 1.0;
  #endif
	normal.z = sqrt(clamp(1.0 - dot(normal.xy, normal.xy), 0.0, 1.0));
	vec3 worldNormal = tangentToWorld * normal;
  #if defined(r_normalAmbient)
	ambientDiff = 0.781341 * normal.z + 0.218659;
  #endif
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
	vec3 worldNormal = surfNormal;
#endif

#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || (defined(USE_TCGEN) && defined(USE_NORMALMAP))
	worldNormal = normalize(worldNormal);
#endif

#if defined(USE_TCGEN) && defined(USE_NORMALMAP)
	if (u_TCGen0 == TCGEN_ENVIRONMENT_MAPPED)
	{
		tex = -reflect(normalize(SampleToView), worldNormal).yz * vec2(0.5, -0.5) + 0.5;
	}
#endif

	vec4 diffuse = texture2D(u_DiffuseMap, tex);

#if defined(USE_LIGHT) && defined(USE_FAST_LIGHT)
  #if defined(USE_LIGHTMAP)
	diffuse.rgb *= directedLight;
  #endif
#elif defined(USE_LIGHT)
	worldLight = normalize(worldLight);

	float surfNL = clamp(dot(surfNormal,  worldLight),   0.0, 1.0);

  #if defined(USE_LIGHTMAP) || defined(USE_LIGHT_VERTEX)
    #if defined(USE_STANDARD_DELUXEMAP)
	// Standard deluxe mapping treats the light sample as fully directed
	// and doesn't compensate for light angle attenuation.
	vec3 ambientLight = vec3(0.0);
    #else
	// Separate the light sample into directed and ambient parts.
	//
	// ambientMax  - if the cosine of the angle between the surface
	//               normal and the light is below this value, the light
	//               is fully ambient.
	// directedMax - if the cosine of the angle between the surface
	//               normal and the light is above this value, the light
	//               is fully directed.
	const float ambientMax  = 0.25;
	const float directedMax = 0.5;

	float directedScale = clamp((surfNL - ambientMax) / (directedMax - ambientMax), 0.0, 1.0);
	
	// Scale the directed portion to compensate for the baked-in
	// light angle attenuation.
	directedScale /= max(surfNL, ambientMax);
	
      #if defined(r_normalAmbient)
	directedScale *= 1.0 - r_normalAmbient;
      #endif

	// Recover any unused light as ambient
	vec3 ambientLight = directedLight;
	directedLight *= directedScale;
	ambientLight -= directedLight * surfNL;
    #endif
  #endif
	
	float NL = clamp(dot(worldNormal,  worldLight),   0.0, 1.0);
	float NE = clamp(dot(worldNormal,  SampleToView), 0.0, 1.0);

	float fzero = u_MaterialInfo.x;
	float shininess = u_MaterialInfo.y;

  #if defined(USE_SPECULARMAP)
	vec4 specular = texture2D(u_SpecularMap, tex);
	//specular.rgb = clamp(specular.rgb - diffuse.rgb, 0.0, 1.0);
	shininess *= specular.a;
  #endif

	float directedDiff = NL * CalcDiffuse(worldNormal, worldLight, SampleToView, NE, NL, fzero, shininess);
	diffuse.rgb *= directedLight * directedDiff + ambientDiff * ambientLight;
  
  #if defined(USE_SPECULARMAP)
	vec3 halfAngle = normalize(worldLight + SampleToView);

	float EH = clamp(dot(SampleToView, halfAngle), 0.0, 1.0);
	float NH = clamp(dot(worldNormal,  halfAngle), 0.0, 1.0);

	float directedSpec = NL * CalcSpecular(NH, NL, NE, EH, fzero, shininess);
  
    #if defined(r_normalAmbient)
	vec3 ambientHalf = normalize(surfNormal + SampleToView);
	float ambientSpec = max(dot(ambientHalf, worldNormal) + 0.5, 0.0);
	ambientSpec *= ambientSpec * 0.44;
	ambientSpec = pow(ambientSpec, shininess) * fzero;
	specular.rgb *= directedSpec * directedLight + ambientSpec * ambientLight;
    #else
	specular.rgb *= directedSpec * directedLight;
    #endif
  #endif
#endif

	gl_FragColor = diffuse;

#if defined(USE_SPECULARMAP) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
	gl_FragColor.rgb += specular.rgb;
#endif

	gl_FragColor *= var_Color;
}