aboutsummaryrefslogtreecommitdiff
path: root/KaplaDemo/externalIP/resources/shaders/combine_fs.cpp
blob: 98160e6ab30ac454e1608265be6b5704b6959427 (plain) (blame)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
uniform sampler3D ttt3D;

	uniform float extraNoiseScale = 1.0f;
	uniform float noiseScale = 0.03f;
	float noise(float p) {
		return texture3D(ttt3D, vec3(p*noiseScale*extraNoiseScale, 0.5, 0.5)).x;
	}

	float noise(float p, float q) {
		return texture3D(ttt3D, vec3(p*noiseScale*extraNoiseScale, q*noiseScale*extraNoiseScale, 0.5)).x;
	}

	float snoise(float p) {
		return noise(p)*2.0f - 1.0f;
	}
	float snoise(float p, float q) {
		return noise(p, q)*2.0f - 1.0f;
	}
	float boxstep(float a, float b, float x) {
		return (clamp(((x)-(a)) / ((b)-(a)), 0, 1));

	}

	uniform float Ka = 1;
	uniform float Kd = 0.75;
	uniform float Ks = 0.15;
	uniform float roughness = 0.025;
	uniform vec3 specularcolor = vec3(1, 1, 1);
	uniform float ringscale = 0;
	uniform float grainscale = 0;
	uniform float txtscale = 1;
	uniform float plankspertile = 4;
	uniform vec3 lightwood = vec3(0.57, 0.292, 0.125);
	uniform vec3 darkwood = vec3(0.275, 0.15, 0.06);
	uniform vec3 groovecolor = vec3(.05, .04, .015);
	//uniform float plankwidth = .05;
	uniform float plankwidth = .2;
	uniform float groovewidth = 0.001;
	uniform float plankvary = 0.8;
	uniform float grainy = 1;
	uniform float wavy = 0.08;
	uniform float MINFILTERWIDTH = 1.0e-7;

	vec3 myTexture3D_0(vec3 p)
	{		
		float r;
		float r2;
		float whichrow;
		float whichplank;
		float swidth;
		float twidth;
		float fwidth;
		float ss;
		float tt;
		float w;
		float h;
		float fade;
		float ttt;
		vec3 Ct;
		vec3 woodcolor;
		float groovy;
		float PGWIDTH;
		float PGHEIGHT;
		float GWF;
		float GHF;
		float tilewidth;
		float whichtile;
		float tmp;
		float planklength;


		PGWIDTH = plankwidth + groovewidth;
		planklength = PGWIDTH * plankspertile - groovewidth;

		PGHEIGHT = planklength + groovewidth;
		GWF = groovewidth*0.5 / PGWIDTH;
		GHF = groovewidth*0.5 / PGHEIGHT;

		// Determine how wide in s-t space one pixel projects to 
		float s = p.x;
		float t = p.y;
		float du = 1.0;
		float dv = 1.0;

		swidth = (max(abs(dFdx(s)*du) + abs(dFdy(s)*dv), MINFILTERWIDTH) /
			PGWIDTH) * txtscale;
		twidth = (max(abs(dFdx(t)*du) + abs(dFdy(t)*dv), MINFILTERWIDTH) /
			PGHEIGHT) * txtscale;
		fwidth = max(swidth, twidth);

		ss = (txtscale * s) / PGWIDTH;
		whichrow = floor(ss);
		tt = (txtscale * t) / PGHEIGHT;
		whichplank = floor(tt);

		if (mod(whichrow / plankspertile + whichplank, 2) >= 1) {
			ss = txtscale * t / PGWIDTH;
			whichrow = floor(ss);
			tt = txtscale * s / PGHEIGHT;
			whichplank = floor(tt);
			tmp = swidth;  swidth = twidth;  twidth = tmp;
		}
		ss -= whichrow;
		tt -= whichplank;
		whichplank += 20 * (whichrow + 10);

		if (swidth >= 1)
			w = 1 - 2 * GWF;
		else w = clamp(boxstep(GWF - swidth, GWF, ss), max(1 - GWF / swidth, 0), 1)
			- clamp(boxstep(1 - GWF - swidth, 1 - GWF, ss), 0, 2 * GWF / swidth);
		if (twidth >= 1)
			h = 1 - 2 * GHF;
		else h = clamp(boxstep(GHF - twidth, GHF, tt), max(1 - GHF / twidth, 0), 1)
			- clamp(boxstep(1 - GHF - twidth, 1 - GHF, tt), 0, 2 * GHF / twidth);
		// This would be the non-antialiased version:
		//w = step (GWF,ss) - step(1-GWF,ss);
		//h = step (GHF,tt) - step(1-GHF,tt);

		groovy = w*h;



		// Add the ring patterns
		fade = smoothstep(1 / ringscale, 8 / ringscale, fwidth);
		if (fade < 0.999) {

			ttt = tt / 4 + whichplank / 28.38 + wavy * noise(8 * ss, tt / 4);
			r = ringscale * noise(ss - whichplank, ttt);
			r -= floor(r);
			r = 0.3 + 0.7*smoothstep(0.2, 0.55, r)*(1 - smoothstep(0.75, 0.8, r));
			r = (1 - fade)*r + 0.65*fade;

			// Multiply the ring pattern by the fine grain

			fade = smoothstep(2 / grainscale, 8 / grainscale, fwidth);
			if (fade < 0.999) {
				r2 = 1.3 - noise(ss*grainscale, (tt*grainscale / 4));
				r2 = grainy * r2*r2 + (1 - grainy);
				r *= (1 - fade)*r2 + (0.75*fade);

			}
			else r *= 0.75;

		}
		else r = 0.4875;


		// Mix the light and dark wood according to the grain pattern 
		woodcolor = lerp(lightwood, darkwood, r);

		// Add plank-to-plank variation in overall color 
		woodcolor *= (1 - plankvary / 2 + plankvary * noise(whichplank + 0.5));

		Ct = lerp(groovecolor, woodcolor, groovy);
		return Ct;

	}

	float noise3D_1(vec3 p)
	{
		return texture3D(ttt3D, p).x*2.0f - 1.0f;
	}

	float turbulence_1(vec3 p, int octaves, float lacunarity, float gain) {

		float freq = 1.0f;
		float amp = 0.8f;
		float sum = 0.0f;

		for (int i = 0; i<octaves; i++) {
			sum += abs(noise3D_1(p*freq))*amp;
			freq *= lacunarity;
			amp *= gain;
		}
		return sum;
	}

	float spike_1(float c, float w, float x) {
		return smoothstep(c - w, c, x) * smoothstep(c + w, c, x);
	}

	vec3 myTexture3D_1(vec3 p)
	{


		float noiseScale = 0.1f*extraNoiseScale;
		float noise = turbulence_1(p*noiseScale, 3, 3.0f, 0.5f);
		//noise = turbulence(p*noiseScale + vec3(noise, noise, noise*0.3)*0.01f, 8, 3.0f, 0.5f);

		//noise = spike(0.35f, 0.05f, noise);
		//noise = noise;

		vec3 base = lerp(vec3(164, 148, 108)*1.63 / 255, vec3(178, 156, 126)*1.73 / 255, spike_1(0.5f, 0.3f, turbulence_1(p*noiseScale*0.7f + vec3(noise*0.5, noise, noise)*0.011f, 2, 2.0f, 0.5f)));
		//vec3 b2 = lerp(base, vec3(0.0f, 0.0f, 0.0f), noise); 
		vec3 b2 = lerp(base, vec3(173, 160, 121)*1.73 / 255, noise);



		return b2*0.75f;

	}

	vec3 myTexture3DCom(vec3 p, float mat) {
		// Depend on material ID
		
		if (mat < 0.5f) {
			//return myTexture3D_0(p);
			return vec3(173, 160, 151) *0.85/ 255;
			//return lightwood*1.3;
		}
		else 
		if (mat < 1.5f) {
			//return myTexture3D_1(p);
			return vec3(173, 100, 21)*1.73 / 255;
		} else {
			return vec3(1.0f, 0.0f, 0.0f);
			
		}

	}
// scene reflection 
uniform float reflectionCoeff = 0.0f;
uniform float specularCoeff = 0.0f;

uniform sampler2DRect reflectionTex;

// Shadow map
uniform float shadowAmbient = 0.0;
uniform float hdrScale = 5.0;
uniform sampler2D texture;
uniform sampler2DArrayShadow stex;
uniform sampler2DArrayShadow stex2;
uniform sampler2DArrayShadow stex3;
uniform samplerCube skyboxTex;
uniform vec2 texSize; // x - size, y - 1/size
uniform vec4 far_d;

// Spot lights
uniform vec3 spotLightDir;
uniform vec3 spotLightPos;
uniform float spotLightCosineDecayBegin;
uniform float spotLightCosineDecayEnd;

uniform vec3 spotLightDir2;
uniform vec3 spotLightPos2;
uniform float spotLightCosineDecayBegin2;
uniform float spotLightCosineDecayEnd2;

uniform vec3 spotLightDir3;
uniform vec3 spotLightPos3;
uniform float spotLightCosineDecayBegin3;
uniform float spotLightCosineDecayEnd3;

uniform vec3 parallelLightDir;
uniform float shadowAdd;
uniform int useTexture;
uniform int numShadows;


uniform float roughnessScale;
uniform vec3 ambientColor;

uniform sampler2DArray diffuseTexArray;
uniform sampler2DArray bumpTexArray;
uniform sampler2DArray specularTexArray;
uniform sampler2DArray emissiveReflectSpecPowerTexArray;

uniform vec2 shadowTaps[12];


float shadowCoeff1(float bscale)
{	

	int index = 3;
	
	if(gl_FragCoord.z < far_d.x)
		index = 0;
	else if(gl_FragCoord.z < far_d.y)
		index = 1;
	else if(gl_FragCoord.z < far_d.z)
		index = 2;
	
	vec4 shadow_coord = gl_TextureMatrix[index]*vec4(gl_TexCoord[1].xyz, 1);

	shadow_coord.w = shadow_coord.z + shadowAdd*bscale;
		// tell glsl in which layer to do the look up
	shadow_coord.z = float(index);

	
	// Gaussian 3x3 filter
//	return shadow2DArray(stex, shadow_coord).x;
	/*
	const float X = 1.0f;
	float ret = shadow2DArray(stex, shadow_coord).x * 0.25;
	ret += shadow2DArrayOffset(stex, shadow_coord, ivec2( -X, -X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex, shadow_coord, ivec2( -X, 0)).x * 0.125;
	ret += shadow2DArrayOffset(stex, shadow_coord, ivec2( -X, X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex, shadow_coord, ivec2( 0, -X)).x * 0.125;
	ret += shadow2DArrayOffset(stex, shadow_coord, ivec2( 0, X)).x * 0.125;
	ret += shadow2DArrayOffset(stex, shadow_coord, ivec2( X, -X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex, shadow_coord, ivec2( X, 0)).x * 0.125;
	ret += shadow2DArrayOffset(stex, shadow_coord, ivec2( X, X)).x * 0.0625;	
	return ret;*/
	const int numTaps = 6;
	float radius = 0.0003f/pow(2,index);
	float s = 0.0f;
	for (int i = 0; i < numTaps; i++)
	{
		s += shadow2DArray(stex, shadow_coord + vec4(shadowTaps[i] * radius, 0.0f, 0.0f)).r;
	}
	s /= numTaps;
	return s;
}
float shadowCoeff2()
{
	const int index = 1;

	//int index = 3;
	//if(gl_FragCoord.z < far_d.x)
	//	index = 0;
	//else if(gl_FragCoord.z < far_d.y)
	//	index = 1;
	//else if(gl_FragCoord.z < far_d.z)
	//	index = 2;

	vec4 shadow_coord = gl_TextureMatrix[index]*vec4(gl_TexCoord[1].xyz, 1);

	shadow_coord.w = shadow_coord.z + shadowAdd;
	shadow_coord.z = float(0);
	//	return shadow2DArray(stex, shadow_coord).x;

	const float X = 1.0f;
	float ret = shadow2DArray(stex2, shadow_coord).x * 0.25;
	ret += shadow2DArrayOffset(stex2, shadow_coord, ivec2( -X, -X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex2, shadow_coord, ivec2( -X, 0)).x * 0.125;
	ret += shadow2DArrayOffset(stex2, shadow_coord, ivec2( -X, X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex2, shadow_coord, ivec2( 0, -X)).x * 0.125;
	ret += shadow2DArrayOffset(stex2, shadow_coord, ivec2( 0, X)).x * 0.125;
	ret += shadow2DArrayOffset(stex2, shadow_coord, ivec2( X, -X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex2, shadow_coord, ivec2( X, 0)).x * 0.125;
	ret += shadow2DArrayOffset(stex2, shadow_coord, ivec2( X, X)).x * 0.0625;
	return ret;
}
float shadowCoeff3()
{
	const int index = 2;

	//int index = 3;
	//if(gl_FragCoord.z < far_d.x)
	//	index = 0;
	//else if(gl_FragCoord.z < far_d.y)
	//	index = 1;
	//else if(gl_FragCoord.z < far_d.z)
	//	index = 2;

	vec4 shadow_coord = gl_TextureMatrix[index]*vec4(gl_TexCoord[1].xyz, 1);

	shadow_coord.w = shadow_coord.z + shadowAdd;
	shadow_coord.z = float(0);

	//	return shadow2DArray(stex, shadow_coord).x;

	const float X = 1.0f;
	float ret = shadow2DArray(stex3, shadow_coord).x * 0.25;
	ret += shadow2DArrayOffset(stex3, shadow_coord, ivec2( -X, -X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex3, shadow_coord, ivec2( -X, 0)).x * 0.125;
	ret += shadow2DArrayOffset(stex3, shadow_coord, ivec2( -X, X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex3, shadow_coord, ivec2( 0, -X)).x * 0.125;
	ret += shadow2DArrayOffset(stex3, shadow_coord, ivec2( 0, X)).x * 0.125;
	ret += shadow2DArrayOffset(stex3, shadow_coord, ivec2( X, -X)).x * 0.0625;
	ret += shadow2DArrayOffset(stex3, shadow_coord, ivec2( X, 0)).x * 0.125;
	ret += shadow2DArrayOffset(stex3, shadow_coord, ivec2( X, X)).x * 0.0625;
	return ret;
}



uniform float RollOff = 0.5f;
uniform float fresnelBias = 0.0;
uniform float fresnelScale = 1.0;
uniform float fresnelPower = 3.0;   // 5.0 is physically correct
void main()
{

/*
	int index = 3;
	
	if(gl_FragCoord.z < far_d.x)
		index = 0;
	else if(gl_FragCoord.z < far_d.y)
		index = 1;
	else if(gl_FragCoord.z < far_d.z)
		index = 2;
	if (index == 3) gl_FragColor = vec4(1,0,0,1);
	if (index == 2) gl_FragColor = vec4(0,1,0,1);
	if (index == 1) gl_FragColor = vec4(0,0,1,1);
	if (index == 0) gl_FragColor = vec4(1,1,0,1);
	return;*/
	/*
	int index = 3;
	
	if(gl_FragCoord.z < far_d.x)
		index = 0;
	else if(gl_FragCoord.z < far_d.y)
		index = 1;
	else if(gl_FragCoord.z < far_d.z)
		index = 2;

	vec4 shadow_coord = gl_TextureMatrix[index]*vec4(gl_TexCoord[1].xyz, 1);

	shadow_coord.w = shadow_coord.z + shadowAdd;
	// tell glsl in which layer to do the look up
	shadow_coord.z = float(index)*0.33333333f;
	gl_FragColor = vec4(shadow_coord.xyz,1.0f);
	return;
	*/
	//// TODO, expose this as user parameter
	const float skyLightIntensity = 0.2;
	const float rimLightIntensity = 0.3;

	vec3 normal = normalize(gl_TexCoord[2].xyz);
	vec3 t0 = gl_TexCoord[3].xyz;
	vec3 t1 = gl_TexCoord[4].xyz;

	vec3 diffuseMat;
	vec3 specularMat;
	vec3 bump;
	vec3 emissiveReflectSpecPow;

	// read in material color for diffuse, specular, bump, emmisive

	// 3D texture	
	diffuseMat = myTexture3DCom(gl_TexCoord[0].xyz, gl_TexCoord[6].w); 
	//diffuseMat = myTexture3D(gl_TexCoord[0].xyz);//texture3D(ttt3D, gl_TexCoord[0].xyz);
	//diffuseMat = texture3D(ttt3D, gl_TexCoord[0].xyz);
	
	specularMat = vec3(1.0);
	bump = texture2D(texture, gl_TexCoord[5].xy).xyz;
	if (dot(bump,bump) < 0.01) bump = vec3(0.5,0.5,1);		
	emissiveReflectSpecPow = vec3(0.0,0.0,0.0);


	// apply bump to the normal
	bump = (bump - vec3(0.5,0.5,0.5)) * 2.0f;
	bump.xy *= roughnessScale*0.1;	

	float sc = 1.0f;
	normal = normalize(t0*bump.x + t1*bump.y + sc*normal * bump.z);

	//gl_FragColor.xyz = normal*0.5 + vec3(0.5,0.5,0.5);
	//gl_FragColor.w = 1;
	//return;
	vec3 eyeVec = normalize(gl_TexCoord[1].xyz);

	// apply gamma correction for diffuse textures
	//diffuseMat = pow(diffuseMat, 0.45);

	float specularPower = emissiveReflectSpecPow.b*255.0f + 1.0f;

	// TODO - fix this
	specularPower = 10.0f;

	float emissive = 0.0f;
	float reflectivity = emissiveReflectSpecPow.b;
	float fresnel = fresnelBias + fresnelScale*pow(1.0 - max(0.0, dot(normal, eyeVec)), fresnelPower);
	float specular = 0.0f;

	vec3 skyNormal = reflect(eyeVec, normal);
	vec3 skyColor = skyLightIntensity * textureCube(skyboxTex, skyNormal).rgb;
	vec3 ambientSkyColor = diffuseMat * skyColor;

	vec3 diffuseColor = vec3(0.0, 0.0, 0.0);	

	if (numShadows >= 1) {

		vec3 lightColor = hdrScale * vec3(1.0, 1.0, 1.0);
		vec3 shadowColor = vec3(0.4, 0.4, 0.7); // colored shadow
		//vec3 lvec = normalize(spotLightDir);
		vec3 lvec = normalize(spotLightPos - gl_TexCoord[1].xyz);
		float ldn = max(0.0f, dot(normal, lvec));
		float cosine = dot(lvec, spotLightDir);
		float intensity = smoothstep(spotLightCosineDecayBegin, spotLightCosineDecayEnd, cosine);

		float bscale = 1;//1.0f-ldn;
		
		float shadowC = shadowCoeff1(bscale);
		//gl_FragColor = vec4(shadowC,shadowC,shadowC,1.0f);
		//return;
		vec3 irradiance = shadowC * ldn * lightColor;

		// diffuse irradiance
		diffuseColor += diffuseMat * irradiance*intensity;

		// add colored shadow
		diffuseColor += (1.0 - shadowC*ldn) * shadowAmbient * shadowColor * diffuseMat*intensity;

		vec3 r = reflect(lvec, normal);	
		specular += pow(max(0.0, dot(r, eyeVec)), specularPower)*shadowC*intensity;
	}

	// add rim light
	if (numShadows >= 2) {
		vec3 lightColor = hdrScale * vec3(1.0, 1.0, 1.0);
		vec3 lvec = normalize(spotLightDir2);
		float ldn = max(0.0f, dot(normal, lvec));
		vec3 irradiance = ldn * lightColor;

		// diffuse irradiance
		diffuseColor += diffuseMat * irradiance;
	}

	vec3 color = vec3(0.0, 0.0, 0.0);

	color += diffuseColor;
	color += ambientSkyColor;
	color += specular*specularMat;
	color += hdrScale * emissive * diffuseMat;

	//vec3 reflectColor = diffuseMat * texture2DRect(reflectionTex, gl_FragCoord.xy).rgb;
	//color = reflectionCoeff * reflectColor + (1.0f - reflectionCoeff) * color;
	color = (fresnel * skyColor + (1.0 - fresnel) * color) * reflectivity + (1.0 - reflectivity) * color;

	gl_FragColor.rgb = color;
	gl_FragColor.w = gl_Color.w;

	float fog = clamp(gl_Fog.scale*(gl_Fog.end+gl_TexCoord[1].z), 0.0, 1.0);
	vec4 fogCol = gl_Fog.color;
	gl_FragColor = mix(fogCol, gl_FragColor, fog);
}