使网格部分为半透明并仍接收正确的阴影

时间:2019-06-16 12:21:36

标签: unity3d shader

我正在创建类似Minecraft的游戏,但不是在第一人称视角下而是在俯视图中。

Screenshot 1

问题在于,一旦您深入挖掘某个内容,就不再可以看到玩家(显然)。因此,我尝试制作一个可以在网格中切出一个孔的着色器,以便可以看到地下位置。

我对着色器确实很不好,但是以某种方式我设法编写了类似的代码。

Screenshot 2

正如您在此处看到的那样,该孔还在阴影投射中切了一个孔,尽管实际的天花板仍应存在。

这是代码:

Shader "Custom/NewSurfaceShader"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        _Cutoff("Cutoff", Range(0, 1)) = 0.5

        _PlayerPosition ("Player Position", Vector) = (0, 0, 0)
    }
    SubShader
    {  
        Tags { "Queue"="Geometry" }

        CGPROGRAM

            #pragma surface surf Standard fullforwardshadows addshadow
            #pragma target 3.0

            sampler2D _MainTex;

            struct Input
            {
                float2 uv_MainTex;
                float3 worldPos;
            };

            half _Glossiness;
            half _Metallic;
            fixed4 _Color;
            fixed3 _PlayerPosition;

            void surf (Input IN, inout SurfaceOutputStandard o)
            {
                fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
                o.Albedo = c.rgb;
                o.Metallic = _Metallic;
                o.Smoothness = _Glossiness;
                 if (distance(IN.worldPos.xz, _PlayerPosition.xz) < 7 && IN.worldPos.y > _PlayerPosition.y)
                {            
                    clip(-1);
                }
            }

        ENDCG

        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}

        CGPROGRAM

            #pragma surface surf Standard addshadow alpha:fade
            #pragma target 3.0

            sampler2D _MainTex;

            struct Input
            {
                float2 uv_MainTex;
                float3 worldPos;
            };

            half _Glossiness;
            half _Metallic;
            fixed4 _Color;
            fixed3 _PlayerPosition;

            void surf (Input IN, inout SurfaceOutputStandard o)
            {
                fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
                o.Albedo = c.rgb;
                o.Metallic = 0;
                o.Smoothness = _Glossiness;
                 if (distance(IN.worldPos.xz, _PlayerPosition.xz) < 7 && IN.worldPos.y > _PlayerPosition.y)
                {    
                    o.Albedo *= 0;
                    o.Alpha = 0.2;
                }
                else
                {
                    discard;
                }
            }

        ENDCG

    }
    FallBack Off
}

有没有办法消除阴影的阴影?还有以某种方式编写这样的着色器的总体更好的方法吗?

感谢您的回答,欢呼!

0 个答案:

没有答案
相关问题