Skip to content

Instantly share code, notes, and snippets.

@seandanger
Last active February 13, 2020 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seandanger/214ab9636e46ddb18b5e63d15d9c8c77 to your computer and use it in GitHub Desktop.
Save seandanger/214ab9636e46ddb18b5e63d15d9c8c77 to your computer and use it in GitHub Desktop.
Shader for showing a drink with various fill levels in Unity. Instancing enabled.
Shader "Unlit/DrinkShader"
{
Properties
{
[PerRendererData] _MainTex ("Texture", 2D) = "white" {}
[PerRendererData] _FillTex("FillTexture", 2D) = "white" {}
[PerRendererData] _FillAmount("Fill", Range(0.0,1.0)) = 1.0
[HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap("Pixel Snap", Float) = 0
}
SubShader
{
Tags {
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile_instancing
#pragma multi_compile_local _ PIXELSNAP_ON
#include "UnityCG.cginc"
#ifndef UNITY_SPRITES_INCLUDED
#define UNITY_SPRITES_INCLUDED
#ifdef UNITY_INSTANCING_ENABLED
UNITY_INSTANCING_BUFFER_START(PerDrawSprite)
UNITY_DEFINE_INSTANCED_PROP(fixed4, unity_SpriteRendererColorArray)
UNITY_DEFINE_INSTANCED_PROP(fixed2, unity_SpriteFlipArray)
UNITY_INSTANCING_BUFFER_END(PerDrawSprite)
#define _RendererColor UNITY_ACCESS_INSTANCED_PROP(PerDrawSprite, unity_SpriteRendererColorArray)
#define _Flip UNITY_ACCESS_INSTANCED_PROP(PerDrawSprite, unity_SpriteFlipArray)
#endif
UNITY_INSTANCING_BUFFER_START(MyProps)
UNITY_DEFINE_INSTANCED_PROP(float, _FillAmount)
UNITY_INSTANCING_BUFFER_END(MyProps)
CBUFFER_START(UnityPerDrawSprite)
#ifndef UNITY_INSTANCING_ENABLED
fixed4 _RendererColor;
fixed2 _Flip;
#endif
CBUFFER_END
// Material Color
fixed4 _Color;
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
inline float4 UnityFlipSprite(in float3 pos, in fixed2 flip)
{
return float4(pos.xy * flip, pos.z, 1.0);
}
v2f vert (appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
o.vertex = UnityFlipSprite(v.vertex, _Flip);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.color = v.color * _Color * _RendererColor;
#ifdef PIXELSNAP_ON
o.vertex = UnityPixelSnap(o.vertex);
#endif
return o;
}
sampler2D _MainTex;
sampler2D _FillTex;
fixed4 frag(v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
fixed4 mainCol = tex2D(_MainTex, i.uv);
fixed4 fillCol = tex2D(_FillTex, i.uv);
// TODO: way to do this without branching? does it matter?
if (i.uv.y > UNITY_ACCESS_INSTANCED_PROP(MyProps, _FillAmount))
return mainCol;
return fillCol;
}
#endif // UNITY_SPRITES_INCLUDED
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment