Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uniform sampler2D uColorMap;
- uniform sampler2D uNormalMap;
- uniform sampler2D uEffectsMap;
- uniform sampler2D uDecalMap;
- varying vec2 vTexCoord;
- varying vec3 vEyeVector; // These are all in tangent space
- varying vec3 vLight0Vector;
- varying vec3 vLight1Vector;
- // Constants
- const float KspecExponent = 5.0;
- const vec3 kLampColorNoAlert = vec3(0);
- const vec3 kLampColorYellowAlert = vec3(0.9926, 0.9686, 0.7325);
- const vec3 kLampColorRedAlert = vec3(1.0, 0.1, 0.0);
- // Uniforms from Oolite
- uniform float uTime;
- uniform int alertlevel;
- uniform int damage_amount;
- uniform float hull_heat_level;
- uniform float engine_power;
- uniform vec4 PaintColor1; // used with paintmask map to tint diffuse texture
- uniform vec4 PaintColor2; // used with paintmask map to tint diffuse texture
- uniform vec4 Decal1_Scale_and_Position; // position & scale settings for decal 1
- uniform float Decal1_Rotation; // rotation settings for decal 1
- //uniform vec4 ExhaustPlumeCol; // Exhaust Plume Colour
- // Irregular flickering function
- #ifdef OO_REDUCED_COMPLEXITY
- #define Pulse(v, ts) ((v) * 0.95)
- #define Blink_on_off(value) (1.0)
- #else
- float Pulse(float value, float timeScale)
- {
- float t = uTime * timeScale;
- float s0 = t;
- s0 -= floor(s0);
- float sum = abs( s0 - 0.5);
- float s1 = t * 0.7 - 0.05;
- s1 -= floor(s1);
- sum += abs(s1 - 0.5) - 0.25;
- float s2 = t * 1.3 - 0.3;
- s2 -= floor(s2);
- sum += abs(s2 - 0.5) - 0.25;
- float s3 = t * 5.09 - 0.6;
- s3 -= floor(s3);
- sum += abs(s3 - 0.5) - 0.25;
- return (sum * 0.1 + 0.9) * value;
- }
- // Hull Temperate heat glow effect
- vec3 TemperatureGlow(float level)
- {
- vec3 result = vec3(0);
- result.r = level;
- result.g = level * level * level;
- result.b = max(level - 0.7, 0.0) * 2.0;
- return result;
- }
- // Blink_on_off_function.
- float Blink_on_off(float timeScale)
- {
- float result = step(0.5, sin(uTime * timeScale));
- return 0.5 + result;
- }
- // Cyan color exhaust glow effect
- vec3 cyanGlow(float level)
- {
- vec3 result;
- result.rgb = vec3(0.2, 0.7, 0.9) * level * 1.5;
- //result.rgb = vec3(0.96, 0.5, 0.15) * level * 1.5;
- return result;
- }
- // Red/Orange color heated metal effect
- vec3 redGlow(float level)
- {
- vec3 result;
- result.rgb = vec3(1.5, 0.55, 0.2) * level * 1.3;
- return result;
- }
- #endif
- vec3 FresnelSchlick(vec3 specColor, vec3 light, vec3 halfVec)
- {
- return specColor + (vec3(1.0) - specColor) * pow(1.0 - clamp(dot(light, halfVec), 0.0, 1.0), 5.0);
- }
- vec3 CalcSpecularGGX(vec3 light, vec3 normal, vec3 halfVec, vec3 view, float gloss, float fresnelFactor, vec3 specColor)
- {
- float NdotL = clamp(dot(normal, light), 0.0f, 1.0f);
- float roughness = clamp(1.0f - gloss, 0.0f, 1.0f);
- float alpha = roughness * roughness;
- float NdotH = clamp(dot(normal, halfVec), 0.0f, 1.0f);
- float NdotV = clamp(dot(normal, view), 0.0f, 1.0f);
- float alphaSqr = alpha * alpha;
- float pi = 3.14159f;
- float denom = NdotH * NdotH * (alphaSqr - 1.0f) + 1.0f;
- float distribution = alphaSqr / (pi * denom * denom);
- vec3 fresnel = mix(specColor, FresnelSchlick(specColor, light, halfVec), fresnelFactor);
- float alphaPrime = roughness + 1.0f;
- float k = alphaPrime * alphaPrime / 8.0f;
- float g1vNL = 1.0f / (NdotL * (1.0f - k) + k);
- float g1vNV = 1.0f / (NdotV * (1.0f - k) + k);
- float visibility = g1vNL * g1vNV;
- return distribution * fresnel * visibility * NdotL;
- }
- void Light(in vec3 lightVector, in vec3 normal, in vec3 lightColor, in vec3 eyeVector,
- in float KspecExponent, inout vec3 totalDiffuse, inout vec3 totalSpecular)
- {
- lightVector = normalize(lightVector);
- //vec3 reflection = normalize(-reflect(lightVector, normal));
- vec3 reflection = -reflect(lightVector, normal);
- /* -------------------- Old incorrect code ----------------------------------------------
- float RdotE = max(dot(reflection, eyeVector), 0.0);
- float intensity = pow(max(RdotE, 0.0), gl_FrontMaterial.shininess);
- float NdotE = dot(normal, eyeVector);
- float kRefract = 1.0/ 2.6112; // Index of refraction of titanium.
- float F0 = ((kRefract - 1.0) * (kRefract - 1.0)) / ((kRefract + 1.0) * (kRefract + 1.0));
- float Fa = F0 + pow((1.0 - NdotE), 5.0) * (1.0 - F0);
- //vec3 F0 = vec3(0.04);
- //F0 = mix(F0, gl_FrontMaterial.diffuse.rgb, texture2D(uColorMap, vTexCoord).a);
- //vec3 Fa = F0 + pow((1.0 - NdotE), 5.0) * (1.0 - F0);
- intensity *= 0.8 + Fa;
- ---------------------- Old incorrect code --------------------------------------------*/
- //totalDiffuse += gl_FrontMaterial.diffuse.rgb * lightColor * max(dot(normal, lightVector), 0.0);
- totalDiffuse += vec3(0.0) * lightColor * max(dot(normal, lightVector), 0.0);
- //totalDiffuse += vec3(0.08203) * lightColor * max(dot(normal, lightVector), 0.0); // for iron and aluminium
- //totalSpecular += lightColor * pow(max(dot(reflection, eyeVector), 0.0), KspecExponent);
- //totalSpecular += gl_FrontMaterial.specular.rgb * lightColor * intensity;
- //totalSpecular += lightColor * intensity;
- float exponent = gl_FrontMaterial.shininess;
- vec3 halfVector = normalize(lightVector + eyeVector);
- totalSpecular = CalcSpecularGGX(lightVector, normal, halfVector, eyeVector, clamp(exponent / (37.5 + exponent), 0.0f, 0.95f), 1.0, gl_LightSource[1].specular.rgb);
- }
- #define LIGHT(idx, vector) Light(vector, normal, gl_LightSource[idx].diffuse.rgb, eyeVector, KspecExponent, diffuse, specular)
- #ifndef OO_REDUCED_COMPLEXITY
- // function to read in the colour map then scale and position the decal map onto it.
- /*
- "the_decaliser" - this function scales & positions the decal image using data passed
- to it from the main shader
- */
- vec4 the_decaliser(vec4 Your_Decal_Settings, float Your_decal_Rotation)
- {
- // Setup the basic texture co-ords for the decals
- vec2 decal_TexCoord = vTexCoord;
- // Position the decal
- decal_TexCoord -= vec2(Your_Decal_Settings.s, Your_Decal_Settings.t - (0.5 / Your_Decal_Settings.p));
- // Orientate & scale the decal
- float decal_s = sin(Your_decal_Rotation);
- float decal_c = cos(Your_decal_Rotation);
- decal_TexCoord *= mat2(decal_c, decal_s, -decal_s, decal_c);
- decal_TexCoord += vec2(0.5 / Your_Decal_Settings.p);
- decal_TexCoord *= vec2(Your_Decal_Settings.p, Your_Decal_Settings.p);
- // Get texture values.
- vec4 decal_Tex = texture2D(uDecalMap, decal_TexCoord);
- // Modify the Decals texture co-oords
- decal_TexCoord.s += 1.0;
- decal_Tex *= step(1.0, decal_TexCoord.s) *
- step(0.0, decal_TexCoord.t) *
- step(-2.0, -decal_TexCoord.s) *
- step(-1.0, -decal_TexCoord.t);
- // Use the Alpha in the decal as a transparency mask so you can 'cutout' your decal from it's background
- float alpha = decal_Tex.a;
- // Return the scaled, position & rotated decal, it's mixed into the colour texture further on in the shader .
- return alpha * decal_Tex;
- }
- #endif
- void main()
- {
- vec3 eyeVector = normalize(vEyeVector);
- vec2 texCoord = vTexCoord;
- vec3 normal = normalize(texture2D(uNormalMap, vTexCoord).rgb - 0.5);
- vec3 colorMap = texture2D(uColorMap, texCoord).rgb;
- vec4 effectsMap = texture2D(uEffectsMap, texCoord);
- vec3 diffuse = vec3(0.0);
- vec3 specular = vec3(0.0);
- float specIntensity = texture2D(uColorMap, texCoord).a;
- float glowMap = texture2D(uNormalMap, texCoord).a;
- vec3 LampColor = vec3(0.9926, 0.9686, 0.7325);
- #ifdef OO_LIGHT_0_FIX
- LIGHT(0, normalize(vLight0Vector));
- #endif
- LIGHT(1, normalize(vLight1Vector));
- //diffuse += gl_FrontMaterial.ambient.rgb * gl_LightModel.ambient.rgb;
- #ifndef OO_REDUCED_COMPLEXITY
- // Full Shader Mode - Repaint the hull
- colorMap += effectsMap.g * PaintColor1.rgb;
- colorMap += effectsMap.a * PaintColor2.rgb;
- // Full Shader Mode - Apply the decals
- vec4 decals = the_decaliser(Decal1_Scale_and_Position, Decal1_Rotation);
- // mix the decals into the texture using the decal alpha as a mask
- vec3 color = mix(colorMap, decals.rgb, decals.a) * diffuse;
- // Calculate the lighting for full shader mode
- //color += color * specular * specIntensity;
- //color += specular * specIntensity * vec3(1.0, 0.766, 0.336); // gold
- //color += specular * specIntensity * vec3(0.9765625, 0.78125, 0.703125); // copper
- //color += specular * specIntensity * vec3(0.765625, 0.77734375, 0.7734375); // iron
- //color += specular * specIntensity * vec3(0.83203125, 0.8125, 0.78125); // platinum
- //color += specular * specIntensity * vec3(0.96094); // aluminium
- //color += specular * specIntensity * vec3(0.984375, 0.9765625, 0.95703125); // silver
- color += specular * specIntensity * vec3(0.9765625, 0.8984375, 0.5859375); // brass
- #endif
- // Calculate the lighting for simple shader mode
- #ifdef OO_REDUCED_COMPLEXITY
- vec3 color = diffuse * colorMap;
- color += colorMap * specular * specIntensity * 2.5;
- // Add in simple shader hull lights
- color += LampColor * glowMap;
- #endif
- // these next glow effects are only available in full shader mode
- #ifndef OO_REDUCED_COMPLEXITY
- // check Alert Level, Adjust Lamp Colour Accordingly
- if (alertlevel > 0)
- {
- LampColor = (alertlevel > 2) ? kLampColorRedAlert * max(mod(uTime, 1.5), 0.5): kLampColorYellowAlert;
- }
- else
- {
- LampColor = kLampColorNoAlert;
- }
- // Damage Calculator
- float tempvar = float(damage_amount);
- float DamageAmount = mod(tempvar, 100.0) / 100.0;
- vec3 Lampglow = LampColor * glowMap * Pulse(2.0, 1.0) * (1.0 - DamageAmount);
- //vec3 Cyanglow = ExhaustPlumeCol * cyanGlow(effectsMap.b * Pulse(min(engine_power, 1.0), 1.0));
- vec3 Cyanglow = cyanGlow(effectsMap.b * Pulse(min(engine_power, 1.0), 1.0));
- vec3 Redglow = redGlow(effectsMap.r * Pulse(min(engine_power, 1.0), 1.0));
- color += mix(Lampglow + Cyanglow + Redglow,
- Redglow +
- Lampglow * Blink_on_off(Pulse(1.0, 0.4)) * 4.0 +
- Cyanglow * Blink_on_off(Pulse(1.0, 1.0)), max(0.0, DamageAmount - 0.4));
- // Add the all over hull temperature glow. Full Shader mode only
- float hullHeat = max(hull_heat_level - 0.5, 0.0) * 2.0;
- hullHeat = Pulse(hullHeat * hullHeat, 0.1);
- color += TemperatureGlow(hullHeat);
- #endif
- gl_FragColor = vec4(color.rgb, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement