Unity剪影着色器

时间:2015-08-23 00:15:43

标签: unity3d shader

有没有人知道一个免费的Unity / ShaderLAB只是默认的精灵着色器,但是当你走在某些东西后面再也看不到播放器或它的一部分时,它会显示一个完全不透明的单色轮廓,吸取一切。

看起来应该是这样的:http://i.stack.imgur.com/iByV7.png(来自泰坦灵魂)

1 个答案:

答案 0 :(得分:0)

您可以将此着色器应用于播放器对象。对于每个像素,它会将模板标志标记为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则使用轮廓颜色(样本中为黑色),否则为主要颜色(样本中的灰色)和纹理(样本中没有)的组合。注意:只有具有应用于它们的ABOVE着色器的对象才会被剪切 - 因此您可以选择使玻璃颜色透明,并允许,例如......地面花朵,以显示。

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
            }

        }
    }
}