Skip to content

Instantly share code, notes, and snippets.

@Hugosslade
Created September 1, 2021 09:47
Show Gist options
  • Save Hugosslade/aefe6fd68c92dc80919ec85997fe97ad to your computer and use it in GitHub Desktop.
Save Hugosslade/aefe6fd68c92dc80919ec85997fe97ad to your computer and use it in GitHub Desktop.
Shader "Test/SmoothZoomTexture"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
[KeywordEnum(Basic,Manual,Blend,Compare)]Mode("Mode", Int) = 0
_Blend("Blend", Range(0,1)) = 1
[Header(Mip Map)]
_MipMapMin("MipMap Min", Int) = 4
_MipMapMax("MipMap Max", Int) = 0
_MipMapBias("MipMap Bias", Float) = 0
[Header(Scale)]
_VectorScaleMin("Vector Scale Min", Float) = 0.1
_VectorScaleMax("Vector Scale Max", Float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_local _ MODE_BASIC MODE_MANUAL MODE_BLEND MODE_COMPARE
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
half _VectorScaleMin;
half _VectorScaleMax;
half _MipMapMin;
half _MipMapMax;
half _Blend;
half _MipMapBias;
v2f vert (appdata v)
{
v2f o;
v.vertex.xyz *= lerp(_VectorScaleMin, _VectorScaleMax, _Blend);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = float4(TRANSFORM_TEX(v.uv, _MainTex).xy, 0, lerp(_MipMapMin, _MipMapMax, _Blend) + _MipMapBias);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
#if defined(MODE_BASIC) || defined(MODE_COMPARE)
fixed4 basic = tex2D(_MainTex, i.uv);
#endif
#if defined(MODE_MANUAL) || defined(MODE_COMPARE)
fixed4 manual = tex2Dlod(_MainTex, i.uv);
#endif
#if defined(MODE_BLEND) || defined(MODE_COMPARE)
float4 lowerQualityUVs = i.uv;
lowerQualityUVs.w = floor(lowerQualityUVs.w);
fixed4 base = tex2Dlod(_MainTex, lowerQualityUVs);
float4 higherQualityUVs = lowerQualityUVs;
higherQualityUVs.w -= 1;
fixed4 higherQuality = tex2Dlod(_MainTex, higherQualityUVs);
fixed4 blend = lerp(higherQuality, base, frac(i.uv.w));
#endif
#if defined(MODE_BASIC)
return basic;
#elif defined(MODE_MANUAL)
return manual;
#elif defined(MODE_BLEND)
return blend;
#elif defined(MODE_COMPARE)
if (i.uv.x < 0.33)
{
return basic;
}
else if (i.uv.x < 0.66)
{
return blend;
}
else
{
return manual;
}
#else
return fixed4(0, 1, 1, 1);
#endif
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment