当被光线触碰时,Unity会使物体褪色

时间:2016-04-07 13:55:01

标签: c# unity3d fade light

我的游戏中有物体,当它被光线击中时我想隐身

我正在寻找的效果就像时间赛尔达陶笛的真实镜头

我通过在物体方向和角落的光源上做Raycasts来实现它的工作

但现在我想知道我是否可以像真相的镜头那样使它工作(如果光线照射到物体的一部分,它只会使那部分消失。

Shader "Custom/Matte Shadow" {

Properties{
    _Color("Main Color", Color) = (1,1,1,1)
    _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
_Cutoff("Alpha cutoff", Range(0,1)) = 0.5
}

    SubShader{
    Tags{ "Queue" = "AlphaTest" "RenderType" = "TransparentCutout" }
    LOD 200
    Blend Zero SrcColor
    Offset 0, -1

    CGPROGRAM

#pragma surface surf ShadowOnly alphatest:_Cutoff fullforwardshadows

    fixed4 _Color;

    struct Input {
        float2 uv_MainTex;
    };

    inline fixed4 LightingShadowOnly(SurfaceOutput s, fixed3 lightDir, fixed atten) {
        fixed4 c;

        c.rgb = s.Albedo*atten;
        c.a = s.Alpha;
        return c;
    }

    void surf(Input IN, inout SurfaceOutput o) {
        fixed4 c = _Color;
        o.Albedo = c.rgb;
        o.Alpha = 0.0f;
    }
ENDCG
}
Fallback "Transparent/Cutout/VertexLit"
}

目前我有这个着色器。

它使对象透明,但点亮时会收到阴影。

问题在于它仍然可见,我需要它在SHADOWS中的LIGHT和VISIBLE下隐身。

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您的对象足够复杂,您可以根据击中它的光量修改每个顶点的Alpha通道。

有人使用类似的方法here来制造战争迷雾。不是在C#中,而是为了给你一个想法:

function RefreshFog()
{
    while(true)
    {
        var mesh : Mesh = GetComponent(MeshFilter).mesh;
        var vertices = mesh.vertices;
        var colors = mesh.colors;

        for(i=0;i<colors.length;i++)
        {
            //if the alpha of the mesh isnt 1, add 0.25 alpha on each time
            //this should cause less of a flicker than a full reset to 1 alpha causes
            //giving the fog a gradual regrowth feel
            if(colors[i].a <= 1)
            {
                //getting the current colours alpha in a var
                var f : float = colors[i].a;
                //incrementing the alpha by 1/4
                f += 0.1;
                //f shouldnt go above 1, but incase this sets the alpha correctly
                if(f > 1)
                {
                    f = 1;
                }
                colors[i].a = f;

            }

        }
        mesh.colors = colors;

        yield WaitForSeconds(1);
    }
}