Shader "Unlit URP Shader/OutLine2" { Properties { //_MainTex("MainTex",2d)="white"{} //_OutLineColor("OutLineColor",color)=(1,1,1,1) //_OutLineWide("OutLineWide",float)=1 } HLSLINCLUDE #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl" CBUFFER_START(UnityPerMaterial) half4 _OutLineColor; half _OutLineWide; CBUFFER_END float Scale; TEXTURE2D(_Temp);//SAMPLER(sampler_Temp); TEXTURE2D(_Cut);SAMPLER(sampler_Cut); //TEXTURE2D(_MainTex);SAMPLER(sampler_MainTex); //SAMPLER(sampler_BlitTexture); float4 _BlitTexture_TexelSize; TEXTURE2D(_OutLineTex);SAMPLER(sampler_OutLineTex); float Strength; float2 HoV; struct Attributes2 { float4 vertex : POSITION; float3 normal : NORMAL; //float3 smoothNormal : TEXCOORD3; float2 uv : TEXCOORD0; }; struct Varyings2 { float4 position : SV_POSITION; float4 color : COLOR; float2 uv : TEXCOORD0; }; //圆形模糊 float4 Bur(float2 uv) { float2 d=_BlitTexture_TexelSize.xy*Scale; half4 col=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+float2(0,1)*d); col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+float2(-1,0)*d); col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+float2(0,-1)*d); col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+float2(1,0)*d); col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+float2(-1,1)*d)*0.5; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+float2(1,-1)*d)*0.5; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+float2(-1,-1)*d)*0.5; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+float2(1,1)*d)*0.5; return col*0.1666; } float4 BurHoV(float2 uv) { float2 d=_BlitTexture_TexelSize.xy*Scale*HoV; half4 col=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv)*0.4; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+1*d)*0.15; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+-1*d)*0.15; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+2*d)*0.10; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+-2*d)*0.10; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+3*d)*0.05; col+=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,uv+-3*d)*0.05; return col; } float Remap(float x, float t1, float t2, float s1, float s2) { return (s2 - s1) / (t2 - t1) * (x - t1) + s1; } float3 ACESToneMapping(float3 color, float adapted_lum) { const float A = 2.51f; const float B = 0.03f; const float C = 2.43f; const float D = 0.59f; const float E = 0.14f; color *= adapted_lum; return (color * (A * color + B)) / (color * (C * color + D) + E); } Varyings2 vert(Attributes2 v) { Varyings2 o = (Varyings2)0; o.position = TransformObjectToHClip(v.vertex); o.uv=v.uv; o.color = _OutLineColor; return o; } float4 _HeightLightColor; //填充颜色 float4 fragFill(Varyings2 i) : SV_Target { return _OutLineColor; } //降采样模糊 float4 fragDownBlur(Varyings i) : SV_Target { float4 col=Bur(i.texcoord); return col; } //升采样模糊 half4 fragUpBlur(Varyings i) : SV_Target { float4 col=Bur(i.texcoord); float4 tempColor=SAMPLE_TEXTURE2D(_Temp,sampler_LinearClamp,i.texcoord); col.rgb=col.rgb+tempColor.rgb; return col; } //高斯模糊 float4 fragHoV(Varyings i) : SV_Target { float4 col=BurHoV(i.texcoord); return col; } //模板剔除 float4 fragCull(Varyings2 i) : SV_Target { //float4 mainColor=SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.texcoord); //float4 mainColorC=SAMPLE_TEXTURE2D(_Cut,sampler_Cut,i.texcoord); // mainColorC.rgb=ACESToneMapping(mainColorC.rgb,0.4); return float4(0,0,0,0); } //模板剔除2 float4 fragCull2(Varyings i) : SV_Target { return float4(0,1,0,0); } //混合颜色 float4 fragBlend(Varyings i) : SV_Target { float4 mainColor=SAMPLE_TEXTURE2D(_BlitTexture,sampler_LinearClamp,i.texcoord); mainColor.rgb*=Strength; //mainColor.rgb=ACESToneMapping(mainColor.rgb,0.6); return mainColor; } ENDHLSL SubShader { Tags { "Queue"="Transparent" "RenderPipeline" = "UniversalPipeline" } LOD 100 ZWrite Off ZTest Always Cull Off Lighting Off Pass { Name "0_Fill" Stencil { Ref 3 Comp Always Pass Replace Fail Keep } //ZWrite On HLSLPROGRAM #pragma vertex vert #pragma fragment fragFill ENDHLSL } Pass { Name "1_DownBlur" HLSLPROGRAM #pragma vertex Vert #pragma fragment fragDownBlur ENDHLSL } Pass { Name "2_UpBlur" HLSLPROGRAM #pragma vertex Vert #pragma fragment fragUpBlur ENDHLSL } Pass { Name "3_Cut" Stencil { Ref 2 Comp Equal Pass Keep Fail Keep } //ColorMask 0 //BlendOp RevSub HLSLPROGRAM #pragma vertex Vert #pragma fragment fragCull ENDHLSL } Pass { Name "4_Blend" Blend One One HLSLPROGRAM #pragma vertex Vert #pragma fragment fragBlend ENDHLSL } Pass { Name "5_Cut" Stencil { Ref 2 Comp Equal Pass Keep ZFail Keep } //BlendOp RevSub HLSLPROGRAM #pragma vertex Vert #pragma fragment fragCull2 ENDHLSL } } }