Is there a way to slowly fade in distant terrain and objects? So lets say for example you have a large terrain with far away objects but you only are rendering part of the distant objects and terrain. Is there a way to smoothly fade the terrain edges and objects in from a distance instead of getting the jagged lines or flickering objects that are shown in the distance where the rendering cuts off? So as the User gets closer the rest of the geo edges smoothly fade in instead of being jagged / flickering. Since I have mountains really far away and I only render about 30,000 units, it causes anything beyond that to look really bad when it starts rendering into the scene.
Have you tried the fog effect?
It’s in render settings.
Here’s the foundation of a distance-fading shader.
Shader "Custom/AlphaDependingDistance"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Radius ("Radius", Range(0.001, 500)) = 200
_Falloff("Falloff", Range(0.001, 10)) = 4
_FadeColor("Fade Color", Color) = (1.0, 1.0, 1.0, 1.0)
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 worldPos : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata_base v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
float _Radius;
float _Falloff;
half3 _FadeColor;
fixed4 frag(v2f i) : SV_Target {
fixed4 col = tex2D(_MainTex, i.uv);
float dist = distance(i.worldPos, _WorldSpaceCameraPos);
float dist2 = saturate((dist / _Radius) - 1.0); // negative while distance < radius, so clamp to 0
float dist3 = dist2 * ((dist - _Radius) / (_Radius * _Falloff)); // gradient zero to 1 from radius to radius + falloff factor
col.a = saturate(1.0 - dist3);
return fixed4((col.rgb * col.a) + (_FadeColor * (1 - col.a)), col.a);
}
ENDCG
}
}
}