By Seyed Morteza Kamali


2019-04-15 03:13:14 8 Comments

I want to fade the player when he goes out of the area.

For example, suppose a person enters a building. When a person is outside of the building, he shouldn't be seen, but when he enters he gradually becomes visible (using fading effect).

when elephant is inside green area his alpha should be 1

photo_2019-04-15_07-21-39

when elephant is outside of green area his alpha should be 0

photo_2019-04-15_07-21-35

1 comments

@Seyed Morteza Kamali 2019-04-15 03:13:32

You can use world space to fade your character.

The object space (or object coordinate system) is specific to each game object; however, all game objects are transformed into one common coordinate system — the world space.

If a game object is put directly into the world space, the object-to-world transformation is specified by the Transform component of the game object.

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

Fading by world space

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

Surface Shader

After understanding world space we can use it in our surface shader:

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

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How to create a turn-based system with an unknown number of enemies?

  • 2019-09-27 18:34:49
  • Christopher
  • 109 View
  • 0 Score
  • 1 Answer
  • Tags:   unity turn-based

1 Answered Questions

[SOLVED] Shader to see silhouette through alpha blended sprites

2 Answered Questions

[SOLVED] How can i keep the spawned cubes to be inside the given area?

  • 2017-10-27 00:03:44
  • Daniel Lip
  • 277 View
  • 0 Score
  • 2 Answer
  • Tags:   unity c#

2 Answered Questions

[SOLVED] Blinking object

  • 2016-04-27 09:54:42
  • fyvaydmx
  • 872 View
  • 1 Score
  • 2 Answer
  • Tags:   unity

1 Answered Questions

[SOLVED] Make a 2d top down game with round map

1 Answered Questions

[SOLVED] Unity Camera - Clamping when Zoom changes?

  • 2016-09-11 01:56:06
  • Merlin
  • 638 View
  • 0 Score
  • 1 Answer
  • Tags:   unity c# camera

1 Answered Questions

[SOLVED] Unity - Static Camera Perspective, Walls Obscure Player

1 Answered Questions

1 Answered Questions

[SOLVED] Premultiplied Alpha And Alpha Testing

Sponsored Content