Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Created March 23, 2021 04:17
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 partybusiness/69d23660e288d9f22432946d1af5901e to your computer and use it in GitHub Desktop.
Save partybusiness/69d23660e288d9f22432946d1af5901e to your computer and use it in GitHub Desktop.
Some shaders that distort texture sampling in interesting ways.
Shader "Unlit/Diagonal"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
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 = fixed2(i.uv.x +i.uv.y,i.uv.y-i.uv.x);
fixed4 col = tex2D(_MainTex, uv);
return col;
}
ENDCG
}
}
}
Shader "Unlit/Rorschach"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ZagFrequency("Zag Frequency", float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _ZagFrequency;
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 zagLength = 1 / _ZagFrequency;
fixed flooredX = floor(uv.x/(zagLength * 1.5))*(zagLength*1.5);
uv.x = zagLength-abs(zagLength - (uv.x- flooredX)) + flooredX / 3.0;
fixed2 uv2 = fixed2(uv.x +uv.y,uv.y-uv.x);
fixed4 col = tex2D(_MainTex, uv2, ddx(i.uv), ddy(i.uv));
return col;
}
ENDCG
}
}
}
Shader "Unlit/ZigZag"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_ZagFrequency ("Zag Frequency", float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _ZagFrequency;
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 zagLength = 1/_ZagFrequency;
fixed zigLocation = uv.x % zagLength;
fixed halfZag = (zagLength / 2.0);
fixed zagAmplitude = (1 - abs(halfZag - zigLocation) / halfZag) * halfZag;
fixed4 col = tex2D(_MainTex, uv + fixed2(0,zagAmplitude));
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment