shader 模拟深度,高度雾效(无光模式)针对单个物体

 

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,欢迎探讨研究。

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-12-19
  • 2021-12-04
  • 2021-05-31
  • 2022-12-23
猜你喜欢
  • 2021-05-24
  • 2021-08-12
  • 2023-03-14
  • 2021-07-07
  • 2021-04-02
  • 2021-11-10
相关资源
相似解决方案