-
Notifications
You must be signed in to change notification settings - Fork 4
/
defaultMaterial.frag.glsl
157 lines (121 loc) · 5 KB
/
defaultMaterial.frag.glsl
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
#include "include/common.glsl"
#include "include/lightmap.glsl"
#include "include/uniforms.glsl"
#include "include/varying_material.glsl"
#include_if(NUM_DLIGHTS) "include/dlights.glsl"
#include_if(APPLY_FOG) "include/fog.glsl"
#include_if(APPLY_GREYSCALE) "include/greyscale.glsl"
#include_if(APPLY_OFFSETMAPPING) "include/material_offsetmapping.frag.glsl"
#include_if(NUM_LIGHTMAPS) "include/material_lightmaps.frag.glsl"
#include_if(APPLY_DIRECTIONAL_LIGHT) "include/material_dirlight.frag.glsl"
uniform sampler2D u_BaseTexture;
uniform sampler2D u_NormalmapTexture;
uniform sampler2D u_GlossTexture;
#ifdef APPLY_DECAL
uniform sampler2D u_DecalTexture;
#endif
#ifdef APPLY_ENTITY_DECAL
uniform sampler2D u_EntityDecalTexture;
#endif
#if defined(APPLY_OFFSETMAPPING) || defined(APPLY_RELIEFMAPPING)
uniform float u_OffsetMappingScale;
#endif
#ifdef APPLY_DRAWFLAT
uniform myhalf3 u_WallColor;
uniform myhalf3 u_FloorColor;
#endif
uniform myhalf2 u_GlossFactors; // gloss scaling and exponent factors
void main()
{
#if defined(APPLY_OFFSETMAPPING) || defined(APPLY_RELIEFMAPPING)
// apply offsetmapping
vec2 TexCoordOffset = OffsetMapping(u_NormalmapTexture, v_TexCoord_FogCoord.st, v_EyeVector, u_OffsetMappingScale);
#define v_TexCoord TexCoordOffset
#else
#define v_TexCoord v_TexCoord_FogCoord.st
#endif
myhalf3 surfaceNormal;
myhalf3 surfaceNormalModelspace;
myhalf3 weightedDiffuseNormalModelspace;
myhalf4 color = myhalf4 (1.0, 1.0, 1.0, 1.0);
myhalf4 decal = myhalf4 (0.0, 0.0, 0.0, 1.0);
myhalf3 specular = myhalf3 (0.0);
// get the surface normal
surfaceNormal = normalize(myhalf3(qf_texture (u_NormalmapTexture, v_TexCoord)) - myhalf3 (0.5));
surfaceNormalModelspace = normalize(v_StrMatrix * surfaceNormal);
myhalf3 lightColor = myhalf3 (0.0);
#if defined(APPLY_LIGHTING)
#ifdef APPLY_VERTEX_LIGHTING
lightColor += qf_FrontColor.rgb * LinearColor(u_LightstyleColor[0]); // qf_FrontColor is already linear
#endif
#ifdef APPLY_DIRECTIONAL_LIGHT
lightColor += DirectionalLightColor(surfaceNormalModelspace, weightedDiffuseNormalModelspace);
#endif
#ifdef NUM_LIGHTMAPS
lightColor += DeluxemapColor(surfaceNormalModelspace, weightedDiffuseNormalModelspace);
#endif
#if defined(NUM_DLIGHTS)
lightColor += DynamicLightsColor(v_Position, surfaceNormalModelspace);
#endif // NUM_DLIGHTS
lightColor *= u_LightingIntensity;
#endif // APPLY_DIRECTIONAL_LIGHT || NUM_LIGHTMAPS || APPLY_VERTEX_LIGHTING
#ifdef APPLY_SPECULAR
#ifdef NORMALIZE_DIFFUSE_NORMAL
myhalf3 specularNormal = normalize (myhalf3 (normalize (weightedDiffuseNormalModelspace)) + myhalf3 (normalize (u_EntityDist - v_Position.xyz)));
#else
myhalf3 specularNormal = normalize (weightedDiffuseNormalModelspace + myhalf3 (normalize (u_EntityDist - v_Position.xyz)));
#endif
myhalf specularProduct = myhalf(dot (surfaceNormalModelspace, specularNormal));
specular = myhalf3(qf_texture(u_GlossTexture, v_TexCoord).r * u_GlossFactors.x * pow(myhalf(max(specularProduct, 0.0)), u_GlossFactors.y));
#endif // APPLY_SPECULAR
#if defined(APPLY_BASETEX_ALPHA_ONLY) && !defined(APPLY_DRAWFLAT)
color.rgb = lightColor.rgb + specular.rgb;
color = min(color, myhalf4(qf_texture(u_BaseTexture, v_TexCoord).a));
#else
myhalf4 diffuse;
#ifdef APPLY_DRAWFLAT
myhalf n = myhalf(step(DRAWFLAT_NORMAL_STEP, abs(v_StrMatrix[2].z)));
diffuse = myhalf4(mix(u_WallColor, u_FloorColor, n), myhalf(qf_texture(u_BaseTexture, v_TexCoord).a));
#else
diffuse = myhalf4(qf_texture(u_BaseTexture, v_TexCoord));
#endif
#ifdef APPLY_ENTITY_DECAL
myhalf3 entColor = LinearColor(u_EntityColor.rgb);
#ifdef APPLY_ENTITY_DECAL_ADD
decal.rgb = myhalf3(qf_texture(u_EntityDecalTexture, v_TexCoord));
diffuse.rgb += entColor * decal.rgb;
#else
decal = myhalf4(entColor, 1.0) * myhalf4(qf_texture(u_EntityDecalTexture, v_TexCoord));
diffuse.rgb = mix(diffuse.rgb, decal.rgb, decal.a);
#endif // APPLY_ENTITY_DECAL_ADD
#endif // APPLY_ENTITY_DECAL
color = myhalf4(lightColor.rgb * (diffuse.rgb + specular.rgb), diffuse.a);
#endif // defined(APPLY_BASETEX_ALPHA_ONLY) && !defined(APPLY_DRAWFLAT)
#ifdef APPLY_DECAL
#ifdef APPLY_DECAL_ADD
decal.rgb = myhalf3(qf_FrontColor.rgb) * myhalf3(qf_texture(u_DecalTexture, v_TexCoord));
color.rgb += decal.rgb;
#else
decal = myhalf4(qf_FrontColor.rgb, 1.0) * myhalf4(qf_texture(u_DecalTexture, v_TexCoord));
color.rgb = mix(color.rgb, decal.rgb, decal.a);
#endif // APPLY_DECAL_ADD
color.a *= myhalf(qf_FrontColor.a);
#else // APPLY_DECAL
# if defined(APPLY_ENV_MODULATE_COLOR)
color *= myhalf4(qf_FrontColor);
# else
color.a *= myhalf(qf_FrontColor.a);
# endif
#endif // APPLY_DECAL
#ifdef QF_ALPHATEST
QF_ALPHATEST(color.a);
#endif
#ifdef APPLY_GREYSCALE
color.rgb = Greyscale(color.rgb);
#endif
#if defined(APPLY_FOG) && !defined(APPLY_FOG_COLOR)
myhalf fogDensity = FogDensity(v_TexCoord_FogCoord.pq);
color.rgb = mix(color.rgb, LinearColor(u_FogColor), fogDensity);
#endif
qf_FragColor = vec4(sRGBColor(color));
}