228 lines
8.1 KiB
HLSL
Executable File
228 lines
8.1 KiB
HLSL
Executable File
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include <HLSLSupport.cginc>
|
|
|
|
struct vertex_t
|
|
{
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
float4 position : POSITION;
|
|
float3 normal : NORMAL;
|
|
float4 color : COLOR;
|
|
float4 texcoord0 : TEXCOORD0;
|
|
float2 texcoord1 : TEXCOORD1;
|
|
float2 texcoord3 : TEXCOORD3; // vertex left bottom position
|
|
float2 texcoord4 : TEXCOORD4; // UV left bottom position
|
|
float2 texcoord5 : TEXCOORD5; // vertex width, height
|
|
float2 texcoord6 : TEXCOORD6; // UV width, height
|
|
float2 texcoord7 : TEXCOORD7; // rotation (x - angle)
|
|
};
|
|
|
|
struct pixel_t
|
|
{
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
UNITY_VERTEX_OUTPUT_STEREO
|
|
float4 position : SV_POSITION;
|
|
float4 faceColor : COLOR;
|
|
float4 outlineColor : COLOR1;
|
|
float4 texcoord0 : TEXCOORD0;
|
|
float4 param : TEXCOORD1; // x = weight, y = no longer used
|
|
float2 mask : TEXCOORD2;
|
|
#if (UNDERLAY_ON | UNDERLAY_INNER)
|
|
float4 texcoord1 : TEXCOORD3;
|
|
float2 underlayParam : TEXCOORD4;
|
|
#endif
|
|
float4 vert_data : TEXCOORD5; // vertex left bottom position (x, y), vertex width (z), height (w)
|
|
float4 uv_data : TEXCOORD6; // UV left bottom position (x, y), UV width (z), height (w)
|
|
float2 rot_data : TEXCOORD7; // sin(a), cos(a)
|
|
};
|
|
|
|
float4 SRGBToLinear(float4 rgba)
|
|
{
|
|
return float4(lerp(rgba.rgb / 12.92f, pow((rgba.rgb + 0.055f) / 1.055f, 2.4f), step(0.04045f, rgba.rgb)), rgba.a);
|
|
}
|
|
|
|
float _UIMaskSoftnessX;
|
|
float _UIMaskSoftnessY;
|
|
|
|
pixel_t VertShader(vertex_t input)
|
|
{
|
|
pixel_t output;
|
|
|
|
UNITY_INITIALIZE_OUTPUT(pixel_t, output);
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_TRANSFER_INSTANCE_ID(input, output);
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
|
|
|
|
float bold = step(input.texcoord0.w, 0);
|
|
|
|
float4 vert = input.position;
|
|
vert.x += _VertexOffsetX;
|
|
vert.y += _VertexOffsetY;
|
|
|
|
float4 vPosition = GetVertexPositionInputs(vert).positionCS;
|
|
|
|
float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
|
|
weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
|
|
|
|
// Generate UV for the Masking Texture
|
|
float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
|
|
float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
|
|
|
|
float4 color = input.color;
|
|
#if (FORCE_LINEAR && !UNITY_COLORSPACE_GAMMA)
|
|
color = SRGBToLinear(input.color);
|
|
#endif
|
|
|
|
float opacity = color.a;
|
|
#if (UNDERLAY_ON | UNDERLAY_INNER)
|
|
opacity = 1.0;
|
|
#endif
|
|
|
|
float4 faceColor = float4(color.rgb, opacity) * _FaceColor;
|
|
faceColor.rgb *= faceColor.a;
|
|
|
|
float4 outlineColor = _OutlineColor;
|
|
outlineColor.a *= opacity;
|
|
outlineColor.rgb *= outlineColor.a;
|
|
|
|
output.position = vPosition;
|
|
output.faceColor = faceColor;
|
|
output.outlineColor = outlineColor;
|
|
output.texcoord0 = float4(input.texcoord0.xy, maskUV.xy);
|
|
output.param = float4(0.5 - weight, 0, _OutlineWidth * _ScaleRatioA * 0.5, 0);
|
|
|
|
float2 mask = float2(0, 0);
|
|
#if UNITY_UI_CLIP_RECT
|
|
mask = vert.xy * 2 - clampedRect.xy - clampedRect.zw;
|
|
#endif
|
|
output.mask = mask;
|
|
|
|
#if (UNDERLAY_ON || UNDERLAY_INNER)
|
|
float4 underlayColor = _UnderlayColor;
|
|
underlayColor.rgb *= underlayColor.a;
|
|
|
|
float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
|
|
float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
|
|
|
|
output.texcoord2 = float4(input.texcoord0 + float2(x, y), input.color.a, 0);
|
|
output.underlayColor = underlayColor;
|
|
#endif
|
|
|
|
// decal
|
|
output.vert_data = float4(input.texcoord3.xy, input.texcoord5.xy);
|
|
output.uv_data = float4(input.texcoord4.xy, input.texcoord6.xy);
|
|
const float angle = input.texcoord7.x;
|
|
output.rot_data = float2(sin(angle), cos(angle));
|
|
|
|
return output;
|
|
}
|
|
|
|
float invLerp(const float from, const float to, const float value)
|
|
{
|
|
return (value - from) / (to - from);
|
|
}
|
|
|
|
float remap(const float origFrom, const float origTo, const float targetFrom, const float targetTo, const float value)
|
|
{
|
|
const float rel = invLerp(origFrom, origTo, value);
|
|
return lerp(targetFrom, targetTo, rel);
|
|
}
|
|
|
|
float3 rotate(const float3 input, float2 m)
|
|
{
|
|
const float c = m.y; // cos(a)
|
|
const float s = m.x; // sin(a)
|
|
return mul(input, float3x3(c,-s, 0, s, c , 0, 0, 0, 1));
|
|
}
|
|
|
|
float4 PixShader(pixel_t input) : SV_Target
|
|
{
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
|
|
// decal start
|
|
const float2 uv = (input.position.xy / _ScaledScreenParams.xy);
|
|
#if UNITY_REVERSED_Z
|
|
float depth = SampleSceneDepth(uv);
|
|
#else
|
|
// Adjust Z to match NDC for OpenGL ([-1, 1])
|
|
float depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(uv));
|
|
#endif
|
|
const float3 world_pos = ComputeWorldSpacePosition(uv, depth, UNITY_MATRIX_I_VP);
|
|
float3 local_pos = TransformWorldToObject(world_pos);
|
|
// move local space to left bottom vertex position
|
|
local_pos -= float3(input.vert_data.xy, 0);
|
|
local_pos = rotate(local_pos,input.rot_data);
|
|
const float width = input.vert_data.z;
|
|
const float height = input.vert_data.w;
|
|
float4 decal_clip = float4(0, 0, 1, 1);
|
|
decal_clip.x = step(0, local_pos.x) * (1 - step(width, local_pos.x));
|
|
decal_clip.y = step(0, local_pos.y) * (1 - step(height, local_pos.y));
|
|
|
|
// remap from (0..vertex_size) to (uv.start..uv.end)
|
|
local_pos.x = remap(0, width, input.uv_data.x, input.uv_data.x + input.uv_data.z, local_pos.x);
|
|
local_pos.y = remap(0, height, input.uv_data.y, input.uv_data.y + input.uv_data.w, local_pos.y);
|
|
// decal end
|
|
|
|
// float d = tex2D(_MainTex, input.texcoord0.xy).a;
|
|
float d = tex2D(_MainTex, local_pos.xy).a;
|
|
|
|
float pixelSize = abs(ddx(input.texcoord0.y)) + abs(ddy(input.texcoord0.y));
|
|
pixelSize *= _TextureHeight * 0.75;
|
|
float scale = 1 / pixelSize * _GradientScale * (_Sharpness + 1);
|
|
|
|
#if (UNDERLAY_ON | UNDERLAY_INNER)
|
|
float layerScale = scale;
|
|
layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
|
|
float layerBias = input.param.x * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
|
|
#endif
|
|
|
|
// scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
|
|
|
|
float4 faceColor = input.faceColor * saturate((d - input.param.x) * scale + 0.5);
|
|
faceColor *= decal_clip.x * decal_clip.y * decal_clip.z;
|
|
|
|
#if OUTLINE_ON
|
|
float4 outlineColor = lerp(input.faceColor, input.outlineColor, sqrt(min(1.0, input.param.z * scale * 2)));
|
|
faceColor = lerp(outlineColor, input.faceColor, saturate((d - input.param.x - input.param.z) * scale + 0.5));
|
|
faceColor *= saturate((d - input.param.x + input.param.z) * scale + 0.5);
|
|
#endif
|
|
|
|
#if UNDERLAY_ON
|
|
d = tex2D(_MainTex, input.texcoord2.xy).a * layerScale;
|
|
faceColor += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - layerBias) * (1 - faceColor.a);
|
|
#endif
|
|
|
|
#if UNDERLAY_INNER
|
|
float bias = input.param.x * scale - 0.5;
|
|
float sd = saturate(d * scale - bias - input.param.z);
|
|
d = tex2D(_MainTex, input.texcoord2.xy).a * layerScale;
|
|
faceColor += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - layerBias)) * sd * (1 - faceColor.a);
|
|
#endif
|
|
|
|
#if MASKING
|
|
float a = abs(_MaskInverse - tex2D(_MaskTex, input.texcoord0.zw).a);
|
|
float t = a + (1 - _MaskWipeControl) * _MaskEdgeSoftness - _MaskWipeControl;
|
|
a = saturate(t / _MaskEdgeSoftness);
|
|
faceColor.rgb = lerp(_MaskEdgeColor.rgb * faceColor.a, faceColor.rgb, a);
|
|
faceColor *= a;
|
|
#endif
|
|
|
|
// Alternative implementation to UnityGet2DClipping with support for softness
|
|
#if UNITY_UI_CLIP_RECT
|
|
half2 maskSoftness = half2(max(_UIMaskSoftnessX, _MaskSoftnessX), max(_UIMaskSoftnessY, _MaskSoftnessY));
|
|
float2 maskZW = 0.25 / (0.25 * maskSoftness + 1 / scale);
|
|
float2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * maskZW);
|
|
faceColor *= m.x * m.y;
|
|
#endif
|
|
|
|
#if (UNDERLAY_ON | UNDERLAY_INNER)
|
|
faceColor *= input.texcoord2.z;
|
|
#endif
|
|
|
|
#if UNITY_UI_ALPHACLIP
|
|
clip(faceColor.a - 0.001);
|
|
#endif
|
|
|
|
return faceColor;
|
|
}
|