Advertisement
Guest User

Griff Cobra MKIII shader, modified for metallic variants

a guest
Aug 14th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.67 KB | None | 0 0
  1. uniform sampler2D uColorMap;
  2. uniform sampler2D uNormalMap;
  3. uniform sampler2D uEffectsMap;
  4. uniform sampler2D uDecalMap;
  5.  
  6.  
  7. varying vec2 vTexCoord;
  8. varying vec3 vEyeVector; // These are all in tangent space
  9. varying vec3 vLight0Vector;
  10. varying vec3 vLight1Vector;
  11.  
  12. // Constants
  13. const float KspecExponent = 5.0;
  14. const vec3 kLampColorNoAlert = vec3(0);
  15. const vec3 kLampColorYellowAlert = vec3(0.9926, 0.9686, 0.7325);
  16. const vec3 kLampColorRedAlert = vec3(1.0, 0.1, 0.0);
  17.  
  18. // Uniforms from Oolite
  19. uniform float uTime;
  20. uniform int alertlevel;
  21. uniform int damage_amount;
  22. uniform float hull_heat_level;
  23. uniform float engine_power;
  24. uniform vec4 PaintColor1; // used with paintmask map to tint diffuse texture
  25. uniform vec4 PaintColor2; // used with paintmask map to tint diffuse texture
  26. uniform vec4 Decal1_Scale_and_Position; // position & scale settings for decal 1
  27. uniform float Decal1_Rotation; // rotation settings for decal 1
  28. //uniform vec4 ExhaustPlumeCol; // Exhaust Plume Colour
  29.  
  30. // Irregular flickering function
  31. #ifdef OO_REDUCED_COMPLEXITY
  32. #define Pulse(v, ts) ((v) * 0.95)
  33. #define Blink_on_off(value) (1.0)
  34. #else
  35.  
  36. float Pulse(float value, float timeScale)
  37. {
  38. float t = uTime * timeScale;
  39.  
  40. float s0 = t;
  41. s0 -= floor(s0);
  42. float sum = abs( s0 - 0.5);
  43.  
  44. float s1 = t * 0.7 - 0.05;
  45. s1 -= floor(s1);
  46. sum += abs(s1 - 0.5) - 0.25;
  47.  
  48. float s2 = t * 1.3 - 0.3;
  49. s2 -= floor(s2);
  50. sum += abs(s2 - 0.5) - 0.25;
  51.  
  52. float s3 = t * 5.09 - 0.6;
  53. s3 -= floor(s3);
  54. sum += abs(s3 - 0.5) - 0.25;
  55.  
  56. return (sum * 0.1 + 0.9) * value;
  57. }
  58.  
  59. // Hull Temperate heat glow effect
  60. vec3 TemperatureGlow(float level)
  61. {
  62. vec3 result = vec3(0);
  63.  
  64. result.r = level;
  65. result.g = level * level * level;
  66. result.b = max(level - 0.7, 0.0) * 2.0;
  67. return result;
  68. }
  69.  
  70. // Blink_on_off_function.
  71. float Blink_on_off(float timeScale)
  72. {
  73. float result = step(0.5, sin(uTime * timeScale));
  74. return 0.5 + result;
  75. }
  76.  
  77. // Cyan color exhaust glow effect
  78. vec3 cyanGlow(float level)
  79. {
  80. vec3 result;
  81. result.rgb = vec3(0.2, 0.7, 0.9) * level * 1.5;
  82. //result.rgb = vec3(0.96, 0.5, 0.15) * level * 1.5;
  83. return result;
  84. }
  85.  
  86. // Red/Orange color heated metal effect
  87. vec3 redGlow(float level)
  88. {
  89. vec3 result;
  90. result.rgb = vec3(1.5, 0.55, 0.2) * level * 1.3;
  91. return result;
  92. }
  93.  
  94. #endif
  95.  
  96.  
  97. vec3 FresnelSchlick(vec3 specColor, vec3 light, vec3 halfVec)
  98. {
  99. return specColor + (vec3(1.0) - specColor) * pow(1.0 - clamp(dot(light, halfVec), 0.0, 1.0), 5.0);
  100. }
  101.  
  102.  
  103. vec3 CalcSpecularGGX(vec3 light, vec3 normal, vec3 halfVec, vec3 view, float gloss, float fresnelFactor, vec3 specColor)
  104. {
  105. float NdotL = clamp(dot(normal, light), 0.0f, 1.0f);
  106. float roughness = clamp(1.0f - gloss, 0.0f, 1.0f);
  107. float alpha = roughness * roughness;
  108.  
  109. float NdotH = clamp(dot(normal, halfVec), 0.0f, 1.0f);
  110. float NdotV = clamp(dot(normal, view), 0.0f, 1.0f);
  111.  
  112. float alphaSqr = alpha * alpha;
  113. float pi = 3.14159f;
  114. float denom = NdotH * NdotH * (alphaSqr - 1.0f) + 1.0f;
  115. float distribution = alphaSqr / (pi * denom * denom);
  116.  
  117. vec3 fresnel = mix(specColor, FresnelSchlick(specColor, light, halfVec), fresnelFactor);
  118.  
  119. float alphaPrime = roughness + 1.0f;
  120. float k = alphaPrime * alphaPrime / 8.0f;
  121. float g1vNL = 1.0f / (NdotL * (1.0f - k) + k);
  122. float g1vNV = 1.0f / (NdotV * (1.0f - k) + k);
  123. float visibility = g1vNL * g1vNV;
  124.  
  125. return distribution * fresnel * visibility * NdotL;
  126. }
  127.  
  128.  
  129. void Light(in vec3 lightVector, in vec3 normal, in vec3 lightColor, in vec3 eyeVector,
  130. in float KspecExponent, inout vec3 totalDiffuse, inout vec3 totalSpecular)
  131. {
  132. lightVector = normalize(lightVector);
  133. //vec3 reflection = normalize(-reflect(lightVector, normal));
  134. vec3 reflection = -reflect(lightVector, normal);
  135.  
  136.  
  137. /* -------------------- Old incorrect code ----------------------------------------------
  138. float RdotE = max(dot(reflection, eyeVector), 0.0);
  139. float intensity = pow(max(RdotE, 0.0), gl_FrontMaterial.shininess);
  140. float NdotE = dot(normal, eyeVector);
  141. float kRefract = 1.0/ 2.6112; // Index of refraction of titanium.
  142. float F0 = ((kRefract - 1.0) * (kRefract - 1.0)) / ((kRefract + 1.0) * (kRefract + 1.0));
  143. float Fa = F0 + pow((1.0 - NdotE), 5.0) * (1.0 - F0);
  144. //vec3 F0 = vec3(0.04);
  145. //F0 = mix(F0, gl_FrontMaterial.diffuse.rgb, texture2D(uColorMap, vTexCoord).a);
  146. //vec3 Fa = F0 + pow((1.0 - NdotE), 5.0) * (1.0 - F0);
  147. intensity *= 0.8 + Fa;
  148. ---------------------- Old incorrect code --------------------------------------------*/
  149.  
  150. //totalDiffuse += gl_FrontMaterial.diffuse.rgb * lightColor * max(dot(normal, lightVector), 0.0);
  151. totalDiffuse += vec3(0.0) * lightColor * max(dot(normal, lightVector), 0.0);
  152. //totalDiffuse += vec3(0.08203) * lightColor * max(dot(normal, lightVector), 0.0); // for iron and aluminium
  153.  
  154. //totalSpecular += lightColor * pow(max(dot(reflection, eyeVector), 0.0), KspecExponent);
  155. //totalSpecular += gl_FrontMaterial.specular.rgb * lightColor * intensity;
  156. //totalSpecular += lightColor * intensity;
  157.  
  158. float exponent = gl_FrontMaterial.shininess;
  159. vec3 halfVector = normalize(lightVector + eyeVector);
  160. totalSpecular = CalcSpecularGGX(lightVector, normal, halfVector, eyeVector, clamp(exponent / (37.5 + exponent), 0.0f, 0.95f), 1.0, gl_LightSource[1].specular.rgb);
  161. }
  162.  
  163.  
  164. #define LIGHT(idx, vector) Light(vector, normal, gl_LightSource[idx].diffuse.rgb, eyeVector, KspecExponent, diffuse, specular)
  165.  
  166. #ifndef OO_REDUCED_COMPLEXITY
  167. // function to read in the colour map then scale and position the decal map onto it.
  168. /*
  169. "the_decaliser" - this function scales & positions the decal image using data passed
  170. to it from the main shader
  171. */
  172. vec4 the_decaliser(vec4 Your_Decal_Settings, float Your_decal_Rotation)
  173. {
  174.  
  175. // Setup the basic texture co-ords for the decals
  176. vec2 decal_TexCoord = vTexCoord;
  177.  
  178. // Position the decal
  179. decal_TexCoord -= vec2(Your_Decal_Settings.s, Your_Decal_Settings.t - (0.5 / Your_Decal_Settings.p));
  180.  
  181. // Orientate & scale the decal
  182. float decal_s = sin(Your_decal_Rotation);
  183. float decal_c = cos(Your_decal_Rotation);
  184. decal_TexCoord *= mat2(decal_c, decal_s, -decal_s, decal_c);
  185.  
  186. decal_TexCoord += vec2(0.5 / Your_Decal_Settings.p);
  187. decal_TexCoord *= vec2(Your_Decal_Settings.p, Your_Decal_Settings.p);
  188.  
  189. // Get texture values.
  190. vec4 decal_Tex = texture2D(uDecalMap, decal_TexCoord);
  191.  
  192. // Modify the Decals texture co-oords
  193. decal_TexCoord.s += 1.0;
  194. decal_Tex *= step(1.0, decal_TexCoord.s) *
  195. step(0.0, decal_TexCoord.t) *
  196. step(-2.0, -decal_TexCoord.s) *
  197. step(-1.0, -decal_TexCoord.t);
  198.  
  199. // Use the Alpha in the decal as a transparency mask so you can 'cutout' your decal from it's background
  200. float alpha = decal_Tex.a;
  201.  
  202. // Return the scaled, position & rotated decal, it's mixed into the colour texture further on in the shader .
  203. return alpha * decal_Tex;
  204. }
  205.  
  206. #endif
  207.  
  208. void main()
  209. {
  210. vec3 eyeVector = normalize(vEyeVector);
  211. vec2 texCoord = vTexCoord;
  212. vec3 normal = normalize(texture2D(uNormalMap, vTexCoord).rgb - 0.5);
  213. vec3 colorMap = texture2D(uColorMap, texCoord).rgb;
  214. vec4 effectsMap = texture2D(uEffectsMap, texCoord);
  215. vec3 diffuse = vec3(0.0);
  216. vec3 specular = vec3(0.0);
  217. float specIntensity = texture2D(uColorMap, texCoord).a;
  218. float glowMap = texture2D(uNormalMap, texCoord).a;
  219. vec3 LampColor = vec3(0.9926, 0.9686, 0.7325);
  220.  
  221.  
  222. #ifdef OO_LIGHT_0_FIX
  223. LIGHT(0, normalize(vLight0Vector));
  224. #endif
  225. LIGHT(1, normalize(vLight1Vector));
  226. //diffuse += gl_FrontMaterial.ambient.rgb * gl_LightModel.ambient.rgb;
  227.  
  228. #ifndef OO_REDUCED_COMPLEXITY
  229. // Full Shader Mode - Repaint the hull
  230. colorMap += effectsMap.g * PaintColor1.rgb;
  231. colorMap += effectsMap.a * PaintColor2.rgb;
  232.  
  233. // Full Shader Mode - Apply the decals
  234. vec4 decals = the_decaliser(Decal1_Scale_and_Position, Decal1_Rotation);
  235. // mix the decals into the texture using the decal alpha as a mask
  236. vec3 color = mix(colorMap, decals.rgb, decals.a) * diffuse;
  237.  
  238. // Calculate the lighting for full shader mode
  239. //color += color * specular * specIntensity;
  240. //color += specular * specIntensity * vec3(1.0, 0.766, 0.336); // gold
  241. //color += specular * specIntensity * vec3(0.9765625, 0.78125, 0.703125); // copper
  242. //color += specular * specIntensity * vec3(0.765625, 0.77734375, 0.7734375); // iron
  243. //color += specular * specIntensity * vec3(0.83203125, 0.8125, 0.78125); // platinum
  244. //color += specular * specIntensity * vec3(0.96094); // aluminium
  245. //color += specular * specIntensity * vec3(0.984375, 0.9765625, 0.95703125); // silver
  246. color += specular * specIntensity * vec3(0.9765625, 0.8984375, 0.5859375); // brass
  247. #endif
  248.  
  249. // Calculate the lighting for simple shader mode
  250. #ifdef OO_REDUCED_COMPLEXITY
  251. vec3 color = diffuse * colorMap;
  252. color += colorMap * specular * specIntensity * 2.5;
  253. // Add in simple shader hull lights
  254. color += LampColor * glowMap;
  255. #endif
  256.  
  257. // these next glow effects are only available in full shader mode
  258. #ifndef OO_REDUCED_COMPLEXITY
  259.  
  260. // check Alert Level, Adjust Lamp Colour Accordingly
  261. if (alertlevel > 0)
  262. {
  263. LampColor = (alertlevel > 2) ? kLampColorRedAlert * max(mod(uTime, 1.5), 0.5): kLampColorYellowAlert;
  264. }
  265. else
  266. {
  267. LampColor = kLampColorNoAlert;
  268. }
  269.  
  270.  
  271. // Damage Calculator
  272. float tempvar = float(damage_amount);
  273. float DamageAmount = mod(tempvar, 100.0) / 100.0;
  274.  
  275. vec3 Lampglow = LampColor * glowMap * Pulse(2.0, 1.0) * (1.0 - DamageAmount);
  276. //vec3 Cyanglow = ExhaustPlumeCol * cyanGlow(effectsMap.b * Pulse(min(engine_power, 1.0), 1.0));
  277. vec3 Cyanglow = cyanGlow(effectsMap.b * Pulse(min(engine_power, 1.0), 1.0));
  278. vec3 Redglow = redGlow(effectsMap.r * Pulse(min(engine_power, 1.0), 1.0));
  279.  
  280. color += mix(Lampglow + Cyanglow + Redglow,
  281. Redglow +
  282. Lampglow * Blink_on_off(Pulse(1.0, 0.4)) * 4.0 +
  283. Cyanglow * Blink_on_off(Pulse(1.0, 1.0)), max(0.0, DamageAmount - 0.4));
  284.  
  285. // Add the all over hull temperature glow. Full Shader mode only
  286. float hullHeat = max(hull_heat_level - 0.5, 0.0) * 2.0;
  287. hullHeat = Pulse(hullHeat * hullHeat, 0.1);
  288. color += TemperatureGlow(hullHeat);
  289. #endif
  290.  
  291. gl_FragColor = vec4(color.rgb, 1.0);
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement