Unity 剪影着色器

4

有没有人知道一个免费的Unity/ShaderLAB,它只是默认的sprite shader,但当你走在某些物体后面,不能再看到玩家,或者只能看到一部分时,它会显示完全不透明、单色的轮廓,覆盖在所有东西上。

它应该看起来像这样:http://i.stack.imgur.com/iByV7.png(来自Titan Souls)


你的游戏是否具有透明度并使用Sprite的“层次顺序”功能?默认的Sprite着色器具有透明度,因此您不能仅更改着色器并使所有内容正常工作(至少在我的技能水平上不行)。 - user3814958
似乎前方的物体充当了一个遮罩。使用该遮罩,您可以在所有东西的顶部渲染一个透明的、黑色调的精灵。当玩家移动并且没有精灵在顶部时,遮罩将为空,因为没有对象需要遮罩。反过来,也不会渲染任何叠加的精灵。我会仔细考虑并回答这个问题 :-) - Zerratar
糟糕!我想不出优雅的做法了。在我的脑海中,你需要一个总是在玩家精灵之上的叠加精灵。此叠加精灵应具有尽可能高的z-index /精灵层顺序,以确保它处于所有其他图像之上。然后,每次玩家移动时都要检查是否存在对象(其他精灵)在玩家精灵的范围内。然后,在每个单独的对象上应用掩码,连同您的玩家精灵一起显示为叠加层,并选择颜色。必须有一种使用着色器的更好方法。 - Zerratar
1个回答

1
你可以将此着色器应用于您的玩家对象。它将在绘制每个像素时将模板标志设置为1。
Shader "Custom/StencilHole" { //also used for silhouetted objects
Properties {}
SubShader {
    // Write the value 1 to the stencil buffer
    Stencil
    {
        Ref 1
        Comp Always
        Pass Replace
    }
    Tags { "Queue" = "Geometry-1" }  // Write to the stencil buffer before drawing any geometry to the screen
    ColorMask 0 // Don't write to any colour channels
    ZWrite Off // Don't write to the Depth buffer
    Pass {}
} 
FallBack "Diffuse"
}

然后,您可以在“着色玻璃”上使用以下着色器。它检查每个像素的模板值:如果等于1,则使用轮廓颜色(在示例中为黑色),否则是主要颜色(在示例中为灰色)和纹理(在示例中不存在)的组合。注意:只有应用了以上着色器的对象才会被勾勒出轮廓-因此您可以选择使玻璃颜色透明,并允许地面上的花朵显示出来。

Shader "GUI/silhouetteStencil" 
{

    Properties {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Color", Color) = (1,1,1,1)
        _silhouetteColor ("silhouette Color", Color) = (0,1,0,1)
    }

    SubShader {
        Tags { "Queue"="Transparent-1" "IgnoreProjector"="True" "RenderType"="Transparent" }
        Lighting On Cull back ZWrite Off Fog { Mode Off }
        Blend SrcAlpha OneMinusSrcAlpha

        Pass 
        {
            // Only render texture & color into pixels when value in the stencil buffer is not equal to 1.
            Stencil 
            {
                Ref 1
                Comp NotEqual
            }
            ColorMaterial AmbientAndDiffuse
            SetTexture [_MainTex] 
            {
                constantColor [_Color]
                combine texture * constant
            }
        }

        Pass
        {
            // pixels whose value in the stencil buffer equals 1 should be drawn as _silhouetteColor
            Stencil {
              Ref 1
              Comp Equal
            }
            SetTexture [_MainTex] 
            {
                constantColor [_silhouetteColor]
                combine constant
            }

        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接