Advertisement
StealthGlobal

Blockland PCSS

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