Shader "Unlit/FogHW"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_FogColor("FogColor",Color) = (1,1,1,1)
_HBegin("HBegin",float) = 0
_HEnd("HBegin",range(0,1)) = 0.2
_WBegin("WBegin",float) = 0
_WEnd("WBegin",float) = 50
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 wordPosition : TEXCOORD1;
float lineDistance : TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _FogColor;
float _HBegin;
float _HEnd;
float _WBegin;
float _WEnd;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.wordPosition = mul(unity_ObjectToWorld, v.vertex).xyz;//顶点世界坐标计算
o.lineDistance = distance(o.wordPosition.xyz, _WorldSpaceCameraPos.xyz);//距离摄像机距离计算
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
//高度
float hValue = saturate((i.wordPosition.y - _HBegin)/(_HEnd- _HBegin));
col.rgb = lerp(_FogColor.rgb, col.rgb, hValue);
//深度
float wValue = saturate((i.lineDistance - _WBegin) / (_WEnd - _WBegin));
col.rgb = lerp(col.rgb, _FogColor.rgb, wValue);
return col;
}
ENDCG
}
}
}
本人qq:344810449,欢迎探讨研究。
有unity,shader,小程序等需求也可以联系本人,非常乐于助人。
如果觉得还不错给博主来个小惊喜,纯属自愿,不强求:
作者:unity游侠
链接:https://blog.csdn.net/y90o08u28/article/details/88536839
来源:CSDN
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。