Advertisement
StealthGlobal

PortNewSoftUltraMod_frag

Dec 28th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 120
  2. #extension GL_EXT_texture_array : require
  3. #extension GL_EXT_texture_array : enable
  4.  
  5. // Port's Poisson Disc (Optimized) Soft Shadow Shader (2015)*
  6. // - Now with an ultra graphics version, with proper blending this time...*
  7. // And various additions*
  8.  
  9. // Configuration: For the adventurous!
  10.  
  11. // Show only shadows?
  12. bool debug = false;
  13.  
  14. // Ultra Mode; use 2 shadows layered for fancier graphics
  15. bool ultraMode = true;
  16.  
  17. // Point Light Brightness (lightFactor), Exposure Control (exposureFactor) (only used in ultra mode)
  18. float lightFactor = 0.25f;
  19. float exposureFactor = 1.0f;
  20.  
  21. // http://forum.blockland.us/index.php?topic=289446.0
  22. float occlusionBlend = 0.875f; //1.0f
  23. bool interiorLightingFix = true; // toggles above fix; above value ignored (set to 1.0f) regardless of setting if false
  24.  
  25. // Specular Lighting
  26. float dirLightSpecularFactor = 0.5f; //0.5f
  27.  
  28. // This is blending. Shadows are rendered to separate layers based on distance.
  29. // This may cause shadows to suddenly change appearance. Use this to change
  30. // how long a distance they will "fade" between the two versions over.
  31. // Blending only works with ultra mode off, since I (not port) can not figure this out
  32.  
  33. float blendAlpha = 0.5f; // bl default is 0.9f
  34. float blendBeta = 1.0f - blendAlpha;
  35.  
  36. // These values are very important. If they're too low, you will see weird
  37. // patterns and waves everywhere. If they're too high, shadows will be
  38. // disconnected from their objects. They need to be adjusted carefully.
  39. // These are set specifically for Max quality with max drawing distance.
  40. // You'll need to change them based on your shader quality (and if you changed
  41. // the Poisson disk below.. probably).
  42.  
  43. //const float fudgeFactor1 = 0.1f; //0.1f //0.3f ultra
  44. //const float fudgeFactor2 = 0.15f; //0.25f
  45. //const float fudgeFactor3 = 1.0f; //0.7f
  46. //const float fudgeFactor4 = 1.5f; //2.66f
  47.  
  48. const float fudgeFactor1 = 0.2f; //0.1f
  49. const float fudgeFactor2 = 0.5f; //0.25f
  50. const float fudgeFactor3 = 1.2f; //0.7f
  51. const float fudgeFactor4 = 2.8f; //2.66f
  52.  
  53. // How soft should the shadows be? (how far out does the edge go)
  54. // Change this or the magic numbers below to improve your "softness" quality
  55. // normal is always used, ambient is used in ultra mode
  56.  
  57. float normalSampleDistance = 1.0f / 800.0f;
  58. float ambientSampleDistance = 1.0f / 200.0f;
  59.  
  60. // Magic numbers below
  61. int poissonDiskCount = 32;
  62. vec2 poissonDisk[59] = vec2[](
  63.   vec2(0.01020043f, 0.3103616f),
  64.   vec2(-0.4121873f, -0.1701329f),
  65.   vec2(0.4333374f, 0.6148015f),
  66.   vec2(0.1092096f, -0.2437763f),
  67.   vec2(0.6641068f, -0.1210794f),
  68.   vec2(-0.1726627f, 0.8724736f),
  69.   vec2(-0.8549297f, 0.2836411f),
  70.   vec2(0.5146544f, -0.6802685f),
  71.   vec2(0.04769185f, -0.879628f),
  72.   vec2(-0.9373617f, -0.2187589f),
  73.   vec2(-0.69226f, -0.6652822f),
  74.   vec2(0.9230682f, 0.3181772f),
  75.   // these points might be bad:
  76.   vec2(-0.1565961f, 0.8773971f),
  77.   vec2(-0.5258075f, 0.3916658f),
  78.   vec2(0.515902f, 0.3077986f),
  79.   vec2(-0.006838934f, 0.2577735f),
  80.   vec2(-0.9315282f, -0.04518054f),
  81.   vec2(-0.3417063f, -0.1195169f),
  82.   vec2(-0.3221133f, -0.8118886f),
  83.   vec2(0.425082f, -0.3786222f),
  84.   vec2(0.3917231f, 0.9194779f),
  85.   vec2(0.8819267f, -0.1306234f),
  86.   vec2(-0.7906089f, -0.5639677f),
  87.   vec2(0.2073919f, -0.9611396f),
  88.   //below...
  89.   vec2(-0.05151585f, 0.3436534f),
  90.   vec2(0.3648908f, 0.2827295f),
  91.   vec2(-0.2478754f, 0.186921f),
  92.   vec2(0.1171809f, 0.1482293f),
  93.   vec2(-0.1496224f, 0.6990415f),
  94.   vec2(-0.456594f, 0.378567f),
  95.   vec2(-0.4242465f, -0.001935145f),
  96.   vec2(-0.1889321f, -0.2015685f),
  97.   vec2(0.1480272f, 0.6432338f),
  98.   vec2(-0.5046303f, 0.8245607f),
  99.   vec2(0.001617888f, 0.9789896f),
  100.   vec2(-0.6228038f, 0.5963655f),
  101.   vec2(0.4185582f, 0.7959766f),
  102.   vec2(0.06965782f, -0.1184023f),
  103.   vec2(-0.8310863f, 0.2197417f),
  104.   vec2(-0.869589f, 0.4893173f),
  105.   vec2(-0.6366982f, -0.357598f),
  106.   vec2(-0.2509329f, -0.5531961f),
  107.   vec2(-0.03994134f, -0.4170877f),
  108.   vec2(-0.675245f, -0.0009701257f),
  109.   vec2(0.3373009f, -0.4531572f),
  110.   vec2(0.3022793f, -0.02336982f),
  111.   vec2(0.6078352f, 0.5235748f),
  112.   vec2(-0.9277961f, -0.05385896f),
  113.   vec2(0.3847639f, -0.7718652f),
  114.   vec2(0.5278201f, -0.168486f),
  115.   vec2(0.1269102f, -0.8461399f),
  116.   vec2(0.7260014f, -0.4588331f),
  117.   vec2(-0.8775687f, -0.450681f),
  118.   vec2(-0.574103f, -0.7766181f),
  119.   vec2(0.6930821f, 0.2592674f),
  120.   vec2(-0.3360346f, -0.8594083f),
  121.   vec2(-0.2591985f, 0.9300818f),
  122.   vec2(0.939391f, -0.2374034f),
  123.   vec2(0.8332635f, 0.01952092f)
  124.  
  125. );
  126.  
  127. // This has way too much acne
  128. // int poissonDiskCount = 35;
  129. // vec2 poissonDisk[35] = vec2[](
  130. //   vec2(-0.05151585f, 0.3436534f),
  131. //   vec2(0.3648908f, 0.2827295f),
  132. //   vec2(-0.2478754f, 0.186921f),
  133. //   vec2(0.1171809f, 0.1482293f),
  134. //   vec2(-0.1496224f, 0.6990415f),
  135. //   vec2(-0.456594f, 0.378567f),
  136. //   vec2(-0.4242465f, -0.001935145f),
  137. //   vec2(-0.1889321f, -0.2015685f),
  138. //   vec2(0.1480272f, 0.6432338f),
  139. //   vec2(-0.5046303f, 0.8245607f),
  140. //   vec2(0.001617888f, 0.9789896f),
  141. //   vec2(-0.6228038f, 0.5963655f),
  142. //   vec2(0.4185582f, 0.7959766f),
  143. //   vec2(0.06965782f, -0.1184023f),
  144. //   vec2(-0.8310863f, 0.2197417f),
  145. //   vec2(-0.869589f, 0.4893173f),
  146. //   vec2(-0.6366982f, -0.357598f),
  147. //   vec2(-0.2509329f, -0.5531961f),
  148. //   vec2(-0.03994134f, -0.4170877f),
  149. //   vec2(-0.675245f, -0.0009701257f),
  150. //   vec2(0.3373009f, -0.4531572f),
  151. //   vec2(0.3022793f, -0.02336982f),
  152. //   vec2(0.6078352f, 0.5235748f),
  153. //   vec2(-0.9277961f, -0.05385896f),
  154. //   vec2(0.3847639f, -0.7718652f),
  155. //   vec2(0.5278201f, -0.168486f),
  156. //   vec2(0.1269102f, -0.8461399f),
  157. //   vec2(0.7260014f, -0.4588331f),
  158. //   vec2(-0.8775687f, -0.450681f),
  159. //   vec2(-0.574103f, -0.7766181f),
  160. //   vec2(0.6930821f, 0.2592674f),
  161. //   vec2(-0.3360346f, -0.8594083f),
  162. //   vec2(-0.2591985f, 0.9300818f),
  163. //   vec2(0.939391f, -0.2374034f),
  164. //   vec2(0.8332635f, 0.01952092f)
  165. // );
  166.  
  167. // Varying.
  168. varying vec4 vPos;
  169. varying vec3 worldNormal;
  170. varying vec3 worldPos;
  171.  
  172. // Global directional light uniforms.
  173. uniform vec4 dirLightDir;
  174. uniform vec4 dirLightColor;
  175. uniform vec4 dirLightAmbient;
  176. uniform vec4 dirShadowColor;
  177.  
  178. // Misc uniforms.
  179. uniform vec3 camPos;
  180. uniform mat4 obj2World;
  181. uniform mat4 world2Cam;
  182.  
  183. uniform int isParticle;
  184. uniform int doColorMultiply;
  185. uniform int glow;
  186.  
  187. uniform sampler2DArray stex;
  188. uniform sampler2D tex;
  189.  
  190. // Surface calculations, including specular power.
  191. varying vec2 texCoord;
  192. vec4 viewDelta;
  193. float specular;
  194. float NdotL;
  195. vec3 reflectVec;
  196.  
  197. void calculateSurface(vec4 color, inout vec4 albedo)
  198. {
  199.    viewDelta.xyz = worldPos - camPos;
  200.    viewDelta.w   = length(viewDelta.xyz);
  201.    viewDelta.xyz = -normalize(viewDelta.xyz);
  202.  
  203.    vec4 texAlbedo = texture2D(tex, texCoord);
  204.    albedo.rgb = mix(color.rgb, texAlbedo.rgb, texAlbedo.a);
  205.  
  206.    if(doColorMultiply == 1)
  207.       albedo *= gl_Color;
  208.  
  209.    albedo.a = color.a;
  210.  
  211.    NdotL = max(dot(worldNormal, dirLightDir.xyz), 0.0f);
  212.    reflectVec = normalize(reflect(-dirLightDir.xyz, worldNormal));
  213.    specular = pow(max(dot(reflectVec, viewDelta.xyz), 0.0f), 12.0f) * length(texAlbedo.rgb); //change this to change the size of the sun specular reflection
  214.  
  215.    //Uncomment below line for a neat rainbow color effect on everything
  216.    //albedo.rgb = normalize(viewDelta.xyz);
  217. }
  218.  
  219. // Fogging.
  220. uniform vec4 fogBaseColor;
  221. uniform vec4 fogConsts;
  222. uniform sampler2D fogTex;
  223. varying vec2 fogCoords;
  224. void applyFog(inout vec4 albedo)
  225. {
  226.    // Calculate fog.
  227.    vec4 fogColor = texture2D(fogTex, fogCoords) * fogBaseColor;
  228.  
  229.    // Blend it.
  230.    albedo = mix(albedo, fogColor, fogColor.a);
  231. }
  232.  
  233. // Shadowing
  234. uniform vec4 far_d;
  235. uniform vec2 texSize; // x - size, y - 1/size
  236. uniform vec4 zScale;
  237. uniform int shadowSplitCount;
  238. void calculateShadowCoords(inout vec4 shadow_coordA, inout vec4 shadow_coordB, out float blend)
  239. {
  240.    int index = 3;
  241.    float fudgeFactorA = 0.0f;
  242.    float fudgeFactorB = 0.0f;
  243.    fudgeFactorA = fudgeFactor4 / zScale.w;
  244.    fudgeFactorB = fudgeFactor4 / zScale.w;
  245.    blend = 0.0f;
  246.  
  247.    // find the appropriate depth map to look up in based on the depth of this fragment
  248.    if(vPos.y < far_d.x)
  249.    {
  250.       index = 0;
  251.       if(shadowSplitCount > 1)
  252.          blend = clamp( (vPos.y - (far_d.x * blendAlpha)) / (far_d.x * blendBeta), 0.0f, 1.0f);
  253.       fudgeFactorA = fudgeFactor1 / zScale.x;
  254.       fudgeFactorB = fudgeFactor2 / zScale.y;
  255.    }
  256.    else if(vPos.y < far_d.y)
  257.    {
  258.       index = 1;
  259.       if(shadowSplitCount > 2)
  260.          blend = clamp( (vPos.y - (far_d.y * blendAlpha)) / (far_d.x * blendBeta), 0.0f, 1.0f);
  261.       fudgeFactorA = fudgeFactor2 / zScale.y;
  262.       fudgeFactorB = fudgeFactor3 / zScale.z;
  263.    }
  264.    else if(vPos.y < far_d.z)
  265.    {
  266.       index = 2;
  267.       if(shadowSplitCount > 3)
  268.          blend = clamp( (vPos.y - (far_d.z * blendAlpha)) / (far_d.x * blendBeta), 0.0f, 1.0f);
  269.       fudgeFactorA = fudgeFactor3 / zScale.z;
  270.       fudgeFactorB = fudgeFactor4 / zScale.w;
  271.    }
  272.  
  273.    // transform this fragment's position from view space to scaled light clip space
  274.    // such that the xy coordinates are in [0;1]
  275.    // note there is no need to divide by w for orthogonal light sources
  276.    shadow_coordA   = gl_TextureMatrix[index]*vPos;
  277.    shadow_coordA.w = shadow_coordA.z - fudgeFactorA; // Figure the input coordinate for PCF sampling if appropriate.
  278.    shadow_coordA.z = float(index);                   // Encode the layer to sample.
  279.  
  280.    //don't have to set second shadow coord if we're not blending
  281.    if(blend > 0.0f)
  282.    {
  283.       shadow_coordB   = gl_TextureMatrix[index + 1]*vPos;
  284.       shadow_coordB.w = shadow_coordB.z - fudgeFactorB;
  285.       shadow_coordB.z = float(index + 1);
  286.    }
  287. }
  288.  
  289. // Point lighting
  290. uniform vec4     pointLightPos0;
  291. uniform vec4   pointLightColor0;
  292. uniform float pointLightRadius0;
  293.  
  294. uniform vec4     pointLightPos1;
  295. uniform vec4   pointLightColor1;
  296. uniform float pointLightRadius1;
  297.  
  298. uniform vec4     pointLightPos2;
  299. uniform vec4   pointLightColor2;
  300. uniform float pointLightRadius2;
  301.  
  302. uniform vec4     pointLightPos3;
  303. uniform vec4   pointLightColor3;
  304. uniform float pointLightRadius3;
  305.  
  306. uniform vec4     pointLightPos4;
  307. uniform vec4   pointLightColor4;
  308. uniform float pointLightRadius4;
  309.  
  310. uniform vec4     pointLightPos5;
  311. uniform vec4   pointLightColor5;
  312. uniform float pointLightRadius5;
  313.  
  314. uniform vec4     pointLightPos6;
  315. uniform vec4   pointLightColor6;
  316. uniform float pointLightRadius6;
  317.  
  318. uniform vec4     pointLightPos7;
  319. uniform vec4   pointLightColor7;
  320. uniform float pointLightRadius7;
  321.  
  322. vec4 accumulatePointLights()
  323. {
  324.    vec4 pointLightTotal = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  325.    vec3 lightDelta = vec3(0.0f, 0.0f, 0.0f);
  326.    float lightDot = 0.0f;
  327.    float ratio = 0.0f;
  328.  
  329.    // Calculate effects of the 8 point lights.
  330.  
  331.    lightDelta = worldPos.xyz - pointLightPos0.xyz;
  332.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  333.    ratio = 1.0f - (length(lightDelta) / pointLightRadius0);
  334.    ratio = ratio * ratio * ratio * 0.4f;
  335.    ratio = max(ratio, 0.0f);
  336.    pointLightTotal.xyz += ratio * lightDot * pointLightColor0.xyz;
  337.  
  338.    lightDelta = worldPos.xyz - pointLightPos1.xyz;
  339.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  340.    ratio = 1.0f - (length(lightDelta) / pointLightRadius1);
  341.    ratio = ratio * ratio * ratio * 0.4f;
  342.    ratio = max(ratio, 0.0f);
  343.    pointLightTotal.xyz += ratio * lightDot * pointLightColor1.xyz;
  344.  
  345.    lightDelta = worldPos.xyz - pointLightPos2.xyz;
  346.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  347.    ratio = 1.0f - (length(lightDelta) / pointLightRadius2);
  348.    ratio = ratio * ratio * ratio * 0.4f;
  349.    ratio = max(ratio, 0.0f);
  350.    pointLightTotal.xyz += ratio * lightDot * pointLightColor2.xyz;
  351.  
  352.    lightDelta = worldPos.xyz - pointLightPos3.xyz;
  353.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  354.    ratio = 1.0f - (length(lightDelta) / pointLightRadius3);
  355.    ratio = ratio * ratio * ratio * 0.4f;
  356.    ratio = max(ratio, 0.0f);
  357.    pointLightTotal.xyz += ratio * lightDot * pointLightColor3.xyz;
  358.  
  359.    lightDelta = worldPos.xyz - pointLightPos4.xyz;
  360.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  361.    ratio = 1.0f - (length(lightDelta) / pointLightRadius4);
  362.    ratio = ratio * ratio * ratio * 0.4f;
  363.    ratio = max(ratio, 0.0f);
  364.    pointLightTotal.xyz += ratio * lightDot * pointLightColor4.xyz;
  365.  
  366.    lightDelta = worldPos.xyz - pointLightPos5.xyz;
  367.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  368.    ratio = 1.0f - (length(lightDelta) / pointLightRadius5);
  369.    ratio = ratio * ratio * ratio * 0.4f;
  370.    ratio = max(ratio, 0.0f);
  371.    pointLightTotal.xyz += ratio * lightDot * pointLightColor5.xyz;
  372.  
  373.    lightDelta = worldPos.xyz - pointLightPos6.xyz;
  374.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  375.    ratio = 1.0f - (length(lightDelta) / pointLightRadius6);
  376.    ratio = ratio * ratio * ratio * 0.4f;
  377.    ratio = max(ratio, 0.0f);
  378.    pointLightTotal.xyz += ratio * lightDot * pointLightColor6.xyz;
  379.  
  380.    lightDelta = worldPos.xyz - pointLightPos7.xyz;
  381.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  382.    ratio = 1.0f - (length(lightDelta) / pointLightRadius7);
  383.    ratio = ratio * ratio * ratio * 0.4f;
  384.    ratio = max(ratio, 0.0f);
  385.    pointLightTotal.xyz += ratio * lightDot * pointLightColor7.xyz;
  386.  
  387.    return pointLightTotal;
  388. }
  389.  
  390. vec4 accumulateParticlePointLights()
  391. {
  392.    vec4 pointLightTotal = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  393.    vec3 lightDelta = vec3(0.0f, 0.0f, 0.0f);
  394.    float ratio = 0.0f;
  395.  
  396.    // Calculate effects of the 8 point lights.
  397.  
  398.    lightDelta = worldPos.xyz - pointLightPos0.xyz;
  399.    ratio = 1.0f - (length(lightDelta) / pointLightRadius0);
  400.    ratio = ratio * ratio * ratio * 0.4f;
  401.    ratio = max(ratio, 0.0f);
  402.    pointLightTotal.xyz += ratio * pointLightColor0.xyz;
  403.  
  404.    lightDelta = worldPos.xyz - pointLightPos1.xyz;
  405.    ratio = 1.0f - (length(lightDelta) / pointLightRadius1);
  406.    ratio = ratio * ratio * ratio * 0.4f;
  407.    ratio = max(ratio, 0.0f);
  408.    pointLightTotal.xyz += ratio * pointLightColor1.xyz;
  409.  
  410.    return pointLightTotal;
  411. }
  412.  
  413. // Combine specular and direct lighting terms.
  414. // note: if we make combinedColor "out" only, it throws a potentially uninitialized value warning, so we've made it inout
  415. void applyLighting(inout vec4 combinedColor, vec4 albedo, float occlusionFactor)
  416. {
  417.    //large normal means glowing object
  418.    if(glow == 1 || (worldNormal.x + worldNormal.y + worldNormal.z) > 2.0f)
  419.    {
  420.       combinedColor = albedo;
  421.       return;
  422.    }
  423.  
  424.    vec4 dirLightSpecular = occlusionFactor * specular * dirLightColor;
  425.    dirLightSpecular *= dirLightSpecularFactor; //arbitrary adjustment
  426.    vec4 dirLightDirect = ((NdotL * dirLightColor) * occlusionFactor) + (dirLightAmbient * occlusionFactor) + (dirShadowColor * (1.0f - occlusionFactor));
  427.  
  428.    if(NdotL <= 0.04f)
  429.    {
  430.       dirLightDirect = dirShadowColor;
  431.       dirLightSpecular = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  432.    }
  433.    else if(NdotL <= 0.1)
  434.    {
  435.       float val = (NdotL - 0.04f) / (0.1f - 0.04f);
  436.       dirLightDirect = (dirLightDirect * val) + (dirShadowColor * (1.0f - val));
  437.       dirLightSpecular = dirLightSpecular * val;
  438.    }
  439.  
  440.    dirLightDirect += accumulatePointLights() * lightFactor;
  441.  
  442.    dirLightSpecular.a = length(dirLightSpecular.rgb);
  443.    dirLightDirect.a *= min(occlusionFactor + 0.75f, 1.0f);
  444.    combinedColor.rgb = dirLightDirect.rgb * albedo.rgb;
  445.    combinedColor.a = albedo.a;
  446.    combinedColor += dirLightSpecular;
  447. }
  448.  
  449. float poissonSample(vec4 shadow_coord, float spread)
  450. {
  451.   int hit = 0;
  452.  
  453.   for (int i = 0; i < poissonDiskCount; i++) {
  454.     float dist = texture2DArray(stex, vec3(shadow_coord.xy + poissonDisk[i] * spread, shadow_coord.z)).x;
  455.  
  456.     if (dist - shadow_coord.w > 0.0f)
  457.       hit++;
  458.   }
  459.  
  460.   return float(hit) / poissonDiskCount;
  461. }
  462.  
  463. float shadowCoef()
  464. {
  465.     vec4 shadow_coordA = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  466.     vec4 shadow_coordB = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  467.  
  468.     float blend = 0.0f;
  469.  
  470.     calculateShadowCoords(shadow_coordA, shadow_coordB, blend);
  471.  
  472.     float sampleA;
  473.  
  474.     sampleA = ultraMode ? (poissonSample(shadow_coordA, normalSampleDistance) + poissonSample(shadow_coordA, ambientSampleDistance)) / 2 : poissonSample(shadow_coordA, normalSampleDistance);
  475.  
  476.     if (blend > 0.0f)
  477.     {
  478.         float sampleB;
  479.  
  480.         sampleB = ultraMode ? (poissonSample(shadow_coordB, normalSampleDistance) + poissonSample(shadow_coordB, ambientSampleDistance)) / 2 : poissonSample(shadow_coordB, normalSampleDistance);
  481.    
  482.         return clamp((sampleB * blend) + (sampleA * (1.0f - blend)), 0.0f, 1.0f) * exposureFactor;
  483.     }
  484.  
  485.     return sampleA * exposureFactor;
  486. }
  487.  
  488. void main()
  489. {
  490.    vec4 albedo = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  491.    calculateSurface(gl_Color, albedo);
  492.  
  493.    float occlusionFactor = 0.0f;
  494.    if(NdotL > -0.01f)
  495.    {
  496.       if(shadowSplitCount <= 0)
  497.          occlusionFactor = 1.0f;
  498.       else
  499.          occlusionFactor = shadowCoef();
  500.    }
  501.  
  502.    // Apply lighting and fog.
  503.    vec4 fragColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  504.    if(isParticle == 1)
  505.    {
  506.       vec4 texAlbedo = texture2D(tex, texCoord);
  507.  
  508.       vec4 dirLightDirect = (dirLightColor * occlusionFactor) + (dirLightAmbient * occlusionFactor) + (dirShadowColor * (1.0f - occlusionFactor));
  509.       vec4 plt = accumulateParticlePointLights();
  510.  
  511.       vec4 lightTotal = dirLightDirect + plt;
  512.       lightTotal.x = clamp(lightTotal.x, 0.0f, 1.2f);
  513.       lightTotal.y = clamp(lightTotal.y, 0.0f, 1.2f);
  514.       lightTotal.z = clamp(lightTotal.z, 0.0f, 1.2f);
  515.  
  516.       fragColor = texAlbedo * gl_Color * lightTotal;
  517.  
  518.       applyFog(fragColor);
  519.       fragColor.a = texAlbedo.a * gl_Color.a;
  520.    }
  521.    else
  522.    {
  523.         if (interiorLightingFix)
  524.             applyLighting(fragColor, albedo, 1 - (1 - occlusionFactor) * occlusionBlend);
  525.         else
  526.             applyLighting(fragColor, albedo, occlusionFactor);
  527.         applyFog(fragColor);
  528.    }
  529.  
  530.    // Uncomment to viz depth in B.
  531.    //fragColor.z = vPos.y * 0.01f;
  532.  
  533.    gl_FragColor = fragColor;
  534.    
  535.    // Uncomment to show shadows only  
  536.    if (debug)
  537.         gl_FragColor = vec4(occlusionFactor, occlusionFactor, occlusionFactor, 1);
  538. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement