1- #version 120
1+ #version 130
22
33#include "/ lib/ lib.glsl"
44#define SHADOW_MAP_BIAS 0.85
@@ -11,6 +11,11 @@ uniform sampler2D gcolor;
1111uniform sampler2D gnormal;
1212uniform sampler2D shadow;
1313uniform sampler2D depthtex0;
14+ uniform sampler2D depthtex1;
15+ uniform sampler2D shadowtex1;
16+ uniform sampler2D gtexture;
17+ uniform sampler2D colortex4;
18+ uniform sampler2D noisetex;
1419uniform vec3 sunPosition;
1520uniform vec3 moonPosition;
1621uniform float rainStrength;
@@ -22,9 +27,6 @@ varying vec2 lmcoord;
2227varying vec2 texcoord;
2328varying float extShadow;
2429varying vec3 lightPosition;
25- varying vec3 mySunColor;
26- varying vec3 mySkyColor;
27- varying vec4 positionInViewCoord;
2830
2931uniform vec3 cameraPosition;
3032uniform vec3 fogColor;
@@ -56,49 +58,72 @@ vec2 rand2d(highp vec2 coord)
5658 return vec2 (x, y);
5759}
5860
61+ vec3 normalDecode(vec2 enc) {
62+ vec4 nn = vec4 (2.0 * enc - 1.0 , 1.0 , - 1.0 );
63+ float l = dot (nn.xyz,- nn.xyw);
64+ nn.z = l;
65+ nn.xy *= sqrt (l);
66+ return nn.xyz * 2.0 + vec3 (0.0 , 0.0 , - 1.0 );
67+ }
68+
5969#ifdef ShadowMapping
60- float shadowMapping(vec4 worldPosition, float dist) {
61- if (dist > 0.9 ) // distance
70+ float shadowMapping(vec4 worldPosition, float dist, vec3 normal, float alpha) {
71+ if (dist > 0.9 )
6272 return extShadow;
6373 float shade = 0.0 ;
64- vec4 shadowposition = shadowModelView * worldPosition;
65- shadowposition = shadowProjection * shadowposition;
66- float distb = sqrt (shadowposition.x * shadowposition.x + shadowposition.y * shadowposition.y);
67- float distortFactor = (1.0 - SHADOW_MAP_BIAS) + distb * SHADOW_MAP_BIAS;
68- shadowposition.xy /= distortFactor;
69- shadowposition /= shadowposition.w;
70- shadowposition = shadowposition * 0.5 + 0.5 ;
71-
72- float shadowSamplesRadius = 0.001 ;
73-
74- for (float i = 1.0 ; i <= ShadowSamples; i += 1.0 )
74+ float angle = dot (lightPosition, normal);
75+ if (angle <= 0.1 && alpha > 0.99 )
76+ {
77+ shade = 1.0 ;
78+ }
79+ else
7580 {
81+ vec4 shadowposition = shadowModelView * worldPosition;
82+ shadowposition = shadowProjection * shadowposition;
83+ float distb = sqrt (shadowposition.x * shadowposition.x + shadowposition.y * shadowposition.y);
84+ float distortFactor = (1.0 - SHADOW_MAP_BIAS) + distb * SHADOW_MAP_BIAS;
85+ shadowposition.xy /= distortFactor;
86+ shadowposition /= shadowposition.w;
87+ shadowposition = shadowposition * 0.5 + 0.5 ;
88+ float shadowSamplesRadius = 0.001 ;
89+
90+ for (float i = 1.0 ; i <= ShadowSamples; i += 1.0 ){
7691 vec2 sampleCoord = rand2d(shadowposition.xy * i) - 0.5 ;
7792 float shadowDepth = texture2D (shadow, shadowposition.st + sampleCoord * shadowSamplesRadius).z;
78- if (shadowDepth + 0.0005 < shadowposition.z ){
93+ if (shadowDepth + 0.0003 < shadowposition.z ){
7994 shade += 1.0 ;
80- }
95+ }if (angle < 0.2 && alpha > 0.99 )
96+ shade = max (shade, 1.0 - (angle - 0.1 ) * 10.0 );}
97+ shade /= ShadowSamples;
8198 }
82- shade /= ShadowSamples;
83-
8499 shade -= clamp ((dist - 0.7 ) * 5.0 , 0.0 , 1.0 );
85- shade = clamp (shade, 0.0 , 1.0 );
100+ shade = clamp (shade, 0.0 , 1.0 );
86101 return max (shade, extShadow);
87102}
88103#endif
104+
89105void main() {
106+
90107vec4 color = texture2D (gcolor, texcoord.st);
108+ vec3 normal = normalDecode(texture2D (gnormal, texcoord.st).rg);
91109#ifdef ShadowMapping
92110float indoor= smoothstep (.95 ,1 .,lmcoord.y);
93111float cave= smoothstep (.7 ,1 .,lmcoord.y);
94112float day= saturate(skyColor.r* 2 .);
95113float sunset= saturate((fogColor.r-.1 )- fogColor.b);
96- float depth = texture2D (depthtex0, texcoord.st).x;
97- vec4 viewPosition = gbufferProjectionInverse * vec4 (texcoord.s * 2.0 - 1.0 , texcoord.t * 2.0 - 1.0 , 2.0 * depth - 1.0 , 1 .0f);
98- viewPosition /= viewPosition.w;
99- vec4 worldPosition = gbufferModelViewInverse * viewPosition;
100- float dist = length (worldPosition.xyz) / far;
101- float shade = shadowMapping(worldPosition, dist);
114+
115+ float depth0 = texture2D (depthtex0, texcoord.st).x;
116+ vec4 viewPosition0 = gbufferProjectionInverse * vec4 (texcoord.s * 2.0 - 1.0 , texcoord.t * 2.0 - 1.0 , 2.0 * depth0 - 1.0 , 1 .0f);
117+ viewPosition0 /= viewPosition0.w;
118+ vec4 worldPosition0 = gbufferModelViewInverse * viewPosition0;
119+
120+ /* float depth1 = texture2D(depthtex1, texcoord.st).x;
121+ vec4 viewPosition1 = gbufferProjectionInverse * vec4(texcoord.s * 2.0 - 1.0, texcoord.t * 2.0 - 1.0, 2.0 * depth1 - 1.0, 1.0f);
122+ viewPosition1 /= viewPosition1.w;
123+ vec4 worldPosition1 = gbufferModelViewInverse * viewPosition1;*/
124+
125+ float dist = length (worldPosition0.xyz) / far;
126+ float shade = shadowMapping(worldPosition0, dist, normal, color.a);
102127if (12000 < worldTime && worldTime< 13000 ) {
103128color.rgb *= (1.0 - shade * 0.5 * (1.0 - rainStrength* 0.8 )* (1.0 - lmcoord.x* 0.4 )); // dusk
104129}
@@ -120,5 +145,4 @@ return;
120145/* DRAWBUFFERS:0 */
121146gl_FragData [0 ] = color;
122147
123-
124148}
0 commit comments