Advertisement
StealthGlobal

PCSS r5

Apr 29th, 2018
140
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. #extension GL_EXT_gpu_shader4 : require
  6. #extension GL_EXT_gpu_shader4 : enable
  7.  
  8. // Port's Poisson Disc (Optimized) Soft Shadow Shader (2015), modified by Stealth Commander/QuadStorm
  9. // Includes an ultra graphics version, a Percentage-Closer Soft Shadows (PCSS) implementation, and other smaller configuration variables
  10.  
  11. // Revision '5' Release
  12.  
  13.  
  14. // Configuration: For the adventurous!
  15.  
  16. // Configuration Guide
  17. // |default - the values as released as a reference for tuning|
  18. // debug - show only shadows? |false|
  19. // ultraMode - use 2 shadow sample layers ontop of eachother for an 'ultra' graphics effect |false|
  20. // ultraRatio - ratio in size between the normal and ambient samples |5.0f|
  21. // PCSSToggle - toggles PCSS (dynamic soft shadow variance by distance) |true|
  22. // depthLoopNumber - sampling loop amount around each distance point: 1 = 3x3, 2 = 5x5, 3 = 7x7, etc. |2|
  23. // lightSize - how large the light source is -- the larger the softer shadows get faster. larger values can get messy |0.2f|
  24. // searchDistance - the shadow depth search distance -- higher values don't 'bleed' enough and lower values are less precise with shadow depths; /decrease/ for smaller lightsizes, /increase/ for larger ones |1.0f / 1536.0f|
  25. // superGlow - Off-like glow effect; works correctly for transparent glowing bricks |true|
  26. // shadowOpacity - allows for changing how transparent/opaque shadows are -- see https://forum.blockland.us/index.php?topic=289446.0; this toggles the fix |true|
  27. // occlusionBlend - strength of above effect, where 1.0f is no effect (default opaque), and 0.0f is the same as minimum shaders (no shadows) |0.925f|
  28.  
  29. // 'Perfection' can be very hard to come by. You'll probably have to tweak settings per situation to get things right.
  30.  
  31.  
  32. // Ultra Mode; tends to look better with PCSS off, but you can have both on if you must
  33. bool ultraMode = false;
  34. float ultraRatio = 5.0f;
  35.  
  36. // tune the 'ultra' shader effect a bit
  37. // > 0.5f weighs the small sample more, while < 0.5f weighs the large sample more
  38. // values too close to either 0.0f or 1.0f make the effect too subtle and defeat the purpose of ultra mode
  39. float ultraWeightFactor = 0.4f;
  40.  
  41.  
  42. // PCSS, aka fighting everything that's wrong with shadow maps
  43. // but could look good if you get the variables balanced right
  44.  
  45. bool PCSSToggle = true;
  46.  
  47. int depthLoopNumber = 3;
  48. float lightSize = 0.1f;
  49. float searchDistance = 1.0f / 100.0f;
  50.  
  51. bool poissonDepth = false; // poisson depth sampling
  52.  
  53. // PCSS clamping; this aims to mitigate the horrible looking effects of large radii and force a minimum of shadow softness
  54. bool pcssClampRadii = false;
  55. float pcssMinRadius = 0.0002f;
  56. float pcssMaxRadius = 0.004f;
  57.  
  58. // Basic Box PCF (no poisson, a la other soft shadow methods)
  59. bool pcfSampler = true;
  60. int pcfNum = 5;
  61. // a try at less sampling when not needed - quality/performance optimization
  62. bool pcfOptimization = true;
  63.  
  64.  
  65. // Fog
  66. bool linearFog = false; // faster, but sorta less realistic
  67. float fogExp = 1.0f;
  68.  
  69. // Superglow
  70. bool superGlow = true;
  71.  
  72. // Shadow Opacity
  73. bool shadowOpacity = true;
  74. float occlusionBlend = 0.925f;
  75.  
  76. // This is blending. Shadows are rendered to separate layers based on distance.
  77. // This may cause shadows to suddenly change appearance. Use this to change
  78. // how long a distance they will "fade" between the two versions over.
  79. // Blending only works with ultra mode off, since I (not port) can not figure this out
  80.  
  81. float blendAlpha = 0.9f; // bl default is 0.9f
  82. float blendBeta = 1.0f - blendAlpha;
  83.  
  84. // These values are very important. If they're too low, you will see weird
  85. // patterns and waves everywhere. If they're too high, shadows will be
  86. // disconnected from their objects. They need to be adjusted carefully.
  87. // These are set specifically for Max quality with max drawing distance.
  88. // You'll need to change them based on your shader quality (and if you changed
  89. // the Poisson disk below.. probably).
  90.  
  91. const float fudgeFactor1 = 0.2f;
  92. const float fudgeFactor2 = 0.5f;
  93. const float fudgeFactor3 = 1.5f;
  94. const float fudgeFactor4 = 5.0f;
  95.  
  96. // How soft should the shadows be? (how far out does the edge go)
  97. // Change this or the magic numbers below to improve your "softness" quality
  98. // note that fancy powers of two aren't needed, but they're a good starting point
  99.  
  100. float sampleDistance = 1.0f / 1000.0f;
  101.  
  102. // Magic numbers below
  103.  
  104. int poissonDiskCount = 135;
  105. vec2 poissonDisk[135] = vec2[]
  106. (
  107.     vec2(0, 0),
  108.     vec2(0.0951336, -0.1457852),
  109.     vec2(-0.1244571, -0.1191383),
  110.     vec2(-0.06589106, 0.1528488),
  111.     vec2(-0.1134799, -0.01447811),
  112.     vec2(-0.1374047, -0.2194312),
  113.     vec2(0.00450462, -0.2047105),
  114.     vec2(0.1119988, 0.05098353),
  115.     vec2(0.2288575, -0.06912366),
  116.     vec2(-0.2133227, 0.02454831),
  117.     vec2(0.1111896, 0.1807129),
  118.     vec2(0.2559151, 0.02961993),
  119.     vec2(0.3407415, -0.07578353),
  120.     vec2(0.2563416, -0.2436545),
  121.     vec2(-0.1701523, 0.1683661),
  122.     vec2(0.1965078, -0.4170507),
  123.     vec2(0.1183021, -0.2830924),
  124.     vec2(-0.01656891, -0.3364981),
  125.     vec2(-0.254407, -0.2527578),
  126.     vec2(-0.4078597, -0.228698),
  127.     vec2(-0.3459725, -0.3678785),
  128.     vec2(0.2611208, -0.5687482),
  129.     vec2(0.05222826, -0.4770998),
  130.     vec2(0.1594227, -0.7015165),
  131.     vec2(0.2840366, -0.6748313),
  132.     vec2(-0.3264929, -0.1505083),
  133.     vec2(-0.5535817, -0.2131963),
  134.     vec2(-0.00959465, -0.8068637),
  135.     vec2(0.064941, -0.643945),
  136.     vec2(0.2717584, -0.8386889),
  137.     vec2(-0.1910821, -0.4466026),
  138.     vec2(-0.5111607, -0.423937),
  139.     vec2(0.379738, -0.8019445),
  140.     vec2(-0.3860002, -0.5012952),
  141.     vec2(-0.6750128, -0.5125482),
  142.     vec2(0.4067858, -0.9025675),
  143.     vec2(0.4950482, -0.6575115),
  144.     vec2(-0.5073444, -0.6144094),
  145.     vec2(-0.08645296, 0.2604458),
  146.     vec2(-0.6786309, -0.3579207),
  147.     vec2(-0.8759595, -0.3427323),
  148.     vec2(0.5233065, -0.4795123),
  149.     vec2(0.5770249, -0.5666536),
  150.     vec2(0.3506554, -0.5032448),
  151.     vec2(0.05563826, -0.9132139),
  152.     vec2(0.08806939, -0.7815942),
  153.     vec2(0.7659222, -0.5198661),
  154.     vec2(-0.2904063, 0.2057745),
  155.     vec2(0.495086, -0.7972633),
  156.     vec2(-0.7750046, -0.2942277),
  157.     vec2(-0.1838527, -0.8644153),
  158.     vec2(-0.06777722, -0.9496753),
  159.     vec2(0.3823927, 0.1203214),
  160.     vec2(0.4139371, 0.2589353),
  161.     vec2(-0.3244419, 0.01984585),
  162.     vec2(-0.3697801, 0.1100583),
  163.     vec2(-0.8288329, -0.4809016),
  164.     vec2(0.3429501, -0.3237988),
  165.     vec2(-0.5749199, -0.3365948),
  166.     vec2(0.6363789, -0.4035244),
  167.     vec2(-0.1754244, -0.7338435),
  168.     vec2(-0.2328039, 0.3389311),
  169.     vec2(-0.3044877, -0.5779614),
  170.     vec2(-0.4703759, 0.1353574),
  171.     vec2(0.3635537, -0.6099477),
  172.     vec2(0.7902213, -0.3691019),
  173.     vec2(0.604952, -0.7716019),
  174.     vec2(-0.6491721, 0.2100721),
  175.     vec2(0.407153, 0.4124183),
  176.     vec2(0.2635823, 0.1884831),
  177.     vec2(-0.5571768, 0.06792152),
  178.     vec2(0.4192934, -0.1522004),
  179.     vec2(0.8890129, -0.3393337),
  180.     vec2(0.4015584, 0.01058372),
  181.     vec2(0.2435132, 0.3321772),
  182.     vec2(0.5214001, -0.2958628),
  183.     vec2(0.5332999, 0.3964668),
  184.     vec2(0.2882766, 0.5295519),
  185.     vec2(0.8763232, -0.2319224),
  186.     vec2(0.7375241, -0.2650713),
  187.     vec2(0.8960116, -0.442726),
  188.     vec2(0.5325155, 0.08337001),
  189.     vec2(0.5671927, -0.06184172),
  190.     vec2(0.4954312, 0.5668753),
  191.     vec2(-0.02598101, 0.3976752),
  192.     vec2(0.08789875, 0.3352225),
  193.     vec2(-0.1313136, 0.4161511),
  194.     vec2(0.6276786, 0.6873559),
  195.     vec2(0.5912785, -0.1845674),
  196.     vec2(0.4185997, -0.2523295),
  197.     vec2(-0.5374222, -0.03362173),
  198.     vec2(-0.6922106, -0.1171541),
  199.     vec2(-0.1403162, 0.5610359),
  200.     vec2(-0.5648803, 0.3398148),
  201.     vec2(-0.4521095, 0.4548817),
  202.     vec2(0.7312721, 0.02824391),
  203.     vec2(-0.4362847, 0.2814974),
  204.     vec2(-0.5887011, 0.5207564),
  205.     vec2(-0.3412226, -0.7056051),
  206.     vec2(-0.6363071, 0.00310753),
  207.     vec2(-0.02171745, 0.6717157),
  208.     vec2(0.1501283, 0.6256612),
  209.     vec2(-0.7416134, 0.4596929),
  210.     vec2(-0.5666857, 0.6250131),
  211.     vec2(-0.8129191, -0.1920015),
  212.     vec2(-0.8548332, -0.08355456),
  213.     vec2(-0.4705517, 0.5749201),
  214.     vec2(-0.8407493, 0.3725242),
  215.     vec2(-0.1420833, 0.7480395),
  216.     vec2(-0.2413939, 0.6520627),
  217.     vec2(0.01996444, 0.4971052),
  218.     vec2(-0.313913, 0.5066901),
  219.     vec2(-0.8788791, 0.03306975),
  220.     vec2(-0.6664616, 0.7332086),
  221.     vec2(-0.3788642, 0.6391957),
  222.     vec2(0.127771, 0.4344893),
  223.     vec2(-0.6868327, 0.6078894),
  224.     vec2(-0.4726286, 0.6907672),
  225.     vec2(0.5142618, 0.7352716),
  226.     vec2(-0.4096418, 0.79397),
  227.     vec2(-0.9652838, -0.1078783),
  228.     vec2(0.05344208, 0.8427666),
  229.     vec2(0.7249011, 0.5268908),
  230.     vec2(0.7722252, 0.625686),
  231.     vec2(0.12518, 0.7605301),
  232.     vec2(-0.9623628, 0.1637841),
  233.     vec2(-0.7457262, 0.1424999),
  234.     vec2(-0.8787336, 0.2578501),
  235.     vec2(0.08879758, 0.9647315),
  236.     vec2(-0.06732855, 0.9499662),
  237.     vec2(0.1972721, 0.8491885),
  238.     vec2(0.2701098, 0.7291467),
  239.     vec2(0.3723929, 0.8447194),
  240.     vec2(0.4032576, 0.7399523),
  241.     vec2(0.2845929, 0.9401256)
  242. );
  243.  
  244.  
  245. bool debug = false;
  246.  
  247. // Varying.
  248. varying vec4 vPos;
  249. varying vec3 worldNormal;
  250. varying vec3 worldPos;
  251.  
  252. // Global directional light uniforms.
  253. uniform vec4 dirLightDir;
  254. uniform vec4 dirLightColor;
  255. uniform vec4 dirLightAmbient;
  256. uniform vec4 dirShadowColor;
  257.  
  258. // Misc uniforms.
  259. uniform vec3 camPos;
  260. uniform mat4 obj2World;
  261. uniform mat4 world2Cam;
  262.  
  263. uniform int isParticle;
  264. uniform int doColorMultiply;
  265. uniform int glow;
  266.  
  267. uniform sampler2DArray stex;
  268. uniform sampler2D tex;
  269.  
  270. // Surface calculations, including specular power.
  271. varying vec2 texCoord;
  272. vec4 viewDelta;
  273. float specular;
  274. float NdotL;
  275. vec3 reflectVec;
  276.  
  277. void calculateSurface(vec4 color, inout vec4 albedo)
  278. {
  279.    viewDelta.xyz = worldPos - camPos;
  280.    viewDelta.w   = length(viewDelta.xyz);
  281.    viewDelta.xyz = -normalize(viewDelta.xyz);
  282.  
  283.    vec4 texAlbedo = texture2D(tex, texCoord);
  284.    albedo.rgb = mix(color.rgb, texAlbedo.rgb, texAlbedo.a);
  285.  
  286.    if(doColorMultiply == 1)
  287.       albedo *= gl_Color;
  288.  
  289.    albedo.a = color.a;
  290.  
  291.    NdotL = max(dot(worldNormal, dirLightDir.xyz), 0.0f);
  292.    reflectVec = normalize(reflect(-dirLightDir.xyz, worldNormal));
  293.    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
  294.  
  295.    // Uncomment below for a neat rainbow color effect on everything
  296.    // albedo.rgb = normalize(viewDelta.xyz);
  297. }
  298.  
  299. // Fogging.
  300. uniform vec4 fogBaseColor;
  301. uniform vec4 fogConsts;
  302. uniform sampler2D fogTex;
  303. varying vec2 fogCoords;
  304.  
  305. void applyFog(inout vec4 albedo, in float occlusionFactor)
  306. {
  307.    // Calculate fog.
  308.    vec4 fogColor = texture2D(fogTex, fogCoords) * fogBaseColor;
  309.  
  310.    // Blend it.
  311.     if (!linearFog)
  312.         albedo = mix(albedo, fogColor, pow((0.5f + 1.0f / 2.0f * sin(fogColor.a * 3.141592654f - 1.570796327f)), fogExp));
  313.     else
  314.         albedo = mix(albedo, fogColor, fogColor.a);
  315. }
  316.  
  317. // Shadowing
  318. uniform vec4 far_d;
  319. uniform vec2 texSize; // x - size, y - 1/size
  320. uniform vec4 zScale;
  321. uniform int shadowSplitCount;
  322. void calculateShadowCoords(inout vec4 shadow_coordA, inout vec4 shadow_coordB, out float blend)
  323. {
  324.    int index = 3;
  325.    float fudgeFactorA = 0.0f;
  326.    float fudgeFactorB = 0.0f;
  327.    fudgeFactorA = fudgeFactor4 / zScale.w;
  328.    fudgeFactorB = fudgeFactor4 / zScale.w;
  329.    blend = 0.0f;
  330.  
  331.    // find the appropriate depth map to look up in based on the depth of this fragment
  332.    if(vPos.y < far_d.x)
  333.    {
  334.       index = 0;
  335.       if(shadowSplitCount > 1)
  336.          blend = clamp( (vPos.y - (far_d.x * blendAlpha)) / (far_d.x * blendBeta), 0.0f, 1.0f);
  337.       fudgeFactorA = fudgeFactor1 / zScale.x;
  338.       fudgeFactorB = fudgeFactor2 / zScale.y;
  339.    }
  340.    else if(vPos.y < far_d.y)
  341.    {
  342.       index = 1;
  343.       if(shadowSplitCount > 2)
  344.          blend = clamp( (vPos.y - (far_d.y * blendAlpha)) / (far_d.x * blendBeta), 0.0f, 1.0f);
  345.       fudgeFactorA = fudgeFactor2 / zScale.y;
  346.       fudgeFactorB = fudgeFactor3 / zScale.z;
  347.    }
  348.    else if(vPos.y < far_d.z)
  349.    {
  350.       index = 2;
  351.       if(shadowSplitCount > 3)
  352.          blend = clamp( (vPos.y - (far_d.z * blendAlpha)) / (far_d.x * blendBeta), 0.0f, 1.0f);
  353.       fudgeFactorA = fudgeFactor3 / zScale.z;
  354.       fudgeFactorB = fudgeFactor4 / zScale.w;
  355.    }
  356.  
  357.    // transform this fragment's position from view space to scaled light clip space
  358.    // such that the xy coordinates are in [0;1]
  359.    // note there is no need to divide by w for orthogonal light sources
  360.    shadow_coordA   = gl_TextureMatrix[index]*vPos;
  361.    shadow_coordA.w = shadow_coordA.z - fudgeFactorA; // Figure the input coordinate for PCF sampling if appropriate.
  362.    shadow_coordA.z = float(index);                   // Encode the layer to sample.
  363.  
  364.    //don't have to set second shadow coord if we're not blending
  365.    if(blend > 0.0f)
  366.    {
  367.       shadow_coordB   = gl_TextureMatrix[index + 1]*vPos;
  368.       shadow_coordB.w = shadow_coordB.z - fudgeFactorB;
  369.       shadow_coordB.z = float(index + 1);
  370.    }
  371. }
  372.  
  373. // Point lighting
  374. uniform vec4     pointLightPos0;
  375. uniform vec4   pointLightColor0;
  376. uniform float pointLightRadius0;
  377.  
  378. uniform vec4     pointLightPos1;
  379. uniform vec4   pointLightColor1;
  380. uniform float pointLightRadius1;
  381.  
  382. uniform vec4     pointLightPos2;
  383. uniform vec4   pointLightColor2;
  384. uniform float pointLightRadius2;
  385.  
  386. uniform vec4     pointLightPos3;
  387. uniform vec4   pointLightColor3;
  388. uniform float pointLightRadius3;
  389.  
  390. uniform vec4     pointLightPos4;
  391. uniform vec4   pointLightColor4;
  392. uniform float pointLightRadius4;
  393.  
  394. uniform vec4     pointLightPos5;
  395. uniform vec4   pointLightColor5;
  396. uniform float pointLightRadius5;
  397.  
  398. uniform vec4     pointLightPos6;
  399. uniform vec4   pointLightColor6;
  400. uniform float pointLightRadius6;
  401.  
  402. uniform vec4     pointLightPos7;
  403. uniform vec4   pointLightColor7;
  404. uniform float pointLightRadius7;
  405.  
  406. vec4 accumulatePointLights()
  407. {
  408.    vec4 pointLightTotal = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  409.    vec3 lightDelta = vec3(0.0f, 0.0f, 0.0f);
  410.    float lightDot = 0.0f;
  411.    float ratio = 0.0f;
  412.  
  413.    // Calculate effects of the 8 point lights.
  414.  
  415.    lightDelta = worldPos.xyz - pointLightPos0.xyz;
  416.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  417.    ratio = 1.0f - (length(lightDelta) / pointLightRadius0);
  418.    ratio = ratio * ratio * ratio * 0.4f;
  419.    ratio = max(ratio, 0.0f);
  420.    pointLightTotal.xyz += ratio * lightDot * pointLightColor0.xyz;
  421.  
  422.    lightDelta = worldPos.xyz - pointLightPos1.xyz;
  423.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  424.    ratio = 1.0f - (length(lightDelta) / pointLightRadius1);
  425.    ratio = ratio * ratio * ratio * 0.4f;
  426.    ratio = max(ratio, 0.0f);
  427.    pointLightTotal.xyz += ratio * lightDot * pointLightColor1.xyz;
  428.  
  429.    lightDelta = worldPos.xyz - pointLightPos2.xyz;
  430.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  431.    ratio = 1.0f - (length(lightDelta) / pointLightRadius2);
  432.    ratio = ratio * ratio * ratio * 0.4f;
  433.    ratio = max(ratio, 0.0f);
  434.    pointLightTotal.xyz += ratio * lightDot * pointLightColor2.xyz;
  435.  
  436.    lightDelta = worldPos.xyz - pointLightPos3.xyz;
  437.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  438.    ratio = 1.0f - (length(lightDelta) / pointLightRadius3);
  439.    ratio = ratio * ratio * ratio * 0.4f;
  440.    ratio = max(ratio, 0.0f);
  441.    pointLightTotal.xyz += ratio * lightDot * pointLightColor3.xyz;
  442.  
  443.    lightDelta = worldPos.xyz - pointLightPos4.xyz;
  444.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  445.    ratio = 1.0f - (length(lightDelta) / pointLightRadius4);
  446.    ratio = ratio * ratio * ratio * 0.4f;
  447.    ratio = max(ratio, 0.0f);
  448.    pointLightTotal.xyz += ratio * lightDot * pointLightColor4.xyz;
  449.  
  450.    lightDelta = worldPos.xyz - pointLightPos5.xyz;
  451.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  452.    ratio = 1.0f - (length(lightDelta) / pointLightRadius5);
  453.    ratio = ratio * ratio * ratio * 0.4f;
  454.    ratio = max(ratio, 0.0f);
  455.    pointLightTotal.xyz += ratio * lightDot * pointLightColor5.xyz;
  456.  
  457.    lightDelta = worldPos.xyz - pointLightPos6.xyz;
  458.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  459.    ratio = 1.0f - (length(lightDelta) / pointLightRadius6);
  460.    ratio = ratio * ratio * ratio * 0.4f;
  461.    ratio = max(ratio, 0.0f);
  462.    pointLightTotal.xyz += ratio * lightDot * pointLightColor6.xyz;
  463.  
  464.    lightDelta = worldPos.xyz - pointLightPos7.xyz;
  465.    lightDot = max(dot(-normalize(lightDelta), worldNormal), 0.0f);
  466.    ratio = 1.0f - (length(lightDelta) / pointLightRadius7);
  467.    ratio = ratio * ratio * ratio * 0.4f;
  468.    ratio = max(ratio, 0.0f);
  469.    pointLightTotal.xyz += ratio * lightDot * pointLightColor7.xyz;
  470.  
  471.    return pointLightTotal;
  472. }
  473.  
  474. vec4 accumulateParticlePointLights()
  475. {
  476.    vec4 pointLightTotal = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  477.    vec3 lightDelta = vec3(0.0f, 0.0f, 0.0f);
  478.    float ratio = 0.0f;
  479.  
  480.    // Calculate effects of the 8 point lights.
  481.  
  482.    lightDelta = worldPos.xyz - pointLightPos0.xyz;
  483.    ratio = 1.0f - (length(lightDelta) / pointLightRadius0);
  484.    ratio = ratio * ratio * ratio * 0.4f;
  485.    ratio = max(ratio, 0.0f);
  486.    pointLightTotal.xyz += ratio * pointLightColor0.xyz;
  487.  
  488.    lightDelta = worldPos.xyz - pointLightPos1.xyz;
  489.    ratio = 1.0f - (length(lightDelta) / pointLightRadius1);
  490.    ratio = ratio * ratio * ratio * 0.4f;
  491.    ratio = max(ratio, 0.0f);
  492.    pointLightTotal.xyz += ratio * pointLightColor1.xyz;
  493.  
  494.    return pointLightTotal;
  495. }
  496.  
  497. // Combine specular and direct lighting terms.
  498. // note: if we make combinedColor "out" only, it throws a potentially uninitialized value warning, so we've made it inout
  499. void applyLighting(inout vec4 combinedColor, vec4 albedo, float occlusionFactor)
  500. {
  501.    // large normal means glowing object
  502.    if(glow == 1 || (worldNormal.x + worldNormal.y + worldNormal.z) > 2.0f)
  503.    {
  504.       combinedColor = superGlow ? vec4(albedo.xyz * 1.5f, albedo.a) : albedo;
  505.       return;
  506.    }
  507.  
  508.    vec4 dirLightSpecular = occlusionFactor * specular * dirLightColor;
  509.    dirLightSpecular *= 0.5f; // arbitrary adjustment
  510.    vec4 dirLightDirect = ((NdotL * dirLightColor) * occlusionFactor) + (dirLightAmbient * occlusionFactor) + (dirShadowColor * (1.0f - occlusionFactor));
  511.  
  512.    if(NdotL <= 0.04f)
  513.    {
  514.       dirLightDirect = dirShadowColor;
  515.       dirLightSpecular = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  516.    }
  517.    else if(NdotL <= 0.1)
  518.    {
  519.       float val = (NdotL - 0.04f) / (0.1f - 0.04f);
  520.       dirLightDirect = (dirLightDirect * val) + (dirShadowColor * (1.0f - val));
  521.       dirLightSpecular = dirLightSpecular * val;
  522.    }
  523.  
  524.    dirLightDirect += accumulatePointLights();
  525.  
  526.    dirLightSpecular.a = length(dirLightSpecular.rgb);
  527.    dirLightDirect.a *= min(occlusionFactor + 0.75f, 1.0f);
  528.    combinedColor.rgb = dirLightDirect.rgb * albedo.rgb;
  529.    combinedColor.a = albedo.a;
  530.    combinedColor += dirLightSpecular;
  531. }
  532.  
  533. float poissonSampleO(vec4 shadow_coord, float spread)
  534. {
  535.   int hit = 0;
  536.  
  537.   for (int i = 0; i < poissonDiskCount; i++) {
  538.     float dist = texture2DArray(stex, vec3(shadow_coord.xy + poissonDisk[i] * spread, shadow_coord.z)).x;
  539.  
  540.     if (dist - shadow_coord.w > 0.0f)
  541.       hit++;
  542.   }
  543.  
  544.   return float(hit) / poissonDiskCount;
  545. }
  546.  
  547. float pcfSample(vec4 shadow_coord, float spread)
  548. {
  549.     int hit = 0;
  550.     int pcfLoops = 0;
  551.  
  552.     if (pcfOptimization)
  553.     {    
  554.         bool test = abs(spread) <= 0.0005f;
  555.    
  556.         if (test)
  557.         {
  558.             float pcfTest = pcfNum / 2;
  559.             pcfTest = floor(pcfTest);
  560.             pcfNum = int(pcfTest);
  561.         }
  562.     }
  563.    
  564.     for (int x = -pcfNum; x <= pcfNum; x++)
  565.     {
  566.         for (int y = -pcfNum; y <= pcfNum; y++)
  567.         {
  568.             float dist = texture2DArray(stex, vec3(shadow_coord.xy + vec2(x, y) / (pcfNum == 0 ? 1 : pcfNum) * spread, shadow_coord.z)).x;
  569.            
  570.             if (dist - shadow_coord.w > 0.0f)
  571.             {
  572.                 hit++;
  573.             }
  574.            
  575.             pcfLoops++;
  576.         }
  577.     }
  578.     return float (hit) / pcfLoops;
  579. }
  580.  
  581. float poissonSample(vec4 shadow_coord, float spread)
  582. {
  583.     if (!pcfSampler)
  584.         return poissonSampleO(shadow_coord, spread);
  585.     else
  586.         return pcfSample(shadow_coord, spread);
  587. }
  588.  
  589. float distanceCalcExper(vec4 shadow_coord, float searchDistance, float lightSize)
  590. {
  591.     float pcfDepth;
  592.    
  593.     for (int i = 0; i < poissonDiskCount; i++)
  594.     {
  595.         // depth check here?
  596.         pcfDepth -= (texture2DArray(stex, vec3(shadow_coord.xy + poissonDisk[i] * searchDistance, shadow_coord.z)).x - shadow_coord.w) * lightSize / texture2DArray(stex, vec3(shadow_coord.xy + poissonDisk[i] * searchDistance, shadow_coord.z)).x;
  597.     }
  598.     if (pcssClampRadii)
  599.         return clamp(pcfDepth / poissonDiskCount, pcssMinRadius, pcssMaxRadius);
  600.     else
  601.         return pcfDepth / poissonDiskCount;
  602. }
  603.  
  604. float distanceCalcOriginal(vec4 shadow_coord, int loopNumber, float searchDistance, float lightSize)
  605. {
  606.     float pcfDepth;
  607.     int loop;
  608.     int loopCount;
  609.  
  610.     for (int x = -loopNumber; x <= loopNumber; x++)
  611.     {
  612.         for (int y = -loopNumber; y <= loopNumber; y++)
  613.         {
  614.             pcfDepth -= (texture2DArray(stex, vec3(shadow_coord.xy + vec2(x, y) * searchDistance / (loopNumber * 2 + 1), shadow_coord.z)).x - shadow_coord.w) * lightSize / texture2DArray(stex, vec3(shadow_coord.xy + vec2(x, y) * searchDistance, shadow_coord.z)).x;
  615.             loopCount++;
  616.         }
  617.     }
  618.  
  619.     if (pcssClampRadii)
  620.         return clamp(pcfDepth / loopCount, pcssMinRadius, pcssMaxRadius);
  621.     else
  622.         return pcfDepth / loopCount;
  623. }
  624.  
  625.  
  626. float distanceCalc(vec4 shadow_coord, int loopNumber, float searchDistance, float lightSize)
  627. {
  628.     if (poissonDepth)
  629.         return distanceCalcExper(shadow_coord, searchDistance, lightSize);
  630.     else
  631.         return distanceCalcOriginal(shadow_coord, loopNumber, searchDistance, lightSize);
  632. }
  633.  
  634. float shadowCoef()
  635. {
  636.     vec4 shadow_coordA = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  637.     vec4 shadow_coordB = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  638.  
  639.     float blend = 0.0f;
  640.  
  641.     calculateShadowCoords(shadow_coordA, shadow_coordB, blend);
  642.  
  643.     float sampleA;
  644.    
  645.     float sampleADistance;
  646.     float sampleBDistance;
  647.  
  648.     if (PCSSToggle == true)
  649.     {
  650.         sampleADistance = pow(distanceCalc(shadow_coordA, depthLoopNumber, searchDistance, lightSize), 1);
  651.         if (ultraMode == true)
  652.             sampleA = (poissonSample(shadow_coordA, sampleADistance) * ultraWeightFactor + poissonSample(shadow_coordA, pow(distanceCalc(shadow_coordA, depthLoopNumber, searchDistance * ultraRatio, lightSize * ultraRatio), 1)) * (1 - ultraWeightFactor));
  653.         else
  654.             sampleA = poissonSample(shadow_coordA, sampleADistance);
  655.     }
  656.     if (PCSSToggle == false)
  657.     {
  658.         if (ultraMode == true)
  659.             sampleA = (poissonSample(shadow_coordA, sampleDistance) * ultraWeightFactor + poissonSample(shadow_coordA, sampleDistance * ultraRatio) * (1.0f - ultraWeightFactor));
  660.         else
  661.             sampleA = poissonSample(shadow_coordA, sampleDistance);
  662.     }
  663.    
  664.     if (blend > 0.0f)
  665.     {
  666.         float sampleB;
  667.        
  668.         if (PCSSToggle == true)
  669.         {
  670.             sampleBDistance = pow(distanceCalc(shadow_coordB, depthLoopNumber, searchDistance, lightSize), 1);
  671.             if (ultraMode)
  672.                 sampleB = (poissonSample(shadow_coordB, sampleBDistance) * ultraWeightFactor + poissonSample(shadow_coordB, pow(distanceCalc(shadow_coordB, depthLoopNumber, searchDistance * ultraRatio, lightSize * ultraRatio), 1)) * (1 - ultraWeightFactor));
  673.             else
  674.                 sampleB = poissonSample(shadow_coordB, sampleBDistance);
  675.         }
  676.         if (PCSSToggle == false)
  677.         {
  678.             if (ultraMode == true)
  679.                 sampleB = (poissonSample(shadow_coordB, sampleDistance) * ultraWeightFactor + poissonSample(shadow_coordB, sampleDistance * ultraRatio) * (1.0f - ultraWeightFactor));
  680.             else
  681.                 sampleB = poissonSample(shadow_coordB, sampleDistance);
  682.         }
  683.    
  684.         return clamp((sampleB * blend) + (sampleA * (1.0f - blend)), 0.0f, 1.0f);
  685.     }
  686.     return sampleA;
  687.     return sampleA * (1.0f - clamp((sampleDistance / 0.005f), 0.0f, 1.0f));
  688. }
  689.  
  690. void main()
  691. {
  692.    vec4 albedo = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  693.    calculateSurface(gl_Color, albedo);
  694.  
  695.    float occlusionFactor = 0.0f;
  696.    if(NdotL > -0.01f)
  697.    {
  698.       if(shadowSplitCount <= 0)
  699.          occlusionFactor = 1.0f;
  700.       else
  701.          occlusionFactor = shadowCoef();
  702.    }
  703.  
  704.    // Apply lighting and fog.
  705.    vec4 fragColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
  706.    if(isParticle == 1)
  707.    {
  708.       vec4 texAlbedo = texture2D(tex, texCoord);
  709.  
  710.       vec4 dirLightDirect = (dirLightColor * occlusionFactor) + (dirLightAmbient * occlusionFactor) + (dirShadowColor * (1.0f - occlusionFactor));
  711.       vec4 plt = accumulateParticlePointLights();
  712.  
  713.       vec4 lightTotal = dirLightDirect + plt;
  714.       lightTotal.x = clamp(lightTotal.x, 0.0f, 1.2f);
  715.       lightTotal.y = clamp(lightTotal.y, 0.0f, 1.2f);
  716.       lightTotal.z = clamp(lightTotal.z, 0.0f, 1.2f);
  717.  
  718.       fragColor = texAlbedo * gl_Color * lightTotal;
  719.  
  720.       applyFog(fragColor, occlusionFactor);
  721.       fragColor.a = texAlbedo.a * gl_Color.a;
  722.    }
  723.    else
  724.    {
  725.         if (shadowOpacity)
  726.             applyLighting(fragColor, albedo, 1 - (1 - occlusionFactor) * occlusionBlend);
  727.         else
  728.             applyLighting(fragColor, albedo, occlusionFactor);
  729.         applyFog(fragColor, occlusionFactor);
  730.    }
  731.  
  732.    // Uncomment to viz depth in B.
  733.    //fragColor.z = vPos.y * 0.01f;
  734.  
  735.    gl_FragColor = fragColor;
  736.  
  737.    // Uncomment to show shadows only  
  738.    if (debug)
  739.         gl_FragColor = vec4(occlusionFactor, occlusionFactor, occlusionFactor, 1);
  740. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement