@@ -20,44 +20,140 @@ layout (binding = 4) uniform sampler2D DefaultNormal;
20
20
layout (binding = 2 ) uniform sampler2D RoughnessTextureSampler;
21
21
layout (binding = 3 ) uniform sampler2D MetalicTextureSampler;
22
22
23
+ #define MAXLIGHTS 26
24
+
25
+ uniform vec3 LightColors[MAXLIGHTS];
26
+ uniform vec3 LightPositions_worldspace[MAXLIGHTS];
27
+ uniform vec3 Lightdirection[MAXLIGHTS];
28
+ uniform float LightLinears[MAXLIGHTS];
29
+ uniform float LightQuadratics[MAXLIGHTS];
30
+ uniform float LightRadius[MAXLIGHTS];
31
+ uniform float LightCutOff[MAXLIGHTS];
32
+ uniform float LightOuterCutOff[MAXLIGHTS];
33
+ uniform samplerCube depthMap[MAXLIGHTS];
34
+
23
35
uniform float Roughness;
24
36
uniform float Metalic;
25
-
26
37
uniform mat4 V;
27
-
28
38
uniform bool IsEmissive;
39
+ uniform vec3 viewPos;
40
+
41
+
42
+ const float PI = 3.14159265359 ;
43
+ const float far_plane = 25.0 ; // Constant, moved outside main
44
+ vec3 gridSamplingDisk[20 ] = vec3 []
45
+ (
46
+ vec3 (1 , 1 , 1 ), vec3 ( 1 , - 1 , 1 ), vec3 (- 1 , - 1 , 1 ), vec3 (- 1 , 1 , 1 ),
47
+ vec3 (1 , 1 , - 1 ), vec3 ( 1 , - 1 , - 1 ), vec3 (- 1 , - 1 , - 1 ), vec3 (- 1 , 1 , - 1 ),
48
+ vec3 (1 , 1 , 0 ), vec3 ( 1 , - 1 , 0 ), vec3 (- 1 , - 1 , 0 ), vec3 (- 1 , 1 , 0 ),
49
+ vec3 (1 , 0 , 1 ), vec3 (- 1 , 0 , 1 ), vec3 ( 1 , 0 , - 1 ), vec3 (- 1 , 0 , - 1 ),
50
+ vec3 (0 , 1 , 1 ), vec3 ( 0 , - 1 , 1 ), vec3 ( 0 , - 1 , - 1 ), vec3 ( 0 , 1 , - 1 )
51
+ );
52
+
53
+ float ShadowCalculation(vec3 fragPos, int index)
54
+ {
55
+ vec3 fragToLight = fragPos - LightPositions_worldspace[index];
56
+ float currentDepth = length (fragToLight);
57
+ float shadow = 0.0 ;
58
+ float bias = 0.15 ;
59
+ int samples = 20 ;
60
+ float viewDistance = length (viewPos - fragPos);
61
+ float diskRadius = (1.0 + (viewDistance / far_plane)) / 25.0 ;
62
+ for (int i = 0 ; i < samples; ++ i)
63
+ {
64
+ float closestDepth = texture(depthMap[index], fragToLight + gridSamplingDisk[i] * diskRadius).r;
65
+ closestDepth *= far_plane; // undo mapping [0;1]
66
+ if (currentDepth - bias > closestDepth)
67
+ shadow += 1.0 ;
68
+ }
69
+ shadow /= float (samples);
70
+
71
+ return shadow;
72
+ }
73
+
74
+ // ----------------------------------------------------------------------------
75
+ float DistributionGGX(vec3 N, vec3 H, float roughness) {
76
+ float a = roughness * roughness;
77
+ float a2 = a * a;
78
+ float NdotH = max (dot (N, H), 0.0 );
79
+ float NdotH2 = NdotH * NdotH;
80
+
81
+ float denom = (NdotH2 * (a2 - 1.0 ) + 1.0 );
82
+ denom = PI * denom * denom;
83
+
84
+ return a2 / denom;
85
+ }
86
+ // ----------------------------------------------------------------------------
87
+ float GeometrySchlickGGX(float NdotV, float roughness) {
88
+ float k = (roughness + 1.0 );
89
+ k = (k * k) / 8.0 ;
90
+
91
+ return NdotV / (NdotV * (1.0 - k) + k);
92
+ }
93
+ // ----------------------------------------------------------------------------
94
+ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) {
95
+ float NdotV = max (dot (N, V), 0.0 );
96
+ float NdotL = max (dot (N, L), 0.0 );
97
+ return GeometrySchlickGGX(NdotV, roughness) * GeometrySchlickGGX(NdotL, roughness);
98
+ }
99
+ // ----------------------------------------------------------------------------
100
+ vec3 fresnelSchlick(float cosTheta, vec3 F0) {
101
+ return F0 + (1.0 - F0) * pow (clamp (1.0 - cosTheta, 0.0 , 1.0 ), 5.0 );
102
+ }
103
+ // ----------------------------------------------------------------------------
29
104
30
105
31
106
32
107
void main()
33
108
{
34
- vec3 MaterialDiffuseColor = texture(DiffuseTextureSampler, UV).rgb;
109
+ vec3 albedo = vec3 (0 , 0.851 , 0.651 );
110
+ float roughness = 0.5 ;
111
+ float metallic = 1 ;
112
+ vec3 N = TrueNormal;
113
+ vec3 V = normalize (viewPos - FragPos.xyz);
114
+ // Reflectance at normal incidence
115
+ vec3 F0 = mix (vec3 (0.04 ), albedo, metallic);
35
116
36
- float MaterialRoughness = Roughness;
37
- if (MaterialRoughness == - 1 )
38
- MaterialRoughness = texture(RoughnessTextureSampler, UV).r;
117
+ vec3 Lo = vec3 (0.0 );
118
+ for (int i = 0 ; i < MAXLIGHTS; ++ i) {
119
+ if (LightRadius[i] == 0 ) continue ;
120
+
121
+ // Calculate distance between light and fragment
122
+ vec3 L = normalize (LightPositions_worldspace[i] - FragPos.xyz);
123
+ float distance = length (LightPositions_worldspace[i] - FragPos.xyz);
124
+ float attenuation = 1.0 / (1.0 + LightLinears[i] * distance + LightQuadratics[i] * (distance * distance ));
125
+ vec3 radiance = LightColors[i] * attenuation;
126
+
127
+ // Cook-Torrance BRDF
128
+ vec3 H = normalize (V + L);
129
+ float NDF = DistributionGGX(N, H, roughness);
130
+ float G = GeometrySmith(N, V, L, roughness);
131
+ vec3 F = fresnelSchlick(max (dot (H, V), 0.0 ), F0);
132
+
133
+ vec3 numerator = NDF * G * F;
134
+ float denominator = 4.0 * max (dot (N, V), 0.0 ) * max (dot (N, L), 0.0 ) + 0.0001 ;
135
+ vec3 specular = numerator / denominator;
136
+
137
+ vec3 kS = F;
138
+ vec3 kD = (1.0 - kS) * (1.0 - metallic);
139
+ float NdotL = max (dot (N, L), 0.0 );
140
+ // the 1.3 makes it a little brighter
141
+ Lo += ((kD * albedo) / PI + specular) * radiance * NdotL;
142
+
143
+ }
144
+
145
+ vec3 color = Lo * 2 + albedo * vec3 (0.2 );
39
146
40
- float MaterialMetalic = Metalic;
41
- if (MaterialRoughness == - 1 )
42
- MaterialMetalic = texture(MetalicTextureSampler, UV).r;
43
-
44
- // Sample the normal map and transform it to world space using the TBN matrix
45
- vec3 normalMap = texture(NormalTextureSampler, UV).rgb;
46
- normalMap = normalMap * 2.0 - 1.0 ; // Convert from [0,1] range to [-1,1]
47
- vec3 transformedNormal = normalize (TBN * normalMap);
48
-
49
147
148
+ // HDR to ldexp
149
+ color = color / (color + vec3 (1.0 ));
150
+ vec4 waterColor = vec4 (color ,0.7 );
151
+ // waterColor = vec4(N,1);
50
152
// store the fragment position vector in the first gbuffer texture
51
153
gPosition = FragPos;
52
154
// also store the per-fragment normals into the gbuffer
53
155
gRMA = vec4 (0.1 ,1 ,0 ,0 );
54
- gNormal = vec4 (transformedNormal , 0 );
55
- gAlbedo = vec4 ( 0 , 0 , 1 , 0.2 ) ; // RGB for Albedo, R for Specular Intensity
156
+ gNormal = vec4 (N , 0 );
157
+ gAlbedo = waterColor ; // RGB for Albedo, R for Specular Intensity
56
158
gTrueNormal = vec4 (TrueNormal,0 ); // Use transpose of TBN to inverse the transformation
57
- if (IsEmissive){
58
- gEmission = vec4 (MaterialDiffuseColor, 1 );
59
- }
60
-
61
-
62
-
63
159
}
0 commit comments