Advertisement
TableSalt

minimum_frag

Aug 9th, 2023 (edited)
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 120
  2.  
  3. // Varying.
  4. varying vec3 worldNormal;
  5. varying vec3 worldPos;
  6.  
  7. // Global directional light uniforms.
  8. uniform vec4 dirLightDir;
  9. uniform vec4 dirLightColor;
  10. uniform vec4 dirLightAmbient;
  11. uniform vec4 dirShadowColor;
  12.  
  13. // Misc uniforms.
  14. uniform vec3 camPos;
  15. uniform mat4 obj2World;
  16. uniform mat4 world2Cam;
  17.  
  18. uniform int isParticle;
  19. uniform int doColorMultiply;
  20. uniform int glow;
  21.  
  22. uniform sampler2D tex;
  23.  
  24. // Surface calculations, including specular power.
  25. varying vec2 texCoord;
  26. vec4 viewDelta;
  27. float specular;
  28. float NdotL;
  29. vec3 reflectVec;
  30.  
  31. // // MODS // //
  32. float power = 0.275f;       // light strength, default is 0.4f
  33. float glareReduction = 3f;  // affects glare on bricks, default is 1f
  34. float radiance = 1.25f;     // enhances the glowiness of glowing bricks, default is 1f
  35. vec4 glowTint = vec4(0.05f, 0f, 0.05f, 0f); // shifts the hue of glowing bricks, default is 0,0,0,0
  36. // // MODS // //
  37.  
  38. void calculateSurface(vec4 color, inout vec4 albedo)
  39. {
  40.    viewDelta.xyz = worldPos - camPos;
  41.    viewDelta.w   = length(viewDelta.xyz);
  42.    viewDelta.xyz = -normalize(viewDelta.xyz);
  43.  
  44.    vec4 texAlbedo = texture2D(tex, texCoord);
  45.    albedo.rgb = mix(color.rgb, texAlbedo.rgb, texAlbedo.a);
  46.  
  47.    if(doColorMultiply == 1)
  48.       albedo *= gl_Color;
  49.  
  50.    albedo.a = color.a;
  51.  
  52.    NdotL = max(dot(worldNormal, dirLightDir.xyz), 0.0f);
  53.    reflectVec = normalize(reflect(-dirLightDir.xyz, worldNormal));
  54.    specular = pow(max(dot(reflectVec, viewDelta.xyz), 0.0f), 12.0f) * length(texAlbedo.rgb) / glareReduction;
  55.  
  56.    //albedo.rgb = normalize(viewDelta.xyz);
  57. }
  58.  
  59. // Fogging.
  60. uniform vec4 fogBaseColor;
  61. uniform vec4 fogConsts;
  62. uniform sampler2D fogTex;
  63. varying vec2 fogCoords;
  64. void applyFog(inout vec4 albedo)
  65. {
  66.    // Calculate fog.
  67.    vec4 fogColor = texture2D(fogTex, fogCoords) * fogBaseColor;
  68.    
  69.    // Blend it.
  70.    albedo = mix(albedo, fogColor, fogColor.a);
  71. }
  72.  
  73. // Point lighting
  74. uniform vec4     pointLightPos0;
  75. uniform vec4   pointLightColor0;
  76. uniform float pointLightRadius0;
  77.  
  78. uniform vec4     pointLightPos1;
  79. uniform vec4   pointLightColor1;
  80. uniform float pointLightRadius1;
  81.  
  82. uniform vec4     pointLightPos2;
  83. uniform vec4   pointLightColor2;
  84. uniform float pointLightRadius2;
  85.  
  86. uniform vec4     pointLightPos3;
  87. uniform vec4   pointLightColor3;
  88. uniform float pointLightRadius3;
  89.  
  90. uniform vec4     pointLightPos4;
  91. uniform vec4   pointLightColor4;
  92. uniform float pointLightRadius4;
  93.  
  94. uniform vec4     pointLightPos5;
  95. uniform vec4   pointLightColor5;
  96. uniform float pointLightRadius5;
  97.  
  98. uniform vec4     pointLightPos6;
  99. uniform vec4   pointLightColor6;
  100. uniform float pointLightRadius6;
  101.  
  102. uniform vec4     pointLightPos7;
  103. uniform vec4   pointLightColor7;
  104. uniform float pointLightRadius7;
  105.  
  106. vec4 accumulatePointLights()
  107. {
  108.    vec4 pointLightTotal = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  109.    vec3 lightDelta = vec3(0.0f, 0.0f, 0.0f);
  110.    float lightDot = 0.0f;
  111.    float ratio = 0.0f;
  112.  
  113.    // Calculate effects of the 8 point lights.
  114.  
  115.    lightDelta = worldPos.xyz - pointLightPos0.xyz;
  116.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  117.    ratio = 1.0f - (length(lightDelta) / pointLightRadius0);
  118.    ratio = ratio * ratio * ratio * power;
  119.    ratio = max(ratio, 0.0f);
  120.    pointLightTotal.xyz += ratio * lightDot * pointLightColor0.xyz;
  121.  
  122.    lightDelta = worldPos.xyz - pointLightPos1.xyz;
  123.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  124.    ratio = 1.0f - (length(lightDelta) / pointLightRadius1);
  125.    ratio = ratio * ratio * ratio * power;
  126.    ratio = max(ratio, 0.0f);
  127.    pointLightTotal.xyz += ratio * lightDot * pointLightColor1.xyz;
  128.  
  129.    lightDelta = worldPos.xyz - pointLightPos2.xyz;
  130.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  131.    ratio = 1.0f - (length(lightDelta) / pointLightRadius2);
  132.    ratio = ratio * ratio * ratio * power;
  133.    ratio = max(ratio, 0.0f);
  134.    pointLightTotal.xyz += ratio * lightDot * pointLightColor2.xyz;
  135.  
  136.    lightDelta = worldPos.xyz - pointLightPos3.xyz;
  137.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  138.    ratio = 1.0f - (length(lightDelta) / pointLightRadius3);
  139.    ratio = ratio * ratio * ratio * power;
  140.    ratio = max(ratio, 0.0f);
  141.    pointLightTotal.xyz += ratio * lightDot * pointLightColor3.xyz;
  142.  
  143.    lightDelta = worldPos.xyz - pointLightPos4.xyz;
  144.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  145.    ratio = 1.0f - (length(lightDelta) / pointLightRadius4);
  146.    ratio = ratio * ratio * ratio * power;
  147.    ratio = max(ratio, 0.0f);
  148.    pointLightTotal.xyz += ratio * lightDot * pointLightColor4.xyz;
  149.  
  150.    lightDelta = worldPos.xyz - pointLightPos5.xyz;
  151.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  152.    ratio = 1.0f - (length(lightDelta) / pointLightRadius5);
  153.    ratio = ratio * ratio * ratio * power;
  154.    ratio = max(ratio, 0.0f);
  155.    pointLightTotal.xyz += ratio * lightDot * pointLightColor5.xyz;
  156.  
  157.    lightDelta = worldPos.xyz - pointLightPos6.xyz;
  158.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  159.    ratio = 1.0f - (length(lightDelta) / pointLightRadius6);
  160.    ratio = ratio * ratio * ratio * power;
  161.    ratio = max(ratio, 0.0f);
  162.    pointLightTotal.xyz += ratio * lightDot * pointLightColor6.xyz;
  163.  
  164.    lightDelta = worldPos.xyz - pointLightPos7.xyz;
  165.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  166.    ratio = 1.0f - (length(lightDelta) / pointLightRadius7);
  167.    ratio = ratio * ratio * ratio * power;
  168.    ratio = max(ratio, 0.0f);
  169.    pointLightTotal.xyz += ratio * lightDot * pointLightColor7.xyz;
  170.  
  171.    return pointLightTotal;
  172. }
  173.  
  174. vec4 accumulateParticlePointLights()
  175. {
  176.    vec4 pointLightTotal = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  177.    vec3 lightDelta = vec3(0.0f, 0.0f, 0.0f);
  178.    float ratio = 0.0f;
  179.  
  180.    // Calculate effects of the 8 point lights.
  181.  
  182.    lightDelta = worldPos.xyz - pointLightPos0.xyz;
  183.    ratio = 1.0f - (length(lightDelta) / pointLightRadius0);
  184.    ratio = ratio * ratio * ratio * power;
  185.    ratio = max(ratio, 0.0f);
  186.    pointLightTotal.xyz += ratio * pointLightColor0.xyz;
  187.  
  188.    lightDelta = worldPos.xyz - pointLightPos1.xyz;
  189.    ratio = 1.0f - (length(lightDelta) / pointLightRadius1);
  190.    ratio = ratio * ratio * ratio * power;
  191.    ratio = max(ratio, 0.0f);
  192.    pointLightTotal.xyz += ratio * pointLightColor1.xyz;
  193.  
  194.    return pointLightTotal;
  195. }
  196.  
  197. // Combine specular and direct lighting terms.
  198. // note: if we make combinedColor "out" only, it throws a potentially uninitialized value warning, so we've made it inout
  199. void applyLighting(inout vec4 combinedColor, vec4 albedo, float occlusionFactor)
  200. {
  201.    //large normal means glowing object
  202.    if(glow == 1 || (worldNormal.x + worldNormal.y + worldNormal.z) > 2.0f)
  203.    {
  204.       combinedColor = albedo * radiance + glowTint;
  205.       return;
  206.    }  
  207.  
  208.    vec4 dirLightSpecular = occlusionFactor * specular * dirLightColor;
  209.    dirLightSpecular *= 0.5f; //arbitrary adjustment
  210.    vec4 dirLightDirect = ((NdotL * dirLightColor) * occlusionFactor) + (dirLightAmbient * occlusionFactor) + (dirShadowColor * (1.0f - occlusionFactor));
  211.  
  212.    if(NdotL <= 0.04f)
  213.    {
  214.       dirLightDirect = dirShadowColor;
  215.       dirLightSpecular = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  216.    }
  217.    else if(NdotL <= 0.1)
  218.    {
  219.       float val = (NdotL - 0.04f) / (0.1f - 0.04f);
  220.       dirLightDirect = (dirLightDirect * val) + (dirShadowColor * (1.0f - val));
  221.       dirLightSpecular = dirLightSpecular * val;
  222.    }
  223.  
  224.    dirLightDirect += accumulatePointLights();
  225.  
  226.    dirLightSpecular.a = length(dirLightSpecular.rgb);
  227.    dirLightDirect.a *= min(occlusionFactor + 0.75f, 1.0f);
  228.    combinedColor.rgb = dirLightDirect.rgb * albedo.rgb;
  229.    combinedColor.a = albedo.a;
  230.    combinedColor += dirLightSpecular;
  231. }
  232.  
  233. void main()
  234. {
  235.    vec4 albedo = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  236.    calculateSurface(gl_Color, albedo);
  237.  
  238.    float occlusionFactor = 0.0f;
  239.    if(NdotL > -0.01f)
  240.    {
  241.       occlusionFactor = 1.0f;
  242.    }
  243.  
  244.    // Apply lighting and fog.
  245.    vec4 fragColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  246.    if(isParticle == 1)
  247.    {
  248.       vec4 texAlbedo = texture2D(tex, texCoord);
  249.  
  250.  
  251.       fragColor = texAlbedo * gl_Color;
  252.       fragColor.a = texAlbedo.a * gl_Color.a;
  253.          
  254.       vec4 dirLightDirect = (dirLightColor * occlusionFactor) + (dirLightAmbient * occlusionFactor) + (dirShadowColor * (1.0f - occlusionFactor));
  255.       vec4 plt = accumulateParticlePointLights();
  256.  
  257.       vec4 lightTotal = dirLightDirect + plt;
  258.       lightTotal.x = clamp(lightTotal.x, 0.0f, 1.2f);
  259.       lightTotal.y = clamp(lightTotal.y, 0.0f, 1.2f);
  260.       lightTotal.z = clamp(lightTotal.z, 0.0f, 1.2f);
  261.  
  262.       fragColor = texAlbedo * gl_Color * lightTotal;
  263.  
  264.       applyFog(fragColor);
  265.          fragColor.a = texAlbedo.a * gl_Color.a;
  266.      
  267.    }
  268.    else
  269.    {
  270.       applyLighting(fragColor, albedo, occlusionFactor);
  271.       applyFog(fragColor);
  272.    }
  273.    
  274.    gl_FragColor = fragColor;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement