1
/* jaguar_company_cobra_mk3.fragment for the Jaguar Company.
3
* Copyright © 2012 Tricky
5
* This work is licensed under the Creative Commons
6
* Attribution-Noncommercial-Share Alike 3.0 Unported License.
8
* To view a copy of this license, visit
9
* http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter
10
* to Creative Commons, 171 Second Street, Suite 300, San Francisco,
11
* California, 94105, USA.
13
* Cobra MK3 fragment shader for the Jaguar Company.
14
* Based on Griff's multi-decal fragment shader.
17
uniform sampler2D uColorMap;
18
uniform sampler2D uNormalMap;
19
uniform sampler2D uEffectsMap;
20
uniform sampler2D uDecalMap;
22
varying vec2 vTexCoord;
23
varying vec3 vEyeVector; // These are all in tangent space
24
varying vec3 vLight0Vector;
25
varying vec3 vLight1Vector;
28
const float KspecExponent = 5.0;
29
const vec3 kLampColorNoAlert = vec3(0);
30
const vec3 kLampColorYellowAlert = vec3(0.9926, 0.9686, 0.7325);
31
const vec3 kLampColorRedAlert = vec3(1.0, 0.1, 0.0);
33
/* Uniforms from Oolite */
34
uniform bool uHostileTarget;
35
uniform bool uStationAegis;
36
uniform int uScannedShips;
38
uniform bool uNearlyDead;
39
uniform float uHullHeatLevel;
40
uniform float uEnginePower;
41
uniform vec4 uPaintColor1; // used with paintmask map to tint diffuse texture
42
uniform vec4 uPaintColor2; // used with paintmask map to tint diffuse texture
43
/* This is separated because Oolite 1.76.1 and previous do not pass the 4th parameter
44
* in a vec4 vector. Future versions will do so.
46
uniform vec4 uDecal_XY_Scale;
47
uniform float uDecalRotation;
49
/* Irregular flickering function */
50
#ifdef OO_REDUCED_COMPLEXITY
51
#define Pulse(v, ts) ((v) * 0.95)
52
#define Blink_on_off(value) (1.0)
54
float Pulse(float value, float timeScale)
56
float t = uTime * timeScale;
60
float sum = abs( s0 - 0.5);
62
float s1 = t * 0.7 - 0.05;
64
sum += abs(s1 - 0.5) - 0.25;
66
float s2 = t * 1.3 - 0.3;
68
sum += abs(s2 - 0.5) - 0.25;
70
float s3 = t * 5.09 - 0.6;
72
sum += abs(s3 - 0.5) - 0.25;
74
return (sum * 0.1 + 0.9) * value;
77
/* Hull Temperate heat glow effect */
78
vec3 TemperatureGlow(float level)
80
vec3 result = vec3(0);
83
result.g = level * level * level;
84
result.b = max(level - 0.7, 0.0) * 2.0;
89
/* Blink_on_off_function */
90
float Blink_on_off(float timeScale)
92
float result = step(0.5, sin(uTime * timeScale));
97
/* Cyan color exhaust glow effect */
98
vec3 cyanGlow(float level)
101
result.rgb = vec3(0.2, 0.7, 0.9) * level * 1.5;
106
/* Red/Orange color heated metal effect */
107
vec3 redGlow(float level)
110
result.rgb = vec3(1.5, 0.55, 0.2) * level * 1.3;
116
void Light(in vec3 lightVector, in vec3 normal, in vec3 lightColor, in vec3 eyeVector,
117
in float KspecExponent, inout vec3 totalDiffuse, inout vec3 totalSpecular)
119
lightVector = normalize(lightVector);
120
vec3 reflection = normalize(-reflect(lightVector, normal));
122
totalDiffuse += gl_FrontMaterial.diffuse.rgb * lightColor * max(dot(normal, lightVector), 0.0);
123
totalSpecular += lightColor * pow(max(dot(reflection, eyeVector), 0.0), KspecExponent);
126
#define LIGHT(idx, vector) Light(vector, normal, gl_LightSource[idx].diffuse.rgb, eyeVector, KspecExponent, diffuse, specular)
128
#ifndef OO_REDUCED_COMPLEXITY
129
/* function to read in the colour map then scale and position the decal map onto it.
131
* "the_decaliser" - this function scales & positions the decal image using data passed
132
* to it from the main shader
136
/* Setup the basic texture co-ords for the decals */
137
vec2 decal_TexCoord = vTexCoord;
139
/* Cache this value */
140
float tmp = 1.0 / (2.0 * uDecal_XY_Scale.p); // 1/2n = 0.5/n
142
/* Position the decal */
143
decal_TexCoord -= vec2(uDecal_XY_Scale.s, uDecal_XY_Scale.t - tmp);
145
/* Orientate & scale the decal */
146
float decal_s = sin(uDecalRotation);
147
float decal_c = cos(uDecalRotation);
148
decal_TexCoord *= mat2(decal_c, decal_s, -decal_s, decal_c); // it's -decal_s when exported back to oolite
150
decal_TexCoord += vec2(tmp);
151
decal_TexCoord *= vec2(uDecal_XY_Scale.p, uDecal_XY_Scale.p);
153
/* Get texture values */
154
vec4 decal_Tex = texture2D(uDecalMap, decal_TexCoord);
156
/* Modify the Decals texture co-oords */
157
decal_TexCoord.s += 1.0;
158
decal_Tex *= step(1.0, decal_TexCoord.s) *
159
step(0.0, decal_TexCoord.t) *
160
step(-2.0, -decal_TexCoord.s) *
161
step(-1.0, -decal_TexCoord.t);
163
/* Use the Alpha in the decal as a transparency mask so you can 'cutout' your decal from it's background */
164
float alpha = decal_Tex.a;
166
/* Return the scaled, position & rotated decal, it's mixed into the colour texture further on in the shader */
167
return alpha * decal_Tex;
173
vec3 eyeVector = normalize(vEyeVector);
174
vec2 texCoord = vTexCoord;
175
vec3 normal = normalize(texture2D(uNormalMap, vTexCoord).rgb - 0.5);
176
vec3 colorMap = texture2D(uColorMap, texCoord).rgb;
177
vec4 effectsMap = texture2D(uEffectsMap, texCoord);
178
vec3 diffuse = vec3(0.0), specular = vec3(0);
179
float specIntensity = texture2D(uColorMap, texCoord).a;
180
float glowMap = texture2D(uNormalMap, texCoord).a;
181
vec3 LampColor = vec3(0.9926, 0.9686, 0.7325);
184
#ifdef OO_LIGHT_0_FIX
185
LIGHT(0, vLight0Vector);
187
LIGHT(1, vLight1Vector);
188
diffuse += gl_FrontMaterial.ambient.rgb * gl_LightModel.ambient.rgb;
190
#ifndef OO_REDUCED_COMPLEXITY
191
/* Full Shader Mode - Repaint the hull */
192
colorMap += effectsMap.g * uPaintColor1.rgb;
193
colorMap += effectsMap.a * uPaintColor2.rgb;
195
/* Full Shader Mode - Apply the decal */
196
vec4 decal = the_decaliser();
198
/* We want to give the decals a matte finish, one way to achieve this is to mix
199
* them into the final texture fragment result after the lighting has been calculated, so we'll
200
* store the texture + lighting in a vec3 called colour_temp
202
vec3 color_temp = colorMap; // temp vec3 to store the texturemap & lighting
204
/* Calculate the lighting for full shader mode */
205
color_temp += color_temp * specular * 6.0 * specIntensity;
207
/* Mix in the decal over the top of the texture now that the specular lighting has been calculated.
208
* The (0.3 * specular) adds a tiny bit of specular to the decal - remove for a fully matte finish
210
vec3 color = mix(color_temp, decal.rgb * diffuse + (0.3 * specular), decal.a) * diffuse;
213
/* Calculate the lighting for simple shader mode */
214
#ifdef OO_REDUCED_COMPLEXITY
215
vec3 color = diffuse * colorMap;
216
color += colorMap * specular * 6.0 * specIntensity;
217
/* Add in simple shader hull lights */
218
color += LampColor * glowMap;
221
/* these next glow effects are only available in full shader mode */
222
#ifndef OO_REDUCED_COMPLEXITY
223
if (uNearlyDead || uHostileTarget)
224
/* Low energy or we have a hostile target. */
227
if (uScannedShips > 4 || uStationAegis)
228
/* Other ships (other than Jaguar Company) near us or close to the main station. */
231
/* Nothing around. */
234
/* check Alert Level, Adjust Lamp Colour Accordingly
236
* Current alert condition - 0 for docked, 1 for green, 2 for yellow, 3 for red.
239
LampColor = (alertlevel > 2) ? kLampColorRedAlert * max(mod(uTime, 1.0) * 2.0, 0.5) : kLampColorYellowAlert;
241
LampColor = kLampColorNoAlert;
243
/* Add the hull lights glow effects, check if ship is throwingSparks, if so flicker the effects */
246
color += cyanGlow(effectsMap.b * Pulse(min(uEnginePower, 1.0), 1.0)) * Blink_on_off(Pulse(1.0, 0.7));
247
color += LampColor * glowMap * 7.0 * colorMap.g * Blink_on_off(Pulse(1.0, 1.0));
251
color += cyanGlow(effectsMap.b * Pulse(min(uEnginePower, 1.0), 1.0));
252
color += LampColor * glowMap * 7.0 * colorMap.g * Pulse(1.0, 0.3);
255
/* Add in the red heated metal glow around the exhaust */
256
color += redGlow(effectsMap.r * Pulse(min(uEnginePower, 1.0), 1.0));
258
/* Add the all over hull temperature glow. Full Shader mode only */
259
float hullHeat = max(uHullHeatLevel - 0.5, 0.0) * 2.0;
260
hullHeat = Pulse(hullHeat * hullHeat, 0.1);
261
color += TemperatureGlow(hullHeat);
264
gl_FragColor = vec4(color.rgb, 1.0);