当玩家角色进入该区域之内或之外时,我该如何淡化?


17

当他离开该区域时,我想使他褪色。

例如,假设一个人进入建筑物。当一个人在建筑物外面时,不应该看到他,但是当他进入建筑物时,他会逐渐变得可见(使用褪色效果)。

当大象在绿色区域内时,其alpha值应为1

照片_2019-04-15_07-21-39

当大象在绿色区域之外时,其alpha值应为0

照片_2019-04-15_07-21-35

Answers:


34

您可以使用世界空间淡化角色。

对象空间(或对象坐标系)特定于每个游戏对象。但是,所有游戏对象都转换为一个公共坐标系-世界空间。

如果将游戏对象直接放入世界空间,则由游戏对象的“变换”组件指定对象到世界的变换。

https://zh.wikibooks.org/wiki/Cg_Programming/Unity/Shading_in_World_Space

随着世界空间的消逝

记录_2019_04_15_07_13_25_154

Shader "Smkgames/worldSpaceFade" {
      Properties{
         _Size("Size",Vector) = (2,2,0,0)
      }
   SubShader {
      Pass {
         CGPROGRAM

         #pragma vertex vert  
         #pragma fragment frag 


         struct vertexInput {
            float4 vertex : POSITION;
         };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float4 position_in_world_space : TEXCOORD0;
         };

         vertexOutput vert(vertexInput input) 
         {
            vertexOutput output; 

            output.pos =  UnityObjectToClipPos(input.vertex);
            output.position_in_world_space = 
               mul(unity_ObjectToWorld, input.vertex);

            return output;
         }
         float2 _Size;

         float4 frag(vertexOutput input) : COLOR 
         {
             float3 world = input.position_in_world_space;

          float4 equation = pow(world.x/_Size.x,8) + pow(world.z/_Size.y,8);
          return smoothstep(1,0,equation);
         }

         ENDCG  
      }
   }
}

表面着色器

了解世界空间后,我们可以在表面着色器中使用它:

记录_2019_04_15_07_14_24_876

Shader "Smkgames/worldSpaceFade" {
    Properties {
         _Size("Size",Vector) = (2,2,0,0)
        _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
    }
    SubShader {
 Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
     Pass {
        ZWrite On
        ColorMask 0
    }
        LOD 200

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows alpha:fade

        #pragma target 3.0

        sampler2D _MainTex;

        struct Input {
            float2 uv_MainTex;
            float3 worldPos: TEXCOORD2;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;
        float2 _Size;

        void vert (inout appdata_full v, out Input o){
            UNITY_INITIALIZE_OUTPUT(Input,o);
            o.worldPos = mul(unity_ObjectToWorld, v.vertex);
        }


        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o) {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            float4 equation = pow(IN.worldPos.x/_Size.x,8) + pow(IN.worldPos.z/_Size.y,8);
            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = smoothstep(1,0,equation);
        }
        ENDCG
    }
    FallBack "Diffuse"
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.