-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathStandard.ovfx
More file actions
250 lines (208 loc) · 5.96 KB
/
Standard.ovfx
File metadata and controls
250 lines (208 loc) · 5.96 KB
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
#pass SHADOW_PASS
#pass ALBEDO_PASS
#pass METALLIC_PASS
#pass ROUGHNESS_PASS
#pass AO_PASS
#pass NORMAL_PASS
#pass UV_PASS
#pass DEPTH_PASS
#feature PARALLAX_MAPPING
#feature ALPHA_CLIPPING
#feature ALPHA_DITHERING
#feature NORMAL_MAPPING
#feature DISTANCE_FADE
#feature SPECULAR_WORKFLOW
#feature GAMMA_CORRECTION
#shader vertex
#version 450 core
#include ":Shaders/Common/Buffers/EngineUBO.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"
layout (location = 0) in vec3 geo_Pos;
layout (location = 1) in vec2 geo_TexCoords;
layout (location = 2) in vec3 geo_Normal;
layout (location = 3) in vec3 geo_Tangent;
layout (location = 4) in vec3 geo_Bitangent;
out VS_OUT
{
vec3 FragPos;
vec2 TexCoords;
vec3 Normal;
mat3 TBN;
#if defined(PARALLAX_MAPPING)
vec3 TangentViewPos;
vec3 TangentFragPos;
#endif
} vs_out;
void main()
{
vs_out.FragPos = vec3(ubo_Model * vec4(geo_Pos, 1.0));
vs_out.TexCoords = geo_TexCoords;
vs_out.Normal = normalize(mat3(transpose(inverse(ubo_Model))) * geo_Normal);
vs_out.TBN = ConstructTBN(ubo_Model, geo_Normal, geo_Tangent, geo_Bitangent);
#if defined(PARALLAX_MAPPING)
const mat3 TBNi = transpose(vs_out.TBN);
vs_out.TangentViewPos = TBNi * ubo_ViewPos;
vs_out.TangentFragPos = TBNi * vs_out.FragPos;
#endif
gl_Position = ubo_Projection * ubo_View * vec4(vs_out.FragPos, 1.0);
}
#shader fragment
#version 450 core
#include ":Shaders/Common/Buffers/EngineUBO.ovfxh"
#include ":Shaders/Lighting/PBR.ovfxh"
in VS_OUT
{
vec3 FragPos;
vec2 TexCoords;
vec3 Normal;
mat3 TBN;
#if defined(PARALLAX_MAPPING)
vec3 TangentViewPos;
vec3 TangentFragPos;
#endif
} fs_in;
uniform sampler2D u_AlbedoMap;
uniform vec4 u_Albedo = vec4(1.0);
uniform sampler2D u_AmbientOcclusionMap;
uniform sampler2D u_MaskMap;
#if defined(SPECULAR_WORKFLOW)
uniform sampler2D u_SpecularMap;
uniform float u_Specular = 1.0;
uniform sampler2D u_GlossinessMap;
uniform float u_Glossiness = 1.0;
#else
uniform sampler2D u_MetallicMap;
uniform float u_Metallic = 1.0;
uniform sampler2D u_RoughnessMap;
uniform float u_Roughness = 1.0;
#endif
#if defined(NORMAL_MAPPING)
uniform sampler2D u_NormalMap;
#endif
#if defined(PARALLAX_MAPPING)
uniform sampler2D u_HeightMap;
uniform float u_HeightScale = 0.05;
uniform bool u_ParallaxClipEdges = false;
uniform int u_MinLayers = 8;
uniform int u_MaxLayers = 64;
#endif
#if defined(ALPHA_CLIPPING)
uniform float u_AlphaClippingThreshold = 0.1;
#endif
#if defined(SHADOW_PASS)
#undef PARALLAX_MAPPING // Disable parallax mapping in shadow pass
uniform float u_ShadowClippingThreshold = 0.5;
#endif
#if defined(DISTANCE_FADE)
uniform float u_DistanceFadeStart = 100.0;
uniform float u_DistanceFadeLength = 10.0;
#endif
uniform vec2 u_TextureTiling = vec2(1.0, 1.0);
uniform vec2 u_TextureOffset = vec2(0.0, 0.0);
uniform sampler2D _ShadowMap;
uniform mat4 _LightSpaceMatrix;
out vec4 FRAGMENT_COLOR;
void main()
{
#if defined(DISTANCE_FADE)
const float distanceAlpha = DistanceFadeAlpha(fs_in.FragPos, ubo_ViewPos, u_DistanceFadeStart, u_DistanceFadeLength);
const float ditheredDistanceAlpha = Dithering(distanceAlpha, gl_FragCoord.xy);
if (ditheredDistanceAlpha < 0)
{
discard;
}
#endif
#if defined(DEPTH_PASS)
const float near = 0.1;
const float far = 50.0;
const float depth = gl_FragCoord.z;
const float z = depth * 2.0 - 1.0; // back to NDC
float linearDepth = (2.0 * near * far) / (far + near - z * (far - near)) / far;
FRAGMENT_COLOR = vec4(vec3(linearDepth), 1.0);
return;
#endif
vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset);
#if defined(PARALLAX_MAPPING)
texCoords = ApplyParallaxOcclusionMapping(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale, u_MinLayers, u_MaxLayers);
if (u_ParallaxClipEdges && IsParallaxOutOfBounds(texCoords, u_TextureTiling, u_TextureOffset, ubo_Projection))
{
discard;
}
#endif
vec4 albedo = texture(u_AlbedoMap, texCoords) * u_Albedo;
albedo.a *= texture(u_MaskMap, texCoords).r;
#if defined(SHADOW_PASS)
if (albedo.a < u_ShadowClippingThreshold)
{
discard;
}
#else
#if defined(ALPHA_CLIPPING)
if (albedo.a < u_AlphaClippingThreshold)
{
discard;
}
#endif
#if defined(ALPHA_DITHERING)
const float ditheredAlpha = Dithering(albedo.a, gl_FragCoord.xy);
if (ditheredAlpha < 0)
{
discard;
}
#endif
#if defined(NORMAL_MAPPING)
const vec3 normal = ComputeNormal(texCoords, fs_in.Normal, u_NormalMap, fs_in.TBN);
#else
const vec3 normal = normalize(fs_in.Normal);
#endif
#if defined(NORMAL_PASS)
FRAGMENT_COLOR = vec4((normal + 1.0) / 2.0, 1.0);
return;
#endif
const float ao = texture(u_AmbientOcclusionMap, texCoords).r;
#if defined(AO_PASS)
FRAGMENT_COLOR = vec4(ao.rrr, 1.0);
return;
#endif
#if defined(SPECULAR_WORKFLOW)
const float specular = texture(u_SpecularMap, texCoords).r * u_Specular;
const float glossiness = texture(u_GlossinessMap, texCoords).r * u_Glossiness;
const float metallic = 1.0 - specular;
const float roughness = 1.0 - glossiness;
#else
const float metallic = texture(u_MetallicMap, texCoords).r * u_Metallic;
const float roughness = texture(u_RoughnessMap, texCoords).r * u_Roughness;
#endif
#if defined(ALBEDO_PASS)
FRAGMENT_COLOR = vec4(albedo.rgba);
return;
#endif
#if defined(METALLIC_PASS)
FRAGMENT_COLOR = vec4(metallic.rrr, 1.0);
return;
#endif
#if defined(ROUGHNESS_PASS)
FRAGMENT_COLOR = vec4(roughness.rrr, 1.0);
return;
#endif
#if defined(UV_PASS)
FRAGMENT_COLOR = vec4(texCoords.x, texCoords.y, 0.0, 1.0);
return;
#endif
vec3 pbr = PBRLightingModel(
albedo.rgb,
metallic,
roughness,
ao,
normal,
ubo_ViewPos,
fs_in.FragPos,
_ShadowMap,
_LightSpaceMatrix
);
#if defined(GAMMA_CORRECTION)
pbr = pow(pbr, vec3(1.0 / 2.2));
#endif
FRAGMENT_COLOR = vec4(pbr, albedo.a);
#endif
}