func_detail, compositor shuffle, better kernal filters
[tar-legacy.git] / MCDV / shaders / ss_comp_main.fs
1 #version 330 core
2 // Note:: All channels marked with an ** are currently not filled out by the engine.
3
4 // OPENGL
5 // ____________________________________________________________________________________________
6 in vec2 TexCoords;
7 out vec4 FragColor;
8
9 // UNIFORMS
10 // Vector Information _________________________________________________________________________
11 // ( A bunch of vectors that give you the location of different entities )
12 uniform vec3 bounds_NWU; // North-West-Upper coordinate of the playspace (worldspace)
13 uniform vec3 bounds_SEL; // South-East-Lower coordinate of the playspace (worldspace)
14 uniform vec2 bounds_NWU_SS; // North-West coordinate of the playspace (screen space)
15 uniform vec2 bounds_SEL_SS; // South-East coordinate of the playspace (screen space)
16
17 uniform vec2 pos_spawn_ct; // Location of the CT Spawn (0-1)
18 uniform vec2 pos_spawn_t; // Location of the T Spawn (0-1)
19 uniform vec2 bombsite_a; // Location of bomsite A (0-1)
20 uniform vec2 bombsite_b; // Location of bombsite B (0-1)
21
22 // SAMPLER UNIFORMS
23 // Image Inputs _______________________________________________________________________________
24 // ( Standard generated maps from the engine )
25 uniform sampler2D tex_background; // Background texture
26 uniform sampler2D tex_playspace; // Playspace
27 // R: Height (Regular)
28 // G: Height (Reverse rendering order)
29 // **B: Baked Lighting
30 // A: Playable Space (0 or 1)
31
32 uniform sampler2D tex_objectives; // Objectives
33 // R: Buzones (0 or 1)
34 // G: none
35 // B: none
36 // A: Buyzones & Bombsites (mask 0-1)
37
38 uniform sampler2D tex_props; // Props
39 // **R: Height (0-1 normalized)
40 // **G: none
41 // **B: none
42 // **A: Props (0 or 1)
43
44 uniform sampler2D tex_gradient; // Gradient input
45 // RGBA: 256x1 image defining a gradient
46
47 uniform sampler2D texture0; // Custom Image input 3 (**RGBA)
48 uniform sampler2D texture1; // Custom Image input 4 (**RGBA)
49 uniform sampler2D texture2; // Custom Image input 5 (**RGBA)
50
51 // SHADER HELPERS
52 // ____________________________________________________________________________________________
53 // ( A collection of simple blend modes )
54
55 vec3 lerp(vec3 a, vec3 b, float w)
56 {
57 return a + w*(b-a);
58 }
59
60 vec4 blend_normal(vec4 a, vec4 b, float s)
61 {
62 return vec4(lerp(a.rgb, b.rgb, b.a * s), a.a + (b.a * s));
63 }
64
65 vec4 blend_add(vec4 a, vec4 b, float s)
66 {
67 return vec4(a.rgb + (b.rgb * s), a.a);
68 }
69
70 // -------------------------------------- sample helpers --------------------------------------
71
72 vec4 sample_gradient(float height)
73 {
74 return vec4(texture(tex_gradient, vec2(height, 0)));
75 }
76
77 float get_playspace(vec4 sample_playspace) { return sample_playspace.a; }
78 float get_playspace_height(vec4 sample_playspace) { return sample_playspace.a * sample_playspace.r; }
79 float get_playspace_inverse_height(vec4 sample_playspace) { return sample_playspace.a * sample_playspace.g; }
80
81 float get_height(vec4 sample_playspace) { return sample_playspace.g; }
82 float get_baked_light(vec4 sample_playspace) { return sample_playspace.r; }
83
84 // -------------------------------------- kernel filters --------------------------------------
85 // Given an 0-1 mask, return a 'glow value'
86 float kernel_filter_glow(sampler2D sampler, int channelID = 0, int sample_size = 16)
87 {
88 vec2 pixel_size = 1.0 / vec2(textureSize(sampler, 0));
89
90 float sT = 0;
91 int sample_double = sample_size * 2;
92
93 // Process kernel
94 for(int x = 0; x <= sample_double; x++){
95 for(int y = 0; y <= sample_double; y++){
96 sT += texture(sampler, TexCoords + vec2((-sample_size + x) * pixel_size.x, (-sample_size + y) * pixel_size.y))[channelID];
97 }
98 }
99
100 sT /= (sample_double * sample_double);
101
102 return sT;
103 }
104
105 // Given a 0-1 mask, return an outline drawn around that mask
106 float kernel_filter_outline(sampler2D sampler, int channelID = 0, int sample_size = 2)
107 {
108 vec2 pixel_size = 1.0 / vec2(textureSize(sampler, 0));
109
110 float sT = 0;
111 int sample_double = sample_size * 2;
112
113 // Process kernel
114 for(int x = 0; x <= sample_double; x++){
115 for(int y = 0; y <= sample_double; y++){
116 sT += //texture(sampler, TexCoords + vec2((-sample_size + x) * pixel_size.x, (-sample_size + y) * pixel_size.y))[channelID];
117 (sample_size - min(length(vec2(-sample_size + x, -sample_size + y)), sample_size)) *
118 texture(sampler, TexCoords + vec2((-sample_size + x) * pixel_size.x, (-sample_size + y) * pixel_size.y))[channelID];
119 }
120 }
121
122 return max(min(sT, 1) - texture(sampler, TexCoords)[channelID], 0);
123 }
124
125 // SHADER PROGRAM
126 // ____________________________________________________________________________________________
127 // ( Write all your shader code & functions here )
128 vec4 outline_color = vec4(0.8, 0.8, 0.8, 0.6);
129 vec4 ao_color = vec4(0.0, 0.0, 0.0, 1.0);
130
131 vec4 buyzone_color = vec4(0.180, 0.828, 0.225, 0.667);
132 vec4 objective_color = vec4(0.770, 0.295, 0.171, 1.000);
133
134 void main()
135 {
136 vec4 sBackground = vec4(texture(tex_background, TexCoords));
137 vec4 sPlayspace = vec4(texture(tex_playspace, TexCoords));
138 vec4 sObjectives = vec4(texture(tex_objectives, TexCoords));
139
140 vec4 final = sBackground;
141 final = blend_normal(final, ao_color, kernel_filter_glow(tex_playspace, 3, 16)); // Drop shadow
142 final = blend_normal(final, sample_gradient(get_playspace_height(sPlayspace)), get_playspace(sPlayspace)); // Playspace
143 final = blend_normal(final, outline_color, kernel_filter_outline(tex_playspace, 3, 2)); // Outline
144
145 //final = blend_normal(final, objective_color, sObjectives.r * sObjectives.a); // Objectives
146 //final = blend_normal(final, buyzone_color, sObjectives.g * sObjectives.a); // Buyzones
147 // Return the final output color
148 FragColor = final;
149 }