~harrison-rt/+junk/jaguar_company

« back to all changes in this revision

Viewing changes to tags/2.2/jaguar_company_2.2.oxp/Shaders/jaguar_company_cobra_mk3.fragment

  • Committer: Richard Harrison
  • Date: 2013-01-19 22:59:12 UTC
  • Revision ID: harrison.rt@gmail.com-20130119225912-jcnbiiwb94uy0k7v
Tags: 2.3
* Integration with Snoopers OXP if available.
* Pilot name for patrol, tug and miner ships.
* Pilot name transfered to escape pod and then used in rescue message on arrival at a station.
* Use pilot's name if available in attack messages. Otherwise use the displayName.
* Use pilot's name if available as the Snoopers news source. Otherwise use a random name.
* Force Snoopers news to be shown at the base.
* New const in the main script for Snoopers Error Codes.
* Make sure thargoids/tharglets are ALWAYS seen as hostile.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* jaguar_company_cobra_mk3.fragment for the Jaguar Company.
 
2
 *
 
3
 * Copyright © 2012 Tricky
 
4
 *
 
5
 * This work is licensed under the Creative Commons
 
6
 * Attribution-Noncommercial-Share Alike 3.0 Unported License.
 
7
 *
 
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.
 
12
 *
 
13
 * Cobra MK3 fragment shader for the Jaguar Company.
 
14
 * Based on Griff's multi-decal fragment shader.
 
15
 */
 
16
 
 
17
uniform sampler2D   uColorMap;
 
18
uniform sampler2D   uNormalMap;
 
19
uniform sampler2D   uEffectsMap;
 
20
uniform sampler2D   uDecalMap;
 
21
 
 
22
varying vec2        vTexCoord;
 
23
varying vec3        vEyeVector;     // These are all in tangent space
 
24
varying vec3        vLight0Vector;
 
25
varying vec3        vLight1Vector;
 
26
 
 
27
/* Constants */
 
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);
 
32
 
 
33
/* Uniforms from Oolite */
 
34
uniform bool        uHostileTarget;
 
35
uniform bool        uStationAegis;
 
36
uniform int         uScannedShips;
 
37
uniform float       uTime;
 
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.
 
45
 */
 
46
uniform vec4        uDecal_XY_Scale;
 
47
uniform float       uDecalRotation;
 
48
 
 
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)
 
53
#else
 
54
float Pulse(float value, float timeScale)
 
55
{
 
56
    float t = uTime * timeScale;
 
57
 
 
58
    float s0 = t;
 
59
    s0 -= floor(s0);
 
60
    float sum = abs( s0 - 0.5);
 
61
 
 
62
    float s1 = t * 0.7 - 0.05;
 
63
    s1 -= floor(s1);
 
64
    sum += abs(s1 - 0.5) - 0.25;
 
65
 
 
66
    float s2 = t * 1.3 - 0.3;
 
67
    s2 -= floor(s2);
 
68
    sum += abs(s2 - 0.5) - 0.25;
 
69
 
 
70
    float s3 = t * 5.09 - 0.6;
 
71
    s3 -= floor(s3);
 
72
    sum += abs(s3 - 0.5) - 0.25;
 
73
 
 
74
    return (sum * 0.1 + 0.9) * value;
 
75
}
 
76
 
 
77
/* Hull Temperate heat glow effect */
 
78
vec3 TemperatureGlow(float level)
 
79
{
 
80
    vec3 result = vec3(0);
 
81
 
 
82
    result.r = level;
 
83
    result.g = level * level * level;
 
84
    result.b = max(level - 0.7, 0.0) * 2.0;
 
85
 
 
86
    return result;
 
87
}
 
88
 
 
89
/* Blink_on_off_function */
 
90
float Blink_on_off(float timeScale)
 
91
{
 
92
    float result = step(0.5, sin(uTime * timeScale));
 
93
 
 
94
    return result;
 
95
}
 
96
 
 
97
/* Cyan color exhaust glow effect */
 
98
vec3 cyanGlow(float level)
 
99
{
 
100
    vec3 result;
 
101
    result.rgb = vec3(0.2, 0.7, 0.9) * level * 1.5;
 
102
 
 
103
    return result;
 
104
}
 
105
 
 
106
/* Red/Orange color heated metal effect */
 
107
vec3 redGlow(float level)
 
108
{
 
109
    vec3 result;
 
110
    result.rgb = vec3(1.5, 0.55, 0.2) * level * 1.3;
 
111
 
 
112
    return result;
 
113
}
 
114
#endif
 
115
 
 
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)
 
118
{
 
119
    lightVector = normalize(lightVector);
 
120
    vec3 reflection = normalize(-reflect(lightVector, normal));
 
121
 
 
122
    totalDiffuse += gl_FrontMaterial.diffuse.rgb * lightColor * max(dot(normal, lightVector), 0.0);
 
123
    totalSpecular += lightColor * pow(max(dot(reflection, eyeVector), 0.0), KspecExponent);
 
124
}
 
125
 
 
126
#define LIGHT(idx, vector) Light(vector, normal, gl_LightSource[idx].diffuse.rgb, eyeVector, KspecExponent, diffuse, specular)
 
127
 
 
128
#ifndef OO_REDUCED_COMPLEXITY
 
129
/* function to read in the colour map then scale and position the decal map onto it.
 
130
 *
 
131
 * "the_decaliser" - this function scales & positions the decal image using data passed
 
132
 * to it from the main shader
 
133
 */
 
134
vec4 the_decaliser()
 
135
{
 
136
    /* Setup the basic texture co-ords for the decals */
 
137
    vec2 decal_TexCoord = vTexCoord;
 
138
 
 
139
    /* Cache this value */
 
140
    float tmp = 1.0 / (2.0 * uDecal_XY_Scale.p); // 1/2n = 0.5/n
 
141
 
 
142
    /* Position the decal */
 
143
    decal_TexCoord -= vec2(uDecal_XY_Scale.s, uDecal_XY_Scale.t - tmp);
 
144
 
 
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
 
149
 
 
150
    decal_TexCoord += vec2(tmp);
 
151
    decal_TexCoord *= vec2(uDecal_XY_Scale.p, uDecal_XY_Scale.p);
 
152
 
 
153
    /* Get texture values */
 
154
    vec4 decal_Tex = texture2D(uDecalMap, decal_TexCoord);
 
155
 
 
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);
 
162
 
 
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;
 
165
 
 
166
    /* Return the scaled, position & rotated decal, it's mixed into the colour texture further on in the shader */
 
167
    return alpha * decal_Tex;
 
168
}
 
169
#endif
 
170
 
 
171
void main()
 
172
{
 
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);
 
182
    int   alertlevel = 0;
 
183
 
 
184
#ifdef OO_LIGHT_0_FIX
 
185
    LIGHT(0, vLight0Vector);
 
186
#endif
 
187
    LIGHT(1, vLight1Vector);
 
188
    diffuse += gl_FrontMaterial.ambient.rgb * gl_LightModel.ambient.rgb;
 
189
 
 
190
#ifndef OO_REDUCED_COMPLEXITY
 
191
    /* Full Shader Mode - Repaint the hull */
 
192
    colorMap += effectsMap.g * uPaintColor1.rgb;
 
193
    colorMap += effectsMap.a * uPaintColor2.rgb;
 
194
 
 
195
    /* Full Shader Mode - Apply the decal */
 
196
    vec4 decal = the_decaliser();
 
197
 
 
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
 
201
     */
 
202
    vec3 color_temp = colorMap; // temp vec3 to store the texturemap & lighting
 
203
 
 
204
    /* Calculate the lighting for full shader mode */
 
205
    color_temp += color_temp * specular * 6.0 * specIntensity;
 
206
 
 
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
 
209
     */
 
210
    vec3 color = mix(color_temp, decal.rgb * diffuse + (0.3 * specular), decal.a) * diffuse;
 
211
#endif
 
212
 
 
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;
 
219
#endif
 
220
 
 
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. */
 
225
        alertlevel = 3;
 
226
    else
 
227
    if (uScannedShips > 4 || uStationAegis)
 
228
        /* Other ships (other than Jaguar Company) near us or close to the main station. */
 
229
        alertlevel = 2;
 
230
    else
 
231
        /* Nothing around. */
 
232
        alertlevel = 1;
 
233
 
 
234
    /* check Alert Level, Adjust Lamp Colour Accordingly
 
235
     *
 
236
     * Current alert condition - 0 for docked, 1 for green, 2 for yellow, 3 for red.  
 
237
     */
 
238
    if (alertlevel > 1)
 
239
        LampColor = (alertlevel > 2) ? kLampColorRedAlert * max(mod(uTime, 1.0) * 2.0, 0.5) : kLampColorYellowAlert;
 
240
    else
 
241
        LampColor = kLampColorNoAlert;
 
242
 
 
243
    /* Add the hull lights glow effects, check if ship is throwingSparks, if so flicker the effects */
 
244
    if (uNearlyDead)
 
245
    {
 
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));
 
248
    }
 
249
    else
 
250
    {
 
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);
 
253
    }
 
254
 
 
255
    /* Add in the red heated metal glow around the exhaust */
 
256
    color += redGlow(effectsMap.r * Pulse(min(uEnginePower, 1.0), 1.0));
 
257
 
 
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);
 
262
#endif
 
263
 
 
264
    gl_FragColor = vec4(color.rgb, 1.0);
 
265
}