Advertisement
entrusc

Lighting.frag

Mar 25th, 2015
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "MatDefs/ShaderLib/noise3D.glsllib"
  2. #import "MatDefs/ShaderLib/utils.glsllib"
  3. #import "MatDefs/ShaderLib/shadowmap.glsllib"
  4.  
  5. varying vec3 vecPositionInWorld;
  6. varying vec3 vecPosition;
  7. varying vec3 vecNormal;
  8.  
  9. //diffuse
  10. varying vec3 texCoord;
  11.  
  12. //glow + specular + specular with sparkling
  13. varying vec3 texCoord2;
  14.  
  15. #ifdef LIGHTMAPSHADING
  16.     uniform sampler3D m_LightMap0;
  17.     uniform sampler3D m_LightMap1;
  18.     uniform sampler3D m_LightMap2;
  19.     uniform sampler3D m_LightMap3;
  20.     uniform sampler3D m_LightMap4;
  21.     uniform sampler3D m_LightMap5;
  22.     uniform sampler3D m_LightMap6;
  23.     uniform sampler3D m_LightMap7;
  24.     uniform sampler3D m_LightMap8;
  25.  
  26.     uniform sampler2D m_GlobalAttributesMap;
  27.     uniform float m_LightMapNormalMultiplicator;
  28. #else
  29.     uniform bool m_Holographic;
  30.     uniform vec4 m_HolographicColor;
  31. #endif
  32.  
  33. uniform vec3 g_CameraPosition;
  34. uniform float g_Time;
  35.  
  36. #ifdef TEXTUREATLAS
  37.     uniform sampler2DArray m_DiffuseTextures;
  38.     uniform sampler2DArray m_GlowTextures;
  39.     uniform sampler2DArray m_SpecularTextures;
  40.     uniform sampler2DArray m_SpecularWithSparklingTextures;
  41. #else
  42.     uniform sampler2D m_DiffuseTexture;
  43. #endif
  44.  
  45. #ifdef MIPMAPPING
  46.     varying float mipMapDepth;
  47.  
  48.     uniform vec2[5] m_MipMapPositions;
  49.     uniform vec2[5] m_MipMapSizes;
  50. #endif
  51.  
  52.  
  53. /**
  54.  * returns a sigmoid distributed mix of val0 and val1. It is quite
  55.  * similar to the mix() function but it uses a stronger
  56.  * non-linear transition that is based on a variation of the sigmoid
  57.  * function. the factor should be greater or equal to 5.
  58.  */
  59. vec4 getSigMix(vec4 val0, vec4 val1, float pos, float factor) {
  60.     float mixFactor = 1.0 / (1 + exp(-((pos * 2 * factor) - factor)));
  61.     return mix(val0, val1, max(0.0, min(1.0, mixFactor)));
  62. }
  63.  
  64. float rand(vec2 co){
  65.     return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  66. }
  67.  
  68. void main() {
  69.         vec3 normal = vecNormal;
  70.         vec4 diffuseColor = vec4(0.0, 0.0, 0.0, 1.0);
  71.         vec4 glowColor = vec4(0.0, 0.0, 0.0, 0.0);
  72.         vec4 specularColor = vec4(0.0, 0.0, 0.0, 0.0);
  73.  
  74.         vec3 currentTexCoords = vec3(texCoord);
  75.         vec3 currentTexCoords2 = vec3(texCoord2);
  76.  
  77.         #ifdef TEXTUREATLAS
  78.             diffuseColor = texture(m_DiffuseTextures, currentTexCoords);
  79.         #else
  80.             diffuseColor = texture(m_DiffuseTexture, vec2(currentTexCoords));
  81.         #endif
  82.  
  83.         #ifdef LIGHTMAPSHADING
  84.             vec4 globalLight = getAttributeAt(m_GlobalAttributesMap, ivec2(0.0, 0.0));
  85.             vec4 globalColor = getAttributeAt(m_GlobalAttributesMap, ivec2(1.0, 0.0));
  86.  
  87.             float depthFogDistance = decodeFloat(getAttributeAt(m_GlobalAttributesMap, ivec2(2.0, 0.0))) / 1000.0;
  88.  
  89.             //glow + specular
  90.             #ifdef TEXTUREATLAS
  91.  
  92.             //glow
  93.             if (currentTexCoords2.x > 0) {
  94.                 glowColor = texture(m_GlowTextures, vec3(currentTexCoords.x, currentTexCoords.y, currentTexCoords2.x));
  95.             }
  96.  
  97.             //specular + sparkle
  98.             if (currentTexCoords2.y > 0 || currentTexCoords2.z > 0) {
  99.                 bool sparkle = currentTexCoords2.z > 0;
  100.  
  101.                 vec4 specularValueBox;
  102.                 if (!sparkle) {
  103.                     specularValueBox = texture(m_SpecularTextures, vec3(currentTexCoords.x, currentTexCoords.y, currentTexCoords2.y));
  104.                 } else {
  105.                     specularValueBox = texture(m_SpecularWithSparklingTextures, vec3(currentTexCoords.x, currentTexCoords.y, currentTexCoords2.z));
  106.                 }
  107.  
  108.                 vec3 reflectionVector = reflect(normalize(vecPositionInWorld - g_CameraPosition), normalize(normal));
  109.                 float sparkleFactor = 1.0;
  110.  
  111.                 if (sparkle) {
  112.                     sparkleFactor = (snoise((reflectionVector + vec3(texCoord.x, texCoord.y, 0.0)) * 20.0) + 1.0);
  113.                 }
  114.  
  115.                 vec3 specularLightPosition = vecPosition + reflectionVector;
  116.                 float blockLightAtSpecularPosition = getCubedMixedLightMapValueAt(specularLightPosition,
  117.                     m_LightMap0, m_LightMap1, m_LightMap2, m_LightMap3, m_LightMap4, m_LightMap5, m_LightMap6, m_LightMap7, m_LightMap8).g;
  118.  
  119.                 specularColor = blockLightAtSpecularPosition * specularValueBox;
  120.                 specularColor = specularColor * sparkleFactor;
  121.                 specularColor.a = specularValueBox.a;
  122.             }
  123.             #endif
  124.  
  125.             float alpha = diffuseColor.a;
  126.  
  127.             //important for transparent materials!
  128.             if(alpha < 0.10){
  129.                 discard;
  130.             }
  131.  
  132.             vec3 lightMapSamplePosition = vecPosition + m_LightMapNormalMultiplicator * normal;
  133.  
  134.             vec4 lightMapValueIn = getCubedMixedLightMapValueAt(vecPosition - 0.05 * normal,
  135.                 m_LightMap0, m_LightMap1, m_LightMap2, m_LightMap3, m_LightMap4, m_LightMap5, m_LightMap6, m_LightMap7, m_LightMap8);
  136.             vec4 lightMapValue = getCubedMixedLightMapValueAt(lightMapSamplePosition,
  137.                 m_LightMap0, m_LightMap1, m_LightMap2, m_LightMap3, m_LightMap4, m_LightMap5, m_LightMap6, m_LightMap7, m_LightMap8);
  138.             float dayLight = lightMapValue.r;
  139.             float blockLight = lightMapValue.g;
  140.             float energyGlow = min(0.8, (lightMapValue.b + lightMapValueIn.b) * 0.8);
  141.  
  142.             float dayLightValue = min(dayLight, 1.0);
  143.             float blockLightValue = min(blockLight, 1.0);
  144.  
  145.             //it's allways bright on the top most level
  146.             if (lightMapSamplePosition.y >= 128) {
  147.                 dayLightValue = 1.0;
  148.             }
  149.  
  150.             vec4 totalLighting = vec4(dayLightValue, dayLightValue, dayLightValue, 1.0);
  151.             totalLighting = totalLighting * globalLight + blockLightValue;
  152.             totalLighting = max(totalLighting, vec4(0.02, 0.02, 0.02, 1.0));
  153.  
  154.             gl_FragColor = totalLighting * diffuseColor + specularColor * specularColor.a + glowColor;
  155.             gl_FragColor = mix(gl_FragColor, mix(diffuseColor, vec4(1.0, 0.0, 0.0, 1.0), 0.75), energyGlow);
  156.  
  157.         #else
  158.             if (m_Holographic) {
  159.                 //vec4 scanline = vec4(diffuseColor.r * 0.3, min(1.0, diffuseColor.g * 1.3), diffuseColor.b * 0.3, 0.8);
  160.                 vec4 scanline = diffuseColor * m_HolographicColor;
  161.                 float time = g_Time / 16.0;
  162.                 float noise = (snoise(vec3(vecPosition.y + time, vecPosition.y + time, vecPosition.y + time) * 20.0) + 1.0) / 2.0;
  163.                 if (noise > 0.5) {
  164.                     scanline.a = 0.85;
  165.                 } else {
  166.                     scanline.a = 0.4;
  167.                 }
  168.  
  169.                 gl_FragColor = scanline;
  170.             } else {
  171.                 gl_FragColor = diffuseColor;
  172.             }
  173.         #endif
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement