Add rounded rectangle shaders and remove masks

This commit is contained in:
2020-07-21 16:41:26 -07:00
parent d5702ee0e7
commit e57bed6ed9
9 changed files with 272 additions and 584 deletions

View File

@ -3,6 +3,7 @@ Shader "ConformalDecals/UI/ColorSlider"
Properties
{
_Color("Color", Color) = (0,0,0,0)
_Radius("Radius", Float) = 4
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
@ -57,12 +58,14 @@ Shader "ConformalDecals/UI/ColorSlider"
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#include "HSL.cginc"
#include "SDF.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
#pragma multi_compile_local HUE RED GREEN BLUE
float4 _ClipRect;
float _Radius;
float4 _Color;
struct appdata
@ -92,6 +95,8 @@ Shader "ConformalDecals/UI/ColorSlider"
// sample the texture
fixed4 color = 1;
color.a = saturate(0.5 - sdRoundedUVBox(i.uv, _Radius));
#ifdef HUE
color.rgb = HSL2RGB(float3(i.uv.y, 1, 0.5));
#endif //HUE

View File

@ -3,7 +3,8 @@ Shader "ConformalDecals/UI/HSLSquare"
Properties
{
_Hue("Hue", Range(0,1)) = 0
_Radius("Radius", Float) = 4
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
@ -51,11 +52,13 @@ Shader "ConformalDecals/UI/HSLSquare"
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#include "HSL.cginc"
#include "SDF.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
float _Hue;
float _Radius;
float4 _ClipRect;
struct appdata
@ -84,6 +87,9 @@ Shader "ConformalDecals/UI/HSLSquare"
{
// sample the texture
fixed4 color = 1;
color.a = saturate(0.5 - sdRoundedUVBox(i.uv, _Radius));
color.rgb = HSL2RGB(float3(_Hue, i.uv.x, i.uv.y));
#ifdef UNITY_UI_CLIP_RECT

View File

@ -0,0 +1,105 @@
Shader "ConformalDecals/UI/Color Swatch"
{
Properties
{
_Color("Color", Color) = (0,0,0,0)
_Radius("Radius", Float) = 4
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma require integers
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#include "HSL.cginc"
#include "SDF.cginc"
#pragma multi_compile_local _ UNITY_UI_CLIP_RECT
#pragma multi_compile_local _ UNITY_UI_ALPHACLIP
#pragma multi_compile_local HUE RED GREEN BLUE
float4 _ClipRect;
float _Radius;
float4 _Color;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float4 worldPosition : TEXCOORD1;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldPosition = v.vertex;
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 color = _Color;
color.a = saturate(0.5 - sdRoundedUVBox(i.uv, _Radius));
#ifdef UNITY_UI_CLIP_RECT
color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
#endif
#ifdef UNITY_UI_ALPHACLIP
clip (color.a - 0.001);
#endif
return color;
}
ENDCG
}
}
}

View File

@ -153,10 +153,6 @@ inline float BoundsDist(float3 p, float3 normal, float3 projNormal) {
#endif
}
inline float SDFAA(float dist) {
float ddist = length(float2(ddx(dist), ddy(dist)));
float pixelDist = dist / ddist;
return saturate(0.5-pixelDist);
}
#endif

38
Assets/Shaders/SDF.cginc Normal file
View File

@ -0,0 +1,38 @@
#ifndef SDF_INCLUDED
#define SDF_INCLUDED
// based on functions by Inigo Quilez
// https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
// SDF of a box
float sdBox( in float2 p, in float2 b ) {
float2 d = abs(p)-b;
return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
}
// SDF of a box with corner radius r
float sdRoundedBox( in float2 p, in float2 b, in float r ) {
float2 d = abs(p)-b+r;
return min(max(d.x,d.y),0.0) + length(max(d,0.0)) - r;
}
// SDF of a box with corner radius r, based on the current UV position
// UV must be ∈ (0,1), with (0,0) on one corner
float sdRoundedUVBox( float2 uv, float r ) {
float dx = ddx(uv.x);
float dy = ddy(uv.y);
float2 dim = abs(float2(1/dx, 1/dy));
float2 halfDim = dim / 2;
float2 pos = (dim * uv) - halfDim;
return sdRoundedBox(pos, halfDim, r);
}
float SDFAA(float dist) {
float ddist = length(float2(ddx(dist), ddy(dist)));
float pixelDist = dist / ddist;
return saturate(0.5-pixelDist);
}
#endif

View File

@ -69,6 +69,7 @@
#include "UnityCG.cginc"
#include "DecalsCommon.cginc"
#include "DecalsSurface.cginc"
#include "SDF.cginc"
#include "StandardDecal.cginc"
ENDCG
@ -98,6 +99,7 @@
#include "UnityCG.cginc"
#include "DecalsCommon.cginc"
#include "DecalsSurface.cginc"
#include "SDF.cginc"
#include "StandardDecal.cginc"
ENDCG