Unity SOFT SHADOWS WITH PCSS

这里是引用
https://blog.csdn.net/kevin_dust/article/details/51903101
http://developer.download.nvidia.com/whitepapers/2008/PCSS_Integration.pdf
https://www.bilibili.com/video/BV1YK4y1T7yY?p=3
相当于抄的第二个引用 ?

请添加图片描述
在这里插入图片描述

本文接上篇博客( Unity SOFT SHADOWS WITH PCF)
在这里插入图片描述
在这里插入图片描述
主要用到了相似三角形对应边成比例来计算 阴影强度

fixed4 frag(v2f i) : SV_Target
{
    fixed4 col = fixed4(1,1,1,1);
    
    float shadow = 0;
   
    float4 coords = i.ShadowCoord;
    float2 uv = coords.xy;
     float zReceiver = coords.z; // Assumed to be eye-space z in this code

     // STEP 1: blocker search
     float avgBlockerDepth = 0;
     float numBlockers = 0;
     FindBlocker( avgBlockerDepth, numBlockers, uv, zReceiver, i);
     if( numBlockers < 1 )
     {
         //There are no occluders so early out (this saves filtering)
         return 1.0f;
     }
     // STEP 2: penumbra size
     float penumbraRatio = PenumbraSize(zReceiver, avgBlockerDepth);
     float filterRadiusUV = penumbraRatio * LIGHT_SIZE_UV * NEAR / coords.z;
     filterRadiusUV *= UVLightSize;
     // STEP 3: filtering
     
     // Possion
     for (int j = 0; j < BLOCKER_SEARCH_NUM_SAMPLES; ++j)
     {
         float2 offset = PoissonOffsets[j] * filterRadiusUV;
         shadow += lookup(offset, i);
     }
     shadow /= BLOCKER_SEARCH_NUM_SAMPLES ;
    return col * shadow;
}

查找Blocker
这里用到了PCF 来作为查找 Block的采样

float PenumbraSize(float zReceiver, float zBlocker) //Parallel plane estimation
{
    return (zReceiver - zBlocker) / zBlocker;
}

void FindBlocker(out float avgBlockerDepth,
                out float numBlockers, 
                float2 uv, float zReceiver , v2f j)
{
    //This uses similar triangles to compute what
    //area of the shadow map we should search
     float searchWidth = LIGHT_SIZE_UV * (zReceiver - NEAR) / zReceiver;
     float blockerSum = 0;
     numBlockers = 0;

     float x,y;
     for (y = -3.5 ; y <=3.5 ; y+=1.0)
     {
         for (x = -3.5 ; x <=3.5 ; x+=1.0)
         {
             float scale = searchWidth * 0.001;
             fixed4 dcol = tex2Dproj(_DepthTexture, j.ShadowCoord + float4(  x * scale, 
                                                                             y * scale,
                                                                             0,
                                                                             0));
             float distanceFromLight = DecodeFloatRGBA(dcol);

             float shadowMapDepth = distanceFromLight;
   
             if(shadowMapDepth < zReceiver + directionalLightShadowMapBias){
                 blockerSum += shadowMapDepth;
                 numBlockers++;
             }
         }
     }
     avgBlockerDepth = blockerSum / numBlockers;
} 

顺便说一下
在这里插入图片描述
这里可以调整 cascade 远近

VSM

优化了PCSS
根据范围查询来避免采样带来的noise

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值