遮挡HLSL XNA

时间:2019-06-27 23:25:42

标签: xna shader shadow hlsl pixel-shader

我一直在按照this教程尝试在游戏中实现一些动态阴影。

我成功地将它加入了我的游戏,并在fx文件中进行了修改,以减少阴影长度并在阴影后面留下一些光(白色航行的船和角色显示了此修改)。

enter image description here

但是我遇到了一个问题,当红色航行的船的阴影在白色船的阴影路径中时,它的阴影完全消失了。看起来应该像下一张照片。

enter image description here

负责故意削减阴影以及其背后所有内容(我认为)无意消失的代码如下。

int cutShadow : CUTSHADOW;
int shadowPadding : PADSHADOW;
float4 DrawShadowsPS(float2 TexCoord  : TEXCOORD0) : COLOR0
{
      // distance of this pixel from the center
      float distance = length(TexCoord - 0.5f);
      distance *= renderTargetSize.x;
      //apply a 2-pixel bias
      distance -=2;

      //distance stored in the shadow map
      float shadowMapDistance;

      //coords in [-1,1]
      float nY = 2.0f*( TexCoord.y - 0.5f);
      float nX = 2.0f*( TexCoord.x - 0.5f);

      //we use these to determine which quadrant we are in
      if(abs(nY)<abs(nX))
      {
        shadowMapDistance = GetShadowDistanceH(TexCoord,0);
      }
      else
      {
        shadowMapDistance = GetShadowDistanceV(TexCoord,0);
      }

      //if distance to this pixel is lower than distance from shadowMap, 
      //then we are in light
      float light = distance < shadowMapDistance ? 1:(0.5f); // shadow transparency

      // custom code to reduce the length of the shadow
      if((distance > shadowMapDistance && distance < shadowMapDistance + shadowPadding) || distance > shadowMapDistance + cutShadow)
      {
        light = 1;
      }

      float4 result = light;
      result.b = length(TexCoord - 0.5f);
      result.a = 1;
      return result;
}

此技术的完整代码可用here


对于仍然和我在一起的任何人来说,很显然,我对HLSL还是陌生的。我已经从这个问题中休息了一下,尝试更多地了解HLSL。回到问题后,我仍然很难解决此错误。

我最终尝试以不同的方式解决问题,方法是使此图片中的每个阴影投射对象都具有自己的阴影屏幕(这就是我获得无遮挡阴影的图片的方式),而不是只有一个阴影屏幕用于所有的脚轮。然后,将每个投射对象的阴影(即视口的大小)渲染到设备上。老实说,该解决方案只是垃圾-昂贵得多,并且会导致“光”分层,并且由于要在视口中投射许多阴影而使订单更亮。

我想知道是否可以通过修改上面的像素着色器代码来解决此问题。谁能帮我吗?

0 个答案:

没有答案