98 lines
3.7 KiB
Plaintext
98 lines
3.7 KiB
Plaintext
Shader "URP_Test/PostOutline_2"
|
|
{
|
|
Properties
|
|
{
|
|
_MainTex ("Texture", 2D) = "white" {}
|
|
_SampleDistance ("SampleDistance", Float) = 0.01
|
|
}
|
|
SubShader
|
|
{
|
|
Tags
|
|
{
|
|
"RenderType"="Opaque"
|
|
"RenderPipeline" = "UniversalPipeline"
|
|
}
|
|
|
|
Cull Off
|
|
ZWrite Off
|
|
ZTest Off
|
|
|
|
Pass
|
|
{
|
|
Name "DrawEdge"
|
|
// 轮廓
|
|
HLSLPROGRAM
|
|
#pragma vertex VERT
|
|
#pragma fragment FRAG
|
|
#pragma target 2.0
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
|
|
CBUFFER_START(UnityPerMaterial)
|
|
|
|
float4 _MainTex_TexelSize;
|
|
float _SampleDistance;
|
|
|
|
CBUFFER_END
|
|
|
|
TEXTURE2D(_MainTex);
|
|
SAMPLER(sampler_MainTex);
|
|
|
|
struct a2v
|
|
{
|
|
float4 positionOS : POSITION;
|
|
float2 uv0 : TEXCOORD;
|
|
};
|
|
|
|
struct v2f
|
|
{
|
|
float4 positionCS : SV_POSITION;
|
|
float4 texcoord0 : TEXCOORD0;
|
|
};
|
|
|
|
v2f VERT(a2v i)
|
|
{
|
|
v2f o;
|
|
o.positionCS = TransformObjectToHClip(i.positionOS.xyz);
|
|
o.texcoord0 = float4(i.uv0, _SampleDistance * _MainTex_TexelSize.x, _SampleDistance * _MainTex_TexelSize.y);
|
|
return o;
|
|
}
|
|
|
|
half4 FRAG(v2f i):SV_TARGET
|
|
{
|
|
half4 centerCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy);
|
|
half offsetX = i.texcoord0.z;
|
|
half offsetY = i.texcoord0.w;
|
|
half4 leftCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy + float2(-offsetX, 0));
|
|
half4 rightCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy + float2(offsetX, 0));
|
|
half4 upCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy + float2(0, offsetY));
|
|
half4 downCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy + float2(0, -offsetY));
|
|
// // 左下角
|
|
// half4 leftDownCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy + float2(-offsetX, -offsetY));
|
|
// // 右下角
|
|
// half4 rightDownCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy + float2(offsetX, -offsetY));
|
|
// // 左上角
|
|
// half4 leftUpCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy + float2(-offsetX, offsetY));
|
|
// // 右上角
|
|
// half4 rightUpCol = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord0.xy + float2(offsetX, offsetY));
|
|
|
|
// 检测是否是边缘,保持 left -> right -> up -> down 的优先级
|
|
half centerMask = 1.0h - step(0.5h, centerCol.a);
|
|
half leftMask = centerMask * (1.0h - step(leftCol.a, 0.5h));
|
|
half rightMask = centerMask * (1.0h - leftMask) * (1.0h - step(rightCol.a, 0.5h));
|
|
half upMask = centerMask * (1.0h - leftMask) * (1.0h - rightMask) * (1.0h - step(upCol.a, 0.5h));
|
|
half downMask = centerMask * (1.0h - leftMask) * (1.0h - rightMask) * (1.0h - upMask) * (1.0h - step(downCol.a, 0.5h));
|
|
half3 edgeColor = leftCol.xyz * leftMask
|
|
+ rightCol.xyz * rightMask
|
|
+ upCol.xyz * upMask
|
|
+ downCol.xyz * downMask;
|
|
half edgeAlpha = saturate(leftMask + rightMask + upMask + downMask);
|
|
return half4(edgeColor, edgeAlpha);
|
|
}
|
|
|
|
ENDHLSL
|
|
}
|
|
|
|
}
|
|
}
|