Files
StudyCase/Assets/Resources/SimpleURPToon/URPOutline.shader
2025-11-24 21:29:46 +08:00

123 lines
3.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Shader "Custom/URPOutline"
{
Properties
{
_BaseColor("Base Color", Color) = (1,1,1,1)
_OutlineColor("Outline Color", Color) = (1,0,0,1)
_OutlineWidth("Outline Width", Range(0, 0.1)) = 0.05
}
SubShader
{
Tags
{
"RenderType"="Opaque"
"RenderPipeline"="UniversalPipeline"
"Queue"="Geometry"
}
// 第一个Pass渲染描边
Pass
{
Name "Outline"
Tags { "LightMode" = "SRPDefaultUnlit" }
Cull Front
ZWrite On
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
};
CBUFFER_START(UnityPerMaterial)
float4 _OutlineColor;
float _OutlineWidth;
CBUFFER_END
Varyings vert(Attributes input)
{
Varyings output;
// 沿法线方向膨胀顶点
float3 normalOS = normalize(input.normalOS);
float3 positionOS = input.positionOS.xyz + normalOS * _OutlineWidth;
output.positionCS = TransformObjectToHClip(positionOS);
return output;
}
half4 frag(Varyings input) : SV_Target
{
return _OutlineColor;
}
ENDHLSL
}
// 第二个Pass渲染物体本身
Pass
{
Name "ForwardLit"
Tags { "LightMode" = "UniversalForward" }
Cull Back
ZWrite On
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float3 normalWS : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
float4 _BaseColor;
CBUFFER_END
Varyings vert(Attributes input)
{
Varyings output;
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
output.normalWS = TransformObjectToWorldNormal(input.normalOS);
return output;
}
half4 frag(Varyings input) : SV_Target
{
// 基础光照
Light mainLight = GetMainLight();
float3 normalWS = normalize(input.normalWS);
float NdotL = saturate(dot(normalWS, mainLight.direction));
float3 color = _BaseColor.rgb * (mainLight.color * NdotL + 0.2);
return half4(color, _BaseColor.a);
}
ENDHLSL
}
}
}