Advertisement
tonynogo

Demo 97 - Texture switch depending player distance

Aug 5th, 2017
7,623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/TextureSwitch"
  2. {
  3.     Properties
  4.     {
  5.         _PlayerPos ("Player position", vector) = (0.0, 0.0, 0.0, 0.0)
  6.         _Dist ("Distance", float) = 5.0
  7.         _MainTex ("Texture", 2D) = "white" {}
  8.         _SecondayTex ("Secondary texture", 2D) = "white"{}
  9.     }
  10.     SubShader
  11.     {
  12.         Tags { "RenderType"="Opaque" }
  13.        
  14.         Pass
  15.         {
  16.             CGPROGRAM
  17.             #pragma vertex vert
  18.             #pragma fragment frag
  19.             #include "UnityCG.cginc"
  20.  
  21.             struct v2f {
  22.                 float4 pos : SV_POSITION;
  23.                 float2 uv : TEXCOORD0;
  24.                 float4 worldPos : TEXCOORD1;
  25.             };
  26.  
  27.             v2f vert(appdata_base v)
  28.             {
  29.                 v2f o;
  30.                 // We compute the world position to use it in the fragment function
  31.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  32.                 o.pos = UnityObjectToClipPos(v.vertex);
  33.                 o.uv = v.texcoord;
  34.                 return o;
  35.             }
  36.  
  37.             float4 _PlayerPos;
  38.             sampler2D _MainTex;
  39.             sampler2D _SecondayTex;
  40.             float _Dist;
  41.  
  42.             fixed4 frag(v2f i) : SV_Target
  43.             {
  44.                 // Depending the distance from the player, we use a different texture
  45.                 if(distance(_PlayerPos.xyz, i.worldPos.xyz) > _Dist)
  46.                     return tex2D(_MainTex, i.uv);
  47.                 else
  48.                     return tex2D(_SecondayTex, i.uv);
  49.             }
  50.  
  51.             ENDCG
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement