简体   繁体   English

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

[英]Unity make object fade when touched by light

I have objects in my game I want to be invisible when they get hit by light 我的游戏中有物体,当它被光线击中时我想隐形

The effect I'm looking for is something like the lens of truth from Zelda Ocarina of Time 我正在寻找的效果就像来自时代的塞尔达陶笛的真实镜头

I got it working by doing Raycasts from the lightsource in the direction of my objects and it's corners 我通过在物体方向和角落处从光源做Raycasts来实现它

But now I want to see if I can get it to work like the lens of truth (if the light hits a part of the object it only fades that part. 但是现在我想知道我是否可以像真相的镜头一样工作(如果光线照射到物体的一部分,它只会消失那部分。

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"
}

currently I have this shader. 目前我有这个着色器。

it makes the object transparent but when lit it receives shadows. 它使对象透明,但点亮时会收到阴影。

The problem is that it's still visible, I need it to be invisible under the LIGHT and VISIBLE in the SHADOWS. 问题是它仍然可见,我需要它在SHADOWS中的LIGHT和VISIBLE下是不可见的。

在此输入图像描述

If your objects are complex enough you could modify the alpha channel per vertex based on the amount of light hitting it. 如果您的对象足够复杂,您可以根据击中它的光量修改每个顶点的Alpha通道。

Someone used a similar approach here for fog of war. 有人在这里使用类似的方法解决战争迷雾。 Not in C#, but to give you an idea: 不是在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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM