Skip to content

Instantly share code, notes, and snippets.

@melsov
Created February 15, 2019 17:17
Show Gist options
  • Save melsov/8bb32ee3b5dce97d1a003336ffb18193 to your computer and use it in GitHub Desktop.
Save melsov/8bb32ee3b5dce97d1a003336ffb18193 to your computer and use it in GitHub Desktop.
Shader "Unlit/SliderBar"
{
Properties
{
_MainTex ("Use any fully opaque image. Doesn't effect look.", 2D) = "white" {}
_SlugTex ("Slug Texture", 2D) = "white" {}
_XRepeats ("_XRepeats", Range(1, 50) ) = 10.0
_MainColor ("_Color", Color) = (1.0, 1.0, 1.0, 1.0)
_EmptyColor ("_EmptyColor", Color) = (0.0, 0.0, 0.0, 0.0)
_Value ("_Value", Range(0,1)) = .4
_CutoffAngle("_CutoffAngle", Range(-90, 90)) = 0
[Toggle] _CutoffShouldSkew("_CutoffShouldSkew", Float) = 1
[Toggle] _LeftCuttoff("_LeftCuttoff", Float) = 1
}
SubShader
{
/*
* ***********************************************
* A 'slug style' UI slider.
* Meant to be used as the material
* attached to a Unity ui panel (Image)
*
* GIANT CAVEAT:
* _MainTex has no effect other than existing and being opaque.
*
* Use the default UISprite (or any opaque square) as the panel's Sprite (i.e. _MainTex)
* Entirely transparent columns along the edges in _MainTex can lead to distortions/masking and, in any case,
* it has no effect on the look of the slider beyond telling Unity where to paint.
*
* Transparency in _SlugTex is fine (and expected; use it to create divisions between slugs, for example).
* ***********************************************
*/
Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }
LOD 100
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#define DEG2RAD 0.01745329251
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _SlugTex;
float4 _SlugTex_ST;
float _XRepeats;
float _Value;
float4 _MainColor;
float4 _EmptyColor;
float _CutoffAngle;
fixed _CutoffShouldSkew;
fixed _LeftCuttoff;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed2 uv = i.uv;
fixed skew = i.uv.y * sin(_CutoffAngle * DEG2RAD);
fixed sampleSkew = (_CutoffAngle > 0) * skew + (_CutoffAngle < 0) * (1 - i.uv.y) * sin(-_CutoffAngle * DEG2RAD);
fixed nuggX = 1/_XRepeats;
fixed totalWidth01 = 1 - sin(abs(_CutoffAngle) * DEG2RAD);
fixed reps = (totalWidth01) * _XRepeats;
reps = floor(reps);
half ux = (uv.x - sampleSkew * floor(_CutoffShouldSkew + .5)) * _XRepeats;
uv.x = fmod(ux, 1);
fixed leftCutoff = (ux > reps) * _LeftCuttoff;
fixed rightCutoff = i.uv.x < sampleSkew;
fixed4 col = tex2D(_SlugTex, uv);
fixed t = ux > reps * _Value;
fixed4 shade =
_MainColor * ((1 - t) * (1 - leftCutoff)) +
_EmptyColor * saturate(t + ( leftCutoff));
return col * shade * (1 - leftCutoff) * (1 - rightCutoff);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment