Advertisement
StealthGlobal

Blockland PCSS 'r2'

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