Skip to content

Instantly share code, notes, and snippets.

@lopea
Created March 2, 2019 02:41
Show Gist options
  • Save lopea/07fd682dceae234f3dcc3186686a4a40 to your computer and use it in GitHub Desktop.
Save lopea/07fd682dceae234f3dcc3186686a4a40 to your computer and use it in GitHub Desktop.
a data moshing effect in Unity3D
Shader "Hidden/DataMosh"{
Properties{
_MainTex ("Main Texture", 2D) = "white"{}
}
CGINCLUDE
#include "Unity3D.cginc"
float3 HCLtoRGB(in float3 HCL)
{
float3 RGB = 0;
if (HCL.z != 0)
{
float H = HCL.x;
float C = HCL.y;
float L = HCL.z * HCLmaxL;
float Q = exp((1 - C / (2 * L)) * (HCLgamma / HCLy0));
float U = (2 * L - C) / (2 * Q - 1);
float V = C / Q;
float A = (H + min(frac(2 * H) / 4, frac(-2 * H) / 8)) * PI * 2;
float T;
H *= 6;
if (H <= 0.999)
{
T = tan(A);
RGB.r = 1;
RGB.g = T / (1 + T);
}
else if (H <= 1.001)
{
RGB.r = 1;
RGB.g = 1;
}
else if (H <= 2)
{
T = tan(A);
RGB.r = (1 + T) / T;
RGB.g = 1;
}
else if (H <= 3)
{
T = tan(A);
RGB.g = 1;
RGB.b = 1 + T;
}
else if (H <= 3.999)
{
T = tan(A);
RGB.g = 1 / (1 + T);
RGB.b = 1;
}
else if (H <= 4.001)
{
RGB.g = 0;
RGB.b = 1;
}
else if (H <= 5)
{
T = tan(A);
RGB.r = -1 / T;
RGB.b = 1;
}
else
{
T = tan(A);
RGB.r = 1;
RGB.b = -T;
}
RGB = RGB * V + U;
}
return RGB;
}
float3 RGBtoHSV(in float3 RGB)
{
float3 HCV = RGBtoHCV(RGB);
float S = HCV.y / (HCV.z + Epsilon);
return float3(HCV.x, S, HCV.z);
}
sampler2D _MainTex;
float4 frag(v2f_img i){
float3 h = RGBtoHSV(tex2D(_MainTex, i.uv).rgb);
float a = h.r + atan(i.uv.y,i.uv.x) + _Time.y * 0.1;
float2x2 mat = float2x2(a,-a,a,a);
float4 color = tex2D(_MainTex, uv + mat * float2(log((_Time.y* 0.7)-0.2*0.2 +1),0) * hsv.y);
return color;
}
ENDCG
SubShader{
#pragma target 3.0
#pragma vertex vert_img
#pragma fragment frag
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment