TH1/Unity/Assets/Shaders/UI/Gradient.shader
2025-07-30 23:27:48 +08:00

113 lines
3.6 KiB
GLSL

Shader "TH1Shaders/UI/Gradient_HLSL"
{
Properties
{
[PerRendererData] _MainTex ("Texture", 2D) = "white" {}
_TopColor("Top Color", Color) = (1,1,1,1)
_BottomColor("Bottom Color", Color) = (0,0,0,1)
_Rotation("Rotation (Degrees)", Range(0, 360)) = 0
// Properties for UI Masking
[HideInInspector] _StencilComp("Stencil Comparison", Float) = 8
[HideInInspector] _Stencil("Stencil ID", Float) = 0
[HideInInspector] _StencilOp("Stencil Operation", Float) = 0
[HideInInspector] _StencilWriteMask("Stencil Write Mask", Float) = 255
[HideInInspector] _StencilReadMask("Stencil Read Mask", Float) = 255
[HideInInspector] _ColorMask("Color Mask", Float) = 15
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"RenderType" = "Transparent"
"RenderPipeline" = "UniversalPipeline"
"IgnoreProjector" = "True"
}
Pass
{
// UI Stencil and Blending setup
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
ZWrite Off
ZTest [unity_GUIZTestMode] // For UI layering
Stencil
{
Ref[_Stencil]
Comp[_StencilComp]
Pass[_StencilOp]
ReadMask[_StencilReadMask]
WriteMask[_StencilWriteMask]
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
half4 color : COLOR; // Vertex color from Image component
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
half4 color : COLOR;
};
CBUFFER_START(UnityPerMaterial)
half4 _TopColor;
half4 _BottomColor;
float _Rotation;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = IN.uv;
OUT.color = IN.color;
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
// Rotation logic
float rad = _Rotation * (3.1415926535 / 180.0); // deg to rad
float s = sin(rad);
float c = cos(rad);
// Center the UVs for rotation
float2 centeredUV = IN.uv - 0.5;
// Apply rotation matrix
float2 rotatedUV = float2(
centeredUV.x * c - centeredUV.y * s,
centeredUV.x * s + centeredUV.y * c
);
// Uncenter the UVs
rotatedUV += 0.5;
// Use the Y component of the rotated UV for gradient
float gradientT = saturate(rotatedUV.y);
// Lerp between the two colors
half4 gradientColor = lerp(_BottomColor, _TopColor, gradientT);
// Multiply by the Image's vertex color (so Image.color property works)
gradientColor *= IN.color;
return gradientColor;
}
ENDHLSL
}
}
}