shader 学习记录

    科技2022-07-10  115

    学习shader笔记 记录~ unity版本 2017.4.1 

    shader具体内容:

    basecolor , second tex blend , snow blend , invert color , detail map , uv aniamtion , ramp map ,envCap , flow map , shader 自定义开关

    比较基础,所以不写注释了~ 

     

    源码:

    Shader "Custom/wbshader (Support lightmap, Fog)" {

    Properties {

        _MainTex ("Base (RGB)", 2D) = "white" {}

     

        _SecondTex("Second Tex",2D) = "white" {}

        

        _TintColor ("Tint Color", Color) = (0.5,0.5,0.5,1)

     

        [Space(10)]

     

        [Toggle] _SNOWBLEND ("Snow blend", Float) = 0

     

        [Space(10)]

     

        [ShowWhenHasKeyword(_SNOWBLEND_ON)]_Color ("Match Color ", Color) = (0.22, 0.707, 0.071, 0)

     

        [ShowWhenHasKeyword(_SNOWBLEND_ON)]_WeightVector ("Weight (r: 方向 g: 顶点色A b: 颜色图)", Vector) = (0.15, 0.5, 0.33,1)

     

        [ShowWhenHasKeyword(_SNOWBLEND_ON)]_SmoothEdge ("Smooth", Range (0.01, 1)) = 0.1

     

        [ShowWhenHasKeyword(_SNOWBLEND_ON)]_SnowThreshold ("Threshold", Range (0.01, 1)) = 0.1

     

        [ShowWhenHasKeyword(_SNOWBLEND_ON)]_ThresholdOffset ("Threshold Offset", Range (0, 1)) = 0.1

     

        [Space(10)]

     

        [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend ("Src Blend Mode", Float) = 1

    [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend ("Dst Blend Mode", Float) = 1

    [Enum(Off, 0, On, 1)] _ZWrite ("ZWrite", Float) = 0

    [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest ("ZTest", Float) = 0

    [Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull Mode", Float) = 1

        [KeywordEnum(None, Add, Multiply)] _Overlay ("Overlay mode", Float) = 0

        [PowerSlider(3.0)] _Shininess ("Shininess", Range (0.01, 1)) = 0.08

     

        [Space(5)]

     

        [Toggle] _Invert ("Invert color?", Float) = 0

     

        [Space(5)]

     

        [Toggle] _DETAIL ("Detail Map", Float) = 0

     

        [Space(5)]

     

        [ShowWhenHasKeyword(_DETAIL_ON)] _DetailTex ("Detail Map (RGB)", 2D) = "grey" {}

        [ShowWhenHasKeyword(_DETAIL_ON)] _DetailRepeat("Detail Repeat", float) = 10

        [ShowWhenHasKeyword(_DETAIL_ON)] _DetailAmount("Detail Amount", float) = 0.3

        [ShowWhenHasKeyword(_DETAIL_ON)] _DetailColor("Detail Color", Color) = (0.5,0.5,0.5,1)

     

        [Space(5)]

     

        [Toggle] _UVANIMATION ("UV Animation", Float) = 0

     

        [Space(5)]

     

        [ShowWhenHasKeyword(_UVANIMATION_ON)] _Speed("UV动画速度",Float) = 1

        [ShowWhenHasKeyword(_UVANIMATION_ON)] _UVScale("UV缩放",Float)= 1

     

        [Space(5)]

     

        [Toggle] _CHARACTER ("Character Setting", Float) = 0

     

        //[Space(10)]

     

    //  [ShowWhenHasKeyword(_CHARACTER_ON)] _EnvCap ("EnvCap (RGB)", 2D) = "white" {}

    //  [ShowWhenHasKeyword(_CHARACTER_ON)] _RmapMap ("RampMap (RGB)", 2D) = "white" {}

    //  [ShowWhenHasKeyword(_CHARACTER_ON)] _FlowLight ("Flow Light (RGB)", 2D) = "white" {}

        

        [ShowWhenHasKeyword(_CHARACTER_ON)]_EnvMap ("EnvCap (RGB)", 2D) = "white" {}

        [ShowWhenHasKeyword(_CHARACTER_ON)]_RampMap ("RampMap (RGB)", 2D) = "white" {}

        [ShowWhenHasKeyword(_CHARACTER_ON)]_FlowLightMap ("Flow Light (RGB)", 2D) = "white" {}

        [ShowWhenHasKeyword(_CHARACTER_ON)]_MaskMap ("Mask Map",2D ) = "white" {}

     

        [ShowWhenHasKeyword(_CHARACTER_ON)]_FlowColor ("流光颜色, (A控制叠加方式)", Color) = (1,0.6,0.2,1)

        [ShowWhenHasKeyword(_CHARACTER_ON)]_FlowMultiplier ("流光强度", float) = 4

        [ShowWhenHasKeyword(_CHARACTER_ON)]_FlowThreshold ("FlowThreshold", float) = .75

    }

     

    SubShader {

        

            Blend [_SrcBlend] [_DstBlend]

    ZWrite [_ZWrite]

    ZTest [_ZTest]

    Cull [_Cull]

            

            pass

            {

                CGPROGRAM

                #pragma vertex vert

                #pragma fragment frag

     

                #pragma shader_feature _INVERT_ON

     

                #pragma shader_feature _DETAIL_ON

     

                #pragma shader_feature _CHARACTER_ON

     

                #pragma shader_feature _UVANIMATION_ON

     

                #pragma multi_compile _OVERLAY_NONE _OVERLAY_ADD _OVERLAY_MULTIPLY

     

                #include "UnityCG.cginc"

                sampler2D _MainTex;

     

                sampler2D _SecondTex;

     

                half4 _TintColor;

                float4 _MainTex_ST;

                float4 _SecondTex_ST;

     

                #if _SNOWBLEND_ON

                    float4 _Color;

     

                    float4 _WeightVector;

     

                    float _SmoothEdge;

     

                    float _SnowThreshold;

     

                    float _ThresholdOffset;

                #endif

     

                float _Shininess;

     

                #if _DETAIL_ON

                    sampler2D _DetailTex;

                    float _DetailRepeat;

                    float _DetailAmount;

                    float4 _DetailColor;

    #endif

     

                #if _CHARACTER_ON

                    sampler2D _EnvMap;

                    sampler2D _RampMap;

                    sampler2D _FlowLightMap;

                    sampler2D _MaskMap;

     

                    float4 _FlowColor;

                    float4 _FlowLightMap_ST;

     

                    half _Scroll2X;

                    half _Scroll2Y;

     

                    float _FlowMultiplier;

                    float _FlowThreshold;

    #endif

     

                #if _UVANIMATION_ON

                    float _Speed;

                    float _UVScale;

    #endif

     

                struct appdata

                {

                    float4 vertex : POSITION;

                    float2 uv : TEXCOORD0;

                    float3 normal : NORMAL;

                    fixed4 color : COLOR;

                    float4 texcoord : TEXCOORD1;

                };

                struct v2f

                {

                    float4 uv : TEXCOORD0;

                    float4 vertex : SV_POSITION;

                    half4 capCoord : TEXCOORD2;

                    half4 faceUV : TEXCOORD3;

                    float3 normal :TEXCOORD1;

                    half4 lightDirAndLM : TEXCOORD4;

                    half3 mask :TEXCOORD5;

                };

     

                v2f vert (appdata v)

                {

                    v2f o;

                    o.vertex = UnityObjectToClipPos(v.vertex);

                    o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);

                    o.uv.zw = TRANSFORM_TEX(v.uv, _SecondTex);

     

                    #if _UVANIMATION_ON

                        float2 uv = float2(v.uv.x * _UVScale + _Time.y * _Speed,v.uv.y);//增加UV动画和UVscale

                        o.uv.xy = TRANSFORM_TEX(uv, _MainTex).xy;                   

                    #endif

     

                    half3 normalInView = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);

                    o.capCoord.xy = half2((normalize(normalInView).xy * 0.25) + 0.25) + half2( 0.5, 0) * (1- v.color.r);       //R: lowerLf

                

                    o.capCoord.zw = o.capCoord + half2(0, 0.5);

     

                    #if _CHARACTER_ON

                    o.faceUV.xy = TRANSFORM_TEX(v.texcoord, _FlowLightMap) + frac( half2(_Scroll2X, _Scroll2Y) * _Time.x);

                    #endif

     

                    o.lightDirAndLM.xyz = (ObjSpaceLightDir(v.vertex));

                    o.lightDirAndLM.w = 0.5;

     

                    return o;

                }

                

                fixed4 frag (v2f i) : SV_Target

                {

                    // sample the texture

                    fixed4 col = tex2D(_MainTex, i.uv.xy);

     

                    

     

                    #if _DETAIL_ON

                        half3 detail = tex2D(_DetailTex, i.uv * _DetailRepeat) * 2;

                        // debug

                        //detail = lerp(detail, half3(1,1,1), wokAltitudeFogParams.w);

                        col.rgb *= detail * _DetailAmount + (1-_DetailAmount).xxx * _DetailColor;

                    #endif

     

                    #if _INVERT_ON

        col = 1 - col;

    #endif

     

                    col *= _TintColor;

     

                    fixed4 secCol = tex2D(_SecondTex, i.uv.zw);

     

                    #if _SNOWBLEND_ON

     

                        half colorMatch = 1 - length(col.rgb - _Color.rgb);

     

                        half3 weights = half3(i.mask.gb, colorMatch);

     

                        half weight = saturate( dot( weights, _WeightVector ) );

     

                        half colorAmount = (weight - _SnowThreshold) / _SmoothEdge;

     

                        half value = weight - _SnowThreshold;

                        half amount = saturate( (value - _ThresholdOffset) / _SmoothEdge );

     

                        col.rgb = lerp( col, secCol, saturate( amount * secCol.a));

     

                    #endif

     

    #if _OVERLAY_ADD

        col += secCol;

    #elif _OVERLAY_MULTIPLY

        col *= secCol;

    #endif

     

                    #if _CHARACTER_ON

                        half y = 1.5;

     

                        half2 nDotLUV = 0.5 * half2( dot( normalize (i.normal), normalize(i.lightDirAndLM.xyz)) +1, y ) ;

     

                        half4 rampColor = tex2D(_RampMap, nDotLUV );    

     

                        half2 flowUV = i.faceUV.xy;

     

                        half3 mask = tex2D(_MaskMap, i.uv );

     

                        half3 matcap = tex2D( _EnvMap ,i.capCoord.xy ) * mask.g;

                        matcap += tex2D( _EnvMap ,i.capCoord.zw) * mask.b;

     

                        col.xyz += matcap;

     

                        half4 lightFlow = tex2D (_FlowLightMap, flowUV );

     

                        float v = saturate(saturate(mask.r - _FlowThreshold));

                        lightFlow *= v * _FlowMultiplier;

     

                        col += lightFlow * lerp(half4(1,1,1,1), col, _FlowColor.a) * _FlowColor;   

     

                        col *= (rampColor *2);

                    #endif

     

                    

     

                    col *= _Shininess;

     

                    return col;

                }

     

                ENDCG

            }

            

    }

     

    }

     

    Processed: 0.013, SQL: 8