Unity-URP学习笔记(四)赛璐珞高光

Unity-URP学习笔记(四)赛璐珞高光

URP卡通赛璐璐高光

结果展示

Unity-URP学习笔记(四)赛璐珞高光

Shader展示

Shader "MyURP/CharMainCellPlus"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _OutlineColor("OutlineColor", Color) = (0,0,0,1)
        _OutLineWidth("OutLineWidth", Range(0, 10)) = 0.1
        _ShowRenderQueue("ShowRenderQueue",Int)= -1
        _CellWidth("CellWidth",Range(-1,1))=0.5
        _CellColor("CellColor",Color) = (0,0,0,0)
        _HighLightWidth("HighLightWidth",Range(-1,1))=0.5
        _HighLightColor("HighLightColor",Color) = (1,1,1,1)
    }
    SubShader
    {
        Pass
        {
            Name "BASE"
            Tags 
            {
                "RenderType" = "Opaque"
                "Queue" = "Geometry"
                "LightMode" = "LightweightForward"
            }

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
            #pragma target 3.0
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            //传入顶点着色器的数据
            struct a2v
            {
                float4 vertex   : POSITION;
                float2 uv       : TEXCOORD0;
                float3 normal       : NORMAL;
            };
            //传入片元着色器的数据
            struct v2f
            {
                float4 worldPos : SV_POSITION;
                float2 uv       : TEXCOORD0;
                float3 viewDir      : TEXCOORD1;
                float3 worldNormal  : TEXCOORD2;
            };
            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);
            CBUFFER_START(UnityPerMaterial)
                float4 _Color;
                float4 _MainTex_ST;
                float4 _OutlineColor;
                float _OutLineWidth;
                int _ShowRenderQueue;
                float _HighLightWidth;
                float4 _HighLightColor;
                float _CellWidth;
                float4 _CellColor;
            CBUFFER_END
            //顶点着色器
            v2f vert(a2v v)
            {
                v2f o;
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.worldPos = TransformObjectToHClip(v.vertex);
                o.worldNormal = TransformObjectToWorldNormal(v.normal);
                o.viewDir = normalize(_WorldSpaceCameraPos.xyz-TransformObjectToWorld(v.vertex.xyz));
                return o;
            }
            //片元着色器
            half4 frag(v2f i) : SV_Target
            {
                Light mainLight = GetMainLight();
                half LdotN = dot(mainLight.direction,i.worldNormal);
                LdotN = saturate(step(0,LdotN-_CellWidth));
                ///
                half3 halfAngle = normalize(normalize(mainLight.direction)+normalize(i.viewDir));
                half HdotN = saturate(dot(halfAngle,i.worldNormal));
                HdotN = saturate(ceil(HdotN-_HighLightWidth));
                ///
                half4 Albedo = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
                ///
                half4 BaseColor = lerp(_CellColor,_Color,LdotN)*Albedo;
                half4 FinalColor= lerp(BaseColor,_HighLightColor*Albedo,HdotN);
                return FinalColor;
            }
            ENDHLSL
        }
        UsePass "MyURP/Outline/OUTLINE"
        //UsePass "MyURP/NewOutline/NEWOUTLINE"
    }
}

上一篇:shader学习之路——纹理遮罩


下一篇:贴图扭曲 伪流体(译)