Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static const uint PCSS_SampleCount = 16;
- static const float PCSS_SampleRadius = 1.0f;
- static float2 PCSS_Samples[PCSS_SampleCount] =
- {
- float2(-0.1307115f, 0.1291686f),
- float2(0.6154836f, 0.3147851f),
- float2(-0.4295029f, -0.155486f),
- float2(-0.591889f, 0.6969846f),
- float2(0.2217633f, -0.3848645f),
- float2(-0.2653467f, -0.6504169f),
- float2(-0.1259185f, 0.8731781f),
- float2(0.3423603f, 0.01230872f),
- float2(0.1190722f, 0.4535096f),
- float2(-0.7164063f, 0.1303319f),
- float2(0.6048174f, -0.5608257f),
- float2(0.1267647f, -0.8851751f),
- float2(0.8812442f, -0.224937f),
- float2(0.4235326f, 0.8168082f),
- float2(-0.9404536f, -0.2363012f),
- float2(-0.7055491f, -0.7052079f)
- };
- /*static const uint PCSS_SampleCount = 32;
- static const float PCSS_SampleRadius = 1.0f;
- static const float2 PCSS_Samples[PCSS_SampleCount] = {
- float2(0.06407013, 0.05409927),
- float2(0.7366577, 0.5789394),
- float2(-0.6270542, -0.5320278),
- float2(-0.4096107, 0.8411095),
- float2(0.6849564, -0.4990818),
- float2(-0.874181, -0.04579735),
- float2(0.9989998, 0.0009880066),
- float2(-0.004920578, -0.9151649),
- float2(0.1805763, 0.9747483),
- float2(-0.2138451, 0.2635818),
- float2(0.109845, 0.3884785),
- float2(0.06876755, -0.3581074),
- float2(0.374073, -0.7661266),
- float2(0.3079132, -0.1216763),
- float2(-0.3794335, -0.8271583),
- float2(-0.203878, -0.07715034),
- float2(0.5912697, 0.1469799),
- float2(-0.88069, 0.3031784),
- float2(0.5040108, 0.8283722),
- float2(-0.5844124, 0.5494877),
- float2(0.6017799, -0.1726654),
- float2(-0.5554981, 0.1559997),
- float2(-0.3016369, -0.3900928),
- float2(-0.5550632, -0.1723762),
- float2(0.925029, 0.2995041),
- float2(-0.2473137, 0.5538505),
- float2(0.9183037, -0.2862392),
- float2(0.2469421, 0.6718712),
- float2(0.3916397, -0.4328209),
- float2(-0.03576927, -0.6220032),
- float2(-0.04661255, 0.7995201),
- float2(0.4402924, 0.3640312),
- };*/
- inline float Random(float2 co)
- {
- return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
- }
- inline float2 PCSS_Rotate(float2 pos, float2 rotationTrig)
- {
- return float2(pos.x * rotationTrig.x - pos.y * rotationTrig.y, pos.y * rotationTrig.x + pos.x * rotationTrig.y);
- }
- inline float2 PCSS_BlockerDistance(Texture2D<float2> tex, SamplerState state, float3 projCoord, float searchUV, float2 rotationTrig)
- {
- int blockers = 0;
- float avgBlockerDistance = 0.0f;
- for (int i = 0; i < (int)PCSS_SampleCount; i++)
- {
- float2 offset = PCSS_Samples[i] * searchUV;
- offset = PCSS_Rotate(offset, rotationTrig);
- float z = tex.SampleLevel(state, projCoord.xy + offset, 0.0f).x;
- if (z < projCoord.z)
- {
- blockers++;
- avgBlockerDistance += z;
- }
- }
- avgBlockerDistance /= blockers;
- return float2(avgBlockerDistance, (float)blockers);
- }
- inline float PCSS_PCFFilter(Texture2D<float2> tex, SamplerState state, float3 projCoord, float filterRadiusUV, float penumbra, float2 rotationTrig, float2 grad)
- {
- float sum = 0.0f;
- for (int i = 0; i < (int)PCSS_SampleCount; i++)
- {
- float2 offset = PCSS_Samples[i] * filterRadiusUV;
- offset = PCSS_Rotate(offset, rotationTrig);
- /*float4 tmp = tex.Gather(state, projCoord.xy + offset);
- tmp.x = tmp.x < projCoord.z ? 0.0f : 1.0f;
- tmp.y = tmp.y < projCoord.z ? 0.0f : 1.0f;
- tmp.z = tmp.z < projCoord.z ? 0.0f : 1.0f;
- tmp.w = tmp.w < projCoord.z ? 0.0f : 1.0f;
- sum += lerp(lerp(tmp.w, tmp.z, grad.x), lerp(tmp.x, tmp.y, grad.x), grad.y);*/
- sum += tex.SampleLevel(state, projCoord.xy + offset, 0.0f).x < projCoord.z ? 0.0f : 1.0f;
- }
- sum /= (float)PCSS_SampleCount;
- return sum;
- }
- inline float ShadowMap_PCSS(Texture2D<float2> tex, SamplerState state, float3 projCoord, float resolution, float pixelSize, float lightSize)
- {
- float2 uv = projCoord.xy;
- float depth = projCoord.z;
- float zAwareDepth = depth;
- float rotationAngle = Random(projCoord.xy) * 3.1415926;
- float2 rotationTrig = float2(cos(rotationAngle), sin(rotationAngle));
- float searchSize = lightSize / resolution * 100.0 * saturate(zAwareDepth - .02) / zAwareDepth;
- float2 blockerInfo = PCSS_BlockerDistance(tex, state, projCoord, searchSize, rotationTrig);
- if (blockerInfo.y < 1.0)
- {
- return 1.0f;
- }
- else
- {
- float penumbra = max(zAwareDepth - blockerInfo.x, 0.0);
- float filterRadiusUV = penumbra * lightSize / resolution * 100.0;
- float2 grad = frac(projCoord.xy * resolution + 0.5f);
- float shadow = PCSS_PCFFilter(tex, state, projCoord, filterRadiusUV, penumbra, rotationTrig, grad);
- return shadow;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement