File: main-f.sdr

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (496 lines) | stat: -rw-r--r-- 19,222 bytes parent folder | download
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
//? #version 150
#include "shadows.sdr" //! #include "shadows.sdr"
#include "lighting.sdr" //! #include "lighting.sdr"
#include "gamma.sdr" //! #include "gamma.sdr"

#conditional_include +"LARGE_SHADER" "main_large.sdr"
#conditional_include -"LARGE_SHADER" "main_small.sdr"

#define MAX_LIGHTS 8
#define LT_DIRECTIONAL		0
#define LT_POINT			1
#define LT_TUBE				2

struct model_light {
	vec4 position;

	vec3 diffuse_color;
	int light_type;

	vec3 direction;
	float attenuation;

	float ml_sourceRadius;

};

layout (std140) uniform modelData {
	mat4 modelViewMatrix;
	mat4 modelMatrix;
	mat4 viewMatrix;
	mat4 projMatrix;
	mat4 textureMatrix;
	mat4 shadow_mv_matrix;
	mat4 shadow_proj_matrix[4];
	mat4 envMatrix;

	vec4 color;

	model_light lights[MAX_LIGHTS];

	float outlineWidth;
	float fogStart;
	float fogScale;
	int buffer_matrix_offset;

	vec4 clip_equation;

	float thruster_scale;
	bool use_clip_plane;
	int n_lights;
	float defaultGloss;

	vec3 ambientFactor;
	int desaturate;

	vec3 diffuseFactor;
	int blend_alpha;

	vec3 emissionFactor;

	bool alphaGloss;

	bool gammaSpec;
	bool envGloss;
	int effect_num;
	int sBasemapIndex;


	vec4 fogColor;

	vec3 base_color;
	float anim_timer;

	vec3 stripe_color;
	float vpwidth;

	float vpheight;
	bool team_glow_enabled;
	float znear;
	float zfar;

	float veryneardist;
	float neardist;
	float middist;
	float fardist;

	int sGlowmapIndex;

	int sSpecmapIndex;
	int sNormalmapIndex;
	int sAmbientmapIndex;
	int sMiscmapIndex;

	float alphaMult;

	int flags;
};

in VertexOutput {
#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_ENV
	vec3 envReflect;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_ENV

	mat3 tangentMatrix;

#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_FOG
	float fogDist;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_FOG

	vec4 position;
	vec3 normal;
	vec4 texCoord;

#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_SHADOWS
	vec4 shadowUV[4];
	vec4 shadowPos;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_SHADOWS
} vertIn;

#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_DIFFUSE
uniform sampler2DArray sBasemap;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_DIFFUSE
#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_GLOW
uniform sampler2DArray sGlowmap;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_GLOW
#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_SPEC
uniform sampler2DArray sSpecmap;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_SPEC
#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_ENV
uniform samplerCube sEnvmap;
uniform samplerCube sIrrmap;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_ENV
#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_NORMAL
uniform sampler2DArray sNormalmap;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_NORMAL
#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_AMBIENT
uniform sampler2DArray sAmbientmap;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_AMBIENT
uniform sampler2D sFramebuffer;
#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_MISC
uniform sampler2DArray sMiscmap;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_MISC
#prereplace IF_FLAG_COMPILED MODEL_SDR_FLAG_SHADOWS
uniform sampler2DArray shadow_map;
#prereplace ENDIF_FLAG_COMPILED MODEL_SDR_FLAG_SHADOWS

out vec4 fragOut0;

#ifndef MODEL_SDR_FLAG_SHADOW_MAP
out vec4 fragOut1;
out vec4 fragOut2;
out vec4 fragOut3;
out vec4 fragOut4;
#endif

vec3 FresnelLazarovEnv(vec3 specColor, vec3 view, vec3 normal, float gloss)
{
	// Fresnel for environment lighting 
	// Equation referenced from Dimitar Lazarov's presentation titled Physically Based Rendering in Call of Duty: Black Ops
	return specColor + (vec3(1.0) - specColor) * pow(1.0 - clamp(dot(view, normal), 0.0, 1.0), 5.0) / (4.0 - 3.0 * gloss);
}

void GetLightInfo(int i, out vec3 lightDir, out float attenuation)
{
	lightDir = normalize(lights[i].position.xyz);
	attenuation = 1.0;
	if (lights[i].light_type != LT_DIRECTIONAL) {
		// Positional light source
		float dist = distance(lights[i].position.xyz, vertIn.position.xyz);
		lightDir = (lights[i].position.xyz - vertIn.position.xyz);

		if (lights[i].light_type == LT_TUBE) {  // Tube light
			float beamlength = length(lights[i].direction);
			vec3 beamDir = normalize(lights[i].direction);
			// Get nearest point on line
			float neardist = dot(vertIn.position.xyz - lights[i].position.xyz, beamDir);
			// Move back from the endpoint of the beam along the beam by the distance we calculated
			vec3 nearest = lights[i].position.xyz - beamDir * abs(neardist);
			lightDir = nearest - vertIn.position.xyz;
			dist = length(lightDir);
		}

		lightDir = normalize(lightDir);
		attenuation = 1.0 / (1.0 + lights[i].attenuation * dist);
	}
}
vec3 CalculateLighting(vec3 normal, vec3 diffuseMaterial, vec3 specularMaterial, float gloss, float fresnel, float shadow, float aoFactor)
{
	vec3 eyeDir = vec3(normalize(-vertIn.position).xyz);
	vec3 lightAmbient = (emissionFactor + ambientFactor * ambientFactor) * aoFactor; // ambientFactor^2 due to legacy OpenGL compatibility behavior
	vec3 lightDiffuse = vec3(0.0, 0.0, 0.0);
	vec3 lightSpecular = vec3(0.0, 0.0, 0.0);
	#pragma optionNV unroll all
	for (int i = 0; i < n_lights; ++i) {
        if(i > 0) {
            shadow = 1.0;
        }
		float roughness = clamp(1.0f - gloss, 0.0f, 1.0f);
		float alpha = roughness * roughness;
		vec3 lightDir;
		float attenuation;
		// gather light params
		GetLightInfo(i, lightDir, attenuation);
		vec3 halfVec = normalize(lightDir + eyeDir);
		float NdotL = clamp(dot(normal, lightDir), 0.0f, 1.0f);
		// Ambient, Diffuse, and Specular
		lightDiffuse += (lights[i].diffuse_color.rgb * diffuseFactor * NdotL * attenuation) * shadow;
		lightSpecular += lights[i].diffuse_color.rgb * computeLighting(specularMaterial, diffuseMaterial, lightDir, normal, halfVec, eyeDir, roughness, fresnel, NdotL) * attenuation * shadow;
	}
	return diffuseMaterial * lightAmbient + lightSpecular;
}

void main()
{
	#ifdef MODEL_SDR_FLAG_SHADOW_MAP
		// need depth and depth squared for variance shadow maps
		fragOut0 = vec4(vertIn.position.z, vertIn.position.z * vertIn.position.z * VARIANCE_SHADOW_SCALE_INV, 0.0, 1.0);

		return;
	#else
	vec3 eyeDir = vec3(normalize(-vertIn.position).xyz);
	vec2 texCoord = vertIn.texCoord.xy;

	// setup our baseline values for base, emissive, fresnel, gloss, AO and normal
	// default specular value is set below, as it depends on the diffuse color

	vec4 baseColor = color;
	vec4 emissiveColor = vec4(0.0, 0.0, 0.0, 1.0);
	float fresnelFactor = 0.0;
	float glossData = defaultGloss;
	vec2 aoFactors = vec2(1.0, 1.0);
	vec3 unitNormal = normalize(vertIn.normal);
	vec3 normal = unitNormal;

	#prereplace IF_FLAG MODEL_SDR_FLAG_AMBIENT
		// red channel is ambient occlusion factor which only affects ambient lighting.
		// green is cavity occlusion factor which only affects diffuse and specular lighting.
		aoFactors = texture(sAmbientmap, vec3(texCoord, float(sAmbientmapIndex))).xy;
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_AMBIENT

	#prereplace IF_FLAG MODEL_SDR_FLAG_NORMAL
	   // Normal map - convert from DXT5nm
	   vec2 normalSample;
	   normal.rg = normalSample = (texture(sNormalmap, vec3(texCoord, float(sNormalmapIndex))).ag * 2.0) - 1.0;
	   normal.b = clamp(sqrt(1.0 - dot(normal.rg, normal.rg)), 0.0001, 1.0);
	   normal = vertIn.tangentMatrix * normal;
	   float norm = length(normal);
	   // prevent breaking of normal maps
	   if (norm > 0.0)
		  normal /= norm;
	   else
		  normal = unitNormal;
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_NORMAL

	vec2 distort = vec2(0.0, 0.0);

	if (effect_num >= 0) {
		distort = vec2(cos(vertIn.position.x*vertIn.position.w*0.005+anim_timer*20.0)*sin(vertIn.position.y*vertIn.position.w*0.005),sin(vertIn.position.x*vertIn.position.w*0.005+anim_timer*20.0)*cos(vertIn.position.y*vertIn.position.w*0.005))*0.03;
	}

	#prereplace IF_FLAG MODEL_SDR_FLAG_DIFFUSE
		vec2 diffuseTexCoord = texCoord;
		if (effect_num == 2) {
			diffuseTexCoord = texCoord + distort*(1.0-anim_timer);
		}
		baseColor = texture(sBasemap, vec3(diffuseTexCoord, float(sBasemapIndex)));

        #prereplace IF_FLAG MODEL_SDR_FLAG_HDR
			baseColor.rgb = srgb_to_linear(baseColor.rgb);
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_HDR

		#prereplace IF_FLAG MODEL_SDR_FLAG_ALPHA_MULT
			baseColor.a *= alphaMult;
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_ALPHA_MULT

		if ( blend_alpha == 0 && baseColor.a < 0.95 ) discard; // if alpha blending is not on, discard transparent pixels
		// premultiply alpha if blend_alpha is 1. assume that our blend function is srcColor + (1-Alpha)*destColor.
		// if blend_alpha is 2, assume blend func is additive and don't modify color
		if(blend_alpha == 1) baseColor.rgb = baseColor.rgb * baseColor.a;
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_DIFFUSE

	// Anti-glint "trick" based on Valve's "Advanced VR Rendering" talk at GDC2015:
	// http://media.steampowered.com/apps/valve/2015/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf Page 43
	// basically make surfaces rougher if local normals change too fast in screenspace 
	vec2 normDx = dFdx(unitNormal.xy);
	vec2 normDy = dFdy(unitNormal.xy);
	float glossGeo = 1.0f - pow(clamp(max(dot(normDx,normDx), dot(normDy,normDy)),0.0,1.0),0.33);
	glossData = min(glossData, glossGeo);

	// Now that we have a base color and min gloss value, compute the spec color
	vec4 specColor = vec4(baseColor.rgb * SPEC_FACTOR_NO_SPEC_MAP, glossData);
	
	#prereplace IF_FLAG MODEL_SDR_FLAG_SPEC
		specColor = texture(sSpecmap, vec3(texCoord, float(sSpecmapIndex)));
		#prereplace IF_FLAG MODEL_SDR_FLAG_ALPHA_MULT
			specColor *= alphaMult;
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_ALPHA_MULT

		if(alphaGloss) glossData = specColor.a;
		if(gammaSpec) {
			specColor.rgb = max(specColor.rgb, vec3(0.03f)); // hardcoded minimum specular value. read John Hable's blog post titled 'Everything Is Shiny'. 
			fresnelFactor = 1.0;
		}
		
		#prereplace IF_FLAG MODEL_SDR_FLAG_HDR
			specColor.rgb = srgb_to_linear(specColor.rgb);
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_HDR
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_SPEC

	baseColor.rgb *= aoFactors.y;
	specColor.rgb *= aoFactors.y;

	vec4 teamMask = vec4(0.0);
	vec3 team_color_glow = vec3(0.0);

	#prereplace IF_FLAG MODEL_SDR_FLAG_MISC
		#prereplace IF_FLAG MODEL_SDR_FLAG_TEAMCOLOR
			teamMask = texture(sMiscmap, vec3(texCoord, float(sMiscmapIndex)));

			//For team colors applied to a diffuse or spec map, we assume that the base color of the diffuse 
			//at this point is vec3(0.5). To get accurate results, we subtract 0.5 from the team colors
			vec3 color_offset = vec3(-0.5) * (teamMask.x + teamMask.y);

			vec3 team_color = base_color * teamMask.x + stripe_color * teamMask.y + color_offset;
			team_color_glow = (base_color * teamMask.b) + (stripe_color * teamMask.a);

			#prereplace IF_FLAG MODEL_SDR_FLAG_HDR
				baseColor.rgb = linear_to_srgb(baseColor.rgb);
				specColor.rgb = linear_to_srgb(specColor.rgb);
			#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_HDR

			baseColor.rgb += team_color;					
			baseColor.rgb = max(baseColor.rgb, vec3(0.0));	// We need to make sure that nothing here ever goes negative
			specColor.rgb += team_color;
			specColor.rgb = max(specColor.rgb, vec3(0.03));
			
			#prereplace IF_FLAG MODEL_SDR_FLAG_HDR
				baseColor.rgb = srgb_to_linear(baseColor.rgb);
				specColor.rgb = srgb_to_linear(specColor.rgb);
			#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_HDR
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_TEAMCOLOR
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_MISC

	// Lights aren't applied when we are rendering to the G-buffers since that gets handled later
	#prereplace IF_FLAG MODEL_SDR_FLAG_DEFERRED
		#prereplace IF_FLAG MODEL_SDR_FLAG_LIGHT
			// Ambient lighting still needs to be done since that counts as an "emissive" color
			vec3 lightAmbient = (emissionFactor + ambientFactor * ambientFactor) * aoFactors.x; // ambientFactor^2 due to legacy OpenGL compatibility behavior
			emissiveColor.rgb += baseColor.rgb * lightAmbient;
		#prereplace ELSE_FLAG //MODEL_SDR_FLAG_LIGHT
			#prereplace IF_FLAG MODEL_SDR_FLAG_SPEC
				baseColor.rgb += pow(1.0 - clamp(dot(eyeDir, normal), 0.0, 1.0), 5.0 * clamp(glossData, 0.01, 1.0)) * specColor.rgb;
			#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_SPEC
			// If there is no lighting then we copy the color data so far into the
			emissiveColor.rgb += baseColor.rgb;
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_LIGHT
	#prereplace ELSE_FLAG //MODEL_SDR_FLAG_DEFERRED
		#prereplace IF_FLAG MODEL_SDR_FLAG_LIGHT
			float shadow = 1.0;
			#prereplace IF_FLAG MODEL_SDR_FLAG_SHADOWS
				shadow = getShadowValue(shadow_map, -vertIn.position.z, vertIn.shadowPos.z, vertIn.shadowUV, fardist, middist, neardist, veryneardist);
			#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_SHADOWS
			baseColor.rgb = CalculateLighting(normal, baseColor.rgb, specColor.rgb, glossData, fresnelFactor, shadow, aoFactors.x);
		#prereplace ELSE_FLAG //MODEL_SDR_FLAG_LIGHT
			#prereplace IF_FLAG MODEL_SDR_FLAG_SPEC
				baseColor.rgb += pow(1.0 - clamp(dot(eyeDir, normal), 0.0, 1.0), 5.0 * clamp(glossData, 0.01, 1.0)) * specColor.rgb;
			#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_SPEC
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_LIGHT
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_DEFERRED

	#prereplace IF_FLAG MODEL_SDR_FLAG_ENV
		#prereplace IF_FLAG MODEL_SDR_FLAG_LIGHT
			// Bit of a hack here, pretend we're doing blinn-phong shading and use a (modified version of) the derivation from
			// Plausible Blinn-Phong Reflection of Standard Cube MIP-Maps - McGuire et al.
			// to determine the mip bias.
			// We use the specular term listed at http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
			// 1/(pi*alpha^2) * NdotM^((2/alpha^2)-2)
			// so let s = (2/alpha^2 - 2)
			// 1/2 log(s + 1) = 1/2 log(2/alpha^2 -1)

			const float ENV_REZ = 512; // Ideally this would be #define'd and shader recompiled with envmap rez changes
			const float REZ_BIAS = log2(ENV_REZ * sqrt(3));

			float roughness = clamp(1.0f - glossData, 0.0f, 1.0f);
			float alpha = roughness * roughness;
			float alphaSqr = alpha * alpha;
			float rough_bias = 0.5 * log2(2/alphaSqr - 1);
			float mip_bias = REZ_BIAS - rough_bias;

			// Sample light, using mip bias to blur it.
			vec3 light_dir = reflect(-eyeDir, normal);
			vec3 env_light_dir = vec3(envMatrix * vec4(light_dir, 0.0));
			vec4 specEnvColour = srgb_to_linear(textureLod(sEnvmap, env_light_dir, mip_bias));

			vec3 halfVec = normal;

			// Lots of hacks here to get things to look right. We aren't loading a BRDF split-sum integral texture 
			// or properly calculating IBL levels.
			// Fresnel calculation as with standard lights.

			vec3 fresnel = mix(specColor.rgb, FresnelSchlick(halfVec, eyeDir, specColor.rgb), specColor.a);

			// Pseudo-IBL, so use k_IBL 
			float k = alpha * alpha/ 2.0f;

			float NdotL = max(dot(light_dir, normal),0);

			float g1vNL = GeometrySchlickGGX(NdotL, k);
			vec3 specEnvLighting = specEnvColour.rgb * fresnel * g1vNL;

			vec3 kD = vec3(1.0)-fresnel;
			kD *= (vec3(1.0) - specColor.rgb);
			vec3 diffEnvColor = srgb_to_linear(texture(sIrrmap, vec3(envMatrix * vec4(normal, 0.0))).rgb);
			vec3 diffEnvLighting = kD * baseColor.rgb * diffEnvColor * aoFactors.x;
			emissiveColor.rgb += (specEnvLighting + diffEnvLighting) * baseColor.a;
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_LIGHT
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_ENV

	#prereplace IF_FLAG MODEL_SDR_FLAG_GLOW
		vec3 glowColor = texture(sGlowmap, vec3(texCoord, float(sGlowmapIndex))).rgb;
		#prereplace IF_FLAG MODEL_SDR_FLAG_MISC
			#prereplace IF_FLAG MODEL_SDR_FLAG_TEAMCOLOR
				float glowColorLuminance = dot(glowColor, vec3(0.299, 0.587, 0.114));
				glowColor = team_glow_enabled ? mix(max(team_color_glow, vec3(0.0)), glowColor, clamp(glowColorLuminance - teamMask.b - teamMask.a, 0.0, 1.0)) : glowColor;
			#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_TEAMCOLOR
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_MISC
		#prereplace IF_FLAG MODEL_SDR_FLAG_HDR
			glowColor = srgb_to_linear(glowColor) * GLOW_MAP_SRGB_MULTIPLIER;
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_HDR
		emissiveColor.rgb += glowColor * GLOW_MAP_INTENSITY;
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_GLOW

	#prereplace IF_FLAG MODEL_SDR_FLAG_ALPHA_MULT
		//Since this is emissive, we need to premultiply it for intensity, and apply alpha to not replace existing emissive
		emissiveColor *= alphaMult;
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_ALPHA_MULT

	#prereplace IF_FLAG MODEL_SDR_FLAG_FOG
		vec3 finalFogColor = fogColor.rgb;
		#prereplace IF_FLAG MODEL_SDR_FLAG_HDR
			finalFogColor = srgb_to_linear(finalFogColor);
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_HDR
		#prereplace IF_FLAG MODEL_SDR_FLAG_DIFFUSE
			if(blend_alpha == 1) finalFogColor *= baseColor.a;
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_DIFFUSE
		// This code will only be used by the forward renderer so we apply the fog effect to both the emissive and the base
		// color.
		baseColor.rgb = mix(emissiveColor.rgb + baseColor.rgb, finalFogColor, vertIn.fogDist);
		emissiveColor.rgb = vec3(0.0); // Zero the emissive color since it has already been added by the previous line
		specColor.rgb *= vertIn.fogDist;
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_FOG

	#prereplace IF_FLAG MODEL_SDR_FLAG_DIFFUSE
		if(desaturate == 1) {
			baseColor.rgb = color.rgb * dot(vec3(1.0), baseColor.rgb) * 0.3333333;
		}
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_DIFFUSE
	
	if (effect_num == 0) {
		float shinefactor = 1.0/(1.0 + pow(abs((fract(abs(texCoord.x))-anim_timer) * 1000.0), 2.0)) * 1000.0;
		emissiveColor.rgb += vec3(shinefactor);
		baseColor.a = baseColor.a * clamp(shinefactor * (fract(abs(texCoord.x))-anim_timer) * -10000.0,0.0,1.0);
	} else if (effect_num == 1) {
		float shinefactor = 1.0/(1.0 + pow(abs(vertIn.position.y-anim_timer), 2.0));
		emissiveColor.rgb += vec3(shinefactor);
		#prereplace IF_NOT_FLAG MODEL_SDR_FLAG_LIGHT
			// ATI Wireframe fix *grumble*
			baseColor.a = clamp((vertIn.position.y-anim_timer) * 10000.0,0.0,1.0);
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_LIGHT
	} else if (effect_num == 2) {
		vec2 screenPos = gl_FragCoord.xy * vec2(vpwidth,vpheight);
		baseColor.a = baseColor.a;
		float cloak_interp = (sin(vertIn.position.x*vertIn.position.w*0.005+anim_timer*20.0)*sin(vertIn.position.y*vertIn.position.w*0.005)*0.5)-0.5;
		#prereplace IF_FLAG MODEL_SDR_FLAG_LIGHT
			baseColor.rgb = mix(texture(sFramebuffer, screenPos + distort*anim_timer + anim_timer*0.1*normal.xy).rgb,baseColor.rgb,clamp(cloak_interp+anim_timer*2.0,0.0,1.0));
		#prereplace ELSE_FLAG //MODEL_SDR_FLAG_LIGHT
			baseColor.rgb = mix(texture(sFramebuffer, screenPos + distort*anim_timer + anim_timer*0.1*vertIn.normal.xy).rgb,baseColor.rgb,clamp(cloak_interp+anim_timer*2.0,0.0,1.0));
		#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_LIGHT
	}

	#prereplace IF_NOT_FLAG MODEL_SDR_FLAG_DEFERRED
		// emissive colors won't be added later when we are using forward rendering so we need to do that here
		baseColor.rgb += emissiveColor.rgb;
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_DEFERRED

	fragOut0 = baseColor;

	#prereplace IF_FLAG MODEL_SDR_FLAG_DEFERRED
		fragOut1 = vec4(vertIn.position.xyz, 1.0);
		fragOut2 = vec4(normal, glossData);
		fragOut3 = vec4(specColor.rgb, fresnelFactor);
		fragOut4 = emissiveColor;
	#prereplace ENDIF_FLAG //MODEL_SDR_FLAG_DEFERRED

	#endif
}