/
terrain.shader
670 lines (463 loc) · 21.2 KB
/
terrain.shader
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#include "core.h"
#define RENDER_COMPOSITE_MAP @shPropertyBool(composite_map)
#define COMPOSITE_MAP @shGlobalSettingBool(terrain_composite_map)
#define FOG @shGlobalSettingBool(fog) && !RENDER_COMPOSITE_MAP
#define MRT (!RENDER_COMPOSITE_MAP && @shGlobalSettingBool(mrt_output))
#define SHADOWS @shGlobalSettingBool(shadows_pssm) && !RENDER_COMPOSITE_MAP
#define SHADOWS_DEPTH @shGlobalSettingBool(shadows_depth)
#if SHADOWS
#include "shadows.h"
#endif
#define NUM_LAYERS @shPropertyString(num_layers)
#define DEBUG_BLEND @shGlobalSettingBool(debug_blend)
#define NORMAL_MAPPING @shGlobalSettingBool(terrain_normal)
#define SPECULAR @shGlobalSettingBool(terrain_specular) && !RENDER_COMPOSITE_MAP
#define SPECULAR_EXPONENT 32
#define PARALLAX_MAPPING @shGlobalSettingBool(terrain_parallax) && !RENDER_COMPOSITE_MAP && NORMAL_MAPPING
#define PARALLAX_SCALE 0.03
#define PARALLAX_BIAS -0.04
/// triplanar
#define TRIPLANAR_TYPE @shGlobalSettingString(terrain_triplanarType)
#define TRIPLANAR_FULL (TRIPLANAR_TYPE == 2)
#define TRIPLANAR_1 (TRIPLANAR_TYPE == 1)
#define TRIPLANAR (TRIPLANAR_TYPE) && !RENDER_COMPOSITE_MAP
// 1 layer only
#define TRIPLANAR_LAYER @shGlobalSettingString(terrain_triplanarLayer)
// 2 layers only
#define TRIPLANAR_LAYER2 @shGlobalSettingString(terrain_triplanarLayer2)
#if (MRT) || (FOG) || (SHADOWS)
#define NEED_DEPTH 1
#endif
#if NEED_DEPTH
@shAllocatePassthrough(1, depth)
#endif
@shAllocatePassthrough(2, UV)
@shAllocatePassthrough(4, objSpacePosition)
#if MRT
@shAllocatePassthrough(3, viewPosition)
#endif
#if SHADOWS
@shForeach(3)
@shAllocatePassthrough(4, lightSpacePos@shIterator)
@shEndForeach
#endif
#ifdef SH_VERTEX_SHADER
// ------------------------------------- VERTEX ---------------------------------------
SH_BEGIN_PROGRAM
shUniform(float4x4, worldMatrix) @shAutoConstant(worldMatrix, world_matrix)
shUniform(float4x4, viewProjMatrix) @shAutoConstant(viewProjMatrix, viewproj_matrix)
shUniform(float2, lodMorph) @shAutoConstant(lodMorph, custom, 1001)
shVertexInput(float2, uv0)
shVertexInput(float2, uv1) // lodDelta, lodThreshold
#if MRT
shUniform(float4x4, wvMat) @shAutoConstant(wvMat, worldview_matrix)
#endif
#if SHADOWS
@shForeach(3)
shUniform(float4x4, texViewProjMatrix@shIterator) @shAutoConstant(texViewProjMatrix@shIterator, texture_viewproj_matrix, @shIterator)
@shEndForeach
#endif
@shPassthroughVertexOutputs
SH_START_PROGRAM
{
#if MRT
float3 viewPos = shMatrixMult(wvMat, shInputPosition).xyz;
@shPassthroughAssign(viewPosition, viewPos);
#endif
float4 worldPos = shMatrixMult(worldMatrix, shInputPosition);
// determine whether to apply the LOD morph to this vertex
// we store the deltas against all vertices so we only want to apply
// the morph to the ones which would disappear. The target LOD which is
// being morphed to is stored in lodMorph.y, and the LOD at which
// the vertex should be morphed is stored in uv.w. If we subtract
// the former from the latter, and arrange to only morph if the
// result is negative (it will only be -1 in fact, since after that
// the vertex will never be indexed), we will achieve our aim.
// sign(vertexLOD - targetLOD) == -1 is to morph
float toMorph = -min(0, sign(uv1.y - lodMorph.y));
// morph
// this assumes XZ terrain alignment
worldPos.y += uv1.x * toMorph * lodMorph.x;
shOutputPosition = shMatrixMult(viewProjMatrix, worldPos);
#if NEED_DEPTH
@shPassthroughAssign(depth, shOutputPosition.z);
#endif
@shPassthroughAssign(UV, uv0);
@shPassthroughAssign(objSpacePosition, shInputPosition);
#if SHADOWS
float4 wPos = shMatrixMult(worldMatrix, shInputPosition);
float4 lightSpacePos;
@shForeach(3)
lightSpacePos = shMatrixMult(texViewProjMatrix@shIterator, wPos);
@shPassthroughAssign(lightSpacePos@shIterator, lightSpacePos);
@shEndForeach
#endif
}
#else
// ----------------------------------- FRAGMENT ------------------------------------------
#if !COMPOSITE_MAP
SH_BEGIN_PROGRAM
shSampler2D(normalMap) // global normal map
shSampler2D(lightMap)
@shForeach(@shPropertyString(num_blendmaps))
shSampler2D(blendMap@shIterator)
@shEndForeach
@shForeach(@shPropertyString(num_layers))
shSampler2D(diffuseMap@shIterator)
@shEndForeach
@shForeach(@shPropertyString(num_layers))
shSampler2D(normalMap@shIterator)
@shEndForeach
#if TRIPLANAR || FOG
shUniform(float4x4, worldMatrix) @shAutoConstant(worldMatrix, world_matrix)
#endif
#if TRIPLANAR
shUniform(float, terrainWorldSize) @shSharedParameter(terrainWorldSize)
#endif
#if NORMAL_MAPPING
shUniform(float, scaleNormal) @shUniformProperty1f(scaleNormal, scaleNormal)
#endif
// layer uv multipliers
@shForeach(@shPropertyString(num_uv_mul))
shUniform(float4, uvMul@shIterator) @shUniformProperty4f(uvMul@shIterator, uv_mul_@shIterator)
@shEndForeach
#if FOG
shUniform(float4, fogParams) @shAutoConstant(fogParams, fog_params)
shUniform(float4, fogColorSun) @shSharedParameter(fogColorSun)
shUniform(float4, fogColorAway) @shSharedParameter(fogColorAway)
shUniform(float4, fogColorH) @shSharedParameter(fogColorH)
shUniform(float4, fogParamsH) @shSharedParameter(fogParamsH)
#endif
@shPassthroughFragmentInputs
#if MRT
shDeclareMrtOutput(1)
shDeclareMrtOutput(2)
shUniform(float, far) @shAutoConstant(far, far_clip_distance)
shUniform(float4x4, wvMat) @shAutoConstant(wvMat, worldview_matrix)
#endif
shUniform(float4, lightAmbient) @shAutoConstant(lightAmbient, ambient_light_colour)
@shForeach(1)
shUniform(float4, lightPosObjSpace@shIterator) @shAutoConstant(lightPosObjSpace@shIterator, light_position_object_space, @shIterator)
shUniform(float4, lightSpecular@shIterator) @shAutoConstant(lightSpecular@shIterator, light_specular_colour, @shIterator)
shUniform(float4, lightDiffuse@shIterator) @shAutoConstant(lightDiffuse@shIterator, light_diffuse_colour, @shIterator)
@shEndForeach
shUniform(float3, eyePosObjSpace) @shAutoConstant(eyePosObjSpace, camera_position_object_space)
#if SHADOWS
@shForeach(3)
shSampler2D(shadowMap@shIterator)
shUniform(float2, invShadowmapSize@shIterator) @shAutoConstant(invShadowmapSize@shIterator, inverse_texture_size, @shIterator)
@shEndForeach
shUniform(float3, pssmSplitPoints) @shSharedParameter(pssmSplitPoints)
#endif
#if SHADOWS
shUniform(float4, shadowFar_fadeStart) @shSharedParameter(shadowFar_fadeStart)
#endif
SH_START_PROGRAM
{
#if NEED_DEPTH
float depth = @shPassthroughReceive(depth);
#endif
float2 UV = @shPassthroughReceive(UV);
float4 objSpacePosition = @shPassthroughReceive(objSpacePosition);
// Shadows
#if SHADOWS
@shForeach(3)
float4 lightSpacePos@shIterator = @shPassthroughReceive(lightSpacePos@shIterator);
@shEndForeach
float shadow = pssmDepthShadow (lightSpacePos0, invShadowmapSize0, shadowMap0, lightSpacePos1, invShadowmapSize1, shadowMap1, lightSpacePos2, invShadowmapSize2, shadowMap2, depth, pssmSplitPoints, 0.f);
float fadeRange = shadowFar_fadeStart.x - shadowFar_fadeStart.y;
float fade = 1-((depth - shadowFar_fadeStart.y) / fadeRange);
shadow = (depth > shadowFar_fadeStart.x) ? 1.0 : ((depth > shadowFar_fadeStart.y) ? 1.0-((1.0-shadow)*fade) : shadow);
#endif
#if !(SHADOWS)
float shadow = 1.0;
#endif
shadow *= shSample(lightMap, UV).x;
float3 normal = shSample(normalMap, UV).rgb * 2 - 1;
normal = normalize(normal);
// derive the tangent space basis
float3 tangent = float3(1, 0, 0); // XZ terrain align
float3 binormal = normalize(cross(tangent, normal));
tangent = normalize(cross(normal, binormal)); // note, now we need to re-cross to derive tangent again because it wasn't orthonormal
// derive final matrix
float3x3 TBN = float3x3(tangent, binormal, normal);
#if SH_GLSL
TBN = transpose(TBN);
#endif
float3 lightDir = lightPosObjSpace0.xyz; // directional
float3 eyeDir = eyePosObjSpace.xyz - objSpacePosition.xyz;
#if NORMAL_MAPPING
float3 TSlightDir = normalize(shMatrixMult(TBN, lightDir));
float3 TSeyeDir = normalize(shMatrixMult(TBN, eyeDir));
float3 TShalfAngle = normalize(TSlightDir + TSeyeDir);
#endif
// set up blend values
@shForeach(@shPropertyString(num_blendmaps))
float4 blendValues@shIterator = shSample(blendMap@shIterator, UV);
@shEndForeach
#if TRIPLANAR
// Determine the blend weights for the 3 planar projections.
float3 absNormal = abs( normal.xyz );
float3 blend_weights = absNormal;
// Tighten up the blending zone:
// -> ORIGINAL :
//blend_weights = (blend_weights - 0.2) * 7; // => in original paper,
// but I don't see how the *7 is supposed to change anything,
// since there is a division by itself just after (see "Force weights to sum to 1.0").
blend_weights = (blend_weights - 0.5);
blend_weights = max(blend_weights, 0);
// Force weights to sum to 1.0
blend_weights /= blend_weights.x + blend_weights.y + blend_weights.z;
float2 coord1, coord2, coord3;
float4 col1, col2, col3;
// use world position and divide by terrain world size to get a consistent uv scale (-0.5 ... 0.5)
// this is the same scale that non-triplanar would have
float3 wPos = shMatrixMult(worldMatrix, float4(objSpacePosition.xyz, 1)).xyz / terrainWorldSize;
#endif
#if NORMAL_MAPPING
// normal mapping - per-layer lighting
float3 TSnormal;
float2 litRes = float2(0,0); // diffuse, specular amount
float NdotL;
float specular;
#else
float specularAmount = 0;
#endif
// vars
float3 albedo = float3(0,0,0);
float3 bb;
float4 diffuseSpec;
float uvMul;
float fBlend;
//----- per layer calculations
@shForeach(@shPropertyString(num_layers))
fBlend = blendValues@shPropertyString(blendmap_component_@shIterator);
///---------------------------------------------------------------------------------------------
#if TRIPLANAR
#if (TRIPLANAR_FULL) || (TRIPLANAR_LAYER == @shIterator) || (TRIPLANAR_LAYER2 == @shIterator)
/// triplanar on all or on this layer
coord1 = wPos.yz * uvMul@shPropertyString(uv_component_@shIterator);
coord2 = wPos.zx * uvMul@shPropertyString(uv_component_@shIterator);
coord3 = wPos.xy * uvMul@shPropertyString(uv_component_@shIterator);
coord3.x *= -1.f;
// parallax
#if PARALLAX_MAPPING
if (blend_weights.x > 0) coord1 += TSeyeDir.xy * ( shSample(normalMap@shIterator, coord1).a * PARALLAX_SCALE + PARALLAX_BIAS );
if (blend_weights.y > 0) coord2 += TSeyeDir.xy * ( shSample(normalMap@shIterator, coord2).a * PARALLAX_SCALE + PARALLAX_BIAS );
if (blend_weights.z > 0) coord3 += TSeyeDir.xy * ( shSample(normalMap@shIterator, coord3).a * PARALLAX_SCALE + PARALLAX_BIAS );
#endif
// Sample color maps for each projection, at those UV coords.
col1 = shSample(diffuseMap@shIterator, coord1.yx);
if (blend_weights.y > 0) col2 = shSample(diffuseMap@shIterator, coord2.yx);
if (blend_weights.z > 0) col3 = shSample(diffuseMap@shIterator, coord3);
// Finally, blend the results of the 3 planar projections.
diffuseSpec = col1.xyzw * blend_weights.xxxx + col2.xyzw * blend_weights.yyyy + col3.xyzw * blend_weights.zzzz;
// normal
#if NORMAL_MAPPING
col1 = shSample(normalMap@shIterator, coord1.yx) * 2 - 1;
if (blend_weights.y > 0) col2 = shSample(normalMap@shIterator, coord2.yx) * 2 - 1;
if (blend_weights.z > 0) col3 = shSample(normalMap@shIterator, coord3) * 2 - 1;
TSnormal = normalize(col1.xyz * blend_weights.xxx + col2.xyz * blend_weights.yyy + col3.xyz * blend_weights.zzz);
#endif
#else
/// no triplanar on layer
uvMul = uvMul@shPropertyString(uv_component_@shIterator);
// parallax
#if PARALLAX_MAPPING
float2 layerUV@shIterator = UV * uvMul + TSeyeDir.xy * ( shSample(normalMap@shIterator, UV * uvMul).a * PARALLAX_SCALE + PARALLAX_BIAS );
#else
float2 layerUV@shIterator = UV * uvMul;
#endif
diffuseSpec = shSample(diffuseMap@shIterator, layerUV@shIterator);
// normal
#if NORMAL_MAPPING
TSnormal = normalize(shSample(normalMap@shIterator, layerUV@shIterator).xyz * 2 - 1);
#endif
#endif
#else /// no triplanar
uvMul = uvMul@shPropertyString(uv_component_@shIterator);
// parallax
#if PARALLAX_MAPPING
float2 layerUV@shIterator = UV * uvMul + TSeyeDir.xy * ( shSample(normalMap@shIterator, UV * uvMul).a * PARALLAX_SCALE + PARALLAX_BIAS );
#else
float2 layerUV@shIterator = UV * uvMul;
#endif
diffuseSpec = shSample(diffuseMap@shIterator, layerUV@shIterator);
// normal
#if NORMAL_MAPPING
TSnormal = normalize(shSample(normalMap@shIterator, layerUV@shIterator).xyz * 2 - 1);
#endif
#endif
///---------------------------------------------------------------------------------------------
//// albedo
#if DEBUG_BLEND
// for test
bb = float3(0,0,0);
#if @shIterator == 0
bb = float3(1,0,0);
#endif
#if @shIterator == 1
bb = float3(0,1,0);
#endif
#if @shIterator == 2
bb = float3(0,0,1);
#endif
#if @shIterator == 3 // only 4
bb = float3(0.5,0.5,0.5);
#endif
albedo += bb * fBlend;
#else
albedo += diffuseSpec.rgb * fBlend;
#endif
#if NORMAL_MAPPING
TSnormal.z *= scaleNormal;
TSnormal = normalize(TSnormal);
NdotL = max(dot(TSnormal, TSlightDir), 0);
specular = pow(max(dot(TSnormal, TShalfAngle), 0), SPECULAR_EXPONENT) * diffuseSpec.a;
#if @shIterator == 0
litRes.x = NdotL;
#if SPECULAR
litRes.y = specular;
#endif
#else
litRes.x = shLerp (litRes.x, NdotL, fBlend);
#if SPECULAR
litRes.y = shLerp (litRes.y, specular, fBlend);
#endif
#endif
#else
#if @shIterator == 0
specularAmount = diffuseSpec.a;
#else
specularAmount = shLerp(specularAmount, diffuseSpec.a, fBlend);
#endif
#endif
@shEndForeach
//----- per layer
shOutputColour(0).a = 1.f;
shOutputColour(0).rgb = albedo;
// Lighting
#if !NORMAL_MAPPING
// no normal mapping - light all layers at once
float3 diffuse = float3(0,0,0);
lightDir = normalize(lightDir);
float3 halfAngle = normalize(lightDir + eyeDir);
float specular = pow(max(dot(normal, halfAngle), 0), SPECULAR_EXPONENT);
diffuse += lightDiffuse0.xyz * max(dot(normal, lightDir), 0) * shadow;
#if DEBUG_BLEND
shOutputColour(0).xyz *= (float3(0.5,0.5,0.5) + 0.5*diffuse);
#else
shOutputColour(0).xyz *= (lightAmbient.xyz + diffuse);
#endif
#if SPECULAR
shOutputColour(0).xyz += specular * lightSpecular0.xyz * specularAmount * shadow;
#endif
#else
#if DEBUG_BLEND
shOutputColour(0).xyz *= (float3(0.5,0.5,0.5) + litRes.x * float3(0.8,0.8,0.8) * shadow);
#else
shOutputColour(0).xyz *= (lightAmbient.xyz + litRes.x * lightDiffuse0.xyz * shadow);
#endif
#if SPECULAR
shOutputColour(0).xyz += litRes.y * lightSpecular0.xyz * shadow;
#endif
#endif
#if FOG
float worldPosY = shMatrixMult(worldMatrix, float4(objSpacePosition.xyz, 1)).y;
///_ calculate fog
float fogDepth = shSaturate((depth - fogParams.y) * fogParams.w); // w = 1 / (max - min)
float fogDepthH = shSaturate((depth - fogParamsH.z) * fogParamsH.w);
float fogDir = dot( normalize(eyeDir.xz), normalize(lightDir.xz) ) * 0.5 + 0.5;
float fogH = shSaturate( (fogParamsH.x/*h*/ - worldPosY) * fogParamsH.y/*dens*/);
float4 fogClrDir = shLerp( fogColorAway, fogColorSun, fogDir);
float4 fogClrFinal = shLerp( fogClrDir, fogColorH, fogH);
float fogL = shLerp( fogDepth * fogClrDir.a, fogDepthH * fogColorH.a, fogH);
shOutputColour(0).xyz = shLerp( shOutputColour(0).xyz, fogClrFinal.rgb, fogL);
#endif
#if MRT
float3 viewPosition = @shPassthroughReceive(viewPosition);
float3 viewNormal = normalize(shMatrixMult(wvMat, float4(normal, 0)).xyz);
shOutputColour(1) = float4(length(viewPosition) / far, normalize(viewNormal));
shOutputColour(2) = float4(depth / far, 0, depth / objSpacePosition.w, 0);
#endif
}
#else // COMPOSITE_MAP - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SH_BEGIN_PROGRAM
#if FOG
shUniform(float4, fogParams) @shAutoConstant(fogParams, fog_params)
shUniform(float4, fogColorSun) @shSharedParameter(fogColorSun)
shUniform(float4, fogColorAway) @shSharedParameter(fogColorAway)
shUniform(float4, fogColorH) @shSharedParameter(fogColorH)
shUniform(float4, fogParamsH) @shSharedParameter(fogParamsH)
shUniform(float3, eyePosObjSpace) @shAutoConstant(eyePosObjSpace, camera_position_object_space)
shUniform(float4, lightPosObjSpace) @shAutoConstant(lightPosObjSpace, light_position_object_space)
shUniform(float4x4, worldMatrix) @shAutoConstant(worldMatrix, world_matrix)
#endif
@shPassthroughFragmentInputs
#if MRT
shDeclareMrtOutput(1)
shDeclareMrtOutput(2)
shUniform(float, far) @shAutoConstant(far, far_clip_distance)
shUniform(float4x4, wvMat) @shAutoConstant(wvMat, worldview_matrix)
#endif
#if SHADOWS
@shForeach(3)
shSampler2D(shadowMap@shIterator)
shUniform(float2, invShadowmapSize@shIterator) @shAutoConstant(invShadowmapSize@shIterator, inverse_texture_size, @shIterator)
@shEndForeach
shUniform(float3, pssmSplitPoints) @shSharedParameter(pssmSplitPoints)
#endif
#if SHADOWS
shUniform(float4, shadowFar_fadeStart) @shSharedParameter(shadowFar_fadeStart)
#endif
shSampler2D(compositeMap)
#if MRT
shSampler2D(normalMap)
#endif
SH_START_PROGRAM
{
#if NEED_DEPTH
float depth = @shPassthroughReceive(depth);
#endif
float2 UV = @shPassthroughReceive(UV);
float4 objSpacePosition = @shPassthroughReceive(objSpacePosition);
// Shadows
#if SHADOWS
@shForeach(3)
float4 lightSpacePos@shIterator = @shPassthroughReceive(lightSpacePos@shIterator);
@shEndForeach
float shadow = pssmDepthShadow (lightSpacePos0, invShadowmapSize0, shadowMap0, lightSpacePos1, invShadowmapSize1, shadowMap1, lightSpacePos2, invShadowmapSize2, shadowMap2, depth, pssmSplitPoints, 0.f);
float fadeRange = shadowFar_fadeStart.x - shadowFar_fadeStart.y;
float fade = 1-((depth - shadowFar_fadeStart.y) / fadeRange);
shadow = (depth > shadowFar_fadeStart.x) ? 1.0 : ((depth > shadowFar_fadeStart.y) ? 1.0-((1.0-shadow)*fade) : shadow);
#endif
#if !(SHADOWS)
float shadow = 1.0;
#endif
shOutputColour(0) = float4(shSample(compositeMap, UV).xyz, 1);
#if FOG
float3 lightDir = lightPosObjSpace.xyz; // directional
float3 eyeDir = eyePosObjSpace.xyz - objSpacePosition.xyz;
float worldPosY = shMatrixMult(worldMatrix, float4(objSpacePosition.xyz, 1)).y;
///_ calculate fog
float fogDepth = shSaturate((depth - fogParams.y) * fogParams.w); // w = 1 / (max - min)
float fogDepthH = shSaturate((depth - fogParamsH.z) * fogParamsH.w);
float fogDir = dot( normalize(eyeDir.xz), normalize(lightDir.xz) ) * 0.5 + 0.5;
float fogH = shSaturate( (fogParamsH.x/*h*/ - worldPosY) * fogParamsH.y/*dens*/);
float4 fogClrDir = shLerp( fogColorAway, fogColorSun, fogDir);
float4 fogClrFinal = shLerp( fogClrDir, fogColorH, fogH);
float fogL = shLerp( fogDepth * fogClrDir.a, fogDepthH * fogColorH.a, fogH);
shOutputColour(0).xyz = shLerp( shOutputColour(0).xyz, fogClrFinal.rgb, fogL);
#endif
#if MRT
float3 normal = shSample(normalMap, UV).rgb * 2 - 1;
normal = normalize(normal);
float3 viewPosition = @shPassthroughReceive(viewPosition);
float3 viewNormal = normalize(shMatrixMult(wvMat, float4(normal, 0)).xyz);
shOutputColour(1) = float4(length(viewPosition) / far, normalize(viewNormal));
shOutputColour(2) = float4(depth / far, 0, depth / objSpacePosition.w, 0);
#endif
//shOutputColour(0).xy = UV;
}
#endif
#endif